var err_selArr='Please select arrival date';
var err_selDep='Please select departure date';
var err_depEarlier="Departure date is earlier than arrival one. Please correct!";

$(document).ready(function(){
	$('#topinfo p').append(createFavLink('en','Press','to bookmark this page'));

	$('.date-pick').datePicker({createButton:false,clickInput:true});

	$('#arrdtpicker')
		.datePicker({createButton:false})
		.bind(
			'click',
			function()
			{
				$(this).dpDisplay();
				this.blur();
				return false;
			}
		)
		.bind(
			'dateSelected',
			function(e, selectedDate, $td)
			{
				$('#arrdt').attr('value', selectedDate.asString());
			}
		);


		$('#depdtpicker')
		.datePicker({createButton:false})
		.bind(
			'click',
			function()
			{
				$(this).dpDisplay();
				this.blur();
				return false;
			}
		)
		.bind(
			'dateSelected',
			function(e, selectedDate, $td)
			{
				$('#depdt').attr('value', selectedDate.asString());
			}
		);

		$('#as_arrdtpicker')
		.datePicker({createButton:false})
		.bind(
			'click',
			function()
			{
				$(this).dpDisplay();
				this.blur();
				return false;
			}
		)
		.bind(
			'dateSelected',
			function(e, selectedDate, $td)
			{
				$('#as_arrdt').attr('value', selectedDate.asString());
			}
		);

		$('#as_depdtpicker')
		.datePicker({createButton:false})
		.bind(
			'click',
			function()
			{
				$(this).dpDisplay();
				this.blur();
				return false;
			}
		)
		.bind(
			'dateSelected',
			function(e, selectedDate, $td)
			{
				$('#as_depdt').attr('value', selectedDate.asString());
			}
		);

		var tabContainers = $('#propertytabscontent > div');
		tabContainers.hide().filter(':first').show();

		$('.booknowbutton').click(function() {
			$('#propertytabs ul li.last a').click();
			$.scrollTo($('div#propertytabs'),500);
			return false;
		});

		$('#propertytabs ul li a').click(function() {
			tabContainers.hide();
			tabContainers.filter(this.hash).show();
			$('#propertytabs ul li a').removeClass('active').blur();
			$(this).addClass('active');
			return false;
		}).filter(':first').click();

		$('#minisearch .submit').click(function() {
			//if($('#arrdt').val()=="") { alert(err_selArr); return false }
			//if($('#depdt').val()=="") { alert(err_selDep); return false }
			if($('#depdt').val()!="" && $('#arrdt').val()!="") {
				var diff=daydiff(parseDate($("#arrdt").val()), parseDate($("#depdt").val()));
				if(diff < 0) { alert(err_depEarlier); return false; } else $('#nights').attr('value', diff);
			}
		});

		$('#advancedsearchformlarge .submit').click(function() {
			//if($('#arrdt').val()=="") { alert(err_selArr); return false }
			//if($('#depdt').val()=="") { alert(err_selDep); return false }
			if($('#as_depdt').val()!="" && $('#as_arrdt').val()!="") {
				var diff=daydiff(parseDate($("#as_arrdt").val()), parseDate($("#as_depdt").val()));
				if(diff < 0) { alert(err_depEarlier); return false; } else $('#as_nights').attr('value', diff);
			}
		});

		function parseDate(str) {
			var mdy = str.split('/')
			return new Date(mdy[2], mdy[1]-1, mdy[0]);
		}

		function daydiff(first, second) {
				return (second-first)/(1000*60*60*24)
		}

		setTimeout(function() {
			$('#largepropertyphoto').slideshow({timeout:3000});
		}, 5000);

		$('#slideshow-homepage').slideshow({timeout:3000});

		$('#phototablink').click(function() {
			$('#propertytabs ul li.phototab a').click();
			return false;
		});

		//$('.propertygallery a').lightBox();

		$('.ms_cleararr').click(function() {
			$('#arrdt').attr('value', '');
			return false;
		});

		$('.ms_cleardep').click(function() {
			$('#depdt').attr('value', '');
			return false;
		});

		$('.as_cleararr').click(function() {
			$('#as_arrdt').attr('value', '');
			return false;
		});

		$('.as_cleardep').click(function() {
			$('#as_depdt').attr('value', '');
			return false;
		});


});
