$(document).ready(function(){
	// shadows on the main boxes for IE
	if (window.PIE) {
		$('.shadow').each(function() {
		    PIE.attach(this);
		});
	}

	//center main nav	 
	var wrap = $('#main-nav').width();
	var nav = $('#nav').width();
	var reminder = parseInt((wrap - nav) / 2);
	$('#nav').css('padding-left',reminder);

	// submenu handler
	var clicker = $('.sub > li > a');		
	clicker.click(function(){
		if ($(this).next(".toggle").is(":visible")) {
			$(this).next(".toggle").slideUp();
		}
		else {
			$(this).addClass('active').next(".toggle").slideDown().parents('li').siblings().find('.toggle').slideUp();
			$(this).parents('li').siblings().find('a').removeClass('active');
		}
	});
	
	// sidenav handler	
	$('#side-nav .sub a').each(function() {
		if ($(this).is('.active')) {
			$(this).parents().show();
		}
	});
	
	var sideclicker = $('#side-nav > li > a');			
	sideclicker.click(function(){
		if ($(this).next(".sub").is(":visible")) {
			$(this).next(".sub").slideUp();
		}
		else {
			$(this).addClass('active').next(".sub").slideDown().parents('li').siblings().find('.sub').slideUp();
			$(this).parents('li').siblings().find('a').removeClass('active');
		}
	});
	
	
    /* Hide the send to friend form on page load */
    // You can change this to the newsletter form if you want your users to send to friend first
    $('#sendToFriendHolder').hide();
    /* When the send to friend link is clicked slide the send to friend form up */
    $('#sendToFriend').click(function()
	  {
		  $('#NewsletterHolder').slideUp('slow');
		  $('#sendToFriendHolder').slideDown('slow');
		  // Make the entire block element clickable
		  //window.location=$('#footer-actions').find("a").attr("href");return false;
	});
    /* When the hotel newsletter link is clicked, slide the hotel newsletter down */
    $('#subscribeNewsletter').click(function()
	  {
		  $('#sendToFriendHolder').slideUp('slow');
		  $('#NewsletterHolder').slideDown('slow');
		  // Make the entire block element clickable
		  //window.location=$('#footer-actions').find("a").attr("href");return false;
	});

    // Datepicker (date pickers)
	$(function() {
	var dates = $( "#checkin, #checkout" ).datepicker({
			showOn: "both",
			buttonImage: "images/icon_cal.gif",
			buttonImageOnly: true,
			buttonText: 'Show Date Picker',
			dateFormat: 'dd/mm/yy',
			currentText: 'Now',
			altFormat: 'dd/mm/yy',
			
			// Restrict the range of selectable dates
			minDate: 0, maxDate: "+1y",
			
			// Select the date range to search for
			defaultDate: "",
			changeMonth: false,
			numberOfMonths: 1,
			
			onSelect: function( selectedDate ) {
				var option = this.id == "checkin" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		});
	});
		
	// scroller. Init. Need to re-init onActive (Accordion)
  	$('.acc-scroller').jScrollPane({
	  	verticalDragMinHeight: 20,
		verticalDragMaxHeight: 20,
		horizontalDragMinWidth: 20,
		horizontalDragMaxWidth: 20
  	});
	
	// slider
	$('#slider').cycle({
        /*fx:       'fadeZoom',*/
        timeout:   10000
    });
	
	// gallery
	$('#gallery').cycle({
		fx: 'scrollHorz',
		timeout: 0,
		prev: '#prev-gal',
		next: '#next-gal'
	});
	
	// prettyPhoto gallery
	$('#gallery a').prettyPhoto();
	
	// liteAccordion
	$('#acc').liteAccordion({
		containerWidth : 1006,
	    containerHeight : 408,
	    headerWidth : 30,
	    
		firstSlide : 0,
		onActivate : function() {
      	  	this.find('.acc-content').fadeIn(600).find('.acc-scroller').jScrollPane({verticalDragMinHeight: 20, verticalDragMaxHeight: 20, horizontalDragMinWidth: 20, horizontalDragMaxWidth: 20 });
      	  	this.find('.acc-content').parents('li').siblings().find('.acc-content').fadeOut(600);
			
			// Pause Scrolling Images (jQuery Cycle Plugin)
			$('#slider').cycle('pause');
	    },
	    slideSpeed : 500
	});
	
	// liteAccordion : close group if the first slide is clicked and already open
	$('h2:eq(0)', '#acc').click(function() {
    var $this = $(this),
        $slideOne = $this.add($this.next()),
        $slideTwoPos = $('h2:eq(1)', '#acc')[0].offsetLeft;
    
    if (this.offsetLeft === 886) {
        $slideOne.animate({
            left : 0
        }, 800).children().fadeIn();
		
		// Pause Scrolling Images (jQuery Cycle Plugin)
         $('#slider').cycle('pause');
    } else if (!this.offsetLeft && $slideTwoPos !== 30) {
         $slideOne.animate({
            left : 886
        }, 800).children('.acc-content').fadeOut(1600);
		
		// Resume Scrolling Images (jQuery Cycle Plugin)
		$("#slider").cycle('resume');
    } 
  });
  
	// Add Bookmarks
	function addBookmark() 
	{	
        var title = document.title;			
        var url = document.location.href;
        if (window.sidebar)	window.sidebar.addPanel(title, url,"");
        else if( document.all )   window.external.AddFavorite( url, title);
        else return true;
	}
	
	// Format date values to store in DB
	function days_between(date1, date2) {
    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24
    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()
    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)
	}
	
});
