// Create package
if( ! myhands ) { 
	var myhands = {}; 
}

if( ! myhands.handplayer ) { 
	myhands.handplayer = {}; 
}

/**
 * Flash detector static class
 */
myhands.handplayer.FlashDetector = {
	
	/** @type {Number} Detected flash version, for internal usage only. */
	version : -1,
	
	/** @type {Boolean} FlashDetector initialized? for internal usage only. */
	initialized	: false,
	
	/**
	 * Initialize. Do detection and assign detected version.
	 */
	initialize : function() {
		
		// Having ActiveXObject, try to detect with it
		if( window.ActiveXObject ) {
		
			for( var i = 6; i < 11; i++ ) {
					
				try{
					new window.ActiveXObject( "ShockwaveFlash.ShockwaveFlash." + i );
					myhands.handplayer.FlashDetector.version = i;
				} catch( error ) {
					// Error
				}
			}
		
		// If having plugin
		} else if( navigator.plugins && navigator.plugins["Shockwave Flash"] && navigator.plugins["Shockwave Flash"].description ) {
		
			var matches = navigator.plugins["Shockwave Flash"].description.match( /([0-9]+)\.[0-9]+/ );
			
			if( matches[1] && ! isNaN( parseInt( matches[1] ) ) ) {
				myhands.handplayer.FlashDetector.version = parseInt( matches[1] );
			}
		}
	},
	
	
	/**
	 * Get version of flash player
	 *
	 * @return {Number} Detected version, -1 if not detected
	 */
	getVersion : function () { 
		
		if( ! myhands.handplayer.FlashDetector.initialized ) {
			myhands.handplayer.FlashDetector.initialize();
			myhands.handplayer.FlashDetector.initialized = true;
		}

		return myhands.handplayer.FlashDetector.version;
	}
};


/**
 * Flash tag class
 */
myhands.handplayer.FlashTag = function() {

	/** @type {Number} Requred version */
	this.version		= 7;
	
	/** @type {String} Movie source */
	this.src			= null;
	
	/** @type {Number} Width */
	this.width			= 1;
	
	/** @type {Number} Height */
	this.height			= 1;

	/** @type {String} Id, generated automatically */
	this.id				= null;
	
	/** @type {Object} Flash vars */
	this.vars			= {};
	
	/** @type {Boolean} Automatically play? */
	this.play			= true;
	
	/** @type {Boolean} Display menu ? */
	this.menu			= false;
	
	/** @type {String} Quality - one of low, high, autolow, autohigh, best */
	this.quality		= "high";
	
	/** @type {String} Scaling- one of showall, noborder, exactfit */
	this.scale			= "noscale";
	
	/** @type {String} Stage align - one of l, t, r, b, tl, tr, bl, br */
	this.salign			= null;
	
	/** @type {String} Window mode - one of window, opaque, transparent */
	this.wmode			= "window";
	
	/** @type {String} Background color */
	this.bgcolor		= null;
	
	/** @type {String} Movie base url */
	this.base			= null;
	
	/** @type {String} allow script access mode */
	this.allowScriptAccess = "always";
	
	/** @type {Boolean} Allow full screen mode */
	this.allowFullScreen = true;

}

/**
 * Get flash tag html 
 *
 * @return {String} flash tag html
 */
myhands.handplayer.FlashTag.prototype.getHtml = function() {
	
	// Default values for object, embed and params HTMLs
	var objectHtml 	= 	'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" align="middle"';
	var paramsHtml	= 	'';
	var embedHtml 	= 	'<embed swLiveConnect="true" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"';
	
	// Add size
	var sizeHtml = ' style="width: ' + ( this.width.toString().match( /^[0-9]+$/ ) ? ( this.width + "px" ) : this.width ) + '; height: ' + ( this.height.toString().match( /^[0-9]+$/ ) ? ( this.height + "px" ) : this.height ) + ';"';
	objectHtml += sizeHtml; embedHtml += sizeHtml;
	
	// Add version
	objectHtml += 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + this.version + ',0,0,0"';
		
	// Add src
	if( this.src ) {
		paramsHtml += '<param name="movie" value="' + this.src + '" />';
		embedHtml	+= ' src="' + this.src + '"';
	}
		
	// Add id if set
	if( this.id ) {
		objectHtml 	+= '" id="' + this.id + '"';
		embedHtml 	+= ' name="' + this.id + '" id="' + this.id + '"';
	}
		
	// Add bgcolor
	if( this.bgcolor ) {
		paramsHtml += '<param name="bgcolor" value="' + this.bgcolor + '" />';
		embedHtml 	+= ' bgcolor="' + this.bgcolor + '"';
	}
	
	// Add menu settings only if menu is set to false
	if( ! this.menu ) {
		paramsHtml 	+= '<param name="menu" value="false" />';
		embedHtml 	+= ' menu="false"';
	}
	
	// Add scale
	if( this.scale ) {
		paramsHtml += '<param name="scale" value="' + this.scale + '" />';
		embedHtml  += ' scale="' + this.scale + '"';
	}
	
	// Add quality
	if( this.quality ) {
		paramsHtml += '<param name="quality" value="' + this.quality + '" />';
		embedHtml  += ' quality="' + this.quality + '"';
	}

	// Add play only if set to false
	if( ! this.play ) {
		paramsHtml += '<param name="play" value="false" />';
		embedHtml  += ' play="false"';
	}
	
	// Add stage align
	if( this.salign ) {
		paramsHtml += '<param name="salign" value="' + this.salign + '" />';
		embedHtml  += ' salign="' + this.salign + '"';
	}
	
	// Add window mode
	if( this.wmode ) {
		paramsHtml += '<param name="wmode" value="' + this.wmode + '" />';
		embedHtml  += ' wmode="' + this.wmode + '"';
	}

	// Add base
	if( this.base ) {
		paramsHtml += '<param name="base" value="' + this.base + '" />';
		embedHtml  += ' base="' + this.base + '"';
	}
	
	// Add allowFullScreen
	if( this.allowFullScreen ) {
		paramsHtml += '<param name="allowFullScreen" value="true" />';
		embedHtml  += ' allowFullScreen="true"';
	}
	
	// Add allowScriptAccess
	if( this.allowScriptAccess ) {
		paramsHtml += '<param name="allowScriptAccess" value="' + this.allowScriptAccess + '" />';
		embedHtml  += ' allowScriptAccess="' + this.allowScriptAccess + '"';
	}
	
	// Add flashvars, flash var htmlElementId added automatically
	if( ! this.vars ) {
		this.vars = {};
	}
	
	var varsHtml = [];

	for( var i in this.vars ) {
		varsHtml.push( i + "=" + escape( this.vars[i] ) );
	}
	
	if( varsHtml.length > 0 ) {
		varsHtml = varsHtml.join( "&amp;" );
		paramsHtml 	+= '<param name="flashvars" value="' + varsHtml + '">';
		embedHtml	+= ' flashvars="' + varsHtml + '"';
	}
	
	// Return
	return objectHtml + ">" + paramsHtml + embedHtml + " /></object>";
}
	
/**
 * Write to html element
 *
 * @param {String} id Id of element to write to
 * @return {String} flash tag html
 */
myhands.handplayer.FlashTag.prototype.writeToElement = function( id ) {
		
	// Try to write to element, and initialize some stuff
	try {
		document.getElementById( id ).innerHTML = this.getHtml();
	} catch( error ) {
		// Error
	}
}

/**
 * Widget version 1
 */
myhands.handplayer.WidgetV1 = {

	/** @type {Array} all available skins */
	skins : ["overcards.535x468", "myhands.535x468"],
	
	/**
	 * Get widget html
	 */
	getHtml : function( skin, lang, handId, baseUrl ) {
		
		var retVal = null;
		
		var WidgetV1 = myhands.handplayer.WidgetV1;
		
		if( WidgetV1.skins.join( "" ).indexOf( skin ) >= 0 ) {
		
			var flashTag = new myhands.handplayer.FlashTag();
			
			flashTag.id = null;
			flashTag.src = "http://www.myhands.com/handplayer/v1/" + skin + "/player.swf";
			flashTag.version = 8;
			flashTag.width = skin.split( "." )[1].split( "x" )[0];
			flashTag.height = skin.split( "." )[1].split( "x" )[1];
			flashTag.wmode = "transparent";
			flashTag.vars.skin = skin;
			flashTag.vars.lang = lang;
			flashTag.vars.handId = handId;
			flashTag.vars.locationUrl = document.location.href;
			flashTag.vars.referrerUrl = document.referrer ? document.referrer : "";
			
			flashTag.vars.baseUrl = baseUrl ? baseUrl : "";
			
			retVal = flashTag.getHtml();
		}
		
		return retVal;
	}
}

/**
 * Various utilities
 */
myhands.handplayer.Util = {

	/**
	 * Create random string
	 * 
	 * @return {String}
	 */
	randomString : function( length, chars ) {
		
		if( ! length ) {
			length = 32;
		}
		
		if( ! chars ) {
			chars = "AaBbCcDdEeFfGgHhJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz01234567890";
		}
		
		chars = chars.split( "" );
		
		var retVal = "";
		
		for( var i = 0; i < length; i++ ) {
			retVal += chars[Math.round( Math.random() * ( chars.length - 1 ) )];
		}
		
		return retVal;
	},

	/**
	 * Parse uri and return it's parts:
	 * source, protocol, authority, domain, port, path, directoryPath, fileName, query, anchor
	 */
	parseUri : function ( uri ) {
	
		var retVal 		= {};
		
		var partNames 	= ["source" ,"protocol", "authority", "domain", "port", "path", "directoryPath", "fileName", "query", "anchor"];
		var parts 		= new RegExp( "^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?" ).exec( uri );
		
		// Generate return value
		for( var i = 0; i < partNames.length; i++ ) {
			retVal[partNames[i]] = parts[i] ? parts[i] : "";
		}
		
		// Fixate directory path
		if( retVal.directoryPath.length > 0 ) {
			retVal.directoryPath = retVal.directoryPath.replace( /\/?$/, "/" );
		}
		
		return retVal;
	}
	
}

/**
 * Widget version 2
 */
myhands.handplayer.WidgetV2 = {

	/**
	 * Get widget html
	 * 	Available arguments:
	 *		width 		- flash width in pixels or percents
	 *		height 		- flash height in pixels or percents
	 * 		wmode		- flash window mode
	 *		config		- configuration name to use
	 *		handId		- hand id
	 *		listId		- list id
	 *		searchId	- search id
	 */
	getHtml : function( args ) {
	
		var retVal = null;
		
		// Normalize arguments
		args = ( typeof( args ) == "object" ) ? args : {};
		
		var domain		= "www.myhands.com";
		var uriParts 	= myhands.handplayer.Util.parseUri( document.location.href );
		
		// Development mode domains
		if( ( uriParts.domain.length > 0 ) && ( ( ["danas.myhands6.dev", "serv.myhands.com", "danas.myhands.dev"] ).join( "," ).indexOf( uriParts.domain ) >= 0 ) ) {
			domain = uriParts.domain;
		}
		
		var flashTag 	= new myhands.handplayer.FlashTag();
		
		flashTag.id = "handplayer" + myhands.handplayer.Util.randomString( 16 );
		flashTag.src = "http://" + domain + "/handplayer/v2/preloader.swf?random=" + myhands.handplayer.Util.randomString( 16 );
		flashTag.version = 9;
		flashTag.width = args.width || "100%";
		flashTag.height = args.height || "100%";
		flashTag.wmode = args.wmode || "window";
		flashTag.vars.handId = args.handId;
		flashTag.vars.listId = args.listId;
		flashTag.vars.searchId = args.searchId;
		flashTag.vars.embedMode = args.embedMode ? "1" : "0";
		flashTag.vars.devMode = args.devMode ? "1" : "0";
		
		return flashTag.getHtml();
	}

}

/**
 * Widget version 3
 */
myhands.handplayer.WidgetV3 = {

	/**
	 * Get widget html
	 * 	Available arguments:
	 *		width 		- flash width in pixels or percents
	 *		height 		- flash height in pixels or percents
	 * 		wmode		- flash window mode
	 *		handId		- hand id
	 *		listId		- list id
	 *		searchId	- search id
	 */
	getHtml : function( args ) {
	
		var retVal = null;
		
		// Normalize arguments
		args = ( typeof( args ) == "object" ) ? args : {};
		
		var domain		= "www.myhands.com";
		var uriParts 	= myhands.handplayer.Util.parseUri( document.location.href );
		
		// Development mode domains
		if( ( uriParts.domain.length > 0 ) && ( ( ["danas.myhands6.dev", "serv.myhands.com", "danas.myhands.dev"] ).join( "," ).indexOf( uriParts.domain ) >= 0 ) ) {
			domain = uriParts.domain;
		}
		
		var flashTag 	= new myhands.handplayer.FlashTag();
		
		flashTag.id = "handplayer" + myhands.handplayer.Util.randomString( 16 );
		flashTag.src = "http://" + domain + "/handplayer/v3/preloader.swf?random=" + myhands.handplayer.Util.randomString( 16 );
		flashTag.version = 9;
		flashTag.width = args.width || "100%";
		flashTag.height = args.height || "100%";
		flashTag.wmode = args.wmode || "window";
		flashTag.vars.handId = args.handId;
		flashTag.vars.listId = args.listId;
		flashTag.vars.searchId = args.searchId;
		flashTag.vars.embedMode = args.embedMode ? "1" : "0";
		flashTag.vars.devMode = args.devMode ? "1" : "0";
		
		return flashTag.getHtml();
	}

}