/**
 * @fileoverview Venda.Widget.GStoreLocatorBasic - using feature of Venda store locator but display the map by using Googlemaps graphical interface
 * @requires /venda-support/js/Venda.js
 * @requires Googlemaps API Key - a key can be generated at http://code.google.com/apis/maps/signup.html and enter key in ebiz switch
 */

// Create GStoreLocatorSimple namespace
Venda.namespace('Widget.GStoreLocatorBasic');

/**
* Stub function is used to support JSDoc.
* @class Venda.Widget.GStoreLocatorBasic
* @constructor
*/
Venda.Widget.GStoreLocatorBasic = function(){};

// Global Variables
Venda.Widget.GStoreLocatorBasic.map = null;					// Googlemap object
Venda.Widget.GStoreLocatorBasic.showSatellite = false;		// Option to show Satellite map type
Venda.Widget.GStoreLocatorBasic.showHybrid = false;			// Option to show Hybrid map type
Venda.Widget.GStoreLocatorBasic.showInfo = false;			// Option to show Store information window
Venda.Widget.GStoreLocatorBasic.infoAddress = "";			// Text to be displayed in the info. window
Venda.Widget.GStoreLocatorBasic.icon = new GIcon();			// Icon object
Venda.Widget.GStoreLocatorBasic.icon.iconSize = new GSize(20, 34);
Venda.Widget.GStoreLocatorBasic.icon.shadowSize = new GSize(37, 34);
Venda.Widget.GStoreLocatorBasic.icon.iconAnchor = new GPoint(10, 34);

/**
 * Initilise map.  
 * @param {string} mapCanvas 					Specify a html container to display the map
 * @param {string} storeAddress 				Specify an address to search in googlemaps
*/
Venda.Widget.GStoreLocatorBasic.init = function(mapCanvas,storeAddress) {
	if (GBrowserIsCompatible()) {
		this.map = new GMap2(document.getElementById(mapCanvas));
		this.map.enableScrollWheelZoom();
		this.map.addControl(new GMapTypeControl());
		this.map.addControl(new GLargeMapControl());
		// show satellite or hybrid map type options
		if(!this.showSatellite){this.map.removeMapType(G_SATELLITE_MAP);}
		if(!this.showHybrid){this.map.removeMapType(G_HYBRID_MAP);}
		// set default map type
		this.map.setMapType(this.defaultMapType);
		this.searchAddress(storeAddress);
	}else{
		document.getElementById("gmap_errors").innerHTML = this.notSupportedMsg;
	}
};

/**
 * Search store by the given address and displays on the map.
 * @param {string} storeAddress 				Specify an address to search in googlemaps
*/
Venda.Widget.GStoreLocatorBasic.searchAddress = function(storeAddress) {
	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);
				// display map and marker
				Venda.Widget.GStoreLocatorBasic.map.setCenter(point, Venda.Widget.GStoreLocatorBasic.zoomLvl);
				var marker = new GMarker(point,Venda.Widget.GStoreLocatorBasic.icon);
				Venda.Widget.GStoreLocatorBasic.map.addOverlay(marker);
				if(Venda.Widget.GStoreLocatorBasic.showInfo){
					Venda.Widget.GStoreLocatorBasic.map.openInfoWindowHtml(point,Venda.Widget.GStoreLocatorBasic.infoAddress);
					GEvent.addListener(marker, "click", function() {
						Venda.Widget.GStoreLocatorBasic.map.openInfoWindowHtml(point,Venda.Widget.GStoreLocatorBasic.infoAddress);
					});
				}
			}else{
				document.getElementById("gmap_errors").innerHTML = "Could not locate the address";
			}
		});	
	localSearch.execute(storeAddress);
};

/**
 * Unload the googlemaps object.
 * @param {object} func 				Specify the googlemaps object
*/
Venda.Widget.GStoreLocatorBasic.addUnLoadEvent = function(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  };
	}
};