var map;

var icon = new GIcon();
icon.image = "http://www.thebusinesshours.co.uk/images/arrow.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 setupDragZoom() {
      /* first set of options is for the visual overlay.*/
      var boxStyleOpts = {
          opacity: .2,
          border: "2px solid red"
      };

      /* second set of options is for everything else */
      var otherOpts = {
          buttonHTML: "<img src='http://gmaps-utility-library.googlecode.com/svn/trunk/dragzoom/release/examples/zoom-button.gif' />",
          buttonZoomingHTML: "<img src='http://gmaps-utility-library.googlecode.com/svn/trunk/dragzoom/release/examples/zoom-button-activated.gif' />",
          buttonStartingStyle: {width: '24px', height: '24px'},
          overlayRemoveTime: 1000
      };

      map.addControl(new DragZoomControl(boxStyleOpts, otherOpts, {}), 
                                         new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(13, 120)));
    }

function usePointFromPostcode(postcode,callbackFunction) {
	var localSearch = new GlocalSearch();
	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);
				//alert(resultLat + "--" + resultLng);
				//var marker = new GMarker(point,icon);
				//map = new GMap2(document.getElementById("map"));
				//map.addOverlay(marker);
				callbackFunction(point);
				setCenterToPoint(point);
			}else{
				alert("Postcode not found!");
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

function placeMarkerAtPoint(point)
{
	var marker = new GMarker(point);
	
	// to show the bubble text
	/*GEvent.addListener(marker, "mouseover", function() {
		var tab1 = new GInfoWindow("Info", '<div id="tab1" class="bubble">hghgf</div>');
		var infoTabs = [tab1];
	marker.openInfoWindowHtml("lllll");
	*/
	/*GEvent.addListener(marker, "mouseover", function() {
		//ddrivetip('$'+price,'yellow', 300)  // yellow is bgcolor and 300 is width
		ddrivetip(price)
	});
	GEvent.addListener(marker, "mouseout", function() {
		hideddrivetip()
	});*/
	map.addOverlay(marker);
}

function setCenterToPoint(point)
{
	map.setCenter(point, 15);
}

function showPointLatLng(point)
{
	alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		 //map.addMapType(G_PHYSICAL_MAP) ;
		 //map.removeMapType(G_HYBRID_MAP); // removes Hybrid button from map
		 //map.addControl(new GScaleControl()) ;
		 map.addControl(new GOverviewMapControl()) ;
		//map.addControl(new GLargeMapControl()); // for '+' , '-' , the direction buttons and the zoom ladder
		//map.addControl(new GSmallMapControl()); // for '+' , '-' and the direction buttons
		map.addControl(new GSmallZoomControl());  // for '+' & '-' only
		map.addControl(new GMapTypeControl(new GSize(20,10)));
		map.enableDoubleClickZoom();
		//map.setCenter(new GLatLng(54.622978,-2.592773), 5, G_HYBRID_MAP);
		map.setCenter(new GLatLng(51.30,0.30), 10);
		setupDragZoom();
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addLoadEvent2(postcode,func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
	  
    window.onload = func;
  } else {
    window.onload = function() {
      //alert(func);
	  oldonload();
      func(postcode,placeMarkerAtPoint);
    }
  }
}

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);
//addLoadEvent2("WC2B 5PQ",usePointFromPostcode);