var g_lastPath = null;
var g_mainRegSWin=null;
var g_mainAcctWin=null;

//(A) indefinitely until user closes it; 
//(B) as long as the pre-roll video; 
//(C) N seconds longer than the preroll video. 
var g_adWaitFunc = "A";
var g_prerollvideoExist = 5; //seconds

// 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 "openSimpleRegister":
			var url=value[0]+"secure/simpleregform?is_modal=yes";
			g_mainRegSWin = window.open(url,"simpleregister_","scrollbars=yes,menubar=no,height=760,width=1000,top=0,left=0,resizable=no,toolbar=no,location=no,status=yes");
			if(g_mainRegSWin!=null)
				g_mainRegSWin.focus();
			checkRegSWindowClosed();
			nltrack("sv1", null, true);
			break;
		case "openMyAccount":
			var url=value[0]+"secure/myaccount?is_modal=yes";
			g_mainAcctWin = window.open(url,"myaccount_","scrollbars=yes,menubar=no,height=760,width=1000,top=0,left=0,resizable=no,toolbar=no,location=no,status=yes");
			if(g_mainAcctWin!=null)
				g_mainAcctWin.focus();
			break;
		case "trackevent":
			var src = "trackevent.htm?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);
			document.getElementById("trackevent").src = src;
			break;
		case "trackpageview":
			var src = "trackpageview.htm?code=" + value[0];
			if (value[1] != "")
				src += "&page=" + value[1];
    		src += "&ord=" + Math.ceil(Math.random()*1000000000);
    		//alert(src);
			document.getElementById("trackpageview").src = src;
			break;
	}
}

function checkRegSWindowClosed()
{
	if (window.g_mainRegSWin==null)
		return;
	
	if (!window.g_mainRegSWin.closed)
		setTimeout("checkRegSWindowClosed()", 1000);
	else
		consoleCallback("refreshConsole", "");
}

// 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
		wmp.contentWindow.writeWMP(path,360,null,null,null,null,null,null,false);		
}

function popat(value)
{
	alert(value);
}

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)
			wmp = document.getElementById("wmppane").contentWindow.document.getElementById("wmpMain");
		else
			wmp = document.getElementById("wmppane").contentWindow.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 = value[1] + "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
			(document.getElementById("wmppane").contentWindow ? document.getElementById("wmppane").contentWindow.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);
}
var playWidth = "100%";
var playHeight = "100%";

function writeFlexPlayer(isProduction, embed, isDebug, isInline)
{
	var serverStr = "server=" + location.href.substring(0, location.href.lastIndexOf("/")+1);
	var consoleStr = "console";
	var width = playWidth;
	var height = playHeight;
	if(embed)
		consoleStr = "embed";
	if(isProduction)
	{
		//serverStr = "server=http://localhost/asc/";
		//consoleStr = "http://usl.cdn.neulion.net/u/usl/console";
		//if(embed)
		//	consoleStr = "http://usl.cdn.neulion.net/u/usl/embed";
	}
	if (isDebug)
	{
		serverStr = "server=http://127.0.0.1:8060/usl/";
		consoleStr = "console-debug";
	}
	if (isInline)
		consoleStr = (isDebug ? "inline-debug" : "inline");
	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", width,
			"height", height,
			"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] = "build=" + g_build;
		flashVars[flashVars.length] = "freeCatId=" + g_freeCatId;
		flashVars[flashVars.length] = "freeAdTag=" + g_freeCatAdTag;
		flashVars[flashVars.length] = "freeAdPolicy=" + g_freeCatAdPolicy;
		flashVars[flashVars.length] = "freeAdPolicyParam=" + g_freeCatAdPolicyParam;
		flashVars[flashVars.length] = "glcode=" + g_gl_code;
		flashVars[flashVars.length] = "nltrack=" + g_nl_track;
		flashVars[flashVars.length] = "nltrackping=" + g_nl_track_ping;
		flashVars[flashVars.length] = "fcs_gameIds=" + g_fcs_gameIds;
		flashVars[flashVars.length] = "adWaitFunc=" + g_adWaitFunc;
		flashVars[flashVars.length] = "prerollvideoExist=" + g_prerollvideoExist;
		if (isInline)
		{
			flashVars[flashVars.length] = "inline-catid=" + g_inlineCatId;	
			flashVars[flashVars.length] = "inline-tremor=" + g_inlineTremorParams;		
		}
		
		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", width,
				"height", height,
				"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;
}

function alertjs(str)
{
	alert(str);
}

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();
    document.getElementById("wmppane").contentWindow.davInitStream(wmp, davUrl, scCid, nielsen_clip_title, 10, nielsen_cg, nielsen_si, nielsen_carray);
}

function setPlayingAd(str) {
	// it is for nielsen track
	//var wmp = document.getElementById("wmppane");
    //wmp.contentWindow.setPlayingAd(str);
}