﻿/* 
 * Author: Simon Willison
 * addLoadEvent Function
 * 
 * The addLoadEvent function takes as an argument another function which should be executed once the page has loaded.
 * Unlike assigning directly to window.onload, the function adds the event in such a way 
 * that any previously added onload functions will be executed first.
 *
 * URL: http://simonwillison.net/2004/May/26/addLoadEvent/
 */


function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
		} else{
			window.onload = function(){
				oldonload();
				func();
				}			}
	}
	     
/*
 * Author: Vladimir Solovey
 * Visual Enhancements for JQuery 
 *
 * Use JQuery 1.3.2 (jquery.js)
 * Requires presence checkbox with 'checkbox' class, followed by div tag with 'togglediv' class
 *
 * URL: http://jquery.com/
 */
 
function toggleDivs(){
 return;
    var allHideToggles = $('.togglediv');
    
    for(var i = 0; i<allHideToggles.length; i++){
        $(allHideToggles[i]).attr('id', 'thisToggle'+i); 
        //$('#thisToggle'+i).hide();
    }
    
    var allCheckboxes = $('.checkbox');
     
    for(var ii = 0; ii<allCheckboxes.length; ii++){ 
    $(allCheckboxes[ii]).attr('id', 'thisSelector'+ii); 
        var thisCheck = $('#thisSelector'+ii+' input');
        //$(thisCheck).hide();
        $(thisCheck).attr('rel', 'thisToggle'+ii); 
        
        $(thisCheck).click(function(){ 
            $('#'+this.rel).slideToggle(1200);
        })                
    }       
}

/*
 * Make sure 'collapse' class 'display' property is set to 'none' in CSS
 * This way page does not load all the collapsable divs into view before hiding them through $('.collapse').hide();
 */
 
function collapsePanels(){
    
	$('.collapse_link').click(function(){
	  $('div[id*=' + this.rel + ']').slideToggle(500);
	  return false;
	})     
}

function collapsePanelsCheck(){

	$('.collapse_link').click(function(){
	  $('div[id*=' + this.value + ']').slideToggle(800);
	  //alert(this.value);
	  return true;
	})     
}
  
/*
 * Author: Vladimir Solovey
 * Custom Functions for JQuery UI Datepicker
 *
 * Use JQuery 1.3.2 (jquery.js) and JQuery UI Datepicker 1.7.2 (jquery.ui.js)
 * Requires presence of text input fields with appropriate class names 
 * E.g. 'calendar' for loadCalendar() and 'calendar_startdate' and 'calendar_enddate' for loadCalendars()
 *
 * URL: http://jqueryui.com/
 */
 
function loadCalendar() {

	$('.calendar').datepicker({ 
	    showAnim: 'fadeIn',
	    minDate: 0, 
	    maxDate: '+12M +0D',
	    showOn: 'both',
	    onSelect: fixErrors,
        buttonImage: '/images/design/icon.calendar.png',   
        buttonImageOnly: true,
        buttonText: 'Select Date'     
	    })
}

function loadCalendars() {

	$('.calendar_startdate').datepicker({ 
	    showAnim: 'fadeIn',
	    minDate: 0, 
	    maxDate: '+12M +0D',
	    showOn: 'both',
	    onSelect: compareDates,
        buttonImage: '/images/design/icon.calendar.png',   
        buttonImageOnly: true,
        buttonText: 'Select Date'	     
	    })
	    
	$('.calendar_enddate').datepicker({
	    showAnim: 'fadeIn', 
	    minDate: 1, 
	    maxDate: '+12M +0D',
	    showOn: 'both',
	    onSelect: compareDates,    
        buttonImage: '/images/design/icon.calendar.png',   
        buttonImageOnly: true,
        buttonText: 'Select Date'		     
	    })  
	    
    $('.calendar_resbutton').click(function(){
        pageTracker._link(this.href + '?hotel=' + $('.calendar_location').val() + '&arrive=' + $('.calendar_startdate').val() + '&depart=' + $('.calendar_enddate').val() + '&promo=' + $('.calendar_promo').val()); 
	    return false;
	}) 
	  
    $('.calendar_resedit').click(function(){
        pageTracker._link(this.href + '?hotel=' + $('.calendar_location').val()); 
	    return false;
	})  	     
}
	
function compareDates(){

//if ($('.calendar_startdate').datepicker('getDate') != null && $('.calendar_enddate').datepicker('getDate') != null {
    var startDate = new Date($('.calendar_startdate').datepicker('getDate'));     
    var endDate = new Date($('.calendar_enddate').datepicker('getDate'));   
        if (startDate > endDate){
            var newEndDate = new Date($('.calendar_startdate').datepicker('getDate')); 
            newEndDate.setDate(newEndDate.getDate()+1);
            $('.calendar_enddate').datepicker('disable'); 
            $('.calendar_enddate').datepicker('setDate', new Date(newEndDate));                  
            $('.calendar_enddate').datepicker('enable');
        }
    //}    
}

function fixErrors() {

//Empty onSelect event - designed to prevent JavaScript errors 

}
