/**
 * FlashDetector class
 * Detected player versions: 6 - 10
 */
var FlashDetector = {
	
	/**
	 * Detected flash version
	 *
	 * @access private
	 * @static
	 * @var Number If -1 not detected
	 */
	version 		: -1,
	
	/**
	 * FlashDetector initialized?
	 *
	 * @access private
	 * @static
	 * @var Boolean
	 */
	initialized		: false,
	
	/**
	 * Initialize. Do detection and assign detected version
	 * to FlashDetector.version
	 *
	 * @access private
	 * @static
	 * @return Void
	 */
	initialize : function() {
	
		// Having ActiveXObject, try to detect with it
		if( window.ActiveXObject ) {
		
			for( var i = 6; i < 10; i++ ) {
					
				try{
					new window.ActiveXObject( 'ShockwaveFlash.ShockwaveFlash.' + i );
					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] ) ) ) {
				FlashDetector.version = parseInt( matches[1] );
			}
		
		}
		
	},
	
	/**
	 * Get version of flash player
	 *
	 * @access public
	 * @static
	 * @return Number detected version, -1 if not detected
	 */
	getVersion : function () { 
		
		if( ! FlashDetector.initialized ) {
			FlashDetector.initialize();
			FlashDetector.initialized = true;
		}
	
		return FlashDetector.version;
	
	}

}

/**
 * Flash tag class
 */
var FlashTag = function() {

	/**
	 * Requred version
	 *
	 * @access public
	 * @var Number
	 */
	this.version		= 7;
	
	/**
	 * Movie source
	 *
	 * @access public
	 * @var String
	 */
	this.src			= null;
	
	/**
	 * Object width
	 *
	 * @access public
	 * @var Number
	 */
	this.width			= 1;
	
	/**
	 * Object height
	 *
	 * @access public
	 * @var Number
	 */
	this.height			= 1;
	
	/**
	 * Object id
	 *
	 * @access public
	 * @var String
	 */
	this.id				= null;
	
	/**
	 * Flash vars
	 *
	 * @access public
	 * @var Object
	 */
	this.vars			= {};
	
	/**
	 * Automatically play
	 *
	 * @access public
	 * @var Boolean
	 */
	this.play			= true;
	
	/**
	 * Display menu
	 *
	 * @access public
	 * @var Boolean
	 */
	this.menu			= false;
	
	/**
	 * Loop movie
	 *
	 * @access public
	 * @var Boolean
	 */
	this.loop			= true;
	
	/**
	 * Quality
	 *
	 * @access public
	 * @var String one of low, high, autolow, autohigh, best 
	 */
	this.quality		= 'high';
	
	/**
	 * Scaling
	 *
	 * @access public
	 * @var String one of showall, noborder, exactfit
	 */
	this.scale			= 'noscale';
	
	/**
	 * Stage align
	 *
	 * @access public
	 * @var String one of l, t, r, b, tl, tr, bl, br
	 */
	this.salign			= 'tl';
	
	/**
	 * Window mode
	 * @access public
	 * @var String one of window, opaque, transparent
	 */
	this.wmode			= 'window';
	
	/**
	 * Background color
	 * @access public
	 * @var String 
	 */
	this.bgcolor		= null;
	
	/**
	 * Movie base url
	 * @access public
	 * @var String 
	 */
	this.base			= null;
	
	/**
	 * Use detection
	 *
	 * @access public
	 * @var Boolean
	 */
	this.detect					= true;
	
	/**
	 * When detection is used this HTML is used when
	 * there is no player
	 *
	 * @access public
	 * @var String
	 */
	this.noPlayerHtml 			= null;
	
	/**
	 * When detection is used this HTML is used when
	 * there is no needed player version
	 *
	 * @access public
	 * @var String
	 */
	this.noPlayerVersionHtml 	= null;

}

FlashTag.prototype = {

	/**
	 * Get flash tag html 
	 *
	 * @return String flash tag html
	 */
	getHtml : function() {
	
		var retVal = '';
		
		// If detection and no player
		if( this.detect && ( FlashDetector.getVersion() == -1 ) ) {
			
			retVal = this.noPlayerHtml;
			
		// If detection and no player version
		} else if( this.detect && ( FlashDetector.getVersion() < this.version ) ) {
		
			retVal = this.noPlayerVersionHtml;
		
		} else {
		
			// 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="sameDomain" />';
			var embedHtml 	= 	'<embed swLiveConnect="true" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"';
		
			// Add size
			var sizeHtml = ' width="' + this.width + '" height="' + 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( 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 flashvars
			if( this.vars ) {

				var varsHtml = [];

				for( var i in this.vars ) {
					varsHtml.push( i + '=' + encodeURI( this.vars[i] ) );
				}
				
				if( varsHtml.length > 0 ) {
					varsHtml = varsHtml.join( '&amp;' );
					paramsHtml 	+= '<param name="flashvars" value="' + varsHtml + '">';
					embedHtml	+= ' flashvars="' + varsHtml + '"';
				}

			}

			// Add metacafe.com flashvars
			if( this.metacafe ) {

				var varsHtml = [];

				for( var i in this.metacafe ) {
					varsHtml.push( i + '=' + encodeURI( this.metacafe[i] ) );
				}

				if( varsHtml.length > 0 ) {
					varsHtml = varsHtml.join( '|' );
					paramsHtml 	+= '<param name="flashvars" value="playerVars=' + varsHtml + '">';
					embedHtml	+= ' flashvars="playerVars=' + varsHtml + '"';
				}

			}

			// Create return value
			retVal = objectHtml + '>' + paramsHtml + embedHtml + ' /></object>';
			
		}
		
		return retVal;
	
	},

	/**
	 * Write flash tag html to document
	 *
	 * @return String flash tag html
	 */
	writeToDocument : function() {
	
		try {
			document.write( this.getHtml() );
		} catch( error ) {
			// Error
		}
	
	},
	
	/**
	 * Write flash tag html to element
	 *
	 * @param String id Id of element to write to
	 * @return String flash tag html
	 */
	writeToElement : function( id ) {
	
		try {
			document.getElementById( id ).innerHTML = this.getHtml();
		} catch( error ) {
			// Error
		}
	
	}

}

function showFlash( source ) {
	document.write( source );
}
function skipFlash( id, width, height ) {
	$( id ).innerHTML = '<div style="width:'+width+'px; height:'+height+'px;"></div>'
	
}