/**
 * mmap - Merrcury Map
 * A jQuery plugin to encapsulate all Common Maps behaviours
 * 
 */
;(function($) {
$.mmap.registerMarkerCreator('LodgingCluster', 
	function(){
		this.fitZoom = true; // this tells mmap to fit zoom after all markers has been rendered
		//--- Clusters
		var defaultClusterIcon = new google.maps.Icon();
		defaultClusterIcon.image = image('/img/mmap/lodging/cluster/image.png');
		defaultClusterIcon.shadow = false;
		defaultClusterIcon.iconSize = new google.maps.Size(38, 36);
		defaultClusterIcon.iconAnchor = new google.maps.Point(19, 18);
		defaultClusterIcon.infoWindowAnchor = new google.maps.Point(23,0);
		defaultClusterIcon.printImage = image('/img/mmap/lodging/cluster/printImage.gif');
		defaultClusterIcon.mozPrintImage = image('/img/mmap/lodging/cluster/mozPrintImage.gif');
		defaultClusterIcon.printShadow = image('/img/mmap/lodging/cluster/printShadow.gif');
		defaultClusterIcon.transparent = image('/img/mmap/lodging/cluster/transparent.png');
		defaultClusterIcon.imageMap = [27,0,31,1,33,2,34,3,36,4,37,5,38,6,39,7,40,8,40,9,41,10,42,11,42,12,42,13,43,14,43,15,43,16,44,17,44,18,44,19,44,20,44,21,44,22,44,23,44,24,43,25,43,26,43,27,42,28,42,29,42,30,41,31,40,32,40,33,39,34,38,35,37,36,36,37,34,38,33,39,31,40,27,41,14,41,11,40,9,39,8,38,7,37,5,36,5,35,4,34,3,33,2,32,2,31,1,30,1,29,1,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,1,15,1,14,1,13,2,12,2,11,3,10,4,9,5,8,5,7,7,6,8,5,9,4,11,3,14,2,16,1,20,0];
		this.defaultClusterIcon = defaultClusterIcon;
		this.clusterWeightedIcons = [];
		for (var i = 1; i <= 10; i++){
			var weightedIcon = new google.maps.Icon(this.defaultClusterIcon, image('/img/mmap/lodging/cluster/image_weight_' + i + '.png'))
			weightedIcon.printImage = image('/img/mmap/lodging/cluster/printImage_weight_' + i + '.gif');
			weightedIcon.mozPrintImage = image('/img/mmap/lodging/cluster/mozPrintImage_weight_' + i + '.gif');
			this.clusterWeightedIcons[i] = weightedIcon;
		}
		this.defaultOptions = {
			label: false,
			weight: false
		}
	}, 
	{
		beforeCreate: function(elements, mmap, opts){
			var options = $.extend({}, this.defaultOptions, opts);
			if (options.weight){
				this.minWeight = null;
				this.maxWeight = null;
				for (i in elements){
					var element = elements[i];
					if (typeof element["LodgingCluster"] == 'object') element = element["LodgingCluster"];
					if (this.minWeight == null || parseInt(element[options.weight]) < this.minWeight){
						this.minWeight = parseInt(element[options.weight]);
					}
					if (this.maxWeight == null || parseInt(element[options.weight]) > this.maxWeight){
						this.maxWeight = parseInt(element[options.weight]);
					}
				}
			}
		},
		create: function(element, mmap, opts){
			var options = $.extend({}, this.defaultOptions, opts);
			if (typeof element["LodgingCluster"] == 'object') {
				element = element["LodgingCluster"];
			}
			var icon = this.defaultClusterIcon;
			if (options.weight && typeof element[options.weight] != 'undefined' && element.matched > 0){
				var weight = (parseInt(element[options.weight]) - this.minWeight) / (this.maxWeight - this.minWeight);
				weight = parseInt(weight * 10);
				if(weight == 0){
					weight = 1;
				}
				element["weight"] = weight;
				if (typeof this.clusterWeightedIcons[weight] != 'undefined'){	
					icon = this.clusterWeightedIcons[weight];
				}
			}
			var labeled = options.label && typeof element[options.label] != 'undefined';
			var zIndex = 1100;
			if(element.matched == 0){
				zIndex = 200;
			}
			var settings = {
				icon: icon,
				dragCrossMove: false,
				clickable: true,
				draggable: false,
				hide: false,
		        labelOffset: new google.maps.Size(-200, -300),
		        zIndexProcess: function(){return zIndex}
			};
			if (labeled){
				settings.labelOffset = new google.maps.Size(-5, -15);
				settings.labelText = element[options.label]+" ";
			}
			var boundingBox = element.boundingBox;
			var latLngBounds = new google.maps.LatLngBounds(
					new google.maps.LatLng(boundingBox.southwest.latitude,boundingBox.southwest.longitude), 
					new google.maps.LatLng(boundingBox.northeast.latitude,boundingBox.northeast.longitude)
				);
			var center = latLngBounds.getCenter();
			element.latitude = center.lat();
			element.longitude = center.lng();
			var latLng = new google.maps.LatLng(element.latitude, element.longitude);
			if (labeled){
				var marker = new LabeledMarker(latLng,settings);
			}
			else {
				var marker = new google.maps.Marker(latLng,settings);
			}
			return marker;
		},
		onMouseover: function(marker,latlng){
			var data = marker.data.LodgingCluster;
			if(data["weight"] > 0){
				marker.setImage('/img/mmap/lodging/cluster/image_weight_' + data["weight"] + '_hov.png');
			}
		},
		onMouseout: function(marker){
			var data = marker.data.LodgingCluster;
			if(data["weight"] > 0){
				marker.setImage('/img/mmap/lodging/cluster/image_weight_' + data["weight"] + '.png');
			}
			marker.closeExtInfoWindow(
				map.gmap
			);
		}
	}
);
})(jQuery);