
//Global Params
//===================================================

//Holds the parameters form querystring
var ParamArray = new Array();
//Holds the values form querystring
var ValueArray = new Array();
//parallel to both of the arrays above -- true if Isoberon param, false if pogo param
var IsOberonArray = new Array();
//String delimeted "," list that holds querystring params to ignore (isOberon)
var ParamIgnoreList = new String();
ParamIgnoreList = "code,genre,origin,RefId,Session,ln,SortBy,sDir,Page";

var strCookieName = "OberonPogoComb";

//Recieved from Server side Channel Properties
var PogoChannelIdentifier;
var HostDomain ;

//The urls to post to on POGO's side
var PogoNavHeaderUrl = "/oberon/navheader.jsp";
var PogoAdUrl = "/oberon/ad.jsp";
var PogoEntryUrl = "/oberon/entry.jsp";
var PogoUserCountUrl = "/oberon/usercount.jsp";



//Functions
//===============================================================

function PogoInit(){
	if (IsPogoCookieExists()){
		
		if (IsPogoUrlExists()){
			WriteCookie();
			window.location.replace(RefreshPageFullPath());
		}
		else{
			
			LoadNavHeader();
			
		}
	 }
	 else{
		if (IsPogoUrlExists()){
			WriteCookie();
			window.location.replace(RefreshPageFullPath());
		}
		else{
			if(IsQueryStringExists(window.location.href))
			{
				StripQuerystring(window.location.href);
			}
			LoadEntryPage(PogoChannelIdentifier);
		}
	}
	
} 


//===============================================================

function RefreshPageFullPath()
{
	var tmpStr;
	tmpStr =  PageFullPath() + "?" + GetQueryString(true);
	return tmpStr;
}


function PageFullPath()
{
	
	var tmpStr = new String();
	tmpStr =  window.location.href;
	tmpStr = tmpStr.split("?")[0];
	return tmpStr;
}


//sends the URL for the navheader to load from the POGO site
//builds the Oberon params to send seperately and the Pogo params to send seperately
function LoadNavHeader()
{
	var oIframe = document.getElementById("PogoHeaderIframe"); 
	var tmpStr = new String();
	var ret = IsPogoCookieExists();
	oIframe.height= GetParamValue("ifh");
	oIframe.width= GetParamValue("ifw");

	tmpStr = HostDomain + PogoNavHeaderUrl + "?" + GetQueryString(false)+ "&top=" + escape(PageFullPath() + "?");
	
	if(IsQueryStringExists(window.location.href))
	{
		StripQuerystring(window.location.href);
	}
	tmpStr+=  escape(GetQueryString(true));
	if(null != GetParamValue("intcmp")) tmpStr+= "&intcmp=" + GetParamValue("intcmp");
	if(null != GetParamValue("pagesection")) tmpStr+= "&pageSection=" + GetParamValue("pagesection");
	if(null != GetParamValue("sourceid")) tmpStr+= "&sourceid=" + GetParamValue("sourceid");
	oIframe.src =  tmpStr;
}

function LoadAdBanner(isPogoClub,gameName,isVisable)
{
	var oIframe = document.getElementById("PogoAdIframe");
	var tmpStr = new String();
	var tmpArr2 = "";
	var finalURL = new String();
	var tmpArr = "";
	oIframe.height= 250;
	oIframe.width= 193;
	if(isPogoClub && isVisable)
	{
		StripQuerystring(ReadCookie());
		tmpStr = HostDomain + PogoAdUrl + "?" + "game=" + gameName + "&" +	GetQueryString(false) ;
		
		tmpArr = tmpStr.split("&");
		
		for(i = 0; i < tmpArr.length; i++)
		{
			
			tmpArr2 = tmpArr[i].split("=");
			
			if((tmpArr2[0] != "ifw") && (tmpArr2[0] != "ifh")) 
			{
				finalURL +=  "&" + tmpArr[i];
				
			} 
		}
		oIframe.src =  finalURL.substr(1, finalURL.length) + "&ifw=193&ifh=250";
	}
	else
	{
		oIframe.style.display = "none";
	}

}




function GetParamValue(ParamName){
	for(i=0;i< ParamArray.length;i++)
		{
			if(ParamArray[i].toString().toLowerCase() == ParamName)
			{
				return ValueArray[i];
			}
		}
}

//builds and loads the hidden UserCount Iframe on Pogo Originals game page
function GotoUserCountPage(gameName)
{
	var oUserCountIframe =  document.getElementById("UserCountIframe");
	StripQuerystring(ReadCookie());
	oUserCountIframe.src =  HostDomain + PogoUserCountUrl + "?" + "game=" + gameName + "&"+ GetQueryString(false);
}


//Expects Domain-- http://xxx.xxx.xxx --- and channelIdentifier -- pogo,aol..etc.
//Auto redirects browser to pogo entry page
//Creates the url to be sent only from Oberon params
function LoadEntryPage(channelIdentifier)
{
	var strUrl = HostDomain + PogoEntryUrl + "?" + "channel=" + channelIdentifier + "&page=" + escape(PageFullPath() + "?" + GetQueryString(true));
	window.location.href=  strUrl;
}



//Gets string in QueryString format (including pagename and "?")
function KeyExistsInString(strQueryString)
{
	
	if(IsQueryStringExists(strQueryString))
	{
		StripQuerystring(strQueryString);
		for(i=0;i< ParamArray.length;i++)
		{
			if(ParamArray[i].toString().toLowerCase() == "lkey")
			{
				return true;
			}
		}
		
	}
	
	return false;
}


//Returns true if the key exists in the cookie false if no cookie or no key in cookie
function IsPogoCookieExists()
{
	var strCookieString = ReadCookie();
	return KeyExistsInString(strCookieString);
}


//Returns true if the key exists in the browser address bar, false if no key in browser address bar
function IsPogoUrlExists()
{
	return KeyExistsInString(window.location.href);
}



//Reads the cookie value for the given cookie name
//When getting cookie also decodes from UrlEncoding
function ReadCookie() 
{
 var cookieString=document.cookie;
 var index1=cookieString.indexOf(strCookieName);
 
 if (index1==-1) return ""; 
 
 var index2=cookieString.indexOf(';',index1);
 
 if (index2==-1) index2=cookieString.length; 
 
 return unescape(cookieString.substring(index1+strCookieName.length+1,index2));
 
}




//writes a cookie with the given name/value/domain
//When creating encodes value with UrlEncoding
function WriteCookie()
{
	strCookieValue = window.location.href;
	var curCookie = strCookieName + "=" + escape(strCookieValue);// +";domain=" + window.hostname ;
	document.cookie = curCookie;

}

function IsQueryStringExists(strQueryString){
	if((strQueryString.split("?")[1] == null) || (strQueryString.split("?")[1] == "") ||(strQueryString.split("?")[1] == "undefined"))
	{
		return false;
	}
	
	return true;
}

//Recieves a Querystring formatted string and splits all params,values to different arrays.
//also creates an array that holds true/false for every param and value if they are
//oberon or pogo params
function StripQuerystring(tmpQueryStr)
{
	
	strQueryString = tmpQueryStr;
	
	ParamArray = new Array();
	ValueArray = new Array();
	IsOberonArray = new Array();
	
	if(strQueryString.split("?")[1])
	{
		strQueryString = strQueryString.split("?")[1];
		var KeyValuePairsArray = new Array();
	
		//split to-- key=value pairs
		KeyValuePairsArray = strQueryString.split("&");
	
		for(i=0;i < KeyValuePairsArray.length;i++)
		{	
			ParamArray[i] = KeyValuePairsArray[i].split("=")[0]; //the key
			ValueArray[i] = KeyValuePairsArray[i].split("=")[1]; //the value
		
			//if param exists in ignore list then turn bit on in ParamIgnoreList
			if(ParamIgnoreList.toLowerCase().match(ParamArray[i].toLowerCase()))
			{
				IsOberonArray[i] = true;
			}
			else
			{
				IsOberonArray[i] = false;
			}
	
		}

	}
	
}



//If recieves TRUE then builds a URL for Oberon pages made of oberon params
//If recieves false then builds a URL for Pogo Pages
function GetQueryString(isOberon)
{
	var strUrl = new String();
	strUrl = "";
	
	for(i=0;i<IsOberonArray.length;i++)
	{
		if(IsOberonArray[i] == isOberon)
		{
			strUrl += ParamArray[i] + "=" + ValueArray[i] +"&";
		}
	}
	
	return strUrl.substring(0,strUrl.length-1);
}
