var intLoadInterval = 60000*3; //3 minutes

function getCCID(){

	function get_cookie(c_name){	
		if(document.cookie.length > 0){
			c_start = document.cookie.indexOf(c_name + "=");
			if(c_start != -1){
				c_start = c_start + c_name.length + 1;
				c_end = document.cookie.indexOf(";",c_start);
				if (c_end == -1) c_end = document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
			}
		}
		return "";
	}

	if(get_cookie("CCID") != ""){
		return get_cookie("CCID");
	}
	else{		
		return "GB";
	}	
}


function assign_currency(){

	var getURL = window.location.href;
	if(getURL.indexOf(".dev") == -1 || getURL.indexOf(".com") == -1){
		getURL = "http://www.partybingo.com"
	}
	var getCountry = getURL.substring(7,9);
	
	if(getCountry == "ww"){
		getCountry = "en"
	}	
	
	var currencySymbol = "dollar";	
	
	/*
	IF PARTYBINGO
	*/
	if(getURL.indexOf("partybingo") != -1){

		if(getCountry == "en" && getCCID() == "US"){
			currencySymbol = "dol";
		}
		else if(getCountry == "en" && getCCID() == "IL"){
			currencySymbol = "euro";
		}	
		else if(getCountry == "en" && getCCID() == "GB"){
			currencySymbol = "dollar";		
		}			
		else if(getCountry == "en" && getCCID() == "CA"){
			currencySymbol = "dol";
		}		
		else if(getCountry == "de" && getCCID() == "DE"){
			currencySymbol = "euro";
		}
		else if(getCountry == "es" && getCCID() == "ES"){
			currencySymbol = "euro";
		}		
		else if(getCountry == "da" && getCCID() == "DK"){
			currencySymbol = "euro";
		}						
	}
	return currencySymbol;
}

function callJSON(){			
		
	try{			
		req = new ActiveXObject("Microsoft.XMLHTTP");				
	}
	catch(e){		
		req = new XMLHttpRequest();	
	} 

	req.open("GET", "/scripts/partyGames.js?cachebuster=" + Math.random() + "", true); 
	req.onreadystatechange = myCode; 
	req.send(null); 

	function myCode(){ 			

		if(req.readyState == 4 || req.readyState == 0){ 
			var doc = eval('(' + req.responseText + ')'); 								

			for(var i in doc.partyGames){								
														
				for(var j in doc.partyGames[i].rooms){	
					
					if(doc.partyGames[i].brand == 'PARTY'){
					
						if(doc.partyGames[i].rooms[j].roomName.toLowerCase() == ('Lilac Lounge').toLowerCase()){
							for(var k in doc.partyGames[i].rooms[j].currencyWiseBingoRoomDetail){
								if(doc.partyGames[i].rooms[j].currencyWiseBingoRoomDetail[k].currency == "GBP"){
									create_graphic(new String(''+doc.partyGames[i].rooms[j].currencyWiseBingoRoomDetail[k].currentProgressiveJackpotValue+''),'jp1');
								}
							}
						}	
						if(doc.partyGames[i].rooms[j].roomName.toLowerCase() == ('Diamond Club').toLowerCase()){
							for(var k in doc.partyGames[i].rooms[j].currencyWiseBingoRoomDetail.currencyWiseBingoRoomDetail[k]){
								if(doc.partyGames[i].rooms[j].currencyWiseBingoRoomDetail[k].currency == "GBP"){
									create_graphic(new String(''+doc.partyGames[i].rooms[j].currencyWiseBingoRoomDetail[k].currentProgressiveJackpotValue+''),'jp2');
								}
							}
						}																				
					}									
				}
			}		
		}			
	}		
}		

function create_graphic(num,imgId){
	var strJackPot = new String(num);
	strJackPot = strJackPot.replace(".","");
	
	if(new Number(strJackPot) < 100000){
		strJackPot = strJackPot + "00";
	}
	var arrNumbers = strJackPot.split("");
	var strImg = "";
	var strFilePath = "/images/ticker/";
	var intLength = new Number(arrNumbers.length)-1;		
		
	strImg += "<img src=\"" + strFilePath + "" + assign_currency() + ".jpg\"/>";
	if(arrNumbers[intLength-9] != undefined)strImg += "<img src=\"" + strFilePath + "" + arrNumbers[intLength-9] + ".jpg\"/>";
	if(arrNumbers[intLength-8] != undefined)strImg += "<img src=\"" + strFilePath + "" + arrNumbers[intLength-8] + ".jpg\"/>";
	if(arrNumbers[intLength-7] != undefined)strImg += "<img src=\"" + strFilePath + "" + arrNumbers[intLength-7] + ".jpg\"/>";
	if(arrNumbers[intLength-6] != undefined)strImg += "<img src=\"" + strFilePath + "" + arrNumbers[intLength-6] + ".jpg\"/>";
	if(arrNumbers[intLength-5] != undefined)strImg += "<img src=\"" + strFilePath + "" + arrNumbers[intLength-5] + ".jpg\"/>";
	if(arrNumbers[intLength-5] != undefined)strImg += "<img src=\"" + strFilePath + "comma.jpg\"/>";
	if(arrNumbers[intLength-4] != undefined)strImg += "<img src=\"" + strFilePath + "" + arrNumbers[intLength-4] + ".jpg\"/>";
	if(arrNumbers[intLength-3] != undefined)strImg += "<img src=\"" + strFilePath + "" + arrNumbers[intLength-3] + ".jpg\"/>";
	if(arrNumbers[intLength-2] != undefined)strImg += "<img src=\"" + strFilePath + "" + arrNumbers[intLength-2] + ".jpg\"/>";
	if(arrNumbers[intLength-2] != undefined)strImg += "<img src=\"" + strFilePath + "dot.jpg\"/>";
	if(arrNumbers[intLength-1] != undefined)strImg += "<img src=\"" + strFilePath + "" + arrNumbers[intLength-1] + ".jpg\"/>";
	if(arrNumbers[intLength] != undefined)strImg += "<img src=\"" + strFilePath + "" + arrNumbers[intLength] + ".jpg\"/>";

	if(document.getElementById(imgId))document.getElementById(imgId).innerHTML = strImg;
}

var reloadJSON = setTimeout("callJSON()", intLoadInterval);
window.onload = callJSON;