//<![CDATA[
    var map;
	var localSearch = new GlocalSearch();
	var postcode;
	var dealer_name;
	var homemarker;
	var homepoint;

	function mapLoad() {
	
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map"));
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.addControl(new GScaleControl());
			map.setMapType(G_NORMAL_MAP);
			map.setCenter(new GLatLng(54.876607,-3.120117), 5);
            
			$.post(url,function(xml) {
				plotPoints(xml);
			});				
		
		} else {
			
		  alert("Sorry, the use of Google Maps on this page is not compatible with this browser");
		
		}
	}
	
	function displayStatus(msg) { 
		document.getElementById('directions').innerHTML = msg;
	}


	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();
		  }
		}
	}
	
	function plotPoints(xml) { 

		if($("status",xml).text() == "0") return;

        map.clearOverlays();
        
        if(homemarker) {
        map.addOverlay(homemarker);
        }
        
		$("dealer",xml).each(function(id) {
		
			  var dealer = $("dealer",xml).get(id);
			  
			  var point = new GLatLng($("lat",dealer).text(), $("lng",dealer).text());
			  
			  placeMarkerAtPoint(point, dealer);
		
		 });
	
	}
	
	function getPoint(query, callbackFunction) {
	
		localSearch.setSearchCompleteCallback(null, 
			function() {
				
				if (localSearch.results[0])
				{		
					var resultLat = localSearch.results[0].lat;
					var resultLng = localSearch.results[0].lng;
					postcode = query;
					homepoint = new GLatLng(resultLat,resultLng);
					callbackFunction(homepoint);
				} else{
					
					$("#searcherror").html("<div class='error'>The post code you have entered was not found. Please try again.</div>");
				}
			});	
			
		localSearch.execute(query + ", UK");
	
	}
	
	function createMarker(point, icon, dealer) {
			
			var marker = new GMarker(point, {icon:icon});
			
			GEvent.addListener(marker, "click", function() {
														 
				var html = '';
				dealer_name = $("name",dealer).text();
				
				html = '<div id=\'infowindow\' ><h3><a href=\'' + $("link",dealer).text() + '\'>' + dealer_name + '</a></h3></div>';
				
				var opts = {pixelOffset:new GSize(2,5), maxWidth:100, maxHeight:70};
				marker.openInfoWindowHtml(html, opts);

                address = '<h3><a href=\'' + $("link",dealer).text() + '\'>' + dealer_name + '</a></h3>';

                if($("address1",dealer).text()) {

                    address += $("address1",dealer).text() + '<br/>';
                }

                if($("address2",dealer).text()) {

                    address += $("address2",dealer).text() + '<br/>';
                }

                if($("town",dealer).text()) {

                    address += $("town",dealer).text() + '<br/>';
                }

                if($("postcode",dealer).text()) {

                    address += $("postcode",dealer).text() + '<br/><br/>';
                }

                if($("tel",dealer).text()) {

                    address += '<strong>Tel:</strong> ' + $("tel",dealer).text() + '<br/>';
                }

                if($("fax",dealer).text()) {

                    address += '<strong>Fax:</strong> ' + $("fax",dealer).text() + '<br/>';
                }

                if($("url",dealer).text()) {

                    address += '<br/><strong>Website:</strong><br/><a href=\'' + $("url",dealer).text() + '\' target=\'_blank\'>' + $("url",dealer).text() + '</a><br/>';
                }

				$('#results').html(address);

				if ($("brand",dealer).get(0) != undefined) {
					$('#results').append('<br/><strong>Brands Stocked:</strong>');

                    $("brand",dealer).each(function(id, val) {

					var brand = $("brand",dealer).get(id);

					$('#results').append('<br/>' + $("brandname",brand).text() + '');

                    });

				}
				
			});
			
			GEvent.addListener(marker, "infowindowclose", function() {
			
				$('#results').html('<p>Use this map to find your nearest BriMarc stockist.</p>');

			
			});
						
			return marker;
	}
	
	function placeHomeMarkerAtPoint(point)
	{
		
		map.closeInfoWindow();
		$('#searcherror').html('');
		
		if (homemarker) {
			map.removeOverlay(homemarker);
		}	
				
		var icon = new GIcon(G_DEFAULT_ICON);
		
		homemarker = new GMarker(point,{icon:icon,draggable:true,zIndexProcess:setZIndex});
		homemarker.Zindex = 1;
		
		GEvent.addListener(homemarker, "click", function() {
														 
			var html = '<div><h3>'+ ($("#dealerquery").val()).toUpperCase() +'</h3></div>';
			var opts = {pixelOffset:new GSize(32,5), maxWidth:100};
			homemarker.openInfoWindowHtml(html, opts);
			
			$('#results').html('<h3>' + ($("#dealersearch").val()).toUpperCase() + '</h3>');
			
		});
		
		GEvent.addListener(homemarker, "infowindowclose", function() {
			
			$('#results').html('<p>Use this map to find your nearest BriMarc stockist.</p>');

			
		});

		map.addOverlay(homemarker);
		map.setCenter(point, 9);
	
	}
	
	function setZIndex(a,b) {
 		return a.Zindex;

	}

	function placeMarkerAtPoint(point, dealer)
	{
		var marker;

        /*
		var icon = new GIcon(G_DEFAULT_ICON);
		icon.image = "/images/brimarc/stockists/marker.png";
		icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon.iconSize = new GSize(27, 34);
		icon.shadowSize = new GSize(37, 34);
		icon.iconAnchor = new GPoint(16, 34);
        */
		
		marker = createMarker(point, icon, dealer);
		
		map.addOverlay(marker);
	}
	

	addLoadEvent(mapLoad);
	addUnLoadEvent(GUnload);
//]]>
