//<!-- 
// ------[ Reservation Module ]------------------------------------------------- //	
Reservation = {

	/**
	* Properties
	*/
	timer : null,
	
	defaultValue : 'MM/DD/YY',
	
	button : {
		open : null,
		close : null
		},
		
	form : null,		
	
	field : {
		destination : null,
		arrivalDate : null,
		departureDate : null,
		adults : null,
		children : null,
		rooms : null
		},
	
	/**
	* Init the Reservation Panel to show on click and automatically hide.
	*/
	init : function() {
		/* init button */
		Reservation.button.open = document.getElementById("panel-reservation");
		
		if(Reservation.button.open) {
			Reservation.button.close = document.getElementById("form-reservation-close");
			/* init form */
			Reservation.form = document.getElementById("form-reservation");		
			Reservation.field.destination = document.getElementById("destination");
			Reservation.field.destination.isValid = false;
			Reservation.field.destination.onchange = function() {		
			    this.isValid=!(this.selectedIndex==0);
				}
			
			Reservation.field.arrivalDate = document.getElementById("arrivalDate");
			
			Reservation.field.departureDate = document.getElementById("departureDate");
			
			Reservation.field.adults = document.getElementById("adults");
			Reservation.field.children = document.getElementById("children");
			Reservation.field.rooms = document.getElementById("rooms");
			Reservation.field.departureDate.isValid=false;
			Reservation.field.arrivalDate.isValid=false;
			/* Button Event */

			Reservation.form.onmouseover = function () {
				if(Reservation.timer) clearTimeout(Reservation.timer);
				Reservation.timer = null;
				}
			Reservation.form.onmouseout = function () {
				Reservation.timer = setTimeout("YAHOO.reservation.CloseNow();",2000);
				}
					
			Reservation.button.close.href = "javascript:void(0);";
			Reservation.button.close.className = 'link close';
			
			/* Form Event */
			YAHOO.util.Event.on(Reservation.field.arrivalDate, 'click', Reservation.SetCalendarDateReady, Reservation.field.arrivalDate, true);
			YAHOO.util.Event.on(Reservation.field.departureDate, 'click', Reservation.SetCalendarDateReady, Reservation.field.departureDate, true);
			YAHOO.util.Event.on(Reservation.field.arrivalDate, 'blur', Reservation.SetCalendarDate, Reservation.field.arrivalDate, true);
			YAHOO.util.Event.on(Reservation.field.departureDate, 'blur', Reservation.SetCalendarDate, Reservation.field.departureDate, true);
			}
		},
		
	Open : function() {
		YAHOO.reservation.Direction();
		Reservation.form.className = 'open';
		},

	Close : function() {
		Reservation.form.className = 'close';
		},		
	
	BookNow : function() {
		YAHOO.reservation.Direction();
		YAHOO.reservation.BookNow();
		},
		
	/**
	* Reset the Form and the Calendar
	*/		
	reset : function() {
		Reservation.field.destination.selectedIndex = 0;
		Reservation.field.arrivalDate.value = Reservation.defaultValue;
		Reservation.field.departureDate.value = Reservation.defaultValue;
		Reservation.field.adults.selectedIndex = 0;
		Reservation.field.children.selectedIndex = 0;
		Reservation.field.rooms.selectedIndex = 0;
		Calendar.clear();
		},

	/**
	* Set Calendar Date from date field
	*/				
	SetCalendarDate : function() {
		if (this.value == '') this.value = this.oldValue;
		else if (this.value != Reservation.defaultValue) {
			var validformat = /^\d{2}\/\d{2}\/\d{4}$/;
			var valid = false;
			
			//if (!validformat.test(this.value)) {
				/* alert("Invalid Date Format. Please correct and submit again."); */
			//} else {
			if (validformat.test(this.value)) {
				var month=this.value.split("/")[0];
				var day=this.value.split("/")[1];
				var year=this.value.split("/")[2];			
				var date = new Date(year, month-1, day);
				if ((date.getMonth()+1!=month) || (date.getDate()!=day) || (date.getFullYear()!=year)) {
					// ..
					/* alert("Invalid Day, Month, or Year range detected. Please correct and submit again."); */
					}
				else valid = true;
				}
			
			if(valid) {
			    
				if(this == Reservation.field.arrivalDate) 
					Reservation.setCalendarArrivalDate(date);
				else 
					Reservation.setCalendarDepartureDate(date);
			} else {
				this.value = Reservation.defaultValue;
				
				}
			}
		},

	/**
	* Set the field ready for edition
	*/			
	SetCalendarDateReady: function() {		
		this.oldValue = this.value;
		this.value = '';
		},
		
	/**
	* Set the Arrival Date in the Calendar
	*/		
	setCalendarArrivalDate: function(dateObj) {
		YAHOO.reservation.calendar.select(dateObj);
		YAHOO.reservation.calendar.setMonth(dateObj.getMonth());
		YAHOO.reservation.calendar.setYear(dateObj.getFullYear());
		YAHOO.reservation.calendar.render();			
		},

	/**
	* Set the Departure Date in Calendar
	*/			
	setCalendarDepartureDate: function(dateObj) {
		YAHOO.reservation.calendar.select(dateObj);
		YAHOO.reservation.calendar.setMonth(dateObj.getMonth());
		YAHOO.reservation.calendar.setYear(dateObj.getFullYear());		
		YAHOO.reservation.calendar.render();			
		},
		
	/**
	* Validate Date Range
	*/				
	validateDateRange : function () {
	    //assume that the fields are valid unless specificed otherwise
	    Reservation.field.arrivalDate.isValid=true;
	    Reservation.field.departureDate.isValid=true;
	    //if the field still have the default values('MM/DD/YYYY') it means that the user
	    //has not set the value or that the user has tried to specify an invalid date and the 
	    //form resetted to default
	    if(Reservation.field.arrivalDate.value==Reservation.defaultValue) {
			Reservation.field.arrivalDate.isValid=false;
			}
	    if(Reservation.field.departureDate.value==Reservation.defaultValue) {
			Reservation.field.departureDate.isValid=false;
			}
	    if(Calendar.dates.arrival>=Calendar.dates.departure) {
	        Reservation.field.arrivalDate.isValid=false;
	        Reservation.field.departureDate.isValid=false;
			}
		},
		
	/**
	* Validate Form
	*/				
	validate : function() {
        Reservation.field.destination.onchange();
        Reservation.validateDateRange();	
	    return Reservation.field.destination.isValid && Reservation.field.arrivalDate.isValid && Reservation.field.departureDate.isValid;
		},
		
	/**
	* Submit the Form to NetBooker
	*/		
	submit : function(link) {
	    if(Reservation.validate()) {
	        //The selectedDates array contains every dates between the arrival and departure date. 
	        //To get the number of nights, assuming we just add 1 for arrival date.
	        var nbOfNights=Calendar.dates.selectedDates.length+1;
	        netBookerManager.InitQueryParameters(Reservation.field.destination.options[Reservation.field.destination.selectedIndex]);		        
	        netBookerManager.ViewSearchResult(Calendar.dates.arrival,Calendar.dates.departure,nbOfNights,Reservation.field.adults.value,Reservation.field.children.value,Reservation.field.rooms.value, link);

			// Track Booking to Pegasus as a Page It
			var destination = Reservation.field.destination.options[Reservation.field.destination.selectedIndex].innerHTML;
			return true;
	        }
	    return false;		   
		}
	};
	
YAHOO.util.Event.addListener(window, "load", Reservation.init);

// ------[ Animation /* To be tested with flash. */ ]------------------------------------------------- //	
YAHOO.namespace("reservation");

YAHOO.reservation.init = function() {   
	YAHOO.reservation.Direction();
	YAHOO.reservation.Open();
	YAHOO.reservation.Close();
	};
	
/* Direction */	
YAHOO.reservation.Direction = function() { 
	var currentYPosition = (document.all) ? document.documentElement.scrollTop : window.pageYOffset;
	var windowBottom = YAHOO.util.Dom.getClientHeight()+currentYPosition;
	var posY = YAHOO.util.Dom.getY('panel-reservation')+293;
	if(posY > windowBottom) {
		YAHOO.util.Dom.setStyle('form-reservation','top','');
		YAHOO.util.Dom.setStyle('form-reservation','bottom','-1px');
	} else {
		YAHOO.util.Dom.setStyle('form-reservation','top','-1px');
		YAHOO.util.Dom.setStyle('form-reservation','bottom','');
		}
	};
	
/* Open */	
YAHOO.reservation.Open = function() {  
	var attributesOpen = {
    	width: { to: 288 }, 
		height: { to: 246 }
		};
	var anim = new YAHOO.util.Anim('form-reservation', attributesOpen, 0.5, YAHOO.util.Easing.backOut);
	anim.onStart.subscribe(Reservation.Open);
	YAHOO.util.Event.on('panel-reservation-action', 'click', anim.animate, anim, true);
	};

/* BookNow */	
YAHOO.reservation.BookNow = function() {  
	var attributesOpen = {
    	width: { to: 288 }, 
		height: { to: 246 }
		};
	var anim = new YAHOO.util.Anim('form-reservation', attributesOpen, 0.5, YAHOO.util.Easing.backOut);
	anim.onStart.subscribe(Reservation.Open);
	anim.animate();
	};
	
/* Close */	
YAHOO.reservation.Close = function() {  
	var attributesOpen = {
    	width: { to: 1 }, 
		height: { to: 1 }
		};
	var anim = new YAHOO.util.Anim('form-reservation', attributesOpen, 0.5, YAHOO.util.Easing.backIn);
	anim.onComplete.subscribe(Reservation.Close);
	YAHOO.util.Event.on('form-reservation-close', 'click', anim.animate, anim, true);	
	};
	
YAHOO.reservation.CloseNow = function() { 
	var attributesOpen = {
    	width: { to: 1 }, 
		height: { to: 1 }
		};
	var anim = new YAHOO.util.Anim('form-reservation', attributesOpen, 0.5, YAHOO.util.Easing.backIn);	
	anim.onComplete.subscribe(Reservation.Close);
	anim.animate();
	};
	
YAHOO.util.Event.onAvailable('form-reservation', YAHOO.reservation.init);
YAHOO.util.Event.addListener(window, "resize", YAHOO.reservation.Direction);


// ------[ Calendar Module inside the Reservation Panel ]------------------------------------------------- //	
Calendar = {	

	/**
	* Properties
	*/
	dates: {
		currentDate : new Date(),
		arrival : null,
		departure : null,
		selectedDates : []
		},
	
	    
	/**
	* Init the Calendar
	* @param {lang}	fr/en	The language the calendar should appear.
	*/
	init: function() {
		if(document.getElementById("calendarContainer")) {
			YAHOO.reservation.calendar = new YAHOO.widget.Calendar("YAHOO.reservation.calendar","calendarContainer");
			
			YAHOO.reservation.calendar.customConfig = function() {
				this.Config.Options.MULTI_SELECT = true;
				this.Config.Options.HIDE_BLANK_WEEKS = true;
				this.Config.Options.SHOW_WEEK_FOOTER = false;
				this.Config.Options.NAV_ARROW_LEFT = SitePath+"Images/UI/linkLeft.gif";
				this.Config.Options.NAV_ARROW_RIGHT = SitePath+"Images/UI/linkRight.gif";
				
				//culture variable is global to the page 
				if (Culture == 'fr')
				{ 
	                this.Config.Locale.MONTHS_LONG = ["Janvier","F\u00E9vrier","Mars","Avril","Mai","Juin","Juillet","Ao\u00FBt","Septembre","Octobre","Novembre","D\u00E9cembre"];
				    this.Config.Locale.WEEKDAYS_SHORT = ["D","L","M","M","J","V","S"];
				}		    
				
				//this.Config.Locale.ARRIVAL_MESSAGE = "Select the arrival date";
				//this.Config.Locale.DEPARTURE_MESSAGE = "Select the departure date";
				//this.Config.Locale.RESET_MESSAGE = "Click any date to reset";
				};	
	
			YAHOO.reservation.calendar.setupConfig();	
			YAHOO.reservation.calendar.minDate = new Date();
			YAHOO.reservation.calendar.onSelect = function(selected) { Calendar.setSelection(selected); };
			YAHOO.reservation.calendar.onDeselect = function(selected) { Calendar.setSelectionAndClear(selected); };	
			/*		
			YAHOO.reservation.calendar.renderFooter = function() { 
				if(!this.footer) {
					this.footer = document.createElement("DIV");
					this.footer.className = "calfooter";
					this.footer.innerHTML = this.Locale.ARRIVAL_MESSAGE;
					this.oDomContainer.appendChild(this.footer);    
					}
				};
			*/
			YAHOO.reservation.calendar.render();		
			}
		},

	/**
	* Event: Set the selection from the calendar ehen a user click on a date.
	* @param {String} selected - The JavaScript Date representing the currention selection in the calendar
	*/			
	setSelection : function(selected) {
		this.dates.currentDate = this.parseDate(selected);
		//console.debug(Calendar);

		if(!this.dates.arrival) {
			this.dates.arrival = this.dates.currentDate;
			//YAHOO.reservation.calendar.footer.innerHTML = YAHOO.reservation.calendar.Locale.DEPARTURE_MESSAGE;
			this.setArrivalDate();
			}

		else if(this.dates.currentDate > this.dates.arrival) {
			this.dates.departure = this.dates.currentDate;
			//YAHOO.reservation.calendar.footer.innerHTML = YAHOO.reservation.calendar.Locale.RESET_MESSAGE;
			this.setDepartureDate();
			}

		else if((this.dates.currentDate < this.dates.arrival) && (!this.dates.departure)) {
			this.dates.departure = this.dates.arrival;
			this.dates.arrival = this.dates.currentDate;
			//YAHOO.reservation.calendar.footer.innerHTML = YAHOO.reservation.calendar.Locale.RESET_MESSAGE;
			this.setArrivalDate();
			this.setDepartureDate();
			}

		else if(this.dates.currentDate < this.dates.arrival) {
			this.dates.arrival = this.dates.currentDate;
			this.setArrivalDate();
			}		
		
		if(this.dates.arrival && this.dates.departure) {
			this.setRangeDate();
			}
		},

	/**
	* Set the Arrival Date in the Reservation Form
	*/		
	setArrivalDate : function() {
		Reservation.field.arrivalDate.value = Calendar.return2Digit(this.dates.arrival.getMonth()+1) + "/" + Calendar.return2Digit(this.dates.arrival.getDate()) + "/" + this.dates.arrival.getFullYear();
		},

	/**
	* Set the Departure Date in the Rerservation Form.
	*/			
	setDepartureDate : function() {
		Reservation.field.departureDate.value = Calendar.return2Digit(this.dates.departure.getMonth()+1) + "/" + Calendar.return2Digit(this.dates.departure.getDate()) + "/" + this.dates.departure.getFullYear();
		},

	/**
	* Selection all Date between the Arrival and Departure Date.
	*/		
	setRangeDate : function() {
		this.dates.selectedDates = [];
		var current = new Date(this.dates.arrival);
		while(current.setDate(current.getDate()+1) < this.dates.departure) {
			this.dates.selectedDates.push(new Date(current));
			}		
		YAHOO.reservation.calendar.onSelect = function() { };
		YAHOO.reservation.calendar.select(this.dates.selectedDates);
		YAHOO.reservation.calendar.render();			
		YAHOO.reservation.calendar.onSelect = function(selected) { Calendar.setSelection(selected); };
		},

	/**
	* Parse a date from the calendar and return a Date Object
	* @param {String} selected - The JavaScript Date representing the currention selection in the calendar, [YYYY, MM, DD]
	* @return {Date} - a Date Object.
	*/		
	parseDate : function(selected) {
		var dateArray = selected.toString().split(",");
		var date = new Date(dateArray[0],dateArray[1]-1,dateArray[2]);
		return date;
		},
		
	/**
	* Return a 2 number digit (for month and day from the calendar)
	* @param {num} number
	*/			
	return2Digit : function(num) {
		var num = (num.toString().length == 1) ? ("0" + num) : num;
		return num;
		},
		
	/**
	* Clear all selection on the Calendar and set the current selection as the arrival date.
	* @param {String} selected - The JavaScript Date representing the currention selection in the calendar
	*/	
	setSelectionAndClear : function(selected) {
		this.dates.currentDate = this.parseDate(selected);
		this.dates.arrival = null;
		this.dates.departure = null;
		this.dates.selectedDates = [];
		Reservation.field.departureDate.value = Reservation.defaultValue;
		YAHOO.reservation.calendar.onDeselect = function() { };
		YAHOO.reservation.calendar.deselectAll();
		YAHOO.reservation.calendar.select(this.dates.currentDate);
		YAHOO.reservation.calendar.onDeselect = function(selected) { Calendar.setSelectionAndClear(selected); };	
		YAHOO.reservation.calendar.render();
		},

	/**
	* Clear all date on the calendar
	* @param {selected}	weekBeginDate	The JavaScript Date representing the first day of the week.
	*/			
	clear : function() {
		YAHOO.reservation.calendar.reset();
		this.dates.arrival = null;
		this.dates.departure = null;
		this.dates.selectedDates = [];		
		}		

	};
	
YAHOO.util.Event.addListener(window, "load", Calendar.init);

//-->