var curoInformationGmtOffset = 0;

var curoKeyInformationHTML = "Loading....";
var curoRegionsHTML = new Array();
var curoInRegion = 0;
var Initial_Region = 0;

function curoPageLoadFinished(region_id)
{
	curoKeyInformationHTML="Loaded";
	curoOpenInformation(region_id,"");
	Initial_Region=region_id;
	// curoKeyInformationHTML = document.getElementById("curoKeyInformation").innerHTML;
}

function curoOpenInformation(region, event)
{
  curoInRegion = region;

  if (curoKeyInformationHTML == "Loading....")
  {
    return(0);
  }

  if (curoRegionsHTML[region] != null)
  {
    document.getElementById("curoKeyInformation").innerHTML = curoRegionsHTML[region];
    curoSetClock();
    return(0);
  }

  var xmlhttp = new XMLHttpRequest();

  xmlhttp.open('GET', '/CuroInformation.php?region=' + region, true);

  xmlhttp.onreadystatechange = function()
  {
    if (xmlhttp.readyState == 4)
    {
      if (xmlhttp.status == 200)
      {
        curoPopupInformation(xmlhttp.responseXML, region);
      }
      else
      {
        alert("Overseas Error Message : " + xmlhttp.status);
      }
    }
  };

  xmlhttp.send(null);
}

function curoPopupInformation(xml, region, cursor)
{
	var test1 = "testing";
	var test1 = "testing";
	
	var max1  = xml.getElementsByTagName('max1')[0].firstChild.data;
	
	var test1 = "testing";
	var test1 = "testing2";
	
	var max2  = xml.getElementsByTagName('max2')[0].firstChild.data;
	
	var test1 = "testing3";
	
	var max3  = xml.getElementsByTagName('max3')[0].firstChild.data;
	var max4  = xml.getElementsByTagName('max4')[0].firstChild.data;
	var max5  = xml.getElementsByTagName('max5')[0].firstChild.data;
	var max6  = xml.getElementsByTagName('max6')[0].firstChild.data;
	var max7  = xml.getElementsByTagName('max7')[0].firstChild.data;
	var max8  = xml.getElementsByTagName('max8')[0].firstChild.data;
	var max9  = xml.getElementsByTagName('max9')[0].firstChild.data;
	var max10 = xml.getElementsByTagName('max10')[0].firstChild.data;
	var max11 = xml.getElementsByTagName('max11')[0].firstChild.data;
	var max12 = xml.getElementsByTagName('max12')[0].firstChild.data;

	var innerHTML = "<div id=\"keyInfoHeader\"><img src=\"/images/title_key_information.gif\" align=\"left\" alt=\"Key Information\" /> <img src=\"http://www.overseas-homes-direct.com/images/flags/" + xml.getElementsByTagName("flag")[0].firstChild.data  + "\" height=\"35\" align=\"right\" border=\"1\" alt=\"Flag for " + xml.getElementsByTagName('name')[0].firstChild.data +" \" />\n" + "</div>\n" + "<div id=\"curoInfoName\">&nbsp;&nbsp;&nbsp;<b>" + xml.getElementsByTagName('name')[0].firstChild.data + "</div>\n";

	var div1 =(Math.floor(max1/3)*3)+'';
	var div2 =(Math.floor(max2/3)*3)+'';
	var div3 =(Math.floor(max3/3)*3)+'';
	var div4 =(Math.floor(max4/3)*3)+'';
	var div5 =(Math.floor(max5/3)*3)+'';
	var div6 =(Math.floor(max6/3)*3)+'';
	var div7 =(Math.floor(max7/3)*3)+'';
	var div8 =(Math.floor(max8/3)*3)+'';
	var div9 =(Math.floor(max9/3)*3)+'';
	var div10 =(Math.floor(max10/3)*3)+'';
	var div11 =(Math.floor(max11/3)*3)+'';
	var div12 =(Math.floor(max12/3)*3)+'';

	div1=div1.replace(/-/,"m");
	div2=div2.replace(/-/, 'm');
	div3=div3.replace(/-/,"m");
	div4=div4.replace(/-/,"m");
	div5=div5.replace(/-/,"m");
	div6=div6.replace(/-/,"m");
	div7=div7.replace(/-/,"m");
	div8=div8.replace(/-/,"m");
	div9=div9.replace(/-/,"m");
	div10=div10.replace(/-/,"m");
	div11=div11.replace(/-/,"m");
	div12=div12.replace(/-/,"m");

  	var image_path = xml.getElementsByTagName("image_path")[0].firstChild.data;
  	
  	if (image_path!="")
  	{
  		image_path = "http://" + image_path;
		var newImg = new Image();
		newImg.src = image_path;
		var height = newImg.height;
		var width = newImg.width;
		
		if (height < width)
		{
			portrait="width";
		}else
		{
			portrait="height";
		}
		var value="150";
		if (width==0) 
		{portrait="height"; value="120"; }
		image_path="<img src=\"" + image_path + "\" "+ portrait + "=\"" + value + "\" alt=\"Image for " + xml.getElementsByTagName('name')[0].firstChild.data + "\" />\n";
	}

	if (region>=8)
	{
		innerHTML = innerHTML + "<div id=\"keyInfoTemps\">\n" +
		"      <div id=\"temp"+ div1 + "\">J</div>\n" +
		"      <div id=\"temp"+ div2 + "\">F</div>\n" +  
		"      <div id=\"temp"+ div3 + "\">M</div>\n" +  
		"      <div id=\"temp"+ div4 + "\">A</div>\n" +  
		"      <div id=\"temp"+ div5 + "\">M</div>\n" +  
		"      <div id=\"temp"+ div6 + "\">J</div>\n" +  
		"      <div id=\"temp"+ div7 + "\">J</div>\n" +  
		"      <div id=\"temp"+ div8 + "\">A</div>\n" +  
		"      <div id=\"temp"+ div9 + "\">S</div>\n" +  
		"      <div id=\"temp"+ div10 + "\">O</div>\n" +  
		"      <div id=\"temp"+ div11 + "\">N</div>\n" +  
		"      <div id=\"temp"+ div12 + "\">D</div><br>\n" +  
		"      <div id=\"temp"+ div1 + "\">" + max1 + "</div>\n" +
		"      <div id=\"temp"+ div2 + "\">" + max2 + "</div>\n" +  
		"      <div id=\"temp"+ div3 + "\">" + max3 + "</div>\n" +  
		"      <div id=\"temp"+ div4 + "\">" + max4 + "</div>\n" +  
		"      <div id=\"temp"+ div5 + "\">" + max5 + "</div>\n" +  
		"      <div id=\"temp"+ div6 + "\">" + max6 + "</div>\n" +  
		"      <div id=\"temp"+ div7 + "\">" + max7 + "</div>\n" +  
		"      <div id=\"temp"+ div8 + "\">" + max8 + "</div>\n" +  
		"      <div id=\"temp"+ div9 + "\">" + max9 + "</div>\n" +  
		"      <div id=\"temp"+ div10 + "\">" + max10 + "</div>\n" +  
		"      <div id=\"temp"+ div11 + "\">" + max11 + "</div>\n" +  
		"      <div id=\"temp"+ div12 + "\">" + max12 + "</div><br>\n" +  
		"</div>\n";
	}

	innerHTML = innerHTML +
	"<div id=\"curoBoxText\">\n" + xml.getElementsByTagName("short_description")[0].firstChild.data +
	"</div>\n" +
	"<div id=\"curoBoxLeft\">\n" +
	"  Properties For Sale<br />" + xml.getElementsByTagName("travel")[0].firstChild.data + "<br />\n" +
	"  <br /><br />Flat Prices From<br /> " + xml.getElementsByTagName("flats")[0].firstChild.data + "<br />\n" +
	"  <br /><br />Villa Prices From<br /> " + xml.getElementsByTagName("villas")[0].firstChild.data + "<br /><br />\n" +
	"  <div id=\"curoInformationClock\">Local Time : 00:00:00<br /></div>\n" +
	"</div>\n" +
	"<div id=\"curoBoxRight\">\n" + image_path
	"</div>\n";

	curoRegionsHTML[region] = innerHTML;

	if (curoInRegion == region)
	{
		document.getElementById("curoKeyInformation").innerHTML = innerHTML;
		curoInformationGmtOffset=parseInt(xml.getElementsByTagName("gmt")[0].firstChild.data)*60000;
		curoSetClock();
		if (curoKeyInformationHTML=="Loaded")
		{
			curoKeyInformationHTML = innerHTML;
		}
	}
}

function curoCloseWindow()
{
	if (curoKeyInformationHTML == "Loading....")
	{
		return(0);
	}

	curoInRegion = 0;
	document.getElementById("curoKeyInformation").innerHTML = curoKeyInformationHTML;
}

function curoSetClock()
{
	
  var information = document.getElementById('curoInformation');

  if (curoInRegion > 0)
  {
    window.setTimeout( "curoSetClock()", 1000 );
  }

	
  var date = new Date();
  
  date.setTime(date.getTime()+curoInformationGmtOffset);
  var h=date.getHours();
  var m=date.getMinutes();
  var s=date.getSeconds();
 
 m=curoCheckTime(m);
  s=curoCheckTime(s);

  var hours = date.getHours(); // + parseInt(regionInformationGmtOffset);
  date.setHours(hours);

  if (document.getElementById('curoInformationClock'))
  {
    document.getElementById('curoInformationClock').innerHTML = "Local Time : " + h+":"+m+":"+s;
  }
}

function curoCheckTime(i)
{
if (i<10) 
  {i="0" + i}
  return i
}


function curoGetCursorAndBrowserExtents(event)
{
  event = event || window.event;
  var cursor = {x:0, y:0, width:0, height:0, scrollX:0, scrollY:0};

  if (event.pageX || event.pageY)
  {
    cursor.x = event.pageX;
    cursor.y = event.pageY;
  }
  else
  {
    cursor.x = event.clientX+(document.documentElement.scrollLeft || document.body.scrollLeft)-document.documentElement.clientLeft;
    cursor.y = event.clientY+(document.documentElement.scrollTop || document.body.scrollTop)-document.documentElement.clientTop;
  }

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

  if (typeof(window.pageYOffset) == "number")
  {
    //Netscape compliant
    cursor.scrollX = window.pageXOffset;
    cursor.scrollY = window.pageYOffset;
  }
  else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
  {
    //DOM compliant
    cursor.scrollX = document.body.scrollLeft;
    cursor.scrollY = document.body.scrollTop;
  }
  else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
  {
    //IE6 standards compliant mode
    cursor.scrollX = document.documentElement.scrollLeft;
    cursor.scrollY = document.documentElement.scrollTop;
  }

  return cursor;
}
