/*
Plot a point on the map that corresponds to the given post code
You have to do an ajax lookup if you don't want to pay for the postcode database!
*/
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);
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}


/* Utility functions */

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

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

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

function mapLoad(mapholder, region) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(mapholder));
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
			
		if(region == 'Spain'){
			map.setCenter(new GLatLng(40.433333,-3.7), 5, G_NORMAL_MAP);
			
		}else if(region == 'Ireland'){
			map.setCenter(new GLatLng(53.25,-7.5), 6, G_NORMAL_MAP);
			
		}else{
			//Default to UK
			map.setCenter(new GLatLng(54.622978,-2.592773), 5, G_NORMAL_MAP);
		}
		
		
	}
}


/* onload events */

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 setYellowtomCenter(point)
{
	setCenterToPoint(point);
	placeMarkerAtPoint(point);	
}


/*
Toggle visibility of map in category list
*/
function revealMap(mapblockid, postcode_or_clat, clong){

	//alert('Plotting: ' + postcode_or_clat);

	var mapblock = document.getElementById(mapblockid);
	
	if(mapblock){
	  if(mapblock.style.display == 'none' || mapblock.style.display == ''){
	  	mapblock.style.display = 'block';
	  }else{
	  	mapblock.style.display = 'none';
	  }
	
	  mapLoad(mapblockid);
	  if(clong != ''){
	  	var point = new GLatLng(postcode_or_clat, clong);
	  	setCenterToPoint(point);
	  }else{
	  	usePointFromPostcode(postcode_or_clat, setYellowtomCenter);
	  }
	}

}//revealMap


/*
Add yellowtom group marker
*/
function createMarker(point, groupname, groupdir) {
	var icon = new GIcon();
	
	//icon.image = "http://www.yellowtom.co.uk/images/smoke.png";
	//icon.iconSize = new GSize(21, 26);
	//icon.shadowSize = new GSize(22, 20);
	//icon.iconAnchor = new GPoint(15, 26);
	//icon.infoWindowAnchor = new GPoint(15, 26);
	
	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);
	icon.infoWindowAnchor = new GPoint(15, 26);


  var marker = new GMarker(point, icon);
  
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml("<strong> Yellowtom in "+groupname+"</strong><br/><br/>Click <a href=\"/"+groupdir+"/\">here</a> to find businesses in " + groupname+".");
  });
  
  return marker;
}


/*
Center the map on a given area of the country
(lat/long equates to areas commented)
*/
function centerBigMap(clat,clong){

	bigmap.setCenter(new GLatLng(clat, clong), 8, G_NORMAL_MAP);
	
}//centerBigMap


/*
Show the big map using jquery (must already be loaded)
*/
function revealBigMap(clat, clong){
		
	if ($("#YTBigMapLoader").is(":hidden")) {
		$("#YTBigMapLoader").slideDown("fast", function() { revealBigMapContent(clat, clong); });
		
	}else{
		centerBigMap(clat, clong);
	}
	
}//revealBigMap


/*
Callback for after jquery has finished in revealBigMap (as you can't load a gmap into an unrendered div)
plotgroups() must be an inline function
*/
function revealBigMapContent(clat, clong){

	if (GBrowserIsCompatible()) {
		bigmap = new GMap2(document.getElementById('YTBigMapLoader'));

		bigmap.addControl(new GLargeMapControl());
		bigmap.addControl(new GMapTypeControl());
	
		centerBigMap(clat, clong);

		plotGroups();
	}

}//revealBigMapContent


/*
As for the big map, but used on inline map listings in adverts - jquery again
*/
function revealInlineMap(mapid, postcode, region, lat, long){
		
	if ($("#" + mapid).is(":hidden")) {
		$("#" + mapid).slideDown("fast", function() { revealInlineMapContent(mapid, postcode, region, lat, long); });
		
	}else{
		$("#" + mapid).slideUp("fast");
	}
	
}//revealBigMap


/*
Inline map callback
*/
function revealInlineMapContent(mapid, postcode, region, lat, long){

	mapLoad(mapid, region);
	
	if(lat != '')
	{
		//map.setCenter(new GLatLng(lat,long), 5, G_NORMAL_MAP);	
		var point = new GLatLng(lat,long);
		
		setYellowtomCenter(point);
	}
	else
	{
		if(region=='UK')
		{
			usePointFromPostcode(postcode, setYellowtomCenter);
		}	
	}

}//revealBigMapContent


/*
Google globals for Yellowtom mapping
*/

var bigmap;
var localSearch;
var icon;

$(document).ready(function()
{
	localSearch = new GlocalSearch();
	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);
});
