$(document).ready(function(){
	

	
	function onBefore(curr, next, opts) {
		
	}
	
	function onAfter(curr, next, opts) {
	    var index = opts.currSlide;
		$(this).find('.slide-title').delay(300).animate({top: 210, opacity: 1}, 500).delay(3000).animate({top: 260, opacity: 0}, 500);
	}
	
		// var caption = 'Image ' + (opts.currSlide + 1) + ' of ' + opts.slideCount;
		// $('#caption').html(caption);
	
	
    $('#unit-slideshow .main-images').after('<ul class="thumbnail-list">').cycle({
		fx: 'fade',
		speed: 1000,
		timeout: 5000,
		next: '.right-arrow',
		prev: '.left-arrow',
		before: onBefore,
		after: onAfter,
		pager: 'ul.thumbnail-list',
		// callback fn that creates a thumbnail to use as pager anchor 
	    pagerAnchorBuilder: function(idx, slide) { 
	        return '<li><a href="#" title="'+jQuery(slide).children('.slide-title').html()+'"><img src="'+jQuery(slide).children('img').attr('src')+'" width="85" height="65" /></a></li>'; 
	    }
	});
	
	//Get our elements for faster access and set overlay width
	var div = $('.main-images'),
		ul = $('ul.thumbnail-list'),
		ulPadding = 15;

		//Get menu width
		var divWidth = div.width();

		//Remove scrollbars	
		div.css({overflow: 'hidden'});

		//Find last image container
		var lastLi = ul.find('li:last-child');

		//When user move mouse over menu
		div.mousemove(function(e){
			//As images are loaded ul width increases,
			//so we recalculate it each time
			var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;	
			var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
			div.scrollLeft(left);
		});

	
	$("ul.thumbnail-list a").live("mouseover", function(){
      $('#thumbnail-name').html('<span class="count">'+$(this).attr('title'));
    });
	
	
	$('.thumbnail-head').toggle(
		function(){
			$(this).animate({top: 102}, 200);			
			$('.thumbnail-list').animate({top: 0}, 200, function(){ $('#thumbnail-name').fadeIn('slow'); });
		}, 
		function() {
			$('#thumbnail-name').fadeOut('fast', function(){
				$('.thumbnail-head').animate({top:0}, 50);						
				$('.thumbnail-list').animate({top: -105}, 50);
			});
		}
	);
		
	
	$.fn.clear = function() {
		return this.focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
				$(this).addClass('active_text');
			}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
				$(this).removeClass('active_text');
			}
		});
	};

	$('.text_input, #quick-res-form input').clear();
	
	$('#header-link-currency').click(function(){
		$.jqURL.loc('/currency-convertor/',{w:700,h:300,wintype:'_blank'});
		return false;
	});
	
	$('#header-link-weather').click(function(){
		$.jqURL.loc($(this).attr('href'),{w:700,h:300,wintype:'_blank'});
		return false;
	});
	
	
	/* Date Picker */
	
	$(function(){
		
		var now = new Date();
		var newdate  = new Date();
		
		// today
		window.todays_date = now.getDate() + "/" + now.getMonth() + "/" + now.getFullYear();
		
		// tomorrow
		var end = now.getTime()+(1*24*60*60*1000);
		newdate.setTime(end);
		
		window.tomorrow = newdate.getDate() + "/" + newdate.getMonth() + "/" + newdate.getFullYear();
				
		// 350 days in the future
		var end = now.getTime()+(350*24*60*60*1000);
		newdate.setTime(end);
		
		window.end_date = newdate.getDate() + "/" + newdate.getMonth() + "/" + newdate.getFullYear();
					
					
		$('#calendar-btn').datePicker({ 
			createButton:false, 
			startDate: window.todays_date,
			endDate: window.end_date }).bind('click',function() {updateSelects($(this).dpGetSelected()[0]);$(this).dpDisplay();return false;}).bind('dateSelected',function(e, selectedDate, $td, state) {updateSelects(selectedDate);}).bind('dpClosed',function(e, selected) {updateSelects(selected[0]);});		
		
		var updateSelects = function (selectedDate) {
			selectedDate = new Date(selectedDate);
			var d = selectedDate.getDate();
			var m = selectedDate.getMonth()+1;
			var y = selectedDate.getFullYear();

			if (!isNaN(d)) {
				$("#checkin").val(d+"/"+m+"/"+y);
				
				var selectedDatePlusOneDay = selectedDate.valueOf()+(24*60*60* 1000);
				var selectedTomorrow =new Date(selectedDatePlusOneDay); 
				
				var existing_date = $("#checkout").val().split("/");
				var selectedExisting =new Date(existing_date[2], existing_date[1]-1, existing_date[0]); 
				if(selectedTomorrow>selectedExisting) {               
					var d2 = selectedTomorrow.getDate();
					var m2 = selectedTomorrow.getMonth()+1;
					var y2 = selectedTomorrow.getFullYear();
					$("#checkout").val(d2+"/"+m2+"/"+y2);
				}					
				
			} // end if NaN d
		}
	});
	
	$(function(){
	
		$('#calendar-btn2').datePicker({ createButton:false, startDate: window.tomorrow, endDate: window.end_date }).bind('click',function() {updateSelects($(this).dpGetSelected()[0]);$(this).dpDisplay();return false;}).bind('dateSelected',function(e, selectedDate, $td, state) {updateSelects(selectedDate);}).bind('dpClosed',function(e, selected) {updateSelects(selected[0]);});		
		
		var updateSelects = function (selectedDate) {
			selectedDate = new Date(selectedDate);
			var d = selectedDate.getDate();
			var m = selectedDate.getMonth()+1;
			var y = selectedDate.getFullYear();

			if (!isNaN(d)) {

				$("#checkout").val(d+"/"+m+"/"+y);
				
				
				var selectedDateMinusOneDay = selectedDate.valueOf()-(24*60*60* 1000);
				var selectedYesterday =new Date(selectedDateMinusOneDay); 
				
				var existing_date = $("#checkin").val().split("/");
				var selectedExisting =new Date(existing_date[2], existing_date[1]-1, existing_date[0]); 
				
				if(selectedYesterday<selectedExisting) {               
					var d2 = selectedYesterday.getDate();
					var m2 = selectedYesterday.getMonth()+1;
					var y2 = selectedYesterday.getFullYear();
					$("#checkin").val(d2+"/"+m2+"/"+y2);
				}
				
			}
		}
	});


});
