if(!window.GameCatalog) GameCatalog = {};

GameCatalog.AvatarViewer = Class.create("UA.User");

/**
 * The defaults for the instance-setting object.
 * <ul>Public settings:
 * <li><code>nickname</code> - the nickname, in case the viewer is not for the current-user or shouldn't use the cookie-data</li>
 *	   <br>
 *     Flash settings:<br> 
 *	   Any other setting is passed on to the flash worker object.<br>
 *	   Examples for flash settings:
 *	  <small>(settings that have default values are noted here first)</small><ul>
 * <li><code>height</code> - default 200
 * <li><code>width</code> - default 150
 * <li><code>wmode</code> - default transparent
 * <li><code>quality</code> - default high
 * <li><code>bgcolor</code> - default #ffffff
 * <li><code>menu</code> - default false
 * <li><code>allowscriptaccess</code> - default true
 * <li><code>class</code>
 * <li><code>style</code></li>
 *	   <br>
 *    Internal settings:
 * <li><code>tinyAvatarCashedURL</code> - The base URL on the cached domain to retrieve the user-avatar XML from 
 * <li><code>tinyAvatarLiveURL</code> - The base URL on the live domain to retrieve the user-avatar XML from 
 * <li><code>movie</code> - The URL for the viewer binaries (swf)
 * <li><code>logger</code> - an instance of {@link Log4Js.Logger} or a string to construct one with
 * <li><code>cookieData</code> - the authentication cookie-data. When not provided - assumed the current user, and Clearance.getMagic(Clearance.UNCLASSIFIED) is used
 * </ul>
 * @type Object
 */
GameCatalog.AvatarViewer.defaultSettings = {/* Internal settings*/
											 movie				: "/Community/avatars/2.0/FAV.swf"
										   , tinyAvatarCashedURL: undefined
										   , tinyAvatarLiveURL	: undefined
											/* Flash Settings */
										   , height : 200
										   , width	: 150
										   , wmode	: "transparent"
										   , base	: ""
										   , quality: "high"
										   , bgcolor: "#ffffff"
										   , menu	: false
										   , allowscriptaccess: true
										   , showTypes : ""
										   }

/**
 * a comma delimited string of all internal setting attribute-names
 * @private
 */
GameCatalog.AvatarViewer.internalSettings = 'channel,targetElement,nickname,cookieData,log,guestAvatarXML,tinyAvatarCashedURL,tinyAvatarLiveURL,movie,showTypes'

/**
 * Constructs the GameCatalog.AvatarViewer object
 * 
 * @param {Object} oSettings
 * Any settings that should be passed to the Flash, or internal setting
 *
 * @class
 * A utility class to render an avatar-viewer, or retrieve the HTML needed to render it.
 * 
 * @constructor
 */
GameCatalog.AvatarViewer.prototype.initialize = function( oSettings )
{
	/**
	 * The instance settings based on the {@link GameCatalog.AvatarViewer#defaultFlashSettings defaultFlashSettings} and the <code>oSettings</code> parameter.
	 * @type Object
	 */
	this.settings = Object.extend(GameCatalog.AvatarViewer.defaultSettings);
	this.settings = Object.extend(this.settings, oSettings || {} );

	Claim.isString( this.settings.tinyAvatarCashedURL, "default setting [tinyAvatarCashedURL] is not initiated and not provided on oSettings constructor argument.")
	Claim.isString( this.settings.tinyAvatarLiveURL	 , "default setting [tinyAvatarLiveURL] is not initiated and not provided on oSettings constructor argument.")
	Claim.isString( this.settings.guestAvatarXML	 , "default setting [guestAvatarXML] is not initiated and not provided on oSettings constructor argument.")

	/**
	 * The Log4Js.Logger instance. Can be provided on <code>oSettings.logger</code>
	 * @type Log4Js.Logger
	 */
	this.log = this.settings.logger;
	if(typeof(this.log) == 'string') this.log = new Log4Js.Logger("this.log");
	if(!this.log) this.log = new Log4Js.Logger("AvatarViewer");

	Claim.isTrue(this.log instanceof Log4Js.Logger, "settings.log can be ither a Log4Js.Logger or a string to be used as logger-name" );


	this.log.debug("creating Flash worker-instance");
	/**
	 * The {@link Flash} worker object
	 * @type Flash
	 */
	this.fav = new Flash(this.settings.movie , this.settings, GameCatalog.AvatarViewer.internalSettings );
}


/**
 * returns true if the setting name is internal, and should not pass to the {@link Flash} worker instance.
 * @private
 */
GameCatalog.AvatarViewer.prototype.prv_isInternalSettings = function(sSettingName)
{
	return -1 != GameCatalog.AvatarViewer.internalSettings.indexOf(',' + sSettingName.toLowerCase() + ',');
}

/**
 * gets the cookie-data for the current user
 * @private
 */
GameCatalog.AvatarViewer.prototype.getCookieData = function()
{
	return Clearance.getMagic(Clearance.UNCLASSIFIED);
}
/**
 * return true for viewer for current user, and false for any other user<br> 
 * 
 * @type bool
 * @private
 */
GameCatalog.AvatarViewer.prototype.useLiveProcessing = function()

{
	if( this.settings.nickname )
	{
		return false;
	}
	else
	{
		return true;
	}
}
/**
 * return the provided URL, appended with the user identification.<br>
 * The user-identification can be nick-name and channel, if nickname is provided 
 * in the constructor settings, or cookie-data (if provided - from the settings, 
 * elsewise - from Clearance.getMagic(iLevel).
 * 
 * @type String
 * @private
 */
GameCatalog.AvatarViewer.prototype.appendUserIdentification = function(baseUrl)

{
	if( this.settings.nickname )
	{
		this.log.debug("nickname found on settings object: " + this.settings.nickname);
		baseUrl = Url.appendParamValue(baseUrl, "channel", this.settings.channel);
		//baseUrl = Url.appendParamValue(baseUrl, "nickname", this.settings.nickname);  //RUINING the unicode characters
		baseUrl += ("&nickname=" + encodeURIComponent(this.settings.nickname));         //keeping the unicode characters
	}
	else
	{
		var magic = this.settings.cookieData || this.getCookieData();
		
		if (!magic)
		{
			this.log.debug("no credentials nor nickname provided - guest avatar assumend");
			return this.settings.guestAvatarXML;
		}
		
		this.log.debug("cookieData: " + magic);
		baseUrl = Url.appendParamValue(baseUrl, "cookiedata", magic);
	}
	return baseUrl;
}

/**
 * returns the HTML for the viewer using the worker instance {@link GameCatalog.AvatarViewer#fav}.<br>
 * See {@link Flash#render}.
 *
 * @param {String} targetElement
 * the ID of the DOM Container to render the Flash tag into.<br>
 * When not provided - <code>this.settings.targetElement</code> is used.
 *
 * @type String
 * @returns the prepared HTML for the flash tag
 */
GameCatalog.AvatarViewer.prototype.render = function( targetElement )
{
    if(!targetElement) targetElement = this.settings.targetElement;
    targetElement = $(targetElement);
    
	///////////////////////////////////////////////////////////////////
	//TODO: INSTEAD OF THE HARDCODED true BELLOW
	//      use cached avatar whenever the user hasn't saved his  
	//		avatar using the avatar-studio, noted in session cookie
	//      (or live avatar if he has)
	///////////////////////////////////////////////////////////////////
	//var isAvatarChanged = true;
	///////////////////////////////////////////////////////////////////
    //debugger;
    var useLiveProcessingURL = this.useLiveProcessing();
    var avatarBaseURLSetting = (useLiveProcessingURL) ? "tinyAvatarLiveURL" : "tinyAvatarCashedURL" ;

	//var avatarBaseURLSetting = (isAvatarChanged) ? "tinyAvatarLiveURL" : "tinyAvatarCachedURL" ;
	var avatarURL = this.settings[avatarBaseURLSetting];
	this.log.debug("Selected avatarBaseURL : " + avatarURL );
	//this.log.debug("Avatar changed: [" + isAvatarChanged + "], selected avatarBaseURL : " + avatarURL );

	avatarURL = this.appendUserIdentification(avatarURL);
	this.log.debug("avatarURL : " + avatarURL );

	this.fav.settings.flashvars = ["CurrentAvatarUrl=" , escape( avatarURL)
	                              ,"&showmytypes="     , escape( this.settings.showTypes )
	                              ].join("");
								  
	this.log.info("prepared flashvars: " + this.fav.settings.flashvars );

	this.fav.settings.flashvars = this.fav.settings.flashvars.replace(/https/gi, 'http');
    this.log.info("flashvars:" + this.fav.settings.flashvars );

	//this.dispatch("onRender");
	this.HTML = this.fav.render( targetElement );

	this.log.debug("avatar viewer HTML: " + this.HTML.replace(/\</g,"&lt;"));
	return this.HTML;
}