$(document).ready(function(){
	$(".googleMap").googleMaps();
});

$.fn.googleMaps = function(options) {  

	var defaults = {  
		postcode: "hd94ap",
		imgPath:"http://www.equi-trek.com/webapp/templates_en/images/",
		zoom:10,
		iconHeight:105,
		iconWidth:180,
		fineTune1:40,
		fineTune2:60,
		directions:true
	};  
 
	var options = $.extend(defaults, options);  
	
	return this.each(function() {  
		
		var element = $(this);
		
		//add map vars
		var gdir;
		var geocoder = null;
		var addressMarker;  
		var map;
		var localSearch = new GlocalSearch();
		
		//add icon vars
		var icon = new GIcon();
		icon.image = ""+options.imgPath+"map-marker.png"; //set icon image
		icon.shadow = ""+options.imgPath+"map-marker-shadow.png"; //set icon image shadow
		icon.iconSize = new GSize(options.iconWidth, options.iconHeight);
		icon.shadowSize = new GSize(options.iconWidth, options.iconHeight);
		icon.iconAnchor = new GPoint(options.fineTune1,options.fineTune2);
		
		//add map id
		element.attr("id","mapCanvas");

		//set width and height
		var mapHeight = element.height();
		var mapWidth = element.width();
																
		//style element
		var elementCss = {	
			"border":"1px solid #ccc",
			"background":"url("+options.imgPath+"loading.gif) no-repeat center center"		
		};				
		element.css(elementCss);
								
		//create map function
		getPostcode(options.postcode);
		function createMap(resultLat,resultLng){
	
			//create map
			map = new GMap2(document.getElementById("mapCanvas"));
			map.setCenter(new google.maps.LatLng(resultLat,resultLng), options.zoom);  //set loading location
			var marker = new GMarker(new GLatLng(resultLat,resultLng),icon); 
			map.addOverlay(marker);
			//add controls
			var mapControl = new GMapTypeControl();
			map.addControl(mapControl);
			map.addControl(new GSmallMapControl());
			$(".to").val(""+resultLat+","+resultLng+"")
			
		}
		
		//add directions
		if(options.directions == true){
			//add Directions
			var directionsForm = "\
			<div class='directionsHolder' style='width:"+mapWidth+"px;'>\
				<div class='directionsForm'>\
					<h3>Get Directions</h3>\
					<label>PostCode:</label>\
					<input class='from' type='text'/>\
					<input class='to' type='hidden'/>\
					<input type='submit' value='' class='getDirections'/>\
				</div>\
				<div id='directions'></div>\
				<input type='button' class='reset floatRight'/>\
			</div>";	
			element.after(directionsForm);
			$("#directions, .reset").hide("");
			
			$(".getDirections").click(function(e){
				e.preventDefault();
				var from = $(".from").val();
				var to = $(".to").val();
				var fromLength = from.length;				
				if(from == ""){
					alert("Please enter your postcode.");
				}else if(fromLength !=5 && fromLength !=6){
					alert("Please enter a valid postcode.");
				}else{
					getPostcode(from,to);	
				}	
			});
			
		}
		
		//create directions function
		function createDirections(fromLoc,location){
			gdir = new GDirections(map, document.getElementById("directions")); 
			gdir.load("from: "+fromLoc+"    to: "+location+"" ); //set destiination
			$(".directionsForm").fadeOut("slow");
			setTimeout(function(){
				$("#directions").slideDown("slow",function(){
					$(".reset").fadeIn("slow");
				});
			},1000);
			$(".reset").click(function(){
				window.location = window.location; 
			});
		}
		
				
		//turn postcode into long lat
		function getPostcode(postcode,to) {
			
			localSearch.setSearchCompleteCallback(null, function() {
					
				if (localSearch.results[0]){		
					//get postcode lat long
					var resultLat = localSearch.results[0].lat;
					var resultLng = localSearch.results[0].lng;
					var location = new GLatLng(resultLat,resultLng);
					var location = ""+location.lat()+","+location.lng()+"";
				
					if($(".to").val() == ""){
						createMap(resultLat,resultLng);
					}else{
						createDirections(location,to);
					}
					
				}else{
					alert("Postcode not found!");
				}
			});	
			
			localSearch.execute(postcode+  ", UK");	
												
		}
																														
	});  
};



	



