/*
 * File			: dineto.map.js
 * Author		: Paul Warelis
 * Copyright	: Dine.TO (www.dine.to)
 * Date			: July 30, 2009
 */

(function($) {

	var gm = {};

	// centerPoint = { lat : double, lon : double }
	// zoomLevel = int [optional]
	$.fn.dtoMap = function(centerPoint, zoomLevel) {
		return this.each(function() {
			
			if (GBrowserIsCompatible() == false) return null;
			if (typeof zoomLevel == "undefined") zoomLevel = 14;
			$this = $(this);
			
			var htmlId = $this.attr("id");
			var map = new GMap2( $this.get(0) );
			map.addControl(new GSmallMapControl());
			
			var location = new GLatLng(centerPoint.lat, centerPoint.lon);
			map.setCenter(location, zoomLevel);
	
			gm[htmlId] = map;
			gm[htmlId].mGroup = {};
			return this;
		});
	};

	$.fn.dtoMapRefresh = function() {
		$this = $(this);
		var htmlId = $this.attr("id");
		gm[htmlId].checkResize();
		return this;
	};

	$.fn.dtoMapCenter = function(groupId) {
		// Center map on a specific group of markers
		$this = $(this);
		var htmlId = $this.attr("id");
		
		if (typeof groupId != "undefined") {
			var map = gm[htmlId];
			var group = map.mGroup[groupId];
			var bounds = new GLatLngBounds();
			for(var i=0; i<group.length; i++) {
				bounds.extend(group[i].getLatLng());
			}
			var zoomLevel = map.getBoundsZoomLevel(bounds);
			var centerPoint = bounds.getCenter();
			map.setCenter(centerPoint, zoomLevel);
		}

		return this;
	};

	$.fn.dtoMapGetgMap = function() {
		$this = $(this);
		var htmlId = $this.attr("id");
		return gm[htmlId];
	};

	$.fn.dtoMapGetMarker = function(groupId, markerId) {
		$this = $(this);
		var htmlId = $this.attr("id");
		return gm[htmlId].mGroup[groupId][markerId];
	};

	// markers can be: one json object or an array of objects
	// groupid: 0 - common marker, >0 = id (can be used to show/hide a whole set of markers)
	// The marker consists of : lat, lon, hoverText, infoText
	// if hoverText and infoText is undefined -> marker will not be clickable
	$.fn.dtoMapAddMarkers = function(markers, groupid, icon) {
		var isArray = typeof markers['lat'] == "undefined";
		if (typeof groupid == "undefined") groupid = 0;
		
		$this = $(this);
		var htmlId = $this.attr("id");
		// Create the marker group array if not there yet
		if (typeof gm[htmlId].mGroup[groupid] == "undefined") {
			gm[htmlId].mGroup[groupid] = [];
		}
		var mGroup = gm[htmlId].mGroup[groupid];
		
		if (isArray) {
			for (i in markers) {
				var marker = createMarker(gm[htmlId], markers[i], icon);
				mGroup.push(marker);
				gm[htmlId].addOverlay(marker);
			}
		} else {
			var marker = createMarker(gm[htmlId], markers, icon);
			mGroup.push(marker);
			gm[htmlId].addOverlay(marker);
		}
		return this;
	};

	$.fn.dtoMap.createIcon = function(url, size, anchor, showShadow){
		if (typeof showShadow == "undefined") showShadow = true;
		var gIcon = new GIcon();
		gIcon.image = url;
		gIcon.iconSize = new GSize(size[0], size[1]);
		gIcon.iconAnchor = new GPoint(anchor[0], anchor[1]);
		if (showShadow) {
			gIcon.shadow = "images/gpshadow.png";
			gIcon.shadowSize = new GSize(37, 34);
		}
		gIcon.infoShadowAnchor = new GPoint(size[0]/2, size[1]/2);
		gIcon.infoWindowAnchor = new GPoint(size[0]/2, size[1]/2);
		return gIcon;
   };

	function createMarker(gMap, marker, gIcon) {
		// Set the marker options
		var hoverInfo = typeof marker.hoverText != "undefined";
		var clickInfo = typeof marker.infoText != "undefined";
		
		var options = { clickable : (hoverInfo || clickInfo) };
		if (typeof gIcon != "undefined") {
			options.icon = gIcon;
		}
		
		var latLon = new GLatLng(marker.lat, marker.lon);
		var gMarker = new GMarker(latLon, options);
		
		if (hoverInfo) {
			var tooltip = "<div>"+marker.hoverText+"</div>";
			gMarker.tooltip = new Tooltip(gMarker, tooltip, 0);
			gMarker.tooltip.initialize(gMap);
	
			// Bind hover events
			GEvent.addListener(gMarker, 'mouseover', tipHover);
			GEvent.addListener(gMarker, 'mouseout', tipUnHover);
		}
		if (typeof marker.infoText != "undefined") {
			gMarker.infoText = marker.infoText;
			// Bind click events
			GEvent.addListener(gMarker, 'click', markerClick);
			
		}
		
		return gMarker;
	};

	function tipHover() { this.tooltip.show(); this.tooltip.redraw(true); }
	function tipUnHover() { this.tooltip.hide(); }
	function markerClick() { this.openInfoWindowHtml(this.infoText); }

})(jQuery);























