var xmlhttp;


//------------------------------------------------------------------------||
// Used to create the XHR request for most types of browsers
//------------------------------------------------------------------------||
function checkCreateRequest() {
	xmlhttp = Try.these(
	function() {return new ActiveXObject('Msxml2.XMLHTTP')},
	function() {return new ActiveXObject('Microsoft.XMLHTTP')},
	function() {return new XMLHttpRequest()}
	);
	
	if(document.getElementById("wait") != null)
	{
		$("wait").style.top = (getDocMetrics()["height"] / 2) - 75 + "px";
		$("wait").style.left = (getDocMetrics()["width"] / 2) - 100 + "px";
	}
}




//------------------------------------------------------------------------||
// Used to send a request using the xmlthhp object                        ||
//------------------------------------------------------------------------||

function sendRequest(url , parameters) {
	if($("wait") != undefined)
	{
		$("wait").style.display = "block";
	}
   // Open PHP script for requests
   xmlhttp.open('get', url + parameters, true);
   xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
   xmlhttp.send(null);
}

//------------------------------------------------------------------------||
// Used to change the text on a html element                              ||
//------------------------------------------------------------------------||

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"

    if (DOM) {
        var viewer = $(div2show)
        viewer.innerHTML=text
    }
    else if(IE) {
        document.all[div2show].innerHTML=text
    }
}

//------------------------------------------------------------------------||
// This is used to work out the exact dimentions of the screen            ||
//------------------------------------------------------------------------||

function getDocMetrics() {
  var retval = Array();
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    retval["width"] = window.innerWidth;
    retval["height"] = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    retval["width"] = document.documentElement.clientWidth;
    retval["height"] = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    retval["width"] = document.body.clientWidth;
    retval["height"] = document.body.clientHeight;
  }
  return retval;
}

//------------------------------------------------------------------------||
// All functions here are used to manage the dragging of the windows      ||
//------------------------------------------------------------------------||

// this function allows you to drag elements around the screen
function startDrag(e) {
  if(!e) e = window.event;
  if(e.target)
    elem = e.target;
  else if(e.srcElement)
    elem = e.srcElement;
  else return;
  _oldx = e.clientX;
  _oldy = e.clientY;
  _offsety = parseInt($("predictionStats").style.top);
  _offsetx = parseInt($("predictionStats").style.left);

  if(isNaN(_offsetx)){
    _offsetx = e.clientX;
  } else {
    _offsetx = e.clientX - _offsetx;
  }
  if(isNaN(_offsety)){
    _offsety = e.clientY;
  } else {
    _offsety = e.clientY - _offsety;
  }

  document.onmousemove = _doDrag;
  document.onmouseup = _finishDrag;
}

function _doDrag(e) {
  if(!e) e = window.event;
  var y = e.clientY - _offsety;
  var x = e.clientX - _offsetx;
  $("predictionStats").style.top = y + "px";
  $("predictionStats").style.left = x + "px";
}

function _finishDrag(e) {
  if(!e) e = window.event;
  if(e.target)
    elem = e.target;
  else if(e.srcElement)
    elem = e.srcElement;
  else return;
  document.onmouseup = null;
  document.onmousemove = null;
}


//------------------------------------------------------------------------||
// Used to handle the response back from the php page                     ||
//------------------------------------------------------------------------||

function handleStatsResponse(name) {

	if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			var response = xmlhttp.responseText;
			$("predictionStats").style.top = "100px";
			$("predictionStats").style.left = (getDocMetrics()["width"] / 2) - 130 + "px";
			$("predictionStats").style.display = "block";
			$("prefsTitle").onmousedown = startDrag;
			changeText('prefsWindowTitle', name + '\'s Stats');
			var extra = '<div id="predDetailsContent" class="predDetailsContent"></div><div class="clear"></div>';
			document.getElementById("boxcontent").innerHTML = response+ "" + extra ;
		}
		$("wait").style.display = "none";
    }
}
   
//------------------------------------------------------------------------||
// Used to setup the request to the php page                              ||
//------------------------------------------------------------------------||

function getPredictionStats(userID, name) {
	checkCreateRequest();
	xmlhttp.onreadystatechange = new Function("handleStatsResponse('"+name+"')");
	var getstr = "?action=get_cont_pred_stats&id=" + userID;
	sendRequest('index.php', getstr);
}

//------------------------------------------------------------------------||
// Used to display the contents prediction details for this fixture       ||
//------------------------------------------------------------------------||

function morePredictionDetails(fixtureID, contentantID) {
	if($("predDetailsContent") != null)
	{
		$("predDetailsContent").style.display = "none";
	}
	new Effect.Scale("predictionStats", 190, "scaleX");
	setTimeout("continueGettingDetails("+fixtureID+","+ contentantID+")",1450);	
}

//------------------------------------------------------------------------||
// Used to continue display the predciton details				          ||
//------------------------------------------------------------------------||

function continueGettingDetails(fixtureID, contentantID) {
 	checkCreateRequest();
 	$("predDetailsContent").style.display = "block";
	xmlhttp.onreadystatechange = new Function("handlePredictionResponse("+fixtureID+","+contentantID+")");
	var getstr = "?action=get_detailed_pred_stats&id=" + contentantID +"&fid=" + fixtureID;
	sendRequest('index.php', getstr);
}

//------------------------------------------------------------------------||
// Used to handle the response for the prediction details info            ||
//------------------------------------------------------------------------||

function handlePredictionResponse(fixtureID,contentantID) {
	
	if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			var response = xmlhttp.responseText;
			
			$("predDetailsContent").innerHTML = response;
			Rico.Corner.round('predDetailsContent');
		}
		$("wait").style.display = "none";
    }
}

//------------------------------------------------------------------------||
// Used to close the predictons stats window                              ||
//------------------------------------------------------------------------||

function closePredictionStats(){
	$("predictionStats").style.display = "none";
	$("predictionStats").style.width = "250px";
	
}

