//Main menu related
function SelectTab(){
	try{
		MO(tabNumber, 1);
	}catch(e){
		return;			
	}
}
function Menu(id,toolTip,start,width){
  document.write('<div style="cursor:hand;width:' + width + ';height:17;overflow:hidden"><img id="img' + id + '" src="../img/menu/menu_off.gif" alt="' + toolTip + '" style="margin-left:-' + start + '"></div>');
}
function mLink(tab){
	document.location.href = document.getElementById("a"+tab).href;
}
function MO(tab, action){imgObj = document.getElementById("img"+tab);
	Roller(imgObj, action);
	return;
}
function Roller(obj, action){
	var src = obj.src;
	if (action == 1){
		obj.src = src.substring(0,src.lastIndexOf('_')) + '_on.gif';
	}else{
		obj.src = src.substring(0,src.lastIndexOf('_')) + '_off.gif';
		SelectTab();
	}
}

function DetectSuccessfullGamedekInstall(){ //only for IE
	var str = '<OBJECT id="ArkDownloaderMenu" CLASSID="clsid:CDAA0214-3907-4C47-A3F6-014DA1517440"/>';
	document.all['DivDownloader'].innerHTML = str;
	if (document.all['ArkDownloaderMenu']){
		try{
			var keyValue = document.all['ArkDownloaderMenu'].GetValueFromRegistry('SOFTWARE\\ArkadiumV2\\Settings', 'AffiliateID');
			return 1;			
		}catch(e){
			return 0;			
    }
	}
	return 0;
}

function RefreshPage(){
	document.location.href = document.location.href;
}
function GoToPage(sPage){
	document.location.href = sPage;
}

function SignUp(){
    var retUrl =baseUrl + '/account/GameDekInstaller3.aspx'; //new user, has software
   	if (navigator.appName == 'Microsoft Internet Explorer'){
		if (DetectSuccessfullGamedekInstall() != 1){
			retUrl = baseUrl + '/account/GameDekInstaller2.aspx';  //new user, doesn't have software
		}
	}
	GoToPage(baseUrl + '/login/register.aspx?ReturnUrl='+retUrl);
}
function SignOut(){
		setTimeout('GoToPage("' + baseUrl + '/logout.aspx");', 500);
		Arkv2('02close');
}

function Arkv2(theUrl){
	if (GetCookie('.GAMEDEK_CLIENTAUTH')==null){
	  SetCookie(arkv2Cookie, theUrl);
	  var retUrl = escape(document.location.href);
		if (navigator.appName == 'Microsoft Internet Explorer'){
			if (DetectSuccessfullGamedekInstall() != 1){
			    SetCookie(returnUrlCookie, escape(document.location.href));
				retUrl = baseUrl + '/account/GameDekInstaller.aspx';
			}
		}
		GoToPage(baseUrl + '/login/login.aspx?ReturnUrl='+retUrl);
		return;
	}
	if (navigator.appName == 'Microsoft Internet Explorer'){
		if (DetectSuccessfullGamedekInstall() == 1){
			SetCookie(arkv2Cookie, '');
			DelCookie(arkv2Cookie);
			document.location.href = 'arkv2://' + theUrl;
		}else{
			if (theUrl.indexOf('close')<=0){
				SetCookie(installCookie, 3);  //Tried Install, ActiveX wasn't downloaded
			  SetCookie(arkv2Cookie, theUrl);
				SetCookie(returnUrlCookie, escape(document.location.href));
				document.location.href = baseUrl + '/account/GameDekInstaller.aspx';
			}
		}
	}else{
		try{
			document.location.href = 'arkv2://' + theUrl;
		}
		catch(e){
			if(e.message.indexOf("0x804b0012")>0){
  				SetCookie(installCookie, 6);  //Tried Install, Used not IE browser
				document.getElementById('DivDownloader').style.display = 'block';
				document.getElementById('oMenuPageHeader_lblAd').style.display = 'none';
				alert ("Gamedek software components must be installed in order to continue.\nPlease click the link located under the main menu before proceeding");
			}
		}
	}
}
function CallArkV2(lnkID){
	try{
		Arkv2(arkV2Array[lnkID]);
	}catch(e){
	  alert('Please click OK to refresh this page and continue.');
		RefreshPage();			
	}
}

function GetCookie(sName){
	var aCookie = document.cookie.split("; ");
	for (var i=0;i<aCookie.length;i++){
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0]) return unescape(aCrumb[1]);
	}
	return null;
}
function DelCookie(sName){
	document.cookie = sName + "=; expires=Fri, 31 Dec 1950 23:59:59 GMT;path=/;";
}
function SetCookie(sName, sValue){
	DelCookie(sName);
	if (sValue != null) document.cookie = sName + "=" + escape(sValue) + ";path=/;";
}

function Arkv2AutoRun(arkv2RunUrl){
	var arkv2CookieValue = GetCookie(arkv2Cookie);
	SetCookie(arkv2Cookie, '');
	DelCookie(arkv2Cookie);
	if ((arkv2CookieValue!=null) && (arkv2CookieValue!='') && (arkv2RunUrl!='')){
		Arkv2(arkv2RunUrl);
	} 
}

//Login-password related
function OnFocus(obj){
	obj.style.display = 'none';
	var pwdObj = document.getElementById("oMenuPageHeader_oPassword");
	pwdObj.style.display = 'block';
	pwdObj.focus();
}

//jscript rendering
function TblBeg(){return '<table celpadding=0 cellspacing=0><tr>';}
function TblEnd(){return '</tr></table>';}
function Star(num){ShowStar('',num,-1)}
function StarV(rating,num){ShowStar(rating,num,0)}
function StarH(rating,num){ShowStar(rating,num,1)}
function ShowStar(rating,num,showType){
	var aBeg = '<a href="../Help/ratings.aspx">'
	var aEnd = '</a>'
  if (showType < 0){
    aBeg = '';
    aEnd = '';
  }
	var shift = -15*num; 
	var s = '';
	if (num == 0) num = 1;
	for (var i=0;i<num;i++) s += '<td width=15><div style="width:15;height:15;overflow:hidden">' + aBeg + '<img src="../img/common/stars.gif" style="margin-left:' + shift + '">' + aEnd + '</div></td>';
	s = TblBeg() + s + TblEnd();
	if (showType == 0){
		s = TblBeg() + '<td align=center>' + aBeg + rating + aEnd + '</td></tr><tr><td align=center>' + s + '</td>' + TblEnd();
		s = '<td width=90>' + s + '</td>';
	}
  if (showType == 1){
		s = TblBeg() + '<td>' + s + '</td><td>' + aBeg + rating + aEnd +  '</td>' + TblEnd();
		s = '<td>' + s + '</td>';
	}
	document.write(s);
}
function Leader(place, user, score){
	document.write(TblBeg() + '<td valign=top rowspan=2>' + place + ')&nbsp;</td><td valign=top class=LeaderName>' + user + '</td></tr><tr><td>' + score + '</td>' + TblEnd());
}
function Arrow(){
	document.write("<table cellpadding=0 cellspacing=0><tr><td valign=baseline><img src='../img/common/main_arrow.gif'></td><td>");
}
function gLink(categoryID, gameID, gameName, isNew, isHot, gameWidth, gameHeight, freePlayGameID){
  var sImg = '';
  if (isNew == 1) sImg += NewHot(29,0);
  if (isHot == 1) sImg += NewHot(26,-29);
	var imgPlayUrl = "../img/common/play.gif";
	var hrefUrl = '../Games/RoomList.aspx?categoryID=' + categoryID + '&GameID=' + gameID;
  var sPlayNow = '';
  if (freePlayGameID  > 0) sPlayNow = '<td><a style="cursor:hand" onclick="PlayNowGame(' + freePlayGameID + ',' + gameWidth + ',' + gameHeight + ');GoToPage(this.href)" href="' + hrefUrl + '">' + GetImg(imgPlayUrl,'Play Now',14,49,0) + '</a></td><td width=3></td>';
	var sWrite = TblBeg() + '<td class="arrow"><a href="' + hrefUrl + '"><b><u>' + gameName + '</u></b></a></td>' + sImg;
	sWrite += '</tr><tr><td colspan=3>' + TblBeg() + sPlayNow + '<td><a style="cursor:hand" onclick="GoToPage(this.href)" href="' + hrefUrl + '">' + GetImg(imgPlayUrl,'Compete Live',14,68,49) + '</a></td>' + TblEnd() + '</td>' + TblEnd();
	document.write(sWrite);
}
function NewHot(width,left){
	return '<td><div style="width:' + width + ';height:15;overflow:hidden"><img src="../img/common/newhot.gif" style="margin-left:' + left + '"></div></td>';
}
function Deluxe(num,alt){
	var shift = -81*num; 
	document.write('<div style="cursor:hand;width:81;height:46;overflow:hidden"><img src="../img/deluxe/deluxe.gif" style="margin-left:' + shift + '" alt="' + alt +'"></div>');
}
function GameName(game){
	var img = "../img/common/gameName_line.gif";
	var alt = game + ' games';
	switch (game){
	case '.featured.': 
		return ImgShow(img,alt,19,112,0) 
	case 'arcade': 
		return ImgShow(img,alt,19,87,112) 
	case 'board': 
		return ImgShow(img,alt,19,74,199) 
	case 'card': 
		return ImgShow(img,alt,19,60,273) 
	case 'sport': 
		return ImgShow(img,alt,19,85,333) 
	case 'strategy': 
		return ImgShow(img,alt,19,114,419) 
	case 'word': 
		return ImgShow(img,alt,19,66,532) 
	}
}
function GameImage(game){
	var img = "../img/common/gameImage_line.gif";
	var alt = game + ' games';
	switch (game){
	case '.featured.' : 
		return ImgShow(img,alt,34,37,0) 
	case 'arcade' : 
		return ImgShow(img,alt,34,34,37) 
	case 'board' : 
		return ImgShow(img,alt,34,25,71) 
	case 'card' : 
		return ImgShow(img,alt,34,39,96) 
	case 'sport' : 
		return  ImgShow(img,game,34,41,135) 
	case 'strategy' : 
		return  ImgShow(img,alt,34,33,176) 
	case 'word' : 
		return  ImgShow(img,alt,34,42,209) 
	}
}
function ImgShow(imgUrl,alt,height,width,start){
  document.write(GetImg(imgUrl,alt,height,width,start));
}
function GetImg(imgUrl,alt,height,width,start){
  var sAlt = '';
  if (alt != '') sAlt = ' alt="' + alt + '"';
  return '<div style="height:' + height + ';width:' + width + ';overflow:hidden"><img src="' + imgUrl + '"' + sAlt + ' style="margin-left:-' + start + '"></div>';
}
function PlayNowGame(gameID, gameWidth, gameHeight){
  gameHeight = gameHeight + 90;
  var gameWnd = window.open('http://freeplay.gamedek.com/playGame.aspx?affiliateID=' + '57' + '&gameID=' + gameID,'_blank','width=' + gameWidth + ',height=' + gameHeight + ',resizable');
  if (!gameWnd)	alert("We were not able to launch your game room. If you use a pop-up blocker, please allow pop-ups for this site and try again.");
}