/* <![CDATA[ */
/* map.js */

(function($){
	$(window).load(function(){
		$('#mapform').submit(function(){return Map.setDirections(this.from.value,this.to.value,this.locale.value);});
		$('#fromAddress').focus(function(){
			if(this.value.toLowerCase()=='your address here'){this.value='';}
		}).blur(function(){if(this.value==''){this.value='Your address here';}});
		Map.locale=$('#locale').val();
		$('#locale').change(function(){Map.locale=this.options[this.selectedIndex].value;});
		Map.initialize();
	});
	$(window).unload(function(){GUnload();});
})(jQuery);

var Map={
	from:null,gdir:null,locale:null,luigis:null,map:null,marker:null,print:false,poly:null,to:null,
    directions:function(point){
		Map.map.removeOverlay(Map.marker);
		Map.gdir.clear();
		if(point!==null){
			Map.from=document.getElementById('fromAddress').value+'@'+(new GLatLng(point.lat,point.lng)).toUrlValue(6);
			var address='from: '+Map.from+' to: '+Map.to;
			Map.gdir.load(address,{getPolyline:true,locale:Map.locale});
		}
    },
	doPrint:function(){
        var toURL='map_printable.asp?locale='+Map.locale;
        if(Map.from!==null){toURL+="&fromaddress="+Map.from;}
        var windowReference=window.open(toURL,"");
        return false;
    },
    geocode:function(){
		var location=document.getElementById('fromAddress').value;
		if(location.toLowerCase()!='your address here'){
			$.ajax({
				data:'location='+location,dataType:'xml',
				success:function(xml,status){
					$(xml).find('Result').each(function(){
						var result=$(this);
						if($('Country',result).text()=='GB'){
							var point={lat:$('Latitude',result).text(),lng:$('Longitude',result).text()};
							Map.directions(point);
							return false;
						}
					});
				},
				timeout:10000,type:'get',url:'/components/geocode.asp'
			});
		}
		return false;
    },
    handleErrors:function(){
		var code=Map.gdir.getStatus().code,s='';
		switch(code){
			case G_GEO_UNKNOWN_ADDRESS:s="No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.";break;
			case G_GEO_SERVER_ERROR:s="A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.";break;
			case G_GEO_MISSING_QUERY:s="The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.";break;
			/* Doc bug... this is either not defined, or Doc is wrong
			case G_UNAVAILABLE_ADDRESS:s="The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.";break;
			*/
			case G_GEO_BAD_KEY:s="The given key is either invalid or does not match the domain for which it was given.";break;
			case G_GEO_BAD_REQUEST:s="A directions request could not be successfully parsed.";break;
			default:s="An unknown error occurred.";break;
		}
		alert(s+'\nError code: '+code);
	},
	initialize:function() {
		if (GBrowserIsCompatible()){
			Map.print=(/_printable\.asp$/i.test(document.location.pathname));
			Map.luigis=new GLatLng(51.486033,-0.180888);
			Map.map=new GMap2(document.getElementById("map"));
			var map=Map.map;
			map.addControl(new GLargeMapControl());
			map.setCenter(Map.luigis,13);
			if(!Map.print){
				map.enableDoubleClickZoom();
				map.enableScrollWheelZoom();
				map.addControl(new GMapTypeControl());
			}
			Map.showAddress();
			Map.gdir=new GDirections(map, document.getElementById('directions'));
			Map.to='Luigi\'s Delicatessen@'+Map.luigis.toUrlValue(6);
			GEvent.addListener(Map.gdir,'load',Map.onGDirectionsLoad);
			GEvent.addListener(Map.gdir,'error',Map.handleErrors);
			if(Map.print){
				Map.from=$('#fromAddress').val();
				if(Map.from.length){
					var address='from: '+Map.from+' to: '+Map.to;
					Map.gdir.load(address,{getPolyline:true,locale:Map.locale});
				}
			}
		}
	},
    onGDirectionsLoad:function(){ 
			if(Map.poly){Map.map.removeOverlay(Map.poly);}
			Map.poly=Map.gdir.getPolyline();
			Map.map.addOverlay(Map.poly);
	},
	reload:function(){
		GUnload();
		Map.initialize();
	},
    showAddress:function(){
		var address='349 Fulham Road,Chelsea,London,SW10 9TW.';
		var html='<div style="width:20em;"><h1>Luigi\'s Delicatessen<'+'/h1><address style="float:left;">'+address.replace(/,/g,',<br /'+'>')+'<br />020 7352 7739<'+'/address><img src="/images/luigis_thumb.jpg" style="float:right;" /'+'><'+'/div>';
		Map.map.setCenter(Map.luigis,13);
		Map.marker=new GMarker(Map.luigis);
		Map.map.addOverlay(Map.marker);
		if(!Map.print){
			GEvent.addListener(Map.marker,'click',function(){Map.marker.openInfoWindowHtml(html);	});
			GEvent.trigger(Map.marker,'click');
		}
    }
};

/* ]]> */