$(document).ready(function() {
	
	var lat = $("div#miniMap span[class='lat']").html();
	var lng = $("div#miniMap span[class='long']").html();

	if(l = $("div#miniMap span[class='location']").html()) {
		//ok location span is there
		//thus we need to use google search to get lat and long
		//get target and store in db for future
		usePointFromLocation(l, store, true);
	} else if(l = $("div#miniMap span[class='latlng']").html()) {
		//ok location span is there
		//thus we need to use google search to get lat and long
		//get target and store in db for future
		//DIFFERENCE with test above is this time we dont want to append ,UK
		usePointFromLocation(l, store, false);
	} else if(l = $("div#miniMap span[class='postcode']").html()) {
		//ok postcode span is there
		//thus we need to use google search to get lat and long
		//get target and store in db for future
		usePointFromLocation(l, storePostCode, true);
	} else {
		//lat and long is ok and GoogleMaps friendly thus build map
		makeMap(lat, lng);
	}
	
});

/*----------------------------------
	makeMap - does exactly what it says on the tin
	makes a map and then places a marker where the
	markup suggests
----------------------------------*/
function makeMap(lat, lng) {
	
	if($("#miniMap").googleMap(lat, lng, 12))
	{
		//add markers using lat, lng
		var lat = parseFloat(lat);
		var lng = parseFloat(lng);
		
		var point = new GLatLng(lat, lng);
		var marker = new GMarker(point);
		
		$.googleMap.maps["miniMap"].addOverlay(marker);
		
		//additional map configuration;
		var mapControl = new GSmallZoomControl();
		$.googleMap.maps["miniMap"].addControl(mapControl);
		$.googleMap.maps["miniMap"].setMapType(G_NORMAL_MAP);
		
		$("#miniMap").find("div[dir='ltr']").hide();
	}
	else
	{
		$("div#map").css("display","none");
	}
}

/*----------------------------------
	store - makes an ajax call to a page which
	stores the lat, lng into a db ready for next time ;)
----------------------------------*/
function store(lat, lng, l, id) {

	$.post(deriveRoot()+"insert-new-location-lat-lng/", {
		ajax 	: 'true',
		id		: id,
		lat 	: lat,
		lng 	: lng
	}, function(data) {
		//insert an if statement to doing testing if required
		//we dont need to check whether it was a success or not
		//if it isnt googlemaps will still make a map and the user
		//can still roll to the next stage
		makeMap(lat, lng);
	});
}
/*----------------------------------
	storePostCode - makes an ajax call to a page which
	stores the lat, lng into a db ready for next time ;)
----------------------------------*/
function storePostCode(lat, lng, postcode) {

	$.post(deriveRoot()+"insert-new-postcode/", {
		ajax 	: 'true',
		postcode: postcode,
		lat 	: lat,
		lng 	: lng
	}, function(data) {
		//insert an if statement to doing testing if required
		//we dont need to check whether it was a success or not
		//if it isnt googlemaps will still make a map and the user
		//can still roll to the next stage
		alert(data);
		makeMap(lat, lng);
	});
}

/*----------------------------------
	usePointFromLocation - Uses GoogleSearch API
	to locate the requested location which could be surbub, town/city or county
	or combination of these
----------------------------------*/
function usePointFromLocation(l, callbackFunction, uk) {
	var localSearch = new GlocalSearch();

	localSearch.setSearchCompleteCallback(null,
	
	function() {
		if (localSearch.results[0]) {
			var resultLat = localSearch.results[0].lat;
			var resultLng = localSearch.results[0].lng;
			if(callbackFunction == storePostCode) {
				callbackFunction(resultLat, resultLng, l);
			} else {
				var id = $("div#miniMap span[class='id']").html();
				callbackFunction(resultLat, resultLng, l, id);
			}
		} else {
			$("div#miniMap").html('I\'m afriad we were unable to detect this location!')
		}
	});
	
	if(uk == true) {
		localSearch.execute(l+', UK');
	} else {
		localSearch.execute(l);
	}
}

function deriveRoot()
{
  var root = window.location.protocol+'//'+window.location.hostname+'/';
  
  if ( ( pos = window.location.pathname.search('public_html') ) != -1 )
  {
    root += window.location.pathname.substring(1,pos+12);
  }
  
  if ( window.location.pathname.search('demo') != -1 )
  {
    root += 'demo/';
  }
  
  return root;
}