/*

Flash Detect and Render
=======================

The NBA_FlashObject takes a few required arguments...

	name ......... the id/name of the object/embed
	src .......... the URL of the swf
	width ........ (i think this should be required)
	height ....... (i think this should be required)

...and some optional arguments...

	parameters ... this is a "hash" of keys and values
		{ menu: "true", play: "false", loop: "false" }
		(or set this to null or an empty string to skip)

	flashVars .... this is a hash or a string
		{ cs_url: "/football/nfl/scoreboards/today/" }
		- or -
		"cs_url=/football/nfl/scoreboards/today/"


Sample Usage:
if ( new NBA_FlashDetect().detectVersion( 6 ) ) {

	var cnn_Scoreboard = new NBA_FlashObject( "cnnScoreboard",
		"/.element/img/2.0/swf/nfl_scoreboard.swf",
		420, 85, null, "cs_url=/football/nfl/scoreboards/today/" );

	cnn_Scoreboard.writeHtml();

} else {
	document.write( 'alternate html' );
}

Of course, if you plan to have Flash in lots of places on a page,
it might make more sense to make a global variable for the detection.
You could go as far as creating a session-based cookie...

*/

var VBS_Result = false;

function NBA_FlashDetect() { }

NBA_FlashDetect.prototype.maxVersionToDetect = 11;
NBA_FlashDetect.prototype.minVersionToDetect = 3;

NBA_FlashDetect.prototype.hasPlugin = ( navigator.mimeTypes &&
		navigator.mimeTypes.length &&
		navigator.mimeTypes["application/x-shockwave-flash"] &&
		navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin );

NBA_FlashDetect.prototype.hasActiveX = window.ActiveXObject;

NBA_FlashDetect.prototype.hasWinIE = ( navigator.userAgent &&
		( navigator.userAgent.indexOf( "MSIE" ) != -1 ) &&
		navigator.appVersion &&
		( navigator.appVersion.indexOf( "Win" ) != -1 ) );

NBA_FlashDetect.prototype.getVersion = function () {
	var versionNum = 0;
	var i = 0;

	if ( this.hasActiveX ) {
		var activeXObject = false;
		for ( i = this.maxVersionToDetect; i >= this.minVersionToDetect && !activeXObject; versionNum = ( activeXObject ? i : versionNum ), i-- ) {
			try {
				activeXObject = new ActiveXObject( "ShockwaveFlash.ShockwaveFlash." + i );
			} catch( e ) {
				// do nothing
			}
		}
	} else if ( this.hasWinIE ) {
		VBS_Result = false;
		for ( i = this.maxVersionToDetect; i >= this.minVersionToDetect && !VBS_Result; versionNum = ( VBS_Result ? i : versionNum ), i-- ) {
			execScript( 'on error resume next: VBS_Result = IsObject( CreateObject( "ShockwaveFlash.ShockwaveFlash.' + i + '" ) )', 'VBScript' );
		}
	} else if ( this.hasPlugin ) {
		if ( navigator.plugins && navigator.plugins.length && navigator.plugins["Shockwave Flash"] ) {
			var words = navigator.plugins["Shockwave Flash"].description.split( " " );
			for ( i = 0; i < words.length; ++i ) {
				if ( isNaN( parseInt( words[i] ) ) )
					continue;
				versionNum = words[i];
			}
		}
	}

	return ( versionNum );
}

NBA_FlashDetect.prototype.detectVersion = function ( num ) {
	var isVersionSupported = false;

	if ( ! isNaN( num ) ) {
		//The following four lines fix how different browsers display the value for Flash 10+
		var strNum = this.getVersion();
		strNum = String(strNum)+'.';
		var arrNums = strNum.split('.');
		isVersionSupported = ( arrNums[0] >= parseInt( num ) );
	}
	return ( isVersionSupported );
}


function NBA_FlashObject( p_name, p_src, p_width, p_height, p_parameters, p_flashVars ) {
	this.m_name			= p_name;
	this.m_src			= p_src;
	this.m_width		= p_width;
	this.m_height		= p_height;
	this.m_flashVars	= p_flashVars;

// constructor
	if ( p_parameters )
	{
		this.setParams( p_parameters );
	}
}

// Declare member properties
NBA_FlashObject.prototype.m_name = '';
NBA_FlashObject.prototype.m_src = '';
NBA_FlashObject.prototype.m_width = '';
NBA_FlashObject.prototype.m_height = '';
NBA_FlashObject.prototype.m_flashVars = '';

NBA_FlashObject.prototype.m_params = {
	menu:		"false",
	quality:	"high",
	allowScriptAccess:		"always",
	wmode:		"transparent"

};

NBA_FlashObject.prototype.setParam = function ( p_name, p_value ) {
	this.m_params[ p_name ] = p_value;
}

NBA_FlashObject.prototype.setParams = function ( p_paramHash ) {
	if ( typeof p_paramHash == "object" ) {
		for ( var param in p_paramHash ) {
			if ( p_paramHash[param] ) {
				this.setParam( param, p_paramHash[param] );
			}
		}
	}
}

NBA_FlashObject.prototype.getParam = function ( p_name ) {
	return ( this.m_params[ p_name ] );
}

NBA_FlashObject.prototype.getParams = function () {
	return ( this.m_params );
}

NBA_FlashObject.prototype.getFlashVarsString = function () {
	var flashVarsString = '';

	if ( typeof this.m_flashVars == "string" ) {
		flashVarsString = this.m_flashVars;
	} else if ( typeof this.m_flashVars == "object" ) {
		for ( var flashVar in this.m_flashVars ) {
			if ( flashVarsString != '' ) {
				flashVarsString += "&";
			}
			flashVarsString += flashVar + "=" + escape( this.m_flashVars[flashVar] );
		}
	}

	return ( flashVarsString );
}

NBA_FlashObject.prototype.getAttributeString = function ( p_attr, p_value ) {
	return ( p_value ? ' ' + p_attr + '="' + p_value + '"' : '' );
}

NBA_FlashObject.prototype.getParamTag = function ( p_name, p_value ) {
	return ( p_value ? '<param name="' + p_name + '" value="' + p_value + '">' : '' );
}

NBA_FlashObject.prototype.getHtml = function () {
	var htmlString = '';
	var eachParam = '';
	var flashUrl = 'http://www.macromedia.com/go/getflashplayer';

// open object
	htmlString += '<object type="application/x-shockwave-flash" \
					classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
	htmlString += this.getAttributeString( 'pluginspage', flashUrl );
	htmlString += this.getAttributeString( 'id', this.m_name );
	htmlString += this.getAttributeString( 'data', this.m_src );
	htmlString += this.getAttributeString( 'width', this.m_width );
	htmlString += this.getAttributeString( 'height', this.m_height );
	htmlString += '>';
	htmlString += this.getParamTag( 'movie', this.m_src );
	for ( eachParam in this.getParams() ) {
		htmlString += this.getParamTag( eachParam, this.getParam( eachParam ) );
	}
	htmlString += this.getParamTag( 'flashVars', this.getFlashVarsString() );

// open embed
	htmlString += '<embed type="application/x-shockwave-flash"';
	htmlString += this.getAttributeString( 'pluginspage', flashUrl );
	htmlString += this.getAttributeString( 'name', this.m_name );
	htmlString += this.getAttributeString( 'src', this.m_src );
	htmlString += this.getAttributeString( 'width', this.m_width );
	htmlString += this.getAttributeString( 'height', this.m_height );
	for ( eachParam in this.getParams() ) {
		htmlString += this.getAttributeString( eachParam, this.getParam( eachParam ) );
	}
	htmlString += this.getAttributeString( 'flashVars', this.getFlashVarsString() );
	htmlString += '>';

// close embed
	htmlString += '<\/embed>';

// close object
	htmlString += '<\/object>';

	return ( htmlString );
}

NBA_FlashObject.prototype.writeHtml = function () {
	document.write( this.getHtml() );
}

NBA_FlashObject.prototype.writeMosaicHtml = function (id) {
	document.getElementById(id).innerHTML =  this.getHtml();
}

/* Misc Flash Functions */

function getFlashMovieObject(strName) {
	if (window.document[strName]) {
		return window.document[strName];
	}

	if (navigator.appName.indexOf("Microsoft Internet")==-1) {
		if (document.embeds && document.embeds[strName]){
			return document.embeds[strName]; 
		}
	} else {
		return document.getElementById(strName);
	}
}

function setFlashXMLFile(strVal) {

	//Check if a fully qualified URL was given
	if (strVal.search('http://') == -1) {

		//Check to see if we are in dev or production
		var strHostname = window.location.hostname;

		if (strHostname == 'www.nba.com' || strHostname == 'nba.com') {
			strVal = 'http://i.cdn.turner.com/nba'+strVal;
		} else {
			strVal = 'http://'+strHostname+strVal;
		}
	}

	//Get the current time to break caching
	
	return strVal; 
}

function displayNoFlashWarning(strVersion) {
	document.write("<div>");
	document.write("<div id=\"nbaVPFlashLargeContainer\" style=\"width:150px;height:250px;margin-top:25px;margin-left:394px;text-align:center;\"><div class=\"nbaNoFlashContainer\"><div class=\"nbaNoFlash\"><p class=\"nbaTopGraf\">This NBA.com feature is optimized for Adobe Flash Player version " + strVersion + " or higher.</p><a style=\"color:ee0435;\" href=\"http://www.adobe.com/go/getflashplayer\" target=\"_new\"><b>Install Flash Player</b></a></div></div></div>");
	document.write("</div>");
}

function flashMissingWarning(strVersion) {
	document.write("<div>");
	document.write("<div id=\"nbaVPFlashLargeContainer\" style=\"width:62px;height:278px;\"><div class=\"nbaNoFlashContainer\"><div class=\"nbaNoFlash\"><p class=\"nbaTopGraf\">This NBA.com feature is optimized for Adobe Flash Player version " + strVersion + " or higher.</p><a style=\"color:ee0435;\" href=\"http://www.adobe.com/go/getflashplayer\" target=\"_new\"><b>Install Flash Player</b></a></div></div></div>");
	document.write("</div>");
}
