var g_lastPath = null;

// init the global javascript params, it is called by swf application
function consoleProxy(action, value)
{
	switch(action)
	{
		case "detectPlugins":
			var fit = true;
			var isMac = (navigator.platform.indexOf("Mac") == 0);
			if (!g_isIE && !isMac)
			{
				var fileName = navigator.mimeTypes["application/x-mplayer2"].enabledPlugin.filename;
				if (fileName != "np-mswmp.dll") fit = false;
			}
			return fit; 
		case "changeTitle":
			document.title = "" + value;
			break;
		case "analytics":
			if (value) 
				doTrack("sl/analytics.htm","code=" + value);
			break;
		case "nielsen":
			doTrack("/mediaPlayer/nielsen.dbml", "sccid=" + sccid + "&oemid=" + oemid);
			break;
		case "trackevent":
			var src = "code=" + value[0] + "&category=" + value[1] + "&action=" + value[2];
			if (value[3] != "")
				src += "&label=" + value[3] + (value[4] != "" ? "&value=" + value[4] : "");
    		src += "&ord=" + Math.ceil(Math.random()*1000000000);
    		//alert(src);
			doTrack("sl/trackevent.htm", src);
			break;
		case "trackpageview":
			var src = "code=" + value[0];
			if (value[1] != "")
				src += "&page=" + value[1];
    		src += "&ord=" + Math.ceil(Math.random()*1000000000);
    		//alert(src);
			doTrack("sl/trackevent.htm", src);
			break;
		case "playerInit":
			var useSL = (value[0] == "sl");
			if (g_preIntelMac) useSL = false;
			var ret = (useSL ? "sl/console.htm?" : "consolewmp.htm?") + value[1];
			if (g_embed) ret = (useSL ? "sl/embed.htm?" : "embedwmp.htm?") + value[1];
			window.location.href = ret;
			break;
	}
}

// transfer the companion image url/link into swf application
function consoleCallback(action, value)
{
	// alert("action " + action + ", value " + value);
	var swfConsole = document.getElementById("console");
	if (swfConsole && swfConsole.consoleCallback) swfConsole.consoleCallback(action, value);
}

function playWMV(path, x, y, volume)
{
	resetAdCom();
	//alert("playWmv " + path);
	g_lastPath = path;
	
	var wmp = document.getElementById("wmppane");
	wmp.style.display = "";
	
	if(g_isIE)
	{
		wmp = getPlayer();
		wmp.url = path;
		wmp.settings.volume = volume;
	}
	else if (wmp.contentWindow)
		wmp.contentWindow.writeWMP(path,360,null,null,null,null,null,null,false);	
	else if (writeWMP)	
		writeWMP(path, 360, true, 480, null, "wmppane0");
}

function getQuery(url, name)
{
 	var pos = url.indexOf("?");
 	if (pos <= 0) return "";
 	var query = url.substr(pos + 1);
	var nvs = query.split("&");
	if(nvs.length>0)
	{
		for(var i=0;i<nvs.length;i++)
		{
			var kv = nvs[i].split("=");
			if (kv.length == 2 && kv[0] == name) return kv[1];
		}
	}
	return "";
}

function getPlayer()
{
	var wmp = null;
	try{
		if (g_isIE)
		{
			if (document.getElementById("wmppane").contentWindow)
				wmp = document.getElementById("wmppane").contentWindow.document.getElementById("wmpMain");
			else
				wmp = document.getElementById("wmpMain");
		}
		else
		{
			if (document.getElementById("wmppane").contentWindow)
				wmp = document.getElementById("wmppane").contentWindow.document.getElementById("wmpMain-ff");
			else
				wmp = document.getElementById("wmpMain-ff");
		}
	}catch(e){}
	return wmp;
}

function wmpProxy(action, value)
{
	var wmp = getPlayer();	
	if (wmp == null) return;
	
	switch(action)
	{
	case "fullscreen":
		if( !g_isIE) wmp.DisplaySize=3;
		wmp.fullScreen = true;
		break;
	case "stop":
		wmp.controls.stop();
		break;
	case "fastReverse":
		wmp.controls.fastReverse();
		break;
	case "fastForward":
		wmp.controls.fastForward();
		break;
	case "pause":
		wmp.controls.pause();
		break;
	case "play":
		wmp.controls.play();
		break;
	case "setvolume":
		wmp.settings.volume = value;
		break;
	case "getvolume":
		return wmp.settings.volume;
		break;
	case "shareshow":
		document.getElementById("wmppane").style.display = "none";
		break;
	case "sharehide":
		document.getElementById("wmppane").style.display = "";
		break;
	case "position":
		var p = document.getElementById("wmppane");
		p.style.left = value[0] + "px";
		p.style.top = (parseInt(value[1]) + 90) + "px";
		break;
	case "seek":
		value = parseInt(value);
		wmp.controls.currentposition = value;
		break;
	case "getcurtime":
		return "" + Math.floor(wmp.controls.currentPosition);
		break;
	case "gettoltime":
		return (wmp.currentMedia ? "" + Math.floor(wmp.currentMedia.duration) : "0");
		break;
	case "refreshtime":
		return (wmp.currentMedia ? ["" + Math.floor(wmp.controls.currentPosition), "" + Math.floor(wmp.currentMedia.duration)] : ["0", "0"]);
		break;
	case "getDownloadProgress":
		return "" + Math.floor(wmp.network.downloadProgress);
		break;
	case "ecm":
		wmp.enableContextMenu = true;
		break;
	case "hide":
		if(g_isIE)
			wmp.controls.stop();
		else if (document.getElementById("wmppane").contentWindow)
			document.getElementById("wmppane").contentWindow.writeWMP("");
		else if (writeWMP)
			writeWMP("");
		document.getElementById("wmppane").style.display = "none";
	}
}

function wmpCallback(action, value)
{
	var swfConsole = document.getElementById("console");
	if (!value) value = "";
	if (swfConsole && swfConsole.wmpCallback) swfConsole.wmpCallback(action, value);
}

function getPageUrl()
{
	var url = window.location.href;
	var i = url.indexOf("?");
	if(i>0)
		url = window.location.href.substring(0,i);
	return url.substring(0,url.lastIndexOf("/")+1);
}

function writeFlexPlayer(isProduction, embed, inline)
{
	var serverStr = "server=" + g_server;
	var consoleStr = "console";
	if(embed)
		consoleStr = "embed";
	else if(!isNaN(inline))
		consoleStr = "inline"+inline;
	if(isProduction)
	{
		consoleStr = "console-debug";
	}
	if ( hasProductInstall && !hasRequestedVersion ) {
		// MMdoctitle is the stored document.title value used by the installation process to close the window that started the process
		// This is necessary in order to close browser windows that are still utilizing the older version of the player after installation has completed
		// DO NOT MODIFY THE FOLLOWING FOUR LINES
		// Location visited after installation is complete if installation is required
		var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
		var MMredirectURL = window.location;
	    document.title = document.title.slice(0, 47) + " - Flash Player Installation";
	    var MMdoctitle = document.title;
	
		AC_FL_RunContent(
			"src", "scripts/playerProductInstall",
			"FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
			"width", "100%",
			"height", "100%",
			"align", "middle",
			"id", "console",
			"quality", "high",
			"bgcolor", "#000000",
			"name", "console",
			"allowScriptAccess","always",
			"type", "application/x-shockwave-flash",
			"pluginspage", "http://www.adobe.com/go/getflashplayer"
		);
	} else if (hasRequestedVersion) {
	
		var flashVars = new Array();
		if(g_isIE)
			flashVars[flashVars.length] = "isie=true";
		flashVars[flashVars.length] = serverStr;
		flashVars[flashVars.length] = "usesecure=" + g_usesecure;
		flashVars[flashVars.length] = "compdelay=" + g_compDelay;
		flashVars[flashVars.length] = "build=" + g_build;
		flashVars[flashVars.length] = "nltrack=" + g_nl_track;
		flashVars[flashVars.length] = "nltrackping=" + g_nl_track_ping;
		if(window.location.search.length>1)
		{
			var nvs = window.location.search.substring(1).split("&");
			if(nvs.length>0)
				for(var i=0;i<nvs.length;i++)
					flashVars[flashVars.length] = nvs[i];
		}
		flashVars[flashVars.length] = "referrer="+encodeURIComponent(document.referrer);
	
		// if we've detected an acceptable version
		// embed the Flash Content SWF when all tests are passed
		AC_FL_RunContent(
				"flashVars", flashVars.join("&"),
				"src", consoleStr,
				"width", "100%",
				"height", "560px",
				"align", "middle",
				"id", "console",
				"quality", "high",
				"bgcolor", "#000000",
				"name", "console",
				"allowFullScreen", "true",
				"allowScriptAccess","always",
				"wmode", "opaque",
				"type", "application/x-shockwave-flash",
				"pluginspage", "http://www.adobe.com/go/getflashplayer"
		);
	  } else {  // flash is too old or we can't detect the plugin
	    var alternateContent = 'Alternate HTML content should be placed here. '
	  	+ 'This content requires the Adobe Flash Player. '
	   	+ '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>';
	    document.write(alternateContent);  // insert non-flash content
	  }
}

function getSessionId()
{
	var sid = "";
	if(document.cookie!=null && document.cookie.length>0)
	{
		var crumbs = document.cookie.split(";");
		for(var i=0;i<crumbs.length;i++)
		{
			var curCrumb = crumbs[i].split("=");
			if(curCrumb[0].indexOf("JSESSIONID")==0)
			{
				if(unescape(curCrumb[1])!="undefined")			
				{
					sid = unescape(curCrumb[1]);
					break;
				}
			}
		}
	}
	return sid;
}

var davUrl;
var scCid;
var nielsen_clip_title;
var nielsen_cg;
var nielsen_si;
var nielsen_carray;
function jsDavInit(url, cid) {
    davUrl = url;
    scCid = cid;
}

function jsDavInitStream(clipid, provider, videotype, date, description, sportkey, oemid) {
    // alert("nielsen_clip_id " + clipid + ", neilsen_provider_name " + provider + ", nielsen_video_type " + videotype + ", nielsen_event_date " + date + ", nielsen_short_description " + description + ", nielsen_sport_key " + sportkey);
    nielsen_clip_title = oemid + '-' + clipid + '-' + provider + '-' + videotype + '-' + date + '-' + description; //TL nielsen title
    nielsen_cg = oemid + '-' + videotype + '-' + sportkey; //CG neilsen list this as channel
    nielsen_si = 'blank'; //SI stream url, suppose to be optional
    nielsen_carray = null; //nielsen c-array

	setTimeout("getPlayerSrc()",1);
}

function getPlayerSrc()
{
	var wmp = getPlayer();
    if (document.getElementById("wmppane").contentWindow) 
    	document.getElementById("wmppane").contentWindow.davInitStream(wmp, davUrl, scCid, nielsen_clip_title, 10, nielsen_cg, nielsen_si, nielsen_carray);
    else if(davInitStream)
    	davInitStream(wmp, davUrl, scCid, nielsen_clip_title, 10, nielsen_cg, nielsen_si, nielsen_carray);
}

function setPlayingAd(str) {
	var wmp = document.getElementById("wmppane");
    if (wmp.contentWindow) 
    	wmp.contentWindow.setPlayingAd(str);
    else if (setPlayingAd)
    	setPlayingAd(str);
}

function doTrack(hostName,params){
    document.getElementById('trackDiv').innerHTML = '<if' + 'rame id="iframe_Track" name="iframe_Track" src="' + hostName + '?' + params + '"><\/ifr' + 'ame>';
}
