var map;
var countX = 0;
var localSearch = new GlocalSearch();

var icon = new GIcon();
icon.image = "http://www.google.com/mapfiles/marker.png";
icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);



function usePointFromPostcode(postcode) { //, callbackFunction) {
	
	localSearch.setSearchCompleteCallback(null, 
		function() {
			//alert(postcode);
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				//document.getElementById("result").innerHTML = "Result for " + postcode + " came from Google.";
			  var ajax_connection = createRequest();
			  ajax_connection.open('get', "cache.php?postcode=" + postcode + "&latitude=" + resultLat + "&longitude=" + resultLng);
			  
			  countX = 1;
			  //alert("cache.php?postcode=" + postcode + "&latitude=" + resultLat + "&longitude=" + resultLng);
			  ajax_connection.send(null);
			  //alert("cache.php?postcode=" + postcode + "&latitude=" + resultLat + "&longitude=" + resultLng);
				//callbackFunction(point);
			}else{
				alert("Postcode not found!");
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

function placeMarkerAtPoint(point)
{
	var marker = new GMarker(point,icon);
	//map.addOverlay(marker);
}

function setCenterToPoint(point)
{
	//map.setCenter(point, 17);
}

function showPointLatLng(point)
{
	alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(54.622978,-2.592773), 5, G_HYBRID_MAP);
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}

function createRequest() {
  
  // create an Ajax Request
  
  var ajaxRequest;
  
  try
  {
    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  }    
    catch (e1)
    {
      try
      {
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
        catch (e2)
        {
          ajaxRequest = new XMLHttpRequest();
        }
    }
  
  return ajaxRequest;
}


function usePointFromPostcodeViaCache(postcode) {//, callbackFunction) {
  //map.clearOverlays();
  var ajax_connection = createRequest();
  
  ajax_connection.open('get', "geocoder.php?postcode=" + postcode);
  //alert(postcode);
  // setup the function to deal with the reply
  ajax_connection.onreadystatechange = function(){
    //alert(ajax_connection.readyState);
    if (ajax_connection.readyState == 4) {
      var xmlDoc = ajax_connection.responseXML;
      var markers = xmlDoc.documentElement.getElementsByTagName("location");
      //alert(1);
      if (markers.length > 0)
      {
      	//alert(2);
        var resultLat = markers[0].getAttribute('latitude');
        var resultLng = markers[0].getAttribute('longitude');
        var resultDate = markers[0].getAttribute('date_added');
        var point = new GLatLng(resultLat,resultLng);
        
        //alert(resultLat + '' +resultLng);
        //document.getElementById("result").innerHTML = "Result for " + postcode + " came from cache. It was last cached on " + resultDate;
        //callbackFunction(point);
        //window.location.href='http://www.floorwarmingcompany.co.uk/postcodes/index.php?postcode='+postcode;
        countX = 1;
      }else{
      	//alert (3);
        usePointFromPostcode(postcode);//, callbackFunction);
        //alert(x);
        //window.location.href='http://www.floorwarmingcompany.co.uk/postcodes/index.php?postcode='+postcode;
      }
      
    }
  } 
  
  ajax_connection.send(null);
  
  //alert(1);
  //window.location.href='http://www.floorwarmingcompany.co.uk/postcodes/index.php?postcode='+postcode;
}

function sleeping(){
	//alert(x);
	if (countX == 0) { 
		//document.getElementById('counting').innerHTML = "Working "+Math.random(100);
		setTimeout("sleeping()",1000); 
	}
	else{
		document.getElementById('getList').submit();
	}
	
}
//addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);
