var map;
var localSearch = new GlocalSearch();

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);


function usePointFromPostcode(postcode,sitename,telephone,address,general_class,zoom_factor,view_type,longditude,latitude) {

	var view = G_NORMAL_MAP;
	if(view_type == 's'){
		view = G_SATELLITE_MAP;
    }else if(view_type == 'h'){
		view = G_HYBRID_MAP;
	}

	var zoom = 17;
	if(longditude != ''){
    	long_val = longditude * 1;
	}

	if(latitude != ''){
    	lat_val = latitude * 1;
	}

	if(zoom_factor != ''){
    	zoom = zoom_factor * 1;
	}

	var message = '';
	if(sitename != ''){
		if(general_class != ''){
			message += '<div id="sitename" class="' + general_class + '">' + sitename + '</div>';
		}else{
			message += '<h1>' + sitename + '</h1>';
		}
	}
	if(telephone != ''){
		if(general_class != ''){
			message += '<div id="telephone" class="' + general_class + '">' + telephone + '</div>';
		}else{
			message += '<h2>' + telephone + '</h2>';
		}
	}
	if(address != ''){
		if(general_class != ''){
			message += '<a class="' + general_class + '" href="mailto:' + address + '">' + address + '</a><br />';
		}else{
			message += '<a href="mailto:' + address + '">' + address + '</a><br />';
		}
	}
	if(general_class != ''){
		message += '<div id="message" class="' + general_class + '">Use the arrow buttons to<br />move the map around or simply<br />drag the map with your mouse.</div>';
	}else{
		message += 'Use the arrow buttons to<br />move the map around or simply<br />drag the map with your mouse.';
	}
	localSearch.setSearchCompleteCallback(null,
		function() {
			
			if(postcode == ''){
				var point = new GLatLng(lat_val,long_val);
				mapLoad();
				map.setCenter(point,zoom,view);

				var marker = new GMarker(point,icon);
				map.addOverlay(marker);

				if((sitename != '')||(telephone != '')||(address != '')){
					map.openInfoWindow(point,message);
					GEvent.addListener(marker, "click", function() {
						map.openInfoWindow(point,message);
					});
				}
			}else{
				if (localSearch.results[0])
				{
					var resultLat = localSearch.results[0].lat;
					var resultLng = localSearch.results[0].lng;
					var point = new GLatLng(resultLat,resultLng);
					map.setCenter(point,zoom,view);
	
					var marker = new GMarker(point,icon);
					map.addOverlay(marker);

					if((sitename != '') || (telephone != '') || (address != '')){
						map.openInfoWindow(point,message);
						GEvent.addListener(marker, "click", function() {
							map.openInfoWindow(point,message);
						});
					}
				}else{
					alert("Postcode not found!");
				}
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
	}
}

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);

