/*
	WWCasino
	Description:Functions for WagerWorks Casino integration
*/
/*Keep the system session alive while playing on the WagerWork casino*/
function KeepWWCasinoAlive(){
	oAjax = new CMSAjaxObj(sAjaxUrlWWCasino + "?t=WWint&ran=" + Math.random());
	oAjax.onReadyStateChange = "KeepWWCasinoAliveOnreadyStateChange()";
	oAjax.sendRequest();
}

function KeepWWCasinoAliveOnreadyStateChange(){
	req = oAjax.http_request;
	//ReadyState 4 = Loaded
	if(req.readyState == 4){
		if (req.status == 200){
			//alert(req.responseText); //everything ok alert responseText
			if (req.responseText!="STOP")
				setTimeout("KeepWWCasinoAlive()", 1000 * 300);
			else
				ReloadCurrentUrl();				
		}
		else{
			//we got an error, try again with less interval
			setTimeout("KeepWWCasinoAlive()", 1000 * 120);
			//alert("There was a problem with the request. Status:" + req.status); //Error
		}
	}else{
		//Still loading
	}
}

function ReloadCurrentUrl()
{
	if (document.location.href.indexOf("aspx")==-1)
		return;
	else 		
		document.location.href = document.location.href.substring(0,document.location.href.indexOf("aspx")+4);
}

function WWCasinoGetGameInfoByGameId(){
	//Set loading text
	var oContent = WWGetGameInfoElement();
	WWCasinoFixPositioning();
	//Send the request
	oAjax.onReadyStateChange = "WWCasinoGetGameInfoOnreadyStateChange()";
	oAjax.sendRequest();
}
/*Show gameinfo in div, create the div and add it to bodyelement of the DOM*/
function WWCasinoShowGameInfo(strContent){
	var oContent = WWGetGameInfoElement();
	WWCasinoFixPositioning();
	oContent.className = "ww-gameinfo-display-block";
	var closeTranslation = (typeof(sCloseTranslation) != "undefined") ? sCloseTranslation : "";
	var strCloseHtml = "<div id=\"ww-casino-game-info-close\"><a href=\"#\" title=\""+ closeTranslation +"\" onclick=\"getElm('ww-casino-game-info').className = 'ww-gameinfo-display-none';return false;\"><span>"+ closeTranslation +"</span></a></div>";
	oContent.innerHTML = strCloseHtml + "<div class=\"ww-gameinfo-wagerworks-content\">" + strContent + "</div>";
}
function WWGetGameInfoElement(){
	var oContent = getElm("ww-casino-game-info");
	var loadingTranslation = (typeof(sLoadingTranslation) != "undefined") ? sLoadingTranslation : "";
	if(oContent == null){
		oContent = document.createElement("div");
		oContent.id = "ww-casino-game-info";
		document.body.appendChild(oContent);
	}
	oContent.innerHTML = "<span>"+ loadingTranslation +"</span>";
	oContent.className = "ww-gameinfo-display-loading";
	//oContent.style.left = lastEventClientX + 20  +"px";
	//oContent.style.top = lastEventClientY + "px";
	return oContent;
}
function WWCasinoGetGameInfoOnreadyStateChange(){
	req = oAjax.http_request;
	//ReadyState 4 = Loaded
	if(req.readyState == 4){
		if (req.status == 200){
			//everything ok alert responseText
			WWCasinoShowGameInfo(req.responseText);
		}
		else{
			WWCasinoShowGameInfo("<span class=\"error\">There was a problem with the request.<br/>Status:" + req.status +"<br/><small><em>Please try again later</em></small></span>");
		}
	}else{
		//Still loading
		//var oContent = WWGetGameInfoElement();
	}
}
function WWCasinoFixPositioning(){
	var topScroll;
	if(typeof(window.pageYOffset) != "undefined"){
		topScroll = window.pageYOffset;
	}else{
		topScroll = document.documentElement.scrollTop;
	}
	var oContentWrapper = getElm('container');
	var oContent = getElm("ww-casino-game-info"); //ww game info content div
	var iGameInfoHeight = oContent.offsetHeight;
	var pageContentHeight = oContentWrapper.offsetHeight;
	var topPos = (topScroll + lastEventClientY + iGameInfoHeight);
	oContent.style.left = lastEventClientX + 30  +"px";
	if( (topScroll + lastEventClientY + iGameInfoHeight) > pageContentHeight)
	{
		var diffHeight = (topScroll + lastEventClientY + iGameInfoHeight) - pageContentHeight;
		var newTop = topPos - (diffHeight + iGameInfoHeight);
		oContent.style.top = newTop + "px";
	}else{
		oContent.style.top = (topScroll + lastEventClientY) + "px";
	}
	
}
