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'));

		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', '');
			arrCalendar.selection.clear();
			return false;
		});

		$('.ms_cleardep').click(function() {
			$('#depdt').attr('value', '');
			depCalendar.selection.clear();
			return false;
		});

		$('.as_cleararr').click(function() {
			$('#as_arrdt').attr('value', '');
			as_arrCalendar.selection.clear();
			return false;
		});

		$('.as_cleardep').click(function() {
			$('#as_depdt').attr('value', '');
			as_depCalendar.selection.clear();
			return false;
		});
		
});

function addToShortlist(id) {
		var shortlist = $.cookie('shortlist');
		if(shortlist == null ) shortlist = '';
		if(shortlist !='') shortlist += ',';
		shortlist += id;
		$.cookie('shortlist',shortlist,{path: '/'});
		$('#addtoshotlistlink').fadeOut('medium', function() {
				$('#addedtoshortlist').fadeIn('medium');		
		});
}

function removeFromShortlist(id) {
		var shortlist = $.cookie('shortlist').split(",");
		shortlist = jQuery.grep(shortlist, function(value) {
				return value != id;
		});
		$.cookie('shortlist',shortlist.join(','),{path: '/'});
		
		$('#property'+id).slideUp('medium', function() {
				$('#property'+id).remove();
		});
		if($('.property').length <= 1 )
				$('#emptyshortlistlink').fadeOut('medium', function() {
						$('#emptyshortlist').fadeIn('medium');
				});		
}

function clearShortlist() {
		$.cookie('shortlist',null,{path: '/'});
		$('#emptyshortlistlink').fadeOut('medium', function() {
				$('.property').slideUp('medium', function() {
						$.scrollTo($('div#header'),200, function() {
								$('#emptyshortlist').fadeIn('medium');
						});
				});		
		});
}

function UpdateEndDate(startdatefield, enddatefield, nightsfield) {
		if($('#'+startdatefield).val() != '') {
				var mdy = $('#'+startdatefield).val().split('/');
				var d1 = new Date(mdy[2], mdy[1]-1, mdy[0]);
				var nights = parseInt($('#'+nightsfield).val());
				d1.setDate(d1.getDate()+nights);
				$('#'+enddatefield).attr('value',d1.print('%d/%m/%Y'));
	}
}

function UpdateNumberNights(startdatefield, enddatefield, resultfield, calendarObj) {
		var mdy = $('#'+startdatefield).val().split('/');
		var d1 = new Date(mdy[2], mdy[1]-1, mdy[0]);
		mdy = $('#'+enddatefield).val().split('/');
		var d2 = new Date(mdy[2], mdy[1]-1, mdy[0]);
		var value1 = Math.ceil((d2-d1)/(1000*60*60*24));
		if(value1 > 56) {
				calendarObj.callCloseHandler();
				if(confirm('The period you are trying to select is more than 56 days. We are unable to process bookings of more than 56 days via the website.\r\n\r\nWould you like to contact us for a quote for this period?')) {
							window.location.href = 'https://secure.supercontrol.co.uk/availability/enquiry_form_short.asp?ownerID=345&siteID=525';
				} else {
						value1=56;
						$('#'+resultfield).val(value1);
						UpdateEndDate(startdatefield, enddatefield, resultfield);
				}
		} else {
				$('#'+resultfield).val(value1);
		}
}
