
/**
 * This class draws all of the accommodation points on a Google Map
 * 1) Initializes the map
 * 2) Shows / Hides the map element
 * 3) Creates Icons, Points, Markers
 * 4) Adds Text to Markers and addes Overlays to the map
 */

function resultGMap(opts){
	Object.extend(this, new resultGmapItem());
	
	// Container element
	this.divMapContainer = "div-map-container";
	// Map element
	this.divMapEl = "div-gmap";
	this.myListType = "apartment"; 
	this.divMapButton = "div-map-button";
	this.divListButton = "div-list-button";
	// Icon Images
	this.icon;
	this.imgIconUrl 		= "http://www.barcelonapoint.com/images/icon_google_apt.gif";
	this.imgShadowUrl 		= "http://www.barcelonapoint.com/images/icon_google_apt_shadow.gif";
	this.iconWidth 		= 16;
	this.iconHeight 	= 16;
	this.shadowWidth	= 15;
	this.shadowHeight	= 12;
	// Status
	this.mapStatus = false;
	// Map Varailble
	this.map;
	this.defaultLong	= 2.16157;
	this.defaultLat 	= 41.3863;
	this.defaultZoom = 12;
	this.defaultControl = 'large';
	
	// Basic initialization
	this.getMapBasicInit = function (lat, long ,zoom, controlType){
		
		lat = (lat == '' ) ? this.defaultLat : lat;
		long = (long == '') ? this.defaultLong : long;
		zoom = (zoom < 1) ? this.defaultZoom : zoom;
		controlType = (controlType == '' ) ? this.defaultControl : controlType;
		
		this.map = new GMap2(document.getElementById(this.divMapEl), [G_HYBRID_MAP]);
		this.map.setCenter(new GLatLng(lat,long), zoom);

		switch(controlType){

			case 'small':
				this.map.addControl(new GSmallMapControl());
				this.map.addControl(new GMapTypeControl());
				break;

			case 'large':
				this.map.addControl(new GSmallMapControl());
				this.map.addControl(new GMapTypeControl());
				break;

			case 'none':
				//this.map.addControl(new GSmallMapControl());
				break;

			default:
				this.map.addControl(new GLargeMapControl());
				this.map.addControl(new GMapTypeControl());
				break;

		}
	};
	
	// Show map and load markers
	// Opts are in the format
	// {'lat':n,'long':n,'zoom':n,'control':string}
	this.doShowMap = function (arItems,params,opts){
		if(opts == 'undefined' || opts =='' || !opts){
			opts = {};
			opts['lat']   = this.defaultLat ;
			opts['long']  = this.defaultLong ;
			opts['zoom']  = this.defaultZoom ;
			opts['control']  = this.defaultControl ;
			
			var sumLat = 0;
			var sumLong = 0;
			var countMapItems = 0;
			var txt = ""; 
			if(params.destCode == 'BCN'){
				opts['zoom'] = 13;
			}else{
				// Find average long / lat 
				for (i in arItems){
					if(arItems[i]['long'] != '' && arItems[i]['long'] != null && arItems[i]['long'] != 'undefined' && arItems[i]['long'] < 360 ){
						if(arItems[i]['lat'] != '' && arItems[i]['lat'] != null && arItems[i]['lat'] != 'undefined' && arItems[i]['lat'] > 10){
							sumLat = sumLat + parseFloat(arItems[i]['lat']);
							sumLong = sumLong + parseFloat(arItems[i]['long']);
							countMapItems++;
//							if(arItems[i]['lat'] < 10){
//								alert(arItems[i]['title'] + " - " + arItems[i]['id']);
//							}
//							txt += arItems[i]['lat'] + ":"+parseFloat(arItems[i]['long']) + " | ";
						}
					}
				}
				
				var avgLat = sumLat/countMapItems;
				var avgLong = sumLong/countMapItems;
				
				opts['lat'] = avgLat.toFixed(5);
				opts['long'] = avgLong.toFixed(5);
			}
//			alert(txt);
//			alert (countMapItems + " : " + opts['lat'] + " : " + opts['long']);
		}
		
		//$('#'+this.divMapContainer).show();
		$('#'+this.divMapContainer).show(400);
		$('#'+this.divMapButton).hide();
		$('#'+this.divListButton).show();
		
		
		this.mapStatus = true;
		this.getMapBasicInit(opts['lat'],opts['long'],opts['zoom'], opts['control']);
		this.doAddItemsToMap(arItems,params);
	};
	
	// Hide Map
	this.doHideMap = function(){
		this.mapStatus = false;
		//$('#'+this.divMapContainer).hide();
		$('#'+this.divMapContainer).slideToggle(400);
		$('#'+this.divMapContainer).hide();
		$('#'+this.divMapButton).show();
		$('#'+this.divListButton).hide();
	};
	
	// Add points from item list
	this.doAddItemsToMap = function (arItems,params){
		
		if(arItems.length > 0 ){
			for(i in arItems){
				
				if(arItems[i]['long'] != '' && arItems[i]['long'] != null && arItems[i]['long'] != 'undefined' && arItems[i]['long'] < 360 ){
					if(arItems[i]['lat'] != '' && arItems[i]['lat'] != null && arItems[i]['lat'] != 'undefined' && arItems[i]['lat'] > 10){
				// Create Point
				var point = this.getPoint(arItems[i]['lat'], arItems[i]['long']);
				var html = this._getItemInfoHtml(arItems[i],params);
				
				var icon = false;
				var imgIconUrl 		= "http://www.barcelonapoint.com/images/icon_google_favorite.gif";
				if(window.oMyList){
				if(oMyList.oList['items']){
					
		  			if(oMyList.oList['items']['apartment']){
//		  				alert(this.myListType);
			  			if(oMyList.oList['items'][this.myListType][0][arItems[i]['id']+'-'+arItems[i]['provider']]){
//			  				alert("Founf in my list");
//			  				myListCheck = 'checked';
			  				icon = this.getCreateIcon(0, 0, 0, 0, imgIconUrl)
			  			}
		  			}
		  			
		  			if(oMyList.oList['items']['hotel']){
			  			if(oMyList.oList['items']['hotel'][0][arItems[i]['id']+'-'+arItems[i]['provider']]){
			  				icon = this.getCreateIcon(0, 0, 0, 0, imgIconUrl)
			  			}
		  			}
		  		}
				}
				
				this.getAddMarkerClickInfoText(point,html,icon);
				
				//alert(arItems[i]['lat'] + " : "+ arItems[i]['long'] + " : "+arItems[i]['descr'])
					}}
			}
		}
	};
	
	// Get Point
	this.getPoint = function ( x, y){
		return new GLatLng(x,y);
	};
	
	// Add overlay
	this.doAddOverlay = function (x,y){
		var point = new GLatLng(x,y);
		var marker = new GMarker(point);
		this.map.addOverlay(marker);
		
		return marker;
	};
	
	this.getCreateIcon = function (width , height, shadowWidth, shadowHeight,IconUrl, ShadowUrl){

		IconUrl = (IconUrl != '' && IconUrl != null && IconUrl != false && IconUrl != 'undefined') ? IconUrl : this.imgIconUrl;
		ShadowUrl = (ShadowUrl != '' && ShadowUrl != null && ShadowUrl != false && ShadowUrl != 'undefined') ? ShadowUrl : this.imgShadowUrl;
		width = (width>0) ? width : this.iconWidth;
		height = (height>0) ? height : this.iconHeight;
		shadowWidth = (shadowWidth>0) ? width : this.shadowWidth;
		shadowHeight = (shadowHeight>0) ? shadowHeight : this.shadowHeight;
		
		var icon = new GIcon();
		icon.image = IconUrl;
		icon.shadow = ShadowUrl;
		icon.iconSize = new GSize(width, height);
		icon.shadowSize = new GSize(shadowWidth, shadowHeight);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);

		return icon;

	};
	
	// Create Marker on click 
	this.createMarker = function (point, txt, pIcon) {

		var markerIcon;
		if(pIcon != '' && pIcon != null && pIcon != 'undefined' && pIcon != false){
			markerIcon = pIcon;
		}else{
			markerIcon = this.getCreateIcon();
		}

	  var marker = new GMarker(point,markerIcon);

	  // Show this marker\'s index in the info window when it is clicked.
	  var html = txt;
	  GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	  });

	  return marker;
	};
	
	// Add text to marker click
	this.getAddMarkerClickInfoText = function(point, txt, icon){
		var marker = this.createMarker(point, txt, icon);
		this.map.addOverlay(marker);
	};
	
};
