/*
 * 	 imGoogleMaps - A JQuery Google Maps Implementation
 * 	 @author Les Green
 * 	 Copyright (C) 2008 Intriguing Minds, Inc.
 *   Version 0.5
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.

 *   Demo and Documentation can be found at:   
 *   http://www.grasshopperpebbles.com
 *   
 */

;(function($) {
	$.fn.extend({
        gDirections: function(options) { 
        	opts = $.extend({}, $.googleMaps.defaults, options);
			return this.each(function() {
				new $.googleMaps(this, opts);
			});
        }
    });	

$.googleMaps = function(obj, opts) {
	var map = null;
	var gdir = null;
	var geocoder = null;
	var $this = $(obj);
	var toAddress = (opts.address);
	var baseIcon = null;
	var gSearch = null;
	var gMap = null;
	var gDirects = null;
	init();
	
	function init() {
		gSearch = 'googSearch';
		gMap = 'googMap';
		gDirects = 'googDirections';
		createAuto();
		//if($("#googleTo").val() != '')
		//	toAddress = $("#googleTo").val();
		showMap();
		if (opts.data_url) {
			var d = getDataString();
			doAjax('GET', opts.data_url, d, '', showAddress);
		} else if (opts.address) {
			showAddress(opts.address);
		} else if (opts.lat_lng) {
			showAddress(opts.lat_lng);	
		} else {
			alert('Address must be specified!');	
		}
	};

	function createAuto() {
		var dWidth = (parseInt($this.css("width")) - parseInt(opts.map.width) - 4) + "px";
		var bWidth = parseInt(dWidth) + parseInt(opts.map.width) + 2 + "px";
		$this.append(
			$('<div></div>')
				.attr("id", "googControls")
				.css({backgroundColor: opts.menu_bar.background, color: opts.menu_bar.text, width: bWidth, height: "24px", marginBottom:"2px"})
				.append(
					$('<ul></ul>')	
						.css({listStyle: "none", marginTop:"0", marginBottom:"0", marginLeft:"3px", paddingLeft:"0", display:"block", width:"100px", float:"left"})
						.append(
							$('<li></li>').css({display: "inline", marginRight: "10px", marginTop:"0", marginBottom:"0", marginLeft:"0"}).append(getLink('Get Directions', 'showDlgSearch', '', ''))),
					$('<div></div>').css({float:"right"}).append(
						$('<ul></ul>').css({listStyle: "none", marginTop:"0", marginBottom:"0", marginLeft:"3px", paddingLeft:"0"})
							.append(
								//$('<li></li>').css({display: "inline", marginRight: "10px", marginTop:"0", marginBottom:"0", marginLeft:"0"}).append(getLink('Print Map', 'printMap', 'googleMap')),
								//$('<li></li>').append(getLink('Print Map & Directions', 'printMap', 'googleMap', 'googPrint'))
							))),	
			$('<fieldset></fieldset>')
				.attr("id", "googSearch")
				.css({display: "block", border: "none"})
				.append(
					$('<legend></legend>')
						.css({display: "none"})
						.append('Get Directions'),
					$('<ul></ul>').append(
						$('<li class="A"></li>').append(
							$('<label></label>').attr("for", "googleFrom").append('from A'),
							$('<input type="text" name="googleFrom" id="googleFrom" value="" />')
						),
						$('<li class="B"></li>').append(
							$('<label></label>').attr("for", "googleTo").append('to B'),
							$('<input type="text" name="googleTo" id="googleTo" value="" />').val(toAddress)
						),
						$('<li><strong>format:</strong> street address, town, postal code(optional)</li>'),
						$('<li></li>').append(
							$('<input type="submit" name="btnGetDir" id="btnGetDir" value="Get Directions" />')
						)
					)
				),
			$('<div></div>')
				.attr("id", "googMap")
				.css({width:opts.map.width, height:opts.map.height, marginRight:"2px"}),
			$('<div></div>')
				.attr("id", "googDirections")
				.css("display", "none")
				.html(opts.address),
			$('<div class="ClearAllFloats"></div>')
		);
		$('#btnGetDir').click(function() {
			if (($("#googleFrom").val() != '') && ($("#googleTo").val() != '')) {
				toAddress = $("#googleTo").val();
				gdir.load("from: " + $("#googleFrom").val() + " to: " + toAddress);
			} else {
				alert("From and To addresses must be entered");	
			}
			if ( $(".errorMSG").length > 0 )
				$(".errorMSG").remove();
		});
	};
		
	function showDlgSearch() {
		var dS = $("#"+gSearch).css("display"); // initial block
		var dD = $("#"+gDirects).css("display"); // initial none
		if (dD == "block") {
			$("#"+gDirects).css("display", dS);
			$("#"+gSearch).css("display", dD);
			$("#"+gMap).css("height", opts.map.height);
		} else {
			$("#"+gDirects).css("display", dS);
			$("#"+gSearch).css("display", dD);
			$("#"+gMap).css("height", "400px");
		}
		GUnload();
		showMap();
		showAddress(toAddress);
	};
	
	function showMap() {
		var w = null;
		var h = null;
		var dir = null;
		if (opts.mode == 'auto') {
			dir = 'googDirections';
			w = parseInt(opts.map.width);
			h = parseInt($("#googleMap").css("height"));
		} else {
			dir = opts.directions;
			w = parseInt($("#"+opts.map).css("width"));
			h = parseInt($("#"+opts.map).css("height"));
		}
		//var map = (opts.mode == 'auto') ? 'googleMap' : opts.map;
		//var dir = (opts.mode == 'auto') ? 'googleDirections' : opts.directions;
//		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById(gMap),{size:new GSize(w,h)});
			//map.setCenter(new GLatLng(37.4419, -122.1419), 15);
			//map = new GMap2($("#"+opts.map));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			//map.setMapType(G_NORMAL_MAP);

			baseIcon = new GIcon(G_DEFAULT_ICON);
			baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
			baseIcon.iconSize = new GSize(20, 34);
			baseIcon.shadowSize = new GSize(37, 34);
			baseIcon.iconAnchor = new GPoint(9, 34);
			baseIcon.infoWindowAnchor = new GPoint(9, 2);


			if (opts.directions) {
				gdir = new GDirections(map, document.getElementById(dir));
				GEvent.addListener(gdir, "load", onGDirectionsLoad);
				GEvent.addListener(gdir, "error", handleErrors);
			}
			geocoder = new GClientGeocoder();
//		}
	};
	
	function showAddress(address) {
    	if (geocoder) {
    		if (opts.lat_lng) {
    			//Reverse Geodiing
    			map.setCenter(new GLatLng(address.lat,address.lng), 15);
    			var point = new GLatLng(address.lat,address.lng);
    			var marker = new GMarker(point);
    			map.addOverlay(marker);
    			marker.bindInfoWindowHtml('<p style="color:#000; font-size:11px;">'+address+'</p>');
    		} else {
	    		geocoder.getLatLng(
	        		address,
	          		function(point) {
					if (!point) {
						alert(address + " not found");
					} else {
						map.setCenter(point, 15);
						map.addOverlay(createMarker(address, point, 1));
						//var marker = new GMarker(point);
						//map.addOverlay(marker);
						//var pnt = marker.getPoint();
						//map.panTo(pnt);
						//marker.bindInfoWindowHtml('<p style="color:#000; font-size:11px;">'+address+'</p>');
					}
	          		}
	        	);
    		}
      	}
    };
	
	// Creates a marker whose info window displays the letter corresponding
	// to the given index.
	function createMarker(address, point, index) {
		// Create a lettered icon for this point using our icon class
		var letter = String.fromCharCode("A".charCodeAt(0) + index);
		var letteredIcon = new GIcon(baseIcon);
		letteredIcon.image = "http://maps.google.com/mapfiles/marker" + letter + ".png";

		// Set up our GMarkerOptions object
		markerOptions = { icon:letteredIcon, title:address };
		var marker = new GMarker(point, markerOptions);
		//marker.bindInfoWindowHtml('<p style="color:#000; font-size:11px;">'+address+'</p>');	
		//GEvent.addListener(marker,'infowindowclose', showAddress);
		//GEvent.addListener(marker, "click", function() {
		// marker.openInfoWindowHtml("Marker <b>" + letter + "</b>");
		//});
		return marker;
	}

	
	function handleErrors(){
		var gError = gdir.getStatus().code;
		var msg = '';
		switch(gError) {
			case G_GEO_UNKNOWN_ADDRESS:
				msg = "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_UNAVAILABLE_ADDRESS:
				msg = "The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.";
			case G_GEO_SERVER_ERROR: 
				msg = "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:
				msg = "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;
			case G_GEO_BAD_KEY:
				msg = "The given key is either invalid or does not match the domain for which it was given. ";
				break;
			case G_GEO_BAD_REQUEST:
				msg = "A directions request could not be successfully parsed.";
				break;
			default:
				msg = "An unknown error occurred.";
		}
		$("#"+gSearch+" ul").append('<li class="errorMSG"><span>Error code '+gError+'</span><br />'+msg+'<br />Please adjust the addresses and try again.</li>');
	};
	
	function onGDirectionsLoad(){
		$("#"+gSearch).css("display", "none");
		$("#"+gDirects).html('').css("display", "block");
		//var div1 = document.getElementById('printDir');
		//var div2 = document.getElementById('printMap');
		//if (gdir.getStatus().code == G_GEO_SUCCESS) {
			//div1.innerHTML = '<a href="#" onclick="printContents(1);">Print Directions</a>';
			//div2.innerHTML = '<a href="#" onclick="printContents(2);">Print Map</a>';
		//	$("#googPrint").css("visibility", "visible");
		//}
	};
	
	function getDataString() {
		var str = '';
		if (opts.data) {
			$.each(opts.data, function(i, itm) {
				str += itm.name + "=" + itm.value + "&";							
			});
			//remove last "&"
			str = str.substr(0, (str.length-1));
		}
		return str;
	};
		
	function doAjax(t, u, d, fnBefore, fnSuccess) {
		$.ajax({
			type: t,
			url: u,
			data: d,
			dataType: 'text',
			beforeSend: fnBefore, 
			success: fnSuccess
	 	}); //close $.ajax(
	};
	
	function getLink(t, f, p) {
		var a = document.createElement('a');
		$(a)
		//$('<a></a>)')
			.attr("href", "#")
			.append(t)
			.click(function() {
				var fn = eval(f);
				if (p) {
					fn(p);
				} else {
					fn();
				}
				return false;
			});
			if (opts.mode == 'auto') $(a).css({lineHeight: "26px", marginLeft: "5px", textDecoration: "none"});
		return a;
	};
	
	function printMap(div) {
		var a = window.open('','','width=450,height=400');
		a.document.open("text/html");
		a.document.write($('#'+div).html());		
		a.document.close();
		a.print();
	};
};
	
$.googleMaps.defaults = {
	mode: 'auto',//manual
	data_url: '',
	data: '',
	address: '',
	map: {"width": "400px", "height": "400px"},
	directions: {"background": "#fff", "text": "#000"},
	menu_bar: {"background": "#ccc", "text": "#000"},
	search: '',
	print_class: '',
	button_class: '',
	lat_lng: ''
};

})(jQuery);		   