/*
	Author: Kurt Kellens
	Date: 	05/02/2009
	Description: Google Map Implementation
	Goolge API: V2
	How to: call googlemap function on pageonload or document.ready event (jQuery)
*/

	function googleMap(xmlPath,mapItem,countryCode){
		/*extend the String object*/	
		String.prototype.ltrim = function () { return this.replace(/^ */,""); } 
		String.prototype.rtrim = function () { return this.replace(/ *$/,""); } 
		String.prototype.trim = function () { return this.ltrim().rtrim(); }
		
		function UndefinedToString(field){
			return field==undefined?"":field
		}

		if (GBrowserIsCompatible()) {
	
			var initCountryCode = countryCode;
			var initStateAreaCode = '';
			var gmarkers = [];
			var i=0;
			var map = new GMap2(document.getElementById(mapItem));
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(51, -0.1), 1);

			//Get markers (marker tags)
			var getMarkers = function(){
				gmarkers = [];
				i = 0;
				jQuery.get(xmlPath, function(data) {
											
					jQuery(data).find('country[code='+ initCountryCode +']').each(function(){
						jQuery(this).find('marker').each(function(){
							var $marker = jQuery(this);
							/*Google -- add markers*/
							var lat = parseFloat($marker.attr("lat"));
							var lng = parseFloat($marker.attr("lng"));
							var point = new GLatLng(lat,lng);
							var name = UndefinedToString($marker.attr("name"));
							var phone = UndefinedToString($marker.attr("phone"));
							var url = UndefinedToString($marker.attr("url"));
							var address = UndefinedToString($marker.attr("address"));
							var city = UndefinedToString($marker.attr("city-location"));
							var company = UndefinedToString($marker.attr("company"));
							var zip = UndefinedToString($marker.attr("zip"));
							var loc_state = UndefinedToString($marker.attr("state"));
							var extra = UndefinedToString($marker.attr("extra"));
							// create the marker
							var marker = createMarker(point,name,phone,url,name,address,city,zip,loc_state,extra, company);
							map.addOverlay(marker);
							
							i++;
						})
					});
				});
			};
			
			//Fill Country dropdown (country tags)
			jQuery.get(xmlPath, function(data) {
				var attribSelected;
				var lat;
				var lng;
				var zl;
				var html = '<option>Please select a country</option>';
				jQuery(data).find('country[lng!=""][lat!=""][code!=""]').each(function(){
					var $country = jQuery(this);
					if($country.attr('code') == initCountryCode){
						attribSelected = 'selected="selected"';
						lat = parseFloat($country.attr('lat'));
						lng = parseFloat($country.attr('lng'));
						zl = parseFloat($country.attr('zoomlevel'));
						setMapCenter(new GLatLng(lat,lng),zl);
					}
					else{
						attribSelected = '';
					}
					html += '<option value="' + $country.attr('code') + '" lat="' + $country.attr('lat') +'" lng="' + $country.attr('lng') + '" zl="' + $country.attr('zoomlevel') + '" ' + attribSelected + '>' + $country.attr('name') + '</option>';
				})
				jQuery('#ddlCountry').html(html);
				initDdlStateCity();
			});
			
			//Initialize Dropdown StateCity
			var  initDdlStateCity = function(){
				
				jQuery.get(xmlPath, function(data) {
					var $crs = jQuery(data).find('country[code='+initCountryCode+'] > cityregionstate');
					if($crs.size() > 0){
						//add dropdonwlist city-state-region
						var html='<option value="">Please select a state/region</option>';
						$crs.each(function(){
							html += '<option value="' + jQuery(this).attr('code') + '">' + jQuery(this).attr('code') + '</option>';
						})
						jQuery('#ddlStateCity').html(html);
						jQuery('#phRegion').show();
						jQuery('#phLocation').hide();
					}
					else{
						jQuery('#phRegion').hide();
					}
				});
			};
		
			//trigger function to initialize ddlLocation
			getMarkers();
        
		//Dropdown Country onChange event
		jQuery('#ddlCountry').change(function(){
			map.clearOverlays();
			jQuery("#ddlCountry option:selected").each(function(){
				code = jQuery(this).attr('value');
				if(code!=''){
					initCountryCode = code;
					initStateAreaCode='';
					var lat = parseFloat(jQuery(this).attr('lat'));
					var lng = parseFloat(jQuery(this).attr('lng'));
					var zl = parseFloat(jQuery(this).attr('zl'));
					var mapCenterPoint = new GLatLng(lat, lng);
					setMapCenter(mapCenterPoint, zl);
				}
			});
			initDdlStateCity();
			getMarkers();
		});
		
		//Dropdown StateCity onChange event
		jQuery('#ddlStateCity').change(function(){
			jQuery("#ddlStateCity option:selected").each(function(){
				initStateAreaCode = jQuery(this).attr('value');
			});
			initDdlLocations();
		});
		
		var initDdlLocations = function(){
			
			if(initStateAreaCode !=''){
				jQuery.get(xmlPath, function(data) {
				
					strHTMLlocations = '<option>Select a location</option>';
					i=0;
				
					jQuery(data).find('cityregionstate[code='+ initStateAreaCode +']').each(function(){
						
						jQuery(this).find('marker').each(function(){
							var $marker = jQuery(this);
							
									
									/*Google -- add markers*/
									var lat = parseFloat($marker.attr("lat"));
									var lng = parseFloat($marker.attr("lng"));
									var point = new GLatLng(lat,lng);
									var name = UndefinedToString($marker.attr("name"));
									var phone = UndefinedToString($marker.attr("phone"));
									var url = UndefinedToString($marker.attr("url"));
									var address = UndefinedToString($marker.attr("address"));
									var city = UndefinedToString($marker.attr("city-location"));
									var company = UndefinedToString($marker.attr("company"));
									var zip = UndefinedToString($marker.attr("zip"));
									var loc_state = UndefinedToString($marker.attr("state"));
									var extra = UndefinedToString($marker.attr("extra"));
									
									var code = (lat +""+ lng + "" + name).replace(" ","");
									strHTMLlocations += '<option value="' + code + '">' + $marker.attr('name') + '</option>';
									// create the marker
									
									var marker = createMarker(point,name,phone,url,name,address,city,zip,loc_state,extra,company);
									//map.addOverlay(marker);
									
									i++;
								})
							});
							jQuery("#ddlLocation").html(strHTMLlocations);
							jQuery("#phLocation").show()
						});
					}
					else{
						jQuery("#phLocation").hide()
					}
				}
		
		jQuery('#ddlLocation').change(function(){
			var code = '';
			jQuery("#ddlLocation option:selected").each(function(){
				code = jQuery(this).attr('value');
			});
			if(code !=''){
				//marker_click(code);
				jQuery.get(xmlPath, function(data) {
					jQuery(data).find('country[code='+ initCountryCode +']').each(function(){
						var i=0;
						jQuery(this).find('marker').each(function(){
							var $marker = jQuery(this);
							if(($marker.attr('lat') + $marker.attr('lng') + $marker.attr('name')).replace(" ","") == code){
								marker_click(i);
							}
							i++;
						});
					});
				});
			}	
		});
		
		
		
		
		
		
		function setMapCenter(point,zoomlevel){
			map.setCenter(point, zoomlevel);
		}
      // A function to create the marker and set up the event window
      function createMarker(point,name,phone,url,name,address,city,zip,loc_state,extra, company) {
	 	/*var myIcon = new GIcon();
		myIcon.image = markerImagePath;
		myIcon.iconSize = new GSize(20,20);
		myIcon.iconAnchor = new GPoint(5, 34);
		myIcon.infoWindowAnchor = new GPoint(5, 2);
*/
        var marker = new GMarker(point);
		
        var i = gmarkers.length;
		
		
        GEvent.addListener(marker, "click", function() { 
			
			map.setCenter(this.getLatLng(), 14);
			marker.openInfoWindow("<strong>" + name + "</strong><br/>" + address + "<br/>" + zip + " " + city);
			var html = '';
			html += (name).trim()!=''?'<h2>' + name + '</h2>':'';
			html += (company).trim()!=''?'<h3>' + company + '</h3>':'';
			html += (address).trim()!=''?'<p>' + address + '<br />':'';
			html += (zip).trim()!=''?'' + zip + '<br />':'';
			html += (city).trim()!=''?'' + city + '<br />':'';
			
			html += (loc_state).trim()!=''?'' + loc_state + '</p>':'';
			html += (phone).trim()!=''?'<p>Tel: ' + phone + '</p>':'';
			html += (extra).trim()!=''?'<h4>Services: </h4>' + '<p>' + (extra).replace( /-br-/g, "<br />" ) + '</p>'   :'';
			jQuery('#markerInfo').html(html);
		});
		gmarkers.push(marker);
		return marker;
      }
		
		/*GEvent.addListener(map, "click",function(){
			window.location.hash = "locationsinfo"

			document.getElementById("center").value = map.getCenter();
			document.getElementById("zoom").value = map.getZoom();


		});*/
		

      // This function picks up the click and opens the corresponding info window
      function marker_click(i) {
        map.setCenter(gmarkers[i].getPoint(), 14);
        GEvent.trigger(gmarkers[i], "click");
      }
	  
	  
		
    }
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}


/*
	Author: Hank Brandwijk
	Date: 	5/02/2010
	Description: Google Map Implementation
	Goolge API: V2
	How to: call googlemap function on pageonload or document.ready event (jQuery)
*/

	function googleSingleMap(aLatitude, aLongitude, aDivId) {
		if (GBrowserIsCompatible()) {
		        var map = new GMap2(document.getElementById(aDivId));
		        map.setCenter(new GLatLng(aLatitude, aLongitude), 13);
 
		        var mapTypeControl = new GMapTypeControl();
		        var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
		        var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
		        map.addControl(mapTypeControl, topRight);
		        GEvent.addListener(map, "dblclick", function() {
		          map.removeControl(mapTypeControl);
		          map.addControl(new GMapTypeControl(), bottomRight);
		        });
		        map.addControl(new GSmallMapControl());

		        var point = new GLatLng(aLatitude, aLongitude);
		        map.addOverlay(new GMarker(point));
		}
	}
