/**
 * Avatar.js
 * 유저의 아바타를 출력한다.
 * 정상적인 사용을 위해 AC_OETags.js(플래시 태그 출력 라이브러리 by adobe inc.)가 먼저 로드 되어있어야 한다
 * 
 * @author ckkim
 * @version 1.0
 * -*- coding: utf8 -*-
 */
// -----------------------------------------------------------------------------
 // constants
var AVATAR_WIDTH  = 148;
var AVATAR_HEIGHT = 220;
var AVATAR_GENDER_MALE   = 1;
var AVATAR_GENDER_FEMALE = 2;

if (typeof G_STATIC_SERVER == 'undefined') var G_STATIC_SERVER = 'http://static.plaync.jp/';
if (typeof G_AVATAR_SERVER == 'undefined') var G_AVATAR_SERVER = 'http://avatar.plaync.jp/';
// -----------------------------------------------------------------------------
document.write('<s'+'cript type="text/javascript" src="' + G_STATIC_SERVER + 'personal/avatar/js/AC_OETags.js"></s'+'cript>');
// -----------------------------------------------------------------------------


// override flash tag output function
function AC_FL_RunContent2()
{
	var ret = AC_GetArgs(arguments, ".nc", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000", "application/x-shockwave-flash");
	AC_Writeobj(AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs));
}

// override flash tag output function
function AC_FL_GetContent2()
{
	var ret = AC_GetArgs(arguments, ".nc", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000", "application/x-shockwave-flash");
	return AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

/**
 * shows avatar flash file into html document
 */
var Avatar = function(nickhash, options) {
	this.nickhash = nickhash;
	window[this.id + '_object'] = this;	// alias for access this object by name
	var parts = this.nickhash.split('/');
	var filename = parts.pop();
	var rectangle = (filename.substr(0,1) == 'M');
	this.options = {width: AVATAR_WIDTH, height: rectangle?AVATAR_WIDTH:AVATAR_HEIGHT};
	if (typeof options != 'undefined') {
		if (typeof options.width != 'undefined') this.options.width = options.width;
		if (typeof options.height != 'undefined') this.options.height = options.height;
	}
}

/**
 * static String generateUniqueID()
 * generates random string
 * @return {String} uniqueString
 */
Avatar.generateUniqueID = function()
{
	return 'avatar_' + String(Math.ceil(Math.random() * 1000000));
}

Avatar.prototype = {
	/**
	 * write flash embed tag into document
	 * @method show()
	 * @memberOf Avatar
	 * @param {String} nickhash
	 * @param {Object} option
	 * <pre>
	 * option:
	 *    {String} toElement	html element or element's id string to insert embed tags
	 *    {Number} width		width of flash movie, default is AVATAR_WIDTH
	 *    {Number} height		height of flash movie, defualt is AVATAR_HEIGHT if not rectangular file or AVATAR_WIDTH
	 * </pre>
	 */
	show: function(toElement) {
		var parts = this.nickhash.split('/');
		var filename = parts.pop();
		var rectangle = (filename.substr(0,1) == 'M');
		var src = G_AVATAR_SERVER + 'avatar/' + parts.join('/') + '/' + filename.replace(/^M/,'');
		var width = AVATAR_WIDTH;
		var height = (rectangle ? AVATAR_WIDTH : AVATAR_HEIGHT);
		if (typeof this.options != 'undefined') {
			var width = this.options.width;
			var height = this.options.height;
			rectangle = this.options.mode || rectangle;
		}

		var hasProductInstall = DetectFlashVer(6, 0, 65);
		var hasRequestedVersion = DetectFlashVer(8, 0, 0);
		if ( hasProductInstall && !hasRequestedVersion ) {
			var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
			var MMredirectURL = window.location;
			document.title = document.title.slice(0, 47) + " - Flash Player Installation";
			var MMdoctitle = document.title;
			// XXX: redirect to flash install page
		} else if (hasRequestedVersion) {
			var uniqid = Avatar.generateUniqueID();
			var html = AC_FL_GetContent2(
				"src", src,
				"width", width,
				"height", height,
				"align", "middle",
				"id", uniqid,
				"quality", "high",
				"bgcolor", "#FFFFFF",
				"name", uniqid,
				"wmode", "transparent",
				"flashvars", rectangle ? 'type=M' : '',
				"allowScriptAccess","always",
				"type", "application/x-shockwave-flash",
				"pluginspage", "http://www.adobe.com/go/getflashplayer"
			);
			toElement = typeof toElement == 'string' ? document.getElementById(toElement) : toElement;
			toElement.innerHTML = html;
		}
	},
	/**
	 * returns embeded flash's element
	 */
	getFlash: function() {
		return document.getElementById(this.id);
	},
	/**
	 * sets avatar's face expression
	 * 
	 * @param {String} face (may ["1"..."5"])
	 */
	setFace: function(face)	{
		this.getFlash().setFace(face);
	},
	/**
	 * sets flash movie's scaling option
	 *
	 * @param {String} mode (may ["M","L"])
	 */
	setMode: function(mode)	{
		this.getFlash().setMode(mode);
	}
}