// Progressive Ticker for Casino Home and Download pages
// March 13, 2006 - Carter Gilchrist
// *********************************************************************************
// totals up all of the progressive games (then subtracts Win a Million - $1,000,000)
// takes off $100 and then counts up $1 at a time

// where to output the ticking jackpot value
var divElementName = "jackpot-total";

// create ajax call to get the string of RTG jackpot values
function startProgressiveTicker()
{
	// need to have a div tag to output the value before any of this can run
	if (document.getElementById( divElementName )) {
		try
		{
			xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
			new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
		   // browser doesn't support ajax. 
		   //alert("browser doesn't support ajax");
		}
		xmlhttp.onreadystatechange = triggered;
		xmlhttp.open("GET", "/casino/progressive-jackpots-ticker.jsp" );
		xmlhttp.send(null);
	}
}


// called from the getTotals() method when a response is returned
function triggered()
{
	if ( ( xmlhttp.readyState == 4 ) && ( xmlhttp.status == 200 ) )
	{
		var response = xmlhttp.responseText.replace(/\n/g,"");
		
		// if the response isn't null or blank then process the string
		if ( response.indexOf('OK 200') == -1 && response != null && response != "" )
		{
			calculateTotal( response );
		}
	}
}

// formatting function to chunk the number with commas
function addCommas( nStr )
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


// browser check function for all known issues with browser versions
// so far only firefox v. less than 1.5 has issues
function browserCheck()
{
	if ( is_fx && is_fx_ver < 1.5 ) {
		return false;
	} else {
		return true;
	}
}


// adds $1 to the total and outputs writes it into the span tag
function countUp()
{
	var currency = document.getElementById( "currency" );
	
	if (!currency){
		currency = "$USD";
	} else {
		currency = currency.innerHTML;
	}
	
	jackpotTotal = parseInt( jackpotTotal ) + 1;
	document.getElementById( divElementName ).innerHTML = currency + " " + addCommas( jackpotTotal ) + " ";
}


var jackpotTotal = 0;
// starts the formatting and calculation of 
function calculateTotal( jackpotString )
{
	//make sure RTG isn't down
	if ( jackpotString != "" && jackpotString != null ) {

		// this next line removes the first "&" and the ending "&&check=true"
		if (jackpotString[0] == "&") {
			jackpotString = jackpotString.substring(1, jackpotString.length - 12);
		} else {
		    jackpotString = jackpotString.substring(0, jackpotString.length - 12);
		}
		
		var jackpotsArray = new Array();
		var jackpotValues = new Array();
		var jackpotsArray = jackpotString.split("&&");
		

		for ( var i = 0 ; i<jackpotsArray.length ; i++ )
		{
			var myJackpot = jackpotsArray[i];
			jackpotsArray[i] = myJackpot.substring( myJackpot.indexOf("=") + 1, myJackpot.length)
			
			// add it to the total
			jackpotTotal = jackpotTotal + parseInt( jackpotsArray[i] );
		}

		// there is a progressive called Win A Million which is not included in the total, so subtract that
		// jackpotTotal -= 1000000;

		// subtract 150 dollars from the top, to allow it to tick upwards and not go over the total
		if (jackpotTotal >=150) {
			jackpotTotal -= 150;
        }

		if ( browserCheck() )
		{
			var countup = setInterval( "countUp()", 600 ); 
		}
		else
		{
			countUp();
		}
	}
}

