var VBS_Result = false;

function CNN_FlashDetect() { }

CNN_FlashDetect.prototype.maxVersionToDetect = 8;
CNN_FlashDetect.prototype.minVersionToDetect = 3;

CNN_FlashDetect.prototype.hasPlugin = ( navigator.mimeTypes &&
		navigator.mimeTypes.length &&
		navigator.mimeTypes["application/x-shockwave-flash"] &&
		navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin );

CNN_FlashDetect.prototype.hasActiveX = ( window.ActiveXObject ||
		( ( navigator.userAgent &&
			( navigator.userAgent.indexOf("MSIE") != -1 ) ) &&
		  ( navigator.appVersion &&
			( navigator.appVersion.indexOf("Win") != -1 ) ) ) );

CNN_FlashDetect.prototype.getVersion = function () {
	var versionNum = 0;
	if ( this.hasPlugin ) {
		if ( navigator.plugins && navigator.plugins.length && navigator.plugins["Shockwave Flash"] ) {
			var words = navigator.plugins["Shockwave Flash"].description.split( " " );
			for ( var i = 0; i < words.length; ++i ) {
				if ( isNaN( parseInt( words[i] ) ) )
					continue;
				versionNum = words[i];
			}
		}
	} else if ( this.hasActiveX ) {
		VBS_Result = false;
		for ( var j = this.maxVersionToDetect; j >= this.minVersionToDetect && !VBS_Result; versionNum = j-- ) {
			execScript( 'on error resume next: VBS_Result = IsObject( CreateObject( "ShockwaveFlash.ShockwaveFlash.' + j + '" ) )', 'VBScript' );
		}
	}
	return ( versionNum );
}

CNN_FlashDetect.prototype.detectVersion = function ( num ) {
	var isVersionSupported = false;
	if ( ! isNaN( num ) ) {
		isVersionSupported = ( this.getVersion() >= parseInt( num ) );
	}
	return ( isVersionSupported );
}


function CNN_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
CNN_FlashObject.prototype.m_name = '';
CNN_FlashObject.prototype.m_src = '';
CNN_FlashObject.prototype.m_width = '';
CNN_FlashObject.prototype.m_height = '';
CNN_FlashObject.prototype.m_flashVars = '';

CNN_FlashObject.prototype.m_params = {
	menu:		"false",
	quality:	"high",
	wmode:		"transparent"
};

CNN_FlashObject.prototype.setParam = function ( p_name, p_value ) {
	this.m_params[ p_name.toLowerCase() ] = p_value;
}

CNN_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] );
			}
		}
	}
}

CNN_FlashObject.prototype.getParam = function ( p_name ) {
	return ( this.m_params[ p_name.toLowerCase() ] );
}

CNN_FlashObject.prototype.getParams = function () {
	return ( this.m_params );
}

CNN_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 );
}

CNN_FlashObject.prototype.getAttributeString = function ( p_attr, p_value ) {
	return ( p_value ? ' ' + p_attr + '="' + p_value + '"' : '' );
}

CNN_FlashObject.prototype.getParamTag = function ( p_name, p_value ) {
	return ( p_value ? '<param name="' + p_name + '" value="' + p_value + '">' : '' );
}

CNN_FlashObject.prototype.getHtml = function () {
	var htmlString = '';
	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 ( var oParam in this.getParams() ) {
		htmlString += this.getParamTag( oParam, this.getParam( oParam ) );
	}
	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 ( var eParam in this.getParams() ) {
		htmlString += this.getAttributeString( eParam, this.getParam( eParam ) );
	}
	htmlString += this.getAttributeString( 'flashVars', this.getFlashVarsString() );
	htmlString += '>';

// close embed
	htmlString += '<\/embed>';

// close object
	htmlString += '<\/object>';

	return ( htmlString );
}

CNN_FlashObject.prototype.writeHtml = function () {
	document.write( this.getHtml() );
}

function TPVidModuleUI(sFeed, sNameID)
{
	this.m_feed = sFeed;
    this.STATIC_IMAGE_PATH = "http://i.cnn.net/nascar/.element/img/1.1/sect/video/noflash_image.gif"; // TODO
	// this.COOKIE_DOMAIN="turner.com"; // needed if using on site runs on multiple domain names
    //this path needs to be changed -- jw
    this.NAME = sNameID;
    this.VERSION = 1;
    this.SWF_PATH = "http://i.cnn.net/nascar/.element/swf/2.0/home/tp_vidmodule.swf"; // TODO;
    //this path needs to be changed -- jw
}


TPVidModuleUI.prototype.logSession = function()
{
	// no expiration is set so the cookie is transient
	document.cookie = this.NAME + "=exists; path=/;"; //+ " domain=."+this.COOKIE_DOMAIN;
}


TPVidModuleUI.prototype.doesSessionExist = function()
{
	return ( document.cookie.indexOf(this.NAME + "=exists") != -1 );
}


// Not being used. 
// We are using Macromedia's getFlashVersion() code instead -- kd
TPVidModuleUI.prototype.getSWFPlayerVersion = function() 
{
    var version;
    // TODO :: INSERT CODE HERE
    return version; // integer
}


TPVidModuleUI.prototype.isAolBrowser = function()
{
    return (navigator.userAgent.toLowerCase().indexOf("aol") != -1); // boolean
}


TPVidModuleUI.prototype.getSWFParams = function(sFilePath, fVersion)
{
    var s = "";
    s+='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
    s+='codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ';
    s+='width="300" height="250" id="' + this.NAME + '">';
	s+='<param name="movie" value="' + sFilePath + '?NASCAR_version='+fVersion+'">'; 
    s+='<param name="quality" value="high">';
	s+='<param name="menu" value="true">';
    s+='<param name="FlashVars" value="NASCAR_feed='+this.m_feed+'&NASCAR_sessionExists='+(this.doesSessionExist() ? "true" : "false" )+'">';
	s+='<param name="bgcolor" value="#000000">';
    s+='<embed src="'+sFilePath +'?NASCAR_version='+fVersion+'" quality="high" menu="false" name="' + this.NAME + '" swLiveConnect="true"';
    s+='bgcolor="#000000" width="300" height="250" type="application/x-shockwave-flash" ';
	s+='FlashVars="NASCAR_feed='+this.m_feed+'&NASCAR_sessionExists='+(this.doesSessionExist() ? "true" : "false" )+'" ';
    s+='pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">'
    s+='</embed></object>';
    return s;
}


TPVidModuleUI.prototype.getControl = function(){

	var control;
	if (navigator.appName.indexOf ("Microsoft") !=-1) 
	{
		control = window[ this.NAME ];
	}	
	else 
	{
		control = document[ this.NAME ];
	}
	return control;
}

TPVidModuleUI.prototype.setSoundEnabled = function(bIsSoundEnabled){

	this.getControl().SetVariable("NASCAR_isSoundOn", (bIsSoundEnabled) ? "true" : "false");
}


TPVidModuleUI.prototype.getImageParams = function(){
    
    // TODO :: wrap image with link tags to go get flashplayer or
    //whatever usecase defines
    var s = '<a href="http://www.macromedia.com/go/getflashplayer" target="new"><img src="' + this.STATIC_IMAGE_PATH+ '" width="196" height="199" border="0" alt=""><\/a>';
    return s;
}


TPVidModuleUI.prototype.embed = function(){

    var embedCode;
    if ( (!this.isAolBrowser() && getFlashVersion() >= 6) || (this.isAolBrowser() && getFlashVersion() > 6) )
    {
       // embed swf
       embedCode = this.getSWFParams(this.SWF_PATH, this.VERSION);

    } else {
        // embed static image file
		embedCode = this.getImageParams();
    }

    document.write(embedCode);

	// log module has been created once
	this.logSession();
}


	var flashVersion = 0;
// -- Macromedia Flash javaScript
function getFlashVersion() {

	var agent = navigator.userAgent.toLowerCase(); 
	
   // NS3 needs flashVersion to be a local variable
   if (agent.indexOf("mozilla/3") != -1 && agent.indexOf("msie") == -1) {
      flashVersion = 0;
   }
   
	// NS3+, Opera3+, IE5+ Mac (support plugin array):  check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		var flashPlugin = navigator.plugins['Shockwave Flash'];
		if (typeof flashPlugin == 'object') { 
			if (flashPlugin.description.indexOf('9.') != -1) flashVersion = 9;
			else if (flashPlugin.description.indexOf('8.') != -1) flashVersion = 8;
			else if (flashPlugin.description.indexOf('7.') != -1) flashVersion = 7;
			else if (flashPlugin.description.indexOf('6.') != -1) flashVersion = 6;
			else if (flashPlugin.description.indexOf('5.') != -1) flashVersion = 5;
			else if (flashPlugin.description.indexOf('4.') != -1) flashVersion = 4;
			else if (flashPlugin.description.indexOf('3.') != -1) flashVersion = 3;
		}
	}

	// IE4+ Win32:  attempt to create an ActiveX object using VBScript
	else if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) >= 4 && agent.indexOf("win")!=-1 && agent.indexOf("16bit")==-1) {
		document.write('<scr' + 'ipt language="VBScript"\> \n');
		document.write('on error resume next \n');
		document.write('dim obFlash \n');
		document.write('set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.7") \n');
		document.write('if IsObject(obFlash) then \n');
		document.write('flashVersion = 7 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.6") end if \n');
		document.write('if flashVersion < 7 and IsObject(obFlash) then \n');
		document.write('flashVersion = 6 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.5") end if \n');
		document.write('if flashVersion < 6 and IsObject(obFlash) then \n');
		document.write('flashVersion = 5 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.4") end if \n');
		document.write('if flashVersion < 5 and IsObject(obFlash) then \n');
		document.write('flashVersion = 4 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.3") end if \n');
		document.write('if flashVersion < 4 and IsObject(obFlash) then \n');
		document.write('flashVersion = 3 \n');
		document.write('end if');
		document.write('</scr' + 'ipt\> \n');
  }
		
	// WebTV 2.5 supports flash 3
	else if (agent.indexOf("webtv/2.5") != -1) flashVersion = 3;

	// older WebTV supports flash 2
	else if (agent.indexOf("webtv") != -1) flashVersion = 2;

	// Can't detect in all other cases
	else {
		flashVersion = flashVersion_DONTKNOW;
	}

	return flashVersion;
}

function writeObject(name, url, w, h){

	if ( new CNN_FlashDetect().detectVersion(8)) {
			var cnn_Video = new CNN_FlashObject(name, url, w, h);
			cnn_Video.writeHtml();
		}
	else {
			document.write("<br><b>You need the Flash 8 Plugin to view this page. <a href='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash'>Click here to download Flash now.</a></b>");
	}
}