
// run scripts on load
$(function() {
	
	// set external links
	$('a[rel=external]').attr('target', '_blank');
	
	// load google map
	var map = $('#google-map');
	if(map.length > 0) {
		GoogleMaps.load('google-map');
	}
	
});

/* Google maps code - contact us page
****************************************************************************/
var GoogleMaps = {
	load : function (id) {
		if (GBrowserIsCompatible()) {
			this.map = new GMap2(document.getElementById(id));
			this.map.addControl( new GSmallMapControl() );
			this.aromabath();
		}
	},
	aromabath : function () {
		this.destroy();
		var point = GoogleMapPoint.get('aromabath');
		var p = new GLatLng(parseFloat(point.x),parseFloat(point.y));
		var c = new GLatLng(parseFloat(point.x+0.001),parseFloat(point.y));
		this.map.setCenter(p, 15);
		this.map.addOverlay(new GMarker(p));
//		this.map.setCenter(c, 15);
//		this.map.addOverlay(new GMarker(p, GoogleMapPoint.icon()));
	},
	showDirections : function (from, to) {
		this.destroy();
		this.directions = new GDirections(this.map);
		var searchFrom = GoogleMapPoint.get(from);
		var searchTo   = GoogleMapPoint.get(to);		
		var searchStr  = 'from: '+searchFrom.x+','+searchFrom.y +' to: '+searchTo.x+','+searchTo.y;
		this.directions.load(searchStr);
		GEvent.addListener(this.directions, "addoverlay", function() { 
			GoogleMaps.map.setZoom(15);
		});
	},
	enterAddress : function () {
		$('form#getDirectionsForm').show();
	},
	getDrivingDirections : function () {
		this.destroy();
		this.directions = new GDirections(this.map, document.getElementById('drivingDirections'));
		//this.directions = new GDirections(this.map);
		var searchFrom = $('input#address').val();
		var searchTo   = GoogleMapPoint.get('poke');
		var searchStr  = 'from: '+searchFrom+' to: '+searchTo.x +', '+searchTo.y;

		this.directions.load(searchStr);
		
		$('div#drivingDirections').empty();
		GEvent.addListener(this.directions, "load", function () {
			$('div#nonCarDirections').slideUp(300, function () {
				$('div#drivingDirections').slideDown(300);
			}).empty();
		});
		GEvent.addListener(this.directions, "error", function () { 
			alert('Sorry, We could not find your start address.');
		});
		
	},
	destroy : function () {
		this.map.clearOverlays();
	}
}

var GoogleMapPoint = {
	get : function (key) {51.563346,0.092697
		switch (key) {
			case 'aromabath' 		: return {x:51.563346,y:0.092697, s:'IG3 8EG'}; 											break; 
			case 'eastputney'	 	: return {x:51.45866,y:-0.21106, s:'East+Putney+station+(East+Putney)'};			  		break;
			case 'wandsworthtown' 	: return {x:51.46100,y:-0.18810, s:'Wandsworth+Town+station+(Wandsworth+Town)'};  			break;
		}
	},
	icon : function () {
		var icon = new GIcon();
		icon.image = "/assets/img/pink-logo-googlemaps.png";
		icon.iconSize = new GSize(173, 75);
		icon.shadowSize = new GSize(113, 114);
		icon.iconAnchor = new GPoint(85, 65);
		icon.infoWindowAnchor = new GPoint(0, 0);
		icon.infoShadowAnchor = new GPoint(0, 0);
		return icon;
	}
}
