/*
 * 	 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({
        imGoogleMaps: 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;
	init();
	
	function init() {
		if (opts.mode == 'auto') {
			gSearch = 'dlgSearch';
			gMap = 'googleMap';
			createAuto();
		} else {
			gSearch = opts.search;
			gMap = opts.map;
			createManual();
			$("#"+opts.directions).html(opts.address);
		}
		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>')
				.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>').css({display: "inline", marginRight: "10px", marginTop:"0", marginBottom:"0", marginLeft:"0"}).append(getLink('Print Directions', 'printMap', 'googleDirections'))))),	
			$('<div></div>')
				.attr("id", "dlgSearch")
				.css({float:"left", width:dWidth, minHeight:"100px", backgroundColor:opts.directions.background, color:opts.directions.text, marginBottom:"2px", display:"none", paddingTop:"0px"})
				.append(
					//$('<label></label>').append('From:'),
					$('<p></p>')
						.css({float:"left", width:"15px", height:"15px", backgroundColor:"#64B949", color:"#000", border:"solid thin #000", font:"bold 11px/16px Helvitica, Arial, sans-serif", display:"block", paddingLeft:"6px", marginLeft: "12px"})
						.append('A'),
					$('<input type="text" name="googleFrom" id="googleFrom" value="" /><br /><br /><br />'),
					//$('<label></label>').append("To:"),
					$('<p></p>')
						.css({float:"left", width:"15px", height:"15px", backgroundColor:"#fB7468", color:"#000", border:"solid thin #000", font:"bold 11px/16px Helvitica, Arial, sans-serif", display:"block", paddingLeft:"6px", marginLeft:"12px"})
						.append('B'),
					//$('<input></input>')
						//.attr("type", "text")
						//.attr("name", "googleTo")
						//.attr("id", "googleTo")
						//.val(opts.address),
					$('<input type="text" name="googleTo" id="googleTo" maxlength="50" value="" /><br />').val(opts.address),
					$('<input type="submit" name="btnGetDir" id="btnGetDir" value="Get Directions" />')),
			$('<div></div>')
				.attr("id", "googleMap")
				.css({float:"right", width:opts.map.width, height:opts.map.height, marginRight:"2px"}),
			$('<div></div>')
				.attr("id", "googleDirections")
				.css({float:"left", width:dWidth, height:opts.map.height, backgroundColor:opts.directions.background, color:opts.directions.text, marginRight:"2px", overflow: "auto"})
				.html(opts.address));
		//$("label", $("#dlgSearch")).css({margin:"3px 0 3px 12px", fontSize:"11px", display:"block", width:"40px"});
		$("input", $("#dlgSearch")).css({float:"left", margin:"8px 0 0 3px", fontSize:"11px", width:"175px", padding:"0px"});
		$(":submit", $("#dlgSearch")).css({float:"right", margin: "8px 15px 3px 0", width:"95px"});
		$('#btnGetDir').click(function() {
			if (($("#googleFrom").val() != '') && ($("#googleTo").val() != '')) {
				toAddress = $("#googleTo").val();
				$("#googleDirections").html('').css("width", parseInt(dWidth)-10);
				gdir.load("from: " + $("#googleFrom").val() + " to: " + toAddress);
			} else {
				alert("From and To addresses must be entered");	
			}
		});
	};
		
	function createManual() {
		$this.prepend(
			$('<div></div>')
				.addClass(opts.menu_class)
				.append(
					$('<ul></ul>')	
						.append(
							$('<li></li>').append(getLink('Get Directions', 'showDlgSearch'))),
					$('<div></div>').addClass(opts.print_class).append(
						$('<ul></ul>').append(
							$('<li></li>').append(getLink('Print Map', 'printMap', 'googleMap')),
							$('<li></li>').append(getLink('Print Directions', 'printMap', 'googleDirections'))))),	
			$('<div></div>')
				.attr("id", opts.search)
				.append(
					$('<p></p>')
						.css({float:"left", width:"15px", height:"15px", backgroundColor:"#64B949", color:"#000", border:"solid thin #000", font:"bold 11px/16px Helvitica, Arial, sans-serif", display:"block", paddingLeft:"6px", marginLeft: "12px"})
						.append('A'),
					$('<input type="text" name="googleFrom" id="googleFrom" value="" /><br /><br /><br />'),
					$('<p></p>')
						.css({float:"left", width:"15px", height:"15px", backgroundColor:"#fB7468", color:"#000", border:"solid thin #000", font:"bold 11px/16px Helvitica, Arial, sans-serif", display:"block", paddingLeft:"6px", marginLeft:"12px"})
						.append('B'),
					//$('<input></input>')
					//	.attr("type", "text")
					//	.attr("name", "googleTo")
					//	.attr("id", "googleTo")
					//	.val(opts.address),
					//$('<input></input>')
					//	.attr("type", "submit")
					//	.attr("name", "btnGetDir")
					//	.attr("id", "btnGetDir")
					//	.val("Get Directions")
					//	.addClass(opts.button_class)),
					$('<input type="text" name="googleTo" id="googleTo" maxlength="50" value="" /><br />').val(opts.address),
					$('<input type="submit" name="btnGetDir" id="btnGetDir" value="Get Directions" />').addClass(opts.button_class)),	
					//$('<input type="submit" name="btnGetDir" id="btnGetDir" value="Get Directions" />')),
			$('<div></div>')
				.attr("id", opts.map),
			$('<div></div>')
				.attr("id", opts.directions)
				.html(opts.address));
		$('#btnGetDir').click(function() {
			if (($("#googleFrom").val() != '') && ($("#googleTo").val() != '')) {
				toAddress = $("#googleTo").val();
				$("#"+opts.directions).html('').css("width", parseInt(dWidth)-10);
				gdir.load("from: " + $("#googleFrom").val() + " to: " + toAddress);
			} else {
				alert("From and To addresses must be entered");	
			}
		});
	};	
	
	function showDlgSearch() {
		var d = ($("#"+gSearch).css("display") == "none") ? "block" : "none";
		$("#"+gSearch).css("display", d);
		if (d == "block") {
			$("#"+gMap).css("height", "525px");
		} else {
			$("#"+gMap).css("height", opts.map.height);
		}
		GUnload();
		showMap();
		showAddress(toAddress);
	};
	
	function showMap() {
		var w = null;
		var h = null;
		var dir = null;
		if (opts.mode == 'auto') {
			dir = 'googleDirections';
			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 mp = (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://www.google.com/mapfiles/marker" + letter + ".png";
	
	  // Set up our GMarkerOptions object
	  markerOptions = { icon:letteredIcon };
	  var marker = new GMarker(point, markerOptions);
	  marker.bindInfoWindowHtml('<p style="color:#000; font-size:11px;">'+address+'</p>');	
	  //GEvent.addListener(marker, "click", function() {
	   // marker.openInfoWindowHtml("Marker <b>" + letter + "</b>");
	  //});
	  return marker;
	}

	
	function handleErrors(){
		var gError = gdir.getStatus().code;
		switch(gError) {
			case G_GEO_UNKNOWN_ADDRESS:
				alert("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.\nError code: " + gError);
				break;
			case G_GEO_UNAVAILABLE_ADDRESS:
				alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gError);
			case G_GEO_SERVER_ERROR: 
				alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gError);
				break;
			case G_GEO_MISSING_QUERY:
				alert("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.\n Error code: " + gError);
				break;
			case G_GEO_BAD_KEY:
				alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gError);
				break;
			case G_GEO_BAD_REQUEST:
				alert("A directions request could not be successfully parsed.\n Error code: " + gError);
				break;
			default:
				alert("An unknown error occurred.");
		}
	};
	
	function onGDirectionsLoad(){ 
		//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>';
		//}
	};
	
	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({"color": opts.menu_bar.text, 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": "#3366cc", "text": "#fff"},
	search: '',
	print_class: '',
	button_class: '',
	lat_lng: ''
};

})(jQuery);		   