var map;
var localSearch = new GlocalSearch();
var stored_latitude;
var stored_longitude;
var stored_postcode = '';
/*
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);
*/

var loc;
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);
				//storeLocation(resultLat,resultLng);
				callbackFunction(resultLat,resultLng);
			}else{
				// If postcode is an error need to prompt user to enter the correct postcode
				var pcode = prompt("Please enter a valid postcode");
				if (pcode != null && pcode != "") {
					usePointFromPostcode(pcode,callbackFunction);
					// only for CECB energy selector
					stored_postcode = pcode; // Update postcode
				}
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

function usePointFromPostcodeAndSet(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);
				
				// Store in input fields
				jQuery('#latitude').val(resultLat);
				jQuery('#longitude').val(resultLng);
				
				// Hide invalid postcode
				jQuery('#invalid-postcode-error').hide();
        		
        		jQuery('#map-explanation').show();
        			
				//storeLocation(resultLat,resultLng);
				callbackFunction(resultLat,resultLng);
			}else{
				jQuery('#map-explanation').hide();
				// Show invalid postcode
				jQuery('#invalid-postcode-error').show(); 
				
				// Reset fields
				jQuery('#energyselector_postcode').val('');
				jQuery('#latitude').val(resultLat);
				jQuery('#longitude').val(resultLng);
				
				// Focus on postcode field
				jQuery('#energyselector_postcode').focus();
				
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

var marker;
function placeMarkerAtPoint(lat,long)
{	
	map.clearOverlays();
	loc = new GLatLng(lat, long);
	var marker = new GMarker(loc, {draggable: true});
  	map.addOverlay(marker);
	moveToMarker();
 
	
	GEvent.addListener(marker, "dragstart", function() {
  		//map.closeInfoWindow();
  	});

	GEvent.addListener(marker, "dragend", function() {
 		// marker.openInfoWindowHtml("Just bouncing along...");
 		var myLoc = marker.getLatLng();
 		var lat = myLoc.lat();
		var long = myLoc.lng();
  		loc = new GLatLng(lat, long);
  		moveToMarker();
  	});
  
}

function moveToMarker() {
  map.setCenter(loc);
  map.setZoom(19);
  storeLocation(loc.lat(),loc.lng());
}

function setCenterToPoint(point)
{
	map.setCenter(point, 17);
}



function mapLoad(postcode) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"),{ size: new GSize(500,220) });
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(54.622978,-2.592773), 1, G_SATELLITE_MAP);
		usePointFromPostcode(postcode,placeMarkerAtPoint);
		
	}

}

function mapLoadCustom(postcode,width,height) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"),{ size: new GSize(width,height) });
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(54.622978,-2.592773), 1, G_SATELLITE_MAP);
		usePointFromPostcode(postcode,placeMarkerAtPoint);
		
	}
}

var map_is_loaded = false;
function setMapLoaded(loaded)
{
	map_is_loaded = loaded;
}

function mapLoadAndSet(postcode,width,height) {
	if (map_is_loaded == true) {
		return;
	}

	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"),{ size: new GSize(width,height) });
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(54.049929,-4.460449), 5, G_SATELLITE_MAP);
		if (postcode != '') {
			usePointFromPostcodeAndSet(postcode, placeMarkerAtPoint);
		}else {
			jQuery('#map-explanation').hide();
		}
	}
}

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 storeLocation(lat,long) {
	stored_latitude = lat;
	stored_longitude = long;
	
	// Store in input fields
	jQuery('#latitude').val(lat);
	jQuery('#longitude').val(long);
}

//addLoadEvent(mapLoad);
//addUnLoadEvent(GUnload);

