/*
* Jquery Google MapManager
* Allows you to do fun stuff with Google Maps
* Such as interacting with other elements on the page.
* Large chunks of this code are taken from jMap2 by Tane Piper
* And therefore so is the license:
* Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

(function($){
	
	// Require jmap2 or die
	if ($.jmap == null) {
		throw new Error("JMapManager requires the JMap plugin.");
	}
	
	// Create global object to store jmapmanager in
	$.jmapmanager = $.jmapmanager || {};
	
	$.jmapmanager.bounds = new GLatLngBounds();
	
	$.jmapmanager.JMarkers = {}; // the collection of all marker objects that have been created
	$.jmapmanager.JMarkerSets = {'default':[]}; // arbitrary sets of marker ids
	
	$.jmapmanager.JMarker = function () {
  	this.id = null;
  	this.latitude = null;
  	this.longitude = null;
  	this.defaultHTML = null;
  	this.events = {'click':[]};
  	this.gmapMarker = null;
	};
	
	$.jmapmanager.JMarker.prototype.addToMarkerSet = function (markerSet) {
	  if (typeof($.jmapmanager.JMarkerSets[markerSet]) == "undefined") {
	    $.jmapmanager.JMarkerSets[markerSet] = [];
	  }
		$.jmapmanager.JMarkerSets[markerSet].push(this.id);
		return this;
	}
	
	$.jmapmanager.JMarker.prototype.addListener = function (event, callback) {
		this.events[event].push(callback);
		GEvent.addListener(this.gmapMarker, event, callback);
	}
	
	$.jmapmanager.JMarker.prototype.call = function (event) {
		for (var i = 0; i < this.events[event].length; i++) {
			this.events[event][i]();
		}
	}
	
	// instantiatable marker objects, which wrap the gmaps marker
	$.jmapmanager.addJMarker = function(id, options) {
		jmarker = new $.jmapmanager.JMarker();
		jmarker.id = id;
		jmarker.latitude = options.pointLat;
		jmarker.longitude = options.pointLong;
		jmarker.defaultHTML = options.pointHTML;
		jmarker.gmapMarker = $.jmap.addMarker(options);
		$.jmapmanager.JMarkers[id] = jmarker;
		return jmarker;
	}
	
	$.fn.addJMarker = function(id, options) {
		jmarker = $.jmapmanager.addJMarker(id, options);
		return jmarker;
	}

	$.fn.activateJMarker = function (marker) {
		return this.each(function(){
	 		$(this).click(function () {
				if (typeof(marker == 'string')) {
					jmarker = $.jmapmanager.JMarkers[marker];
				} else {
					jmarker = marker;
				}
				jmarker.gmapMarker.openInfoWindowHtml(jmarker.defaultHTML);
			});
	 	});
	}
	
	$.fn.getJMarker = function (id) {
		return $.jmapmanager.JMarkers[id];
	}
	
	$.fn.redrawMap = function (options) {
		var options = $.extend({}, options);
		
		if ($.jmap.bounds.isEmpty() == false) {
			$.jmap.GMap2.setCenter($.jmap.bounds.getCenter());
			if (typeof(options.maxZoom) != 'undefined') {
				zoomLevel = ($.jmap.GMap2.getBoundsZoomLevel($.jmap.bounds) <= options.maxZoom) ? $.jmap.GMap2.getBoundsZoomLevel($.jmap.bounds) : options.maxZoom;
			} else {
				zoomLevel = $.jmap.GMap2.getBoundsZoomLevel($.jmap.bounds);
			}
			$.jmap.GMap2.setZoom(zoomLevel);
		}
	}
	
	$.jmapmanager.JMarker.prototype.hide = function () {
		this.gmapMarker.hide();
		return this;
	}
	
	$.jmapmanager.JMarker.prototype.show = function () {
		this.gmapMarker.show();
		return this;
	}
	
	$.fn.hideMarkerSet = function (set) {
		for (i = 0; i < $.jmapmanager.JMarkerSets[set].length; i++) {
			var marker = $.jmapmanager.JMarkers[$.jmapmanager.JMarkerSets[set][i]];
			marker.hide();
		}
	}
	
	$.fn.showMarkerSet = function (set) {
		for (i = 0; i < $.jmapmanager.JMarkerSets[set].length; i++) {
			var marker = $.jmapmanager.JMarkers[$.jmapmanager.JMarkerSets[set][i]];
			marker.show();
		}
	}
	
	// GEvent.addListener(marker, "click", function() {
	// 	Map.callEvent("click", id);
	// });
	// 
})(jQuery);
