var lif_map = {

	postcodes : {},

	localSearch: new GlocalSearch(),
	
	icon: new GIcon(),
	
	init: function(postcodes){
		
		lif_map.postcodes = postcodes;
			
		lif_map.icon.image 		= "http://www.google.com/mapfiles/marker.png";
		lif_map.icon.shadow 	= "http://www.google.com/mapfiles/shadow50.png";
		lif_map.icon.iconSize 	= new GSize(20, 34);
		lif_map.icon.shadowSize = new GSize(37, 34);
		lif_map.icon.iconAnchor = new GPoint(10, 34);
			
		lif_map.mapLoad();
		
		for (var postcode in postcodes)
		{
			lif_map.get_point_local(postcode, lif_map.store_point, true);
		}
				
		lif_map.addUnLoadEvent(GUnload);
				
	},
	
	
	get_point_local: function(postcode, callbackFunction, move_to_point){
		
		// try get point locally, then from Google
		
		$.ajax({
			
			url : '/rest/postcode/' + postcode.replace(' ', '_'),
			
			success: function(response){
				
				TEST = response;
				
				var resultLat = parseFloat($(response.firstChild).attr('lat'));
				var resultLng = parseFloat($(response.firstChild).attr('long'));
				var point = new GLatLng(resultLat, resultLng);
							
				callbackFunction(postcode, point, move_to_point);
				
			},
			
			error : function()
			{
				lif_map.get_point_remote(postcode, callbackFunction, move_to_point);
			}
			
		});
		
	},
		
	get_point_remote: function(postcode, callbackFunction, move_to_point){
				
		lif_map.localSearch.setSearchCompleteCallback(null, function(){
			
			if (lif_map.localSearch.results[0]) {
				var resultLat = lif_map.localSearch.results[0].lat;
				var resultLng = lif_map.localSearch.results[0].lng;
				var point = new GLatLng(resultLat, resultLng);
				
				// store lat and long
				
				$.ajax({
					
					type: 	'POST',
					url :	'/rest/postcode/' + postcode.replace(' ', '_'),
					data:	{'lat' :  resultLat,
							 'long':  resultLng}
					
				});
							
				callbackFunction(postcode, point, move_to_point);
			}
			else {
				//alert("Postcode not found!");
			}
		});
		
		lif_map.localSearch.execute(postcode + ", UK");
		
	},
	
	store_point : function(postcode, point, move_to_point)
	{
				
		lif_map.postcodes[postcode].point = point;
		
		if (lif_map.postcodes[postcode].image_path)
		{
			
			var img = new Image();
					
			$(img).load(function(){
								
				$(this).hide();
				
				$('body').append(this);
				
				var width  = $(this).width();
				var height = $(this).height();
					
				lif_map.place_marker(point, '<img src="' + lif_map.postcodes[postcode].image_path + '" width=' + width + ' height=' + height + '>');
				
				if (move_to_point)
					lif_map.move_to_point(point);
			
			}).error(function () {
								
				lif_map.place_marker(point, '', false);
				
				if (move_to_point)
					lif_map.move_to_point(point);
				
	    	}).attr('src', lif_map.postcodes[postcode].image_path);
		
		} else {
								
			lif_map.place_marker(point, '', false);
			
			if (move_to_point)
				lif_map.move_to_point(point);
				
		}
		
	},
	
	place_marker: function(point, html, open_info_window)
	{
				
		if (open_info_window == null)
			open_info_window = true;
		
		var marker = new GMarker(point);
		
		if (html)
		{
	        GEvent.addListener(marker, "click", function() {
	          marker.openInfoWindowHtml(html);
	        });		
		}
			
		lif_map.map.addOverlay(marker);
		
		if (open_info_window)
			marker.openInfoWindowHtml(html);
		
	},
	
	move_to_point: function(point){
		lif_map.map.setCenter(point, 15);
	},
	
	showPointLatLng: function(point){
		alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
	},
	
	mapLoad: function(){
		
		if (GBrowserIsCompatible()) {
			
			lif_map.map = new GMap2($('#map')[0]);
			
			lif_map.map.addControl(new GSmallZoomControl());
			
		}
	},
	
	addLoadEvent: function(func){
	
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		}
		else {
			window.onload = function(){
				oldonload();
				func();
			}
		}
		
	},
	
	addUnLoadEvent: function(func){
		var oldonunload = window.onunload;
		if (typeof window.onunload != 'function') {
			window.onunload = func;
		}
		else {
			window.onunload = function(){
				oldonunload();
				func();
			}
		}
	}
	
}
