/**
 * Construct a new google map hotel instance
 * @class This is the google map hotel instance class.  
 * @return a googleMapHotelObj object
 */
var googleMapHotelObj = new Class({
 /**
 * The values recieved by the contructor that inicializes the instance
 * @type object
 */
	options: {
		//onCreate: $empty, 
		//onReady: $empty, 
		
		autoLoad:true,					//creates the instance automatically on load
		path:'',						//module path
		
		className:'',					//class name

		showNavigation:true,			//show map navigation
		showTerrain:true,				//show map terrain
		showStreetView:true,			//show map streeview
		scrollwheel:false,				//don't allow to zoom the map with the scroll

		defaultView:0,					//initial map terrain type
		zoomLevel:9,
		lat:'32.736462',
		lng:'-16.97937',
		
		language:'en',
		
		type:0,
		name:''
	},
		
	mapDiv:false,				//holds the map div object
	mapDivWrapper:false,		//holds the map div wrapper object or the background div object
	mapInnerWrapper:false,		//holds the wrapper object for the map div and header div 
	headerDiv:false,			//holds the header div object
	imageDiv:false,				//holds the close image div object
	
	googleMap:false,			//holds the google map object
		
	innerWrapperHeight:625,

/**
 * Creates the instance of the class
 * @constructor
 * @param {string} id The id of the wrapper
 * @param {object} options The values that inicializes the instance
 */
	initialize:function(id,options) {	
		this.setOptions(options);	//sets the options

		this.id = id;				//id of the wrapper

		if(this.options.autoLoad)
			googleMapHotelObjCache.add(this);			//puts the instance in the cache array
	},

/**
 * Inicializes and creates the rotator structure
 */
	start:function(){
		this.init();			//inializes the variables
		this.create();			//creates the rotator

		this.fireEvent('onCreate', this);
	},
	
/**
 * Inicializes all the necessary variables
 */
	init:function(){
		//create the map div
		this.mapDiv = new Element('div',{
			'id':'hotelmap_v'+this.id,
			'class':'hotelmap'+this.options.className
		});
		
		//get the map div wrapper
		this.mapDivWrapper = $("hotelmapwrapper_v"+this.id);
	},
	
	
/**
 * Creates the instance structure
 */
	create:function(){
		//Detects if the google map will be embebed or a popup
		if(this.options.type==0)
			this.initPopup();						//inicializes the popup
		else
			this.mapDiv.inject(this.mapDivWrapper);	//puts the map div inside the div wrapper
		
		this.createGoogleMap();
	},
		
/**
 * Creates the google map object
 */
	createGoogleMap:function(){
		//verifies if exist the coordenates
		if(this.options.lat=='' || this.options.lng==''){
			this.options.lat = '32.736462';					//sets the default latitude
			this.options.lng = '-16.97937';					//sets the default longityde
			this.options.zoomLevel = 9;						//sets the default zoom
		}
		
		var view = false;
		switch(this.options.defaultView){
			case 1:
					view = google.maps.MapTypeId.SATELLITE;
					break;
			case 2:
					view = google.maps.MapTypeId.HYBRID;
					break;
			case 3:
					view = google.maps.MapTypeId.TERRAIN;
					break;
			default:
					view = google.maps.MapTypeId.ROADMAP;
		}

		this.googleMap = new google.maps.Map(this.mapDiv, {
			mapTypeId: view,
			navigationControl:this.options.showNavigation,
			mapTypeControl:this.options.showTerrain,
			streetViewControl:this.options.showStreetView,
			zoom: this.options.zoomLevel,
			scrollwheel:this.options.scrollwheel,
			minZoom: 2,
			center: new google.maps.LatLng(this.options.lat,this.options.lng)
		});
		
		if(this.options.lat!='' && this.options.lng!='')
			this.createMarker();							//creates the marker
	
		this.fireEvent('onReady', this.googleMap);			//fires all the events onReady
	},

/**
 * Creates the hotel marker
 */
	createMarker:function() {
		var that = this;

		//creates the marker
		var newicon = new google.maps.MarkerImage(this.options.path+'images/hotel.png');
		var marker = new google.maps.Marker({
			position: new google.maps.LatLng(this.options.lat,this.options.lng),
			map: this.googleMap,
			icon: newicon,
			title: this.options.name
		});
		
		google.maps.event.addListener(marker, 'dblclick', function() {
	       that.googleMap.setZoom(that.googleMap.getZoom()+1);
	       that.googleMap.setCenter(new google.maps.LatLng(that.options.lat,that.options.lng));
		});
	},
		
/**
 * Inicializes the popup
 */
	initPopup:function() {
		var that = this;
		
		//creates the header div
		this.headerDiv = new Element('div',{'id':'hotelmapheader_v'+this.id,'class':'hotelmapheader'+this.options.className});
		this.headerDiv.innerHTML = '&nbsp;&nbsp;'+this.options.name;
		this.headerDiv.setStyles({
			position:"relative",
			height:25,
			lineHeight:25,
			width:600,
			margin:"0 auto",
			border:"1px solid #ffffff",
			backgroundColor:"#000000",
			color:"#ffffff"
		});

		//creates the close image div
		this.imageDiv = new Element('div',{'id':'hotelmapimage_v'+this.id,'class':'hotelmapimage'+this.options.className});
		this.imageDiv.setStyles({
			position:"absolute",
			height:25,
			width:25,
			right:0,
			top:0,
			cursor:'pointer',
			background:"url("+this.options.path+"images/close.png) no-repeat center center"
		});

		//creates the div wrapper for the map div and header div
		this.mapInnerWrapper = new Element('div',{'id':'hotelmapinnerwrapper_v'+this.id,'class':'hotelmapinnerwrapper'+this.options.className});
		this.mapInnerWrapper.setStyles({
			position:"absolute",
			width:"100%",
			height:0,
			overflow:'hidden'
		});

		//sets the style for the background div
		this.mapDivWrapper.setStyles({
			position:"absolute",
			width:"100%",
			backgroundColor:"#000000",
			display:"none",
			opacity:'0.8'
		});

		//sets the style for the google map div
		this.mapDiv.setStyles({
			position:"relative",
			height:400,
			width:600,
			margin:"0 auto",
			border:"1px solid #ffffff"
		});	

		this.imageDiv.inject(this.headerDiv);								//puts the close image div inside the header div
		this.headerDiv.inject(this.mapInnerWrapper);						//puts the header div inside the inner wrapper div
		this.mapDiv.inject(this.mapInnerWrapper);							//puts the map div inside the inner wrapper div
		this.mapInnerWrapper.inject(this.mapDivWrapper.getParent());		//puts the inner wrapper div at the same level of the background div
		
		this.imageDiv.addEvent('click',function(){ that.hide(); });			//adds the close event to the close image div
		this.mapDivWrapper.addEvent('click',function(){ that.hide(); });	//adds the close event to the background div
	},
		
/**
 * Shows the popup window
 */
	show:function() {
		if(!this.mapInnerWrapper || !this.googleMap)
			return;
		
		this.googleMap.setCenter(new google.maps.LatLng(this.options.lat,this.options.lng));	//centers the google map
		this.googleMap.setZoom(this.options.zoomLevel);
		
		this.updateBackground(true);	//detects if the scrool changes

		//shows the popup window
		this.mapDivWrapper.setStyle('display', 'block');
		this.update();
	},

/**
 * hides the popup window
 */
	hide:function() {
		this.updateBackground(false);	//don't detect if the scrool changes
		
		//hides the popup window
		this.mapDivWrapper.setStyle('display', 'none');
		this.mapInnerWrapper.setStyle('height', 0);
	},
	
/**
 * Updates the position of the background div and the google map wrapper div
 */
	update:function() {
		this.mapDivWrapper.setStyles({top: window.getScrollTop(), height: window.getHeight()});
		this.mapInnerWrapper.setStyles({top:window.getScrollTop()+20, height:this.innerWrapperHeight});
	},
	
/**
 * Updates the position of the background div and the google map wrapper div  when the scroll changes
 */
	updateBackground:function(open) {
		var fn = open ? "addEvent" : "removeEvent";
		window[fn]("scroll", this.update)[fn]("resize", this.update);
	}
});
googleMapHotelObj.implement(new Events, new Options);



/**
 * Instances cache object
 * this will store the instances that will be loaded automatically when the page is loaded
 */
var googleMapHotelObjCache = {
/**
 * Holds the class instances
 * @type array
 */
	instances:new Array(),
	
/**
 * Adds an instance of the static list to the cache array
 * @param {object} obj Instance object
 */
	add:function(obj){
		this.instances.push(obj);
	},
	
/**
 * Inicialize all the instances that exists in the cache array
 */
	load:function(){
		for (var i=0; i<this.instances.length; i++)
			this.instances[i].start();
	}
};



/**
 * Inicializes and creates the structure of all the rotator instances
 * that exists in the cache array when the page is loaded
 */
window.addEvent("load",function(){
	googleMapHotelObjCache.load();
});	
