/**
 * mmap - Merrcury Map
 * A jQuery plugin to encapsulate all Common Maps behaviours
 * 
 * @author lucho@letmego.com
 */
;(function($) {
$.mmap.registerMarkerCreator('Lodging', 
	function(){
		this.fitZoom = true; // this tells mmap to fit zoom after all markers has been rendered
		this.markers = [];
		var defaultIcon = new google.maps.Icon();
		defaultIcon.image = image('/img/mmap/lodging/defaultLod.png');
		defaultIcon.shadow = image('/img/mmap/lodging/shadowLod.png');
		defaultIcon.iconSize = new google.maps.Size(27, 18);
		defaultIcon.shadowSize = new google.maps.Size(27, 18);
		defaultIcon.iconAnchor = new google.maps.Point(13, 9);
		defaultIcon.infoWindowAnchor = new google.maps.Point(0,0);
		defaultIcon.printImage = image('/img/mmap/lodging/printDefaultLod.gif');
		defaultIcon.mozPrintImage = image('/img/mmap/lodging/mozPrintDefaultLod.gif');
		defaultIcon.printShadow = image('/img/mmap/lodging/printShadowLod.gif');
		//defaultIcon.transparent = image('/img/mmap/lodging/transparent.png');
		defaultIcon.imageMap = [4,8,10,3,16,2,21,8,19,9,19,15,15,16,11,20,11,15,6,15,6,9];
		
		this.defaultIcon = defaultIcon;
		
		this.weightedIcons = [];
		for (var i = 1; i <= 10; i++){
			var weightedIcon = new google.maps.Icon(this.defaultIcon, image('/img/mmap/lodging/sortedLod_' + i + '.png'))
			weightedIcon.printImage = image('/img/mmap/lodging/printSortedLod_' + i + '.gif');
			weightedIcon.mozPrintImage = image('/img/mmap/lodging/mozPrintSortedLod_' + i + '.gif');
			this.weightedIcons[i] = weightedIcon;
		}
		this.weightedMouseOverIcons = [];
		for (var i = 1; i <= 10; i++){
			var weightedMouseOverIcon = new google.maps.Icon(this.defaultIcon, image('/img/mmap/lodging/sortedLod_' + i + '_hov.png'))
			this.weightedMouseOverIcons[i] = weightedIcon;
		}
		
		this.weightedLargeIcons = [];
		for (var i = 1; i <= 10; i++){
			var weightedLargeIcon = new google.maps.Icon(this.defaultIcon, image('/img/mmap/lodging/sortedLod_lg_' + i + '.png'))
			weightedLargeIcon.printImage = image('/img/mmap/lodging/printSortedLod_lg_' + i + '.gif');
			weightedLargeIcon.mozPrintImage = image('/img/mmap/lodging/mozPrintSortedLod_lg_' + i + '.gif');
			weightedLargeIcon.iconSize = new google.maps.Size(45, 17);
			weightedLargeIcon.shadowSize = new google.maps.Size(45, 17);
			weightedLargeIcon.iconAnchor = new google.maps.Point(22, 8);
			weightedLargeIcon.imageMap = [8,18,7,8,4,9,10,2,39,2,44,9,41,8,42,18,36,18,31,23,31,17,8,18];
			this.weightedLargeIcons[i] = weightedLargeIcon;
		}
		this.weightedMouseOverLargeIcons = [];
		for (var i = 1; i <= 10; i++){
			var weightedMouseOverIcon = new google.maps.Icon(this.defaultIcon, image('/img/mmap/lodging/sortedLod_' + i + '_hov.png'))
			this.weightedMouseOverIcons[i] = weightedIcon;
		}
		
		this.retractedIcon = new google.maps.Icon(this.defaultIcon, image('/img/mmap/lodging/retractedLod.png'));
		this.retractedIcon.printImage = image('/img/mmap/lodging/printRetractedLod.gif');
		this.retractedIcon.mozPrintImage = image('/img/mmap/lodging/mozPrintRetractedLod.gif');
		
		this.messageIcon = new google.maps.Icon(this.defaultIcon, image('/img/mmap/lodging/invitedLod.png'));
		this.messageIcon.printImage = image('/img/mmap/lodging/printInvitedLod.gif');
		this.messageIcon.mozPrintImage = image('/img/mmap/lodging/mozPrintInvitedLod.gif');
		
		this.matchedIcon = new google.maps.Icon(this.defaultIcon, image('/img/mmap/lodging/matchedLod.png'));
		this.matchedIcon.printImage = image('/img/mmap/lodging/printMatchedLod_' + i + '.gif');
		this.matchedIcon.mozPrintImage = image('/img/mmap/lodging/mozPrintMatchedLod_' + i + '.gif');
		
		this.soldOutIcon = new google.maps.Icon(this.defaultIcon, image('/img/mmap/lodging/soldOutLod.png'));
		this.soldOutIcon.printImage = image('/img/mmap/lodging/printSoldOutLod.gif');
		this.soldOutIcon.mozPrintImage = image('/img/mmap/lodging/mozPrintSoldOutLod.gif');
		this.soldOutIcon.iconSize = new google.maps.Size(12, 8);
		this.soldOutIcon.shadow = '';
		this.soldOutIcon.imageMap = [2,7,2,3,0,3,3,0,8,0,11,3,9,3,9,7,3,7];
		
		this.availableIcon = new google.maps.Icon(this.defaultIcon, image('/img/mmap/lodging/availableLod.png'));
		this.availableIcon.printImage = image('/img/mmap/lodging/printAvailableLod.gif');
		this.availableIcon.mozPrintImage = image('/img/mmap/lodging/mozPrintAvailableLod.gif');
		this.availableIcon.iconSize = new google.maps.Size(12, 8);
		this.availableIcon.shadow = '';
		this.availableIcon.imageMap = [2,7,2,3,0,3,3,0,8,0,11,3,9,3,9,7,3,7];
		
		this.inactiveIcon = new google.maps.Icon(defaultIcon, image('/img/mmap/lodging/inactiveLod.png'));
		this.inactiveIcon.printImage = image('/img/mmap/lodging/printInactiveLod.gif');
		this.inactiveIcon.mozPrintImage = image('/img/mmap/lodging/mozPrintInactiveLod.gif');
		this.inactiveIcon.iconSize = new google.maps.Size(12, 8);
		this.inactiveIcon.shadow = '';
		this.inactiveIcon.imageMap = [2,7,2,3,0,3,3,0,8,0,11,3,9,3,9,7,3,7];
		
		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["Lodging"] == 'object') element = element["Lodging"];
					if(parseInt(element[options.weight]) > 0 && typeof element.match != 'undefined' && element.match){
						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["Lodging"] == 'object') element = element["Lodging"];
			var icon = this.defaultIcon;
			if (typeof element.match != 'undefined' && !element.match && element.specialIcon != 'sortedLod' && element.specialIcon != 'invitedLod') {
				icon = this.inactiveIcon;
			} else {
				if (typeof element.match != 'undefined' && element.match && options.weight && element[options.weight] != null){
					var weight = 1;
					if(parseInt(element[options.weight]) > 0 && this.maxWeight > this.minWeight){
						weight = (parseInt(element[options.weight]) - this.minWeight) / (this.maxWeight - this.minWeight);
						weight = parseInt(weight * 10);
					}
					if(weight < 10){
						element.weight = parseInt(weight)+1;
					}
					else{
						element.weight = parseInt(weight);
					}
					if(typeof element[options.label] != 'undefined'){
						if (typeof this.weightedLargeIcons[element.weight] != 'undefined'){	
							icon = this.weightedLargeIcons[element.weight];
						}
					}
					else{
						if (typeof this.weightedLargeIcons[element.weight] != 'undefined'){	
							icon = this.weightedIcons[element.weight];
						}
					}
				}
				else{
					if(options.weight && options.weight != 'costToShow'){
						icon = this.defaultIcon;
						element.specialIcon = 'defaultLod';
					}
				}
			}
			if(element.specialIcon == "retractedLod"){
				icon = this.retractedIcon;
			}
			if(element.specialIcon == "invitedLod"){
				icon = this.messageIcon;
			}
			if(element.specialIcon == "matchedLod"){
				icon = this.matchedIcon;
			}
			if(element.specialIcon == "soldOutLod" || element.available == '0'){
				element.specialIcon = "soldOutLod"
				icon = this.soldOutIcon;
			}
			if(element.specialIcon == "availableLod"){
				icon = this.availableIcon;
			}

			var labeled = options.label && typeof element[options.label] != 'undefined';
			var zIndex = 100;
			if(typeof element.match != 'undefined' && element.match && element[options.weight] > 0 && element.specialIcon != "soldOutLod"){
				if(labeled){
					var rand = Math.floor(Math.random()*11);
					zIndex = parseInt(1000) - (element.weight*40) - parseInt(5)*(rand);
				}
				else{
					zIndex = 502;
				}
			}
			if(element.specialIcon == "defaultLod"){
				zIndex = 501;
			}
			if(element.specialIcon == "retractedLod"){
				zIndex = 500;
			}
			if(element.specialIcon == "invitedLod"){
				zIndex = 400;
			}
			if(element.specialIcon == "matchedLod"){
				zIndex = 300;
			}
			if(element.specialIcon == "soldOutLod"){
				zIndex = 200;
			}
			if(element.specialIcon == "availableLod"){
				zIndex = 100;
			}
			
			var settings = {
				icon: icon,
				dragCrossMove: false,
				clickable: true,
				draggable: false,
				hide: false,
				zIndexProcess: function(){
					return zIndex;
				}
			};
			if (labeled){
				settings.labelOffset = new google.maps.Size(-16, -9);
				settings.labelText = element[options.label];
				settings.labelClass = "priceLabel";
			}
			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
				);
			}
			this.markers['lodging'+element.id] = marker;
			return marker;
		},
		onMouseover: function(marker,latlng){
			if(google.maps.blockMarkers != '1'){
				//@todo Pass texts translated for site, including Separator
				var data = marker.data['Lodging'];
				if(typeof data.weight != 'undefined' && data.weight > 0){
					marker.setImage('/img/mmap/lodging/sortedLod_'+data.weight+'_hov.png');
				}
				else{
					if(data.specialIcon == 'defaultLod'){
						marker.setImage('/img/mmap/lodging/defaultLod_hov.png');
					}
					else{
						marker.setImage('/img/mmap/lodging/inactiveLod_hov.png');
					}
				}
				//var out = '<big><strong>'+data.name_formatted+'</strong></big><br/>';
				//var picture_formatted = '<img src="'+data.picture_formatted+'" class="thuPic" id="picLod'+data.id+'" alt="'+data.name_formatted+'" />';
				//out += '<table><tr><td><div style="height:46px; width: 109px;">'+picture_formatted+'<div></td><td>';
				   out = '<div class="lodName" align="center"><strong>'+data.name_formatted+'</strong></div>';
					out += '<div align="center" style="height:80px;"><img width="160" height="68" src="' + data.picture_formatted;
					out += '" class="thuPic" id="picLod'+data.id+'" alt="'+data.name_formatted+'" /><\/div>';
				
				if(typeof marker.data['Type'] != 'undefined'){
					$.each(marker.data['Type'],function(i){
						if(i>0){
							out += ' | ';
						}
						out += marker.data['Type'][i].name;
					});
					out += '</td></tr></table>';
				}
				//out += '<div align="center" class="greenSt">'+ __('Click marker for more options',true)+'</div>';
				out += '<div align="center"><strong>' +  __('Click marker for more options',true) + '</strong></div>';
	            marker.openExtInfoWindow(
	                map.gmap, // Map instance
	                "mapWindow",
	                out,
	                {beakOffset: 2,
	                 windowWidth: 200
	                }
	            );
	            data.Status = 'mouseOver';
			}
		},
		onMouseout: function(marker){
			var data = marker.data['Lodging'];
			if(typeof data.weight != 'undefined' && data.weight > 0){
				marker.setImage('/img/mmap/lodging/sortedLod_'+data.weight+'.png');
			}
			else{
				if(data.specialIcon == 'soldOutLod'){
					marker.setImage('/img/mmap/lodging/'+data.specialIcon+'.png');
				}
				else{
					if(data.specialIcon == 'defaultLod'){
						marker.setImage('/img/mmap/lodging/defaultLod.png');
					}
					else{
						marker.setImage('/img/mmap/lodging/inactiveLod.png');
					}
				}
			}
			if(google.maps.blockMarkers != '1'){
				if(data.Status == 'mouseOver'){
					marker.closeExtInfoWindow(
						map.gmap
					);
				}
				data.Status = 'mouseOut';
			}
		},
		onClick: function(marker){
			//@todo Pass texts translated for site, including Separator
			var detailsUrl = marker.data['Lodging'].url;
			var data = marker.data['Lodging'];
			var out = '<div style="background-image: url(/img/button/iwClick.gif); padding: 5px 5px 5px 35px; width: 160px; height: 25px;"><a href="/to/'+detailsUrl+'" target="_blank">View Details</a>&nbsp;<a href="/to/'+detailsUrl+'" target="_blank"><img src="/img/icon/newWin12.gif" /></a></div>';
		    marker.openExtInfoWindow(
                map.gmap,
                "mapWindow",
                out,
                {beakOffset: 2,
                 windowWidth: 200,
                 closeButton: true
                }
            );
		    google.maps.blockMarkers = '1';
		    data.Status = 'click';
		}
	}
);
})(jQuery);