var g_lastPath = null;
var g_mainRegSWin=null;
var g_mainAcctWin=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 "generateNLUid":
			g_track_uid = value;
			nltrack("rv", [""]);
			break;		
		case "openRegisterForm":
			var url=value.url+"secure/registerform?is_modal=yes";
			if(value.pt)url+="&pt="+value.pt;
			if(value.pid)url+="&pid="+value.pid;
			g_mainRegSWin = window.open(url,"registerform_","scrollbars=yes,menubar=no,height=772,width=1000,top=0,left=0,resizable=no,toolbar=no,location=no,status=yes");
			if(g_mainRegSWin!=null)
				g_mainRegSWin.focus();
			checkRegSWindowClosed();
			break;				
		case "openSimpleRegister":
			var url=value.url+"secure/simpleregform?is_modal=yes";
			g_mainRegSWin = window.open(url,"simpleregister_","scrollbars=yes,menubar=no,height=710,width=1000,top=0,left=0,resizable=no,toolbar=no,location=no,status=yes");
			if(g_mainRegSWin!=null)
				g_mainRegSWin.focus();
			checkRegSWindowClosed();
			break;
		case "openMyAccount":
			var url=value.url+"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":
			if (!g_payflow_track) return;
			trackGAET(value.category, value.action, value.label, value.value);
			//trackTags(src, "glaw2");
			break;
		case "trackpageview":
			if (!g_payflow_track) return;
			trackGAPV(value.page);
			//trackTags(src, "glaw2");
			break;
		case "trackecommerce":
			if (!g_payflow_track) return;
			trackGEC2(value);
			break;
	}
}

function checkRegSWindowClosed()
{
	if (window.g_mainRegSWin==null)
		return;
	
	if (!window.g_mainRegSWin.closed)
		setTimeout("checkRegSWindowClosed()", 1000);
	else
		consoleCallback("updateGames", "");
}

// 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) return swfConsole.consoleCallback(action, value);
	return "";
}

function mouseoutofSwf()
{
	consoleCallback("mouseOut", "");
}

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 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 getPageQuery()
{
	var qs = window.location.href;
	var i = qs.indexOf("?");
	if(i > 0)
		return qs.substring(i + 1, qs.length);
	return "";
}

function getImageUrl(isProduction)
{
	var url = (isProduction ? "${initParam.locImage}" : "");
	//alert("getImageUrl " + url);
	return url;
}

function getCdnUrl(isProduction)
{
	var url = (isProduction ? "http://ojhl.cdn.neulion.net/u/ojhl/" : "");
	//alert("getCdnUrl " + url);
	return url;
}
	
function writeFlexPlayer(isProduction)
{
	var serverStr = (isProduction ? "http://ojhl.neulion.com/ojhl/" : location.href.substring(0, location.href.lastIndexOf("/")+1));
	var consoleStr = getCdnUrl(isProduction) + "console";
		
	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+"&refreshLiveGameScoreTimeNum="+refreshLiveGameScoreTimeNum+"",
			"width", "100%",
			"height", "100%",
			"align", "middle",
			"id", "console",
			"quality", "high",
			"bgcolor", "#000000",
			"name", "console",
			"scaleMode", "exactFit",
			"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";
		if(g_usesecure)
			flashVars[flashVars.length] = "usesecure=true";
		flashVars[flashVars.length] = "server=" + serverStr;
		flashVars[flashVars.length] = "rightAdTimerNum=" + g_rightAdTimerNum;
		flashVars[flashVars.length] = "liveRefresh=" + g_live_refresh;
		flashVars[flashVars.length] = "locProgramImage=" + g_loc_program_image;
		flashVars[flashVars.length] = "league=" + g_league;
		flashVars[flashVars.length] = "glcode=" + g_gl_code;
		flashVars[flashVars.length] = "externalServer=" + g_external_server;
		flashVars[flashVars.length] = "showChat=" + g_chat_show;
		flashVars[flashVars.length] = "scoreChat=" + encodeURIComponent(g_chat_score);
		flashVars[flashVars.length] = "enableRadio=" + g_radio_enable;
		flashVars[flashVars.length] = "tabNames=" + g_tab_names;
		flashVars[flashVars.length] = "tabDefault=" + g_tab_default;
		flashVars[flashVars.length] = "enableAudioSwitch=" + g_enableAudioSwitch;
		flashVars[flashVars.length] = "timeZoneStr=" + g_timeZoneStr;	
		flashVars[flashVars.length] = "slStatus=" + slCheckResult;
		var isWin = (navigator.platform.indexOf("Win") == 0);
		flashVars[flashVars.length] = "useSilverlight=" + !(g_useWMP && isWin);
		
		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", "100%",
				"align", "middle",
				"id", "console",
				"quality", "high",
				"bgcolor", "#000000",
				"name", "console",
				"scaleMode", "exactFit",
				"wmode", "transparent", //"opaque",
				"allowScriptAccess","always",
				"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 getTrackSId()
{
	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 = trim(crumbs[i]).split("=");
			if(curCrumb[0].indexOf("nltracksid")==0)
			{
				if(unescape(curCrumb[1])!="undefined")			
				{
					sid = unescape(curCrumb[1]);
					break;
				}
			}
		}
	}
	//alert("getTrackSid " + sid);
	return sid;
}

function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

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;
}
