// AJAX API = ABQIAAAAc8xc3JNzWGT7hz1H6Dmy-RT2yXp_ZAY8_ufC3CFXhHIE1NvwkxRKK_dxWXfAve9V6ks_LWJo87L2XQ
// AJAX API = ABQIAAAAc8xc3JNzWGT7hz1H6Dmy-RT2yXp_ZAY8_ufC3CFXhHIE1NvwkxRKK_dxWXfAve9V6ks_LWJo87L2XQ
// Leeds map.setCenter(new GLatLng(53.7942, -1.5601), 13);

var map;
var aCircles = new Array();
var iNumberCircles = 0;

var localSearch = new GlocalSearch();
var bHasLoaded = false; 
var j = 0;
var Smiffiei = 0;
var marker = new Array();
var yourMarker;
var aInfo = new Array();
var mapLoaded = false;

var icon = new GIcon();
icon.image = "/images/presentation/map_icon.gif";
icon.shadow = "/images/presentation/map_icon_shadow.png";

icon.iconSize = new GSize(16, 16);
icon.shadowSize = new GSize(29, 21);
icon.iconAnchor = new GPoint(7, 7);
icon.infoWindowAnchor = new GPoint(34, 0);


function createMarkers(aCircles)
{
    for (var x = 1; x <= aCircles.length; x++)
    {   
        var longitude = aCircles[x][1];
        var latitude = aCircles[x][2];
        var info = aCircles[x][3];
        
        var point = new GLatLng(latitude, longitude);   
	    map.addOverlay(createMarker(point, info));
	}
}


function usePointFromPostcode(postcode, callbackFunction) 
{
	localSearch.setSearchCompleteCallback(null, 
		function() {
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				callbackFunction(point)
			}else{
				//alert("Postcode not found!");
			}
		});	
	localSearch.execute(postcode);
}

function addYourOverlay(point)
{
    map.addOverlay(createYourMarker(point));
    setCenterToPoint(point)
////////////////////////////////////////////////////////////////////////////////
    document.getElementById('long_lat').innerText = point.toString();
////////////////////////////////////////////////////////////////////////////////
}

function createYourMarker(point)
{  
    var yourIcon = new GIcon();
    yourIcon.image = "/images/presentation/pin.png";
    yourIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    yourIcon.iconSize = new GSize(24, 40);
    yourIcon.shadowSize = new GSize(44, 40);
    yourIcon.iconAnchor = new GPoint(12, 40);
    yourIcon.infoWindowAnchor = new GPoint(5, 1);

    yourMarker = new GMarker(point, yourIcon);     
    return yourMarker;
}

function createMarker(point, info)
{  
    marker[j] = new GMarker(point, icon); 
    GEvent.addListener(marker[j], "click",
        function()
        {
            //alert(aInfo[localI]);
            this.openInfoWindowHtml('<div class="infoWindow">'+info+'</div>');
        }
    );
    
    j++;
    
    return marker[(j -1)];
}


function setCenterToPoint(point)
{
	map.setCenter(point, 8);
}

function mapLoad(pageLong, pageLat, aCircles, strMapZoom) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("googleMap"));		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(pageLong, pageLat), strMapZoom, G_HYBRID_MAP);
	}
    for (var p = 0; p <= (aCircles.length -1); p++)
    {   
        var longitude = aCircles[p][0];
        var latitude = aCircles[p][1];
        var info = aCircles[p][2];
        
        var point = new GLatLng(latitude, longitude);   
	    map.addOverlay(createMarker(point, info));
	}
}

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();
	  }
	}
}

//addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);

//startCheck = setTimeout('addDetailsCheck()',2000);

//function addDetailsCheck()
//{
//    if(bHasLoaded == true)
//    {
//        addDetailsToMap();
//    }
//    else
//    {
//        startCheck = setTimeout('addDetailsCheck()',100);
//    }
//}





function placeMarkerFromLongLat(longitude, latitude, info)
{
	var point = new GLatLng(longitude, latitude);   
	placeMarkerAtPoint(point, info)
}

function placeMarkerAtPoint(point, info)
{
	map.addOverlay(createMarker(point, info));
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Bespoke functions to print out the longitude and latitude SQL so it can be entered intot he pagemeta data DB
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function getLongLat(postcode, callbackFunction, info) 
{
    aInfo[j] = info;

	localSearch.setSearchCompleteCallback(null, 
		function() {
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				callbackFunction(point, info)
			}else{
				//alert("Postcode not found!");
			}
		});	
	//Bizzarely, you have to send 'us' with the postcode to get the UK long/lat!!!!!?
	localSearch.execute(postcode);
	//localSearch.execute(postcode+', au');
	
	j++;
}
var strSQL = '';
function displayLongLat(point, info)
{
    var localI = Smiffiei;
    
    var b = point.toString();
    
    var len = b.length;
    len = len - 1;
    b = b.substring(1,len);
    var temp = new Array();
    temp = b.split(', ');

    strSQL += 'UPDATE core_tContentItems_SYS_PageMeta_Versions SET [FieldContent_80C64197-DFD5-41B7-B7F8-994700C07E7D] = ' + temp[0] + ', [FieldContent_7EE083CB-8EDE-4F10-A7DC-994700C0DD42] = ' + temp[1] + ' WHERE [ContentItemVersionID] = ' + "'" + aInfo[localI] + "'" +';\n';
    //document.write(strSQL);
    
    document.getElementById("sqlText").innerText = strSQL;
    
    Smiffiei++;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////