// Create package
if( ! pokernews ) { 
	var pokernews = {}; 
}

if( ! pokernews.widget ) { 
	pokernews.widget = {}; 
}

/**
 * Flash detector static class
 */
pokernews.widget.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 );
					pokernews.widget.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] ) ) ) {
				pokernews.widget.FlashDetector.version = parseInt( matches[1] );
			}
		}
	},
	
	
	/**
	 * Get version of flash player
	 *
	 * @return {Number} Detected version, -1 if not detected
	 */
	getVersion : function () { 
		
		if( ! pokernews.widget.FlashDetector.initialized ) {
			pokernews.widget.FlashDetector.initialize();
			pokernews.widget.FlashDetector.initialized = true;
		}

		return pokernews.widget.FlashDetector.version;
	}
};


/**
 * Flash tag class
 */
pokernews.widget.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				= "flash" + ( Math.random().toString() ).split( "." )[1];
	
	/** @type {Object} Flash vars */
	this.vars			= {};
	
	/** @type {Boolean} Automatically play? */
	this.play			= true;
	
	/** @type {Boolean} Display menu ? */
	this.menu			= false;
	
	/** @type {Boolean} Loop movie? */
	this.loop			= true;
	
	/** @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 {Boolean} Allow full screen mode */
	this.allowFullScreen = true;

}

/** @type {Boolean} use <embed>? */
pokernews.widget.FlashTag.useEmbed = ( navigator.userAgent.toLowerCase().indexOf( "msie" ) == -1 ) && document.embeds;

/**
 * Get flash tag html 
 *
 * @return {String} flash tag html
 */
pokernews.widget.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	= 	'<param name="allowScriptAccess" value="always" />';
	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 + '"';
	}
		
	// Id must be always set!
	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.base ) {
		paramsHtml += '<param name="allowFullScreen" value="true" />';
		embedHtml  += ' allowFullScreen="true"';
	}
	
	// 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 + '"';
	}
	
	// Embeds? Return embed html, else - return object html
	return pokernews.widget.FlashTag.useEmbed ? ( embedHtml + " />" ) : ( objectHtml + ">" + paramsHtml + "</object>" );
}
	
/**
 * Write to html element
 *
 * @param {String} id Id of element to write to
 * @return {String} flash tag html
 */
pokernews.widget.FlashTag.prototype.writeToElement = function( id ) {
		
	// Try to write to element, and initialize some stuff
	try {
		document.getElementById( id ).innerHTML = this.getHtml();
	} catch( error ) {
		// Error
	}
}


/**
 * Odds calculator static class
 */
pokernews.widget.OddsCalculator = {

	/** @type {String} loader url */
	loaderUrl	: "http://www.pokernews.com/swf/promo/odd_calc.swf",

	/**
	 * Create widget
	 *
	 * @param {String} hash unique hash of widget
	 * @param {String} skin skin name
	 * @param {String} promo promo name
	 */
	createFlash : function( hash, skin, promo, lang, version ) {
	
		if( ! lang ) {
			lang = "en";
		}
		
		var OddsCalculator = pokernews.widget.OddsCalculator;
		
		if( pokernews.widget.FlashDetector.getVersion() >= 9 ) {

			var flashTag = new pokernews.widget.FlashTag();
			flashTag.src = OddsCalculator.loaderUrl;
			flashTag.wmode = "transparent";
			flashTag.version = 9;
			flashTag.width = skin.split( "x" )[0];
			flashTag.height = skin.split( "x" )[1];
			flashTag.vars.skin = skin + ( ( promo == "pstrategy" ) ? "" : ( "." + promo ) );
			flashTag.vars.lang = lang;
			flashTag.vars.promo = promo;

			if (version >= 2) {
				document.write(flashTag.getHtml());
			}
			else {
				flashTag.wmode = "window";
				var element = document.getElementById( hash );
				if( element ) {
					element.innerHTML += flashTag.getHtml();
				}
			}
		}

	}

 }
 
 /**
 * Exchange widget
 */
pokernews.widget.Exchange = {
 
	/** @type {Object} promo htmls */
	promoHtmls	: 	{
						"psexchange" : {
						
							"en" : {
								
								"375x312" : '<div id="%HASH%">\n' + 
											'\t<a href="http://www.pokernews.com/pokerstars/" title="PokerStars">PokerStars</a>\n' + 
											'\t<a href="http://www.pokernews.com/exchange/" title="PokerStars Exchange">PokerStars Exchange</a>\n' + 
											'</div>\n' +
											'<script type="text/javascript" src="http://www.pokernews.com/js/pokernews.widget.js"></script>\n' +
											'<script type="text/javascript">pokernews.widget.Exchange.createFlash( "%HASH%", "375x312", "psexchange", "en" );</script>'
							},
							
							"de" : {

								"375x312" : '<div id="%HASH%">\n' +
											'\t<a href="http://de.pokernews.com/pokerstars/" title="PokerStars">PokerStars</a>\n' +
											'\t<a href="http://www.pokernews.com/exchange/" title="PokerStars Exchange">PokerStars Exchange</a>\n' +
											'</div>\n' +
											'<script type="text/javascript" src="http://www.pokernews.com/js/pokernews.widget.js"></script>\n' +
											'<script type="text/javascript">pokernews.widget.Exchange.createFlash( "%HASH%", "375x312", "psexchange", "de" );</script>'
							},

                            "it" : {

								"375x312" : '<div id="%HASH%">\n' +
											'\t<a href="http://it.pokernews.com/pokerstars/" title="PokerStars">PokerStars</a>\n' +
											'\t<a href="http://www.pokernews.com/exchange/" title="PokerStars Exchange">PokerStars Exchange</a>\n' +
											'</div>\n' +
											'<script type="text/javascript" src="http://www.pokernews.com/js/pokernews.widget.js"></script>\n' +
											'<script type="text/javascript">pokernews.widget.Exchange.createFlash( "%HASH%", "375x312", "psexchange", "it" );</script>'
							},
							
							"ru" : {
							
								"375x312" : '<div id="%HASH%">\n' + 
											'\t<a href="http://ru.pokernews.com/pokerstars/" title="PokerStars">PokerStars</a>\n' + 
											'\t<a href="http://www.pokernews.com/exchange/" title="Обменник PokerStars">Обменник PokerStars</a>\n' + 
											'</div>\n' +
											'<script type="text/javascript" src="http://www.pokernews.com/js/pokernews.widget.js"></script>\n' +
											'<script type="text/javascript">pokernews.widget.Exchange.createFlash( "%HASH%", "375x312", "psexchange", "ru" );</script>'
							}
						}
					},

	/** @type {String} loader url */
	loaderUrl	: "http://www.pokernews.com/swf/promo/exchange.swf",
	
	/**
	 * Create widget
	 *
	 * @param {String} hash unique hash of widget
	 * @param {String} skin skin name
	 * @param {String} promo promo name
	 */
	createFlash : function( hash, skin, promo, lang ) {
	
		if( ! lang ) {
			lang = "en";
		}
		
		var Exchange = pokernews.widget.Exchange;
		
		if( pokernews.widget.FlashDetector.getVersion() >= 9 ) {
		
			var element = document.getElementById( hash );
			
			if( element ) {
				
				var flashTag = new pokernews.widget.FlashTag();
				flashTag.src = Exchange.loaderUrl;
				flashTag.version = 9;
				flashTag.width = skin.split( "x" )[0];
				flashTag.height = skin.split( "x" )[1];
				flashTag.vars.skin = skin;
				flashTag.vars.lang 	= lang;
				flashTag.vars.promo = promo;

				element.innerHTML = flashTag.getHtml();
			}
		}
	},
	
	/**
	 * Create code
	 *
	 * @param {String} hash unique hash of widget
	 * @param {String} skin skin name
	 * @param {String} promo promo name
	 */
	createCode : function( skin, promo, lang ) {
	
		if( ! lang ) {
			lang = "en";
		}
		
		var Exchange = pokernews.widget.Exchange;
		var retVal = "";
		
		if( Exchange.promoHtmls[promo] && Exchange.promoHtmls[promo][lang] && Exchange.promoHtmls[promo][lang][skin] ) {
			
			var hash = "";
			
			for( var i = 0; i < 16; i++ ) {
				hash += ( "abcdef0123456789" ).charAt( Math.round( Math.random() * 15 ) );
			}
			
			retVal = Exchange.promoHtmls[promo][lang][skin].split( "%HASH%" ).join( hash );
		}
		
		return retVal;
	}
 
 }