//javascript for cassino
//@sreeju K G 
//AlNadeem Info. Technology

var currentstatus = "0"; //means no cassino, it will be set to 1 if there is a prize
var defaultimageurl = "http://www.runway30.com/Runway30/CreateSmallImage.aspx?width=125&maxheight=90&id=";
var url = "/runway30/CassinoFiles/CassinoChangeDetect.aspx";
var timeoutinterval = 10000; 
var curInterval = timeoutinterval;
var newjob

/******************************************************************************************
*******************************************************************************************
Functions that will request and get the result from the cassinochangedetect.aspx
*******************************************************************************************
******************************************************************************************/
function checkServerForChange()
{	
	clearTimeout(newjob);
	newjob = setTimeout('makeRequest()',curInterval);
}

function makeRequest()
{
	var http_request = false;
	if(window.XMLHttpRequest)
	{
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType)
		{
			http_request.overrideMimeType('text/xml');
		}
	}
	else if(window.ActiveXObject)
	{
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			http_request = new ActiveXObject("Microsot.XMLHTTP");
		}
		
	}

	if(! http_request)
	{
		//alert("oh, sorry dear I am not able to do this task");
		return false;
	}

http_request.onreadystatechange =function() { dotheAction(http_request); };
http_request.open('GET',url, true);
http_request.send(null);			
}

function dotheAction(http_request)
{
	if(http_request.readyState == 4)
	{
		if(http_request.status == 200)
		{
			
			var xmldoc = http_request.responseXML;
			//var root_node = xmldoc.getElementsByTagName('cassino').item(0);
			var rootelement = xmldoc.documentElement;
			var status = rootelement.getElementsByTagName("status").item(0).firstChild.nodeValue;
			if(status == "no")
			{
				//alert("current status" + currentstatus);
				if(currentstatus == 1)
				{
					//alert("load the flash file with default settings");
					flash.revertAction();
					currentstatus = 0;
				}
				curInterval = timeoutinterval;
				checkServerForChange();
			}
			else if(status =="yes")
			{
				//load the flash with the following details
				//alert("go thru xml");
				var casinoid = rootelement.getElementsByTagName("id").item(0).firstChild.nodeValue;
				//alert("casino id " + casinoid);
				var tr = rootelement.getElementsByTagName("timeremaining").item(0);
				var td = tr.getElementsByTagName("day").item(0).firstChild.nodeValue;
				var th = tr.getElementsByTagName("hour").item(0).firstChild.nodeValue;
				var tm = tr.getElementsByTagName("min").item(0).firstChild.nodeValue;
				var ts = tr.getElementsByTagName("sec").item(0).firstChild.nodeValue;
				//alert("time remain: " + td + "." + th + "." + tm + "." + ts);
				var img = rootelement.getElementsByTagName("image").item(0);
				var imageid = img.getElementsByTagName("id").item(0).firstChild.nodeValue;
				//alert("image id " + imageid);
				var user = img.getElementsByTagName("user").item(0);
				var userid = user.getElementsByTagName("id").item(0).firstChild.nodeValue;
				var username = user.getElementsByTagName("name").item(0).firstChild.nodeValue;
				//alert("user - " + username);
				// load the flash file with the following settings
				//alert("current status" + currentstatus);
				if(currentstatus == 0)
				{
					//alert("load the flash with the new settings");
					flash.putIntoAction(casinoid,imageid,username,defaultimageurl+imageid,th,tm,ts);
					currentstatus = 1;
					curInterval = th*60*60*1000 + tm*60*1000 + ts*1000;
					//alert("next check after:" + curInterval);
					checkServerForChange();
				}
			}
			else
			{
				alert("do nothing");
			}
			
		}
		else
		{
			alert("There was a problem with the request.");
			alert(http_request.status);
		}
	}
}



