	$(document).ready(function(){
							   
					
		$('.hint').hint();
		$('area').annotateTHIS();
		$("#firstslide").easySlider({
			nextId: "slider1next",
			prevId: "slider1prev",
			auto:			false
		});
		$("#secondslide").easySlider({
			nextId: "slider2next",
			prevId: "slider2prev",
			auto:			false
		});
		
		// remove final border from ul nav
		$('ul > li:last-child').css('border','none');
		
		// resize slider images
		$( ".resized" ).aeImageResize({ height: 180, width: 180 }); 
		
		// and cottage images
		$( ".resized-big" ).aeImageResize({ height: 350, width: 350 }); 
		$( ".resized-small" ).aeImageResize({height:100, width: 100 }); 
		
		
		// swapsie images around on click
		$('.thumbs img').click(function() {
		var src = ($(this).attr("src")).replace("&size=100", "&size=350");
		var qs = ($(this).attr("rel"));
		
		console.log (qs);
	
		
		$(".lbpModal").attr("href", "http://www.discoverscotland.net/slideshow.php"+qs);
		$("#mainimg").attr("src", src);
		
		
		
		});
		
		// All the shortlist ajax stuff - loading pages is so 2001
		$('.shortlist-add').click(function() {
				var propid = ($(this).attr("id"));							
				$.get("http://"+document.location.hostname+"/shortlist-functions.php", {id:propid}, function(data){
				//alert("Data Loaded: " + data);
				$("span#"+propid).html(data);
				$("span#"+propid).removeClass();
				});
		});
		
		
		$('.shortlist-remove').click(function() {
				var propid = ($(this).attr("id"));							
				$.get("http://"+document.location.hostname+"/shortlist-functions.php", {remove:propid}, function(data){
				$("span#"+propid).html(data);
				$("span#"+propid).removeClass();
				});
		});
		
		
		$('.shortlist-inner-remove').click(function() {
				var propid = ($(this).attr("id"));							
				$.get("http://"+document.location.hostname+"/shortlist-functions.php", {remove:propid}, function(data){
				$("div#"+propid).slideUp('slow');
				});
		});
		
		
		// This works, anyway...
		$('.shortlist-removeall').click(function() {
										
				$.get("http://"+document.location.hostname+"/shortlist-functions.php", {removeall:"1"}, function(data){
				alert(data);
				location.reload();
				});
		});
		
		/*
		archived
		
		var currentTime 	= new Date();
		dayno = currentTime.getDay();
		// saturday is always 6, then?
		// today, it's wednesday, so 3
		// we need a constant to get next saturday
		var daysuntilsat	= 6-dayno;
		
		var nextSat			= (currentTime.getFullYear() + '-' + (currentTime.getMonth()+1) + '-' + (currentTime.getDate()+daysuntilsat));
		var satAfter		= (currentTime.getFullYear() + '-' + (currentTime.getMonth()+1) + '-' + (currentTime.getDate()+daysuntilsat+7))
		
		
		$('#date').DatePicker({
		flat: true,
		date: [nextSat, satAfter],
		current: currentTime,
		format: 'Y-m-d',
		calendars: 1,
		mode: 'range',
		starts: 0
		});
		*/
		
		// ajax for calendar initial load...

		var propid = ($(".calendardiv").attr("id"));
		
		loading = "<img src='http://"+document.location.hostname+"/loading.gif' class='loading'><br/>";
		loadingsmall = "<img src='http://"+document.location.hostname+"/loading-small.gif' class='loading-small'>";
		
		var calmonth = ($("#calmonth").val());
		var calyear = ($("#calyear").val());
		
		$.get("http://"+document.location.hostname+"/calendar-functions.php", {id:propid, month:calmonth, year:calyear}, function(data){																		
		setTimeout("$('.calendardiv').html('"+data+"')", 500);
		});
		
		
		// ...and ajax for updates
		$('.calendarselect').change(function() {
											 
		//$('.pricediv').html("Select a date range above to book this cottage."); UNCOMMENT ME
		$(".calendardiv").html(loading);
		var propid = ($(".calendardiv").attr("id"));
		var calmonth = ($("#calmonth").val());
		var calyear = ($("#calyear").val());
										 
		$.get("http://"+document.location.hostname+"/calendar-functions.php", {id:propid, month:calmonth, year:calyear}, function(data){
		setTimeout("$('.calendardiv').html('"+data+"')", 500);

		});
		});
		
		// this is the cool bit right here: select X number of nights by clicking

		$('.available').live('click', function() {
											   
		// first, we need to check that we haven't clashed with a forthcoming booking
		//so, let's check the next X days for class=unavailable
		var nights = parseFloat($("#calnights").val());
		var id = parseFloat($(this).attr("id"));
		doable = "true";
		
		for( i=0; i < (nights); i++){
		selectednight = id+i;
		if ($("#"+selectednight).hasClass("unavailable")) {
		doable = "false";
		}
		//alert (selectednight+": "+doable);
		}
		
		
		
		if (doable=="true") { 

											   
		// clear the pricing data
		$(".pricediv").html(loadingsmall);
											   
		 // clear old styles
		 $(".day-number").removeClass("selectednight");
		 
		var fromstamp = parseFloat($(this).attr("rel"));
				
		for( i=0; i < (nights); i++){
		selectednight = id+i;
  		$("#"+selectednight).addClass("selectednight");
		}
		// let's just get the leaving date
		selectednight = id+nights;
		// this isn't working if it crosses into the next month!
		//var tostamp = parseFloat($("#"+selectednight).attr("rel"));
		
		var tostamp = (fromstamp + (nights * 24 * 60 * 60));
		

				
		// now let's send this off to a pricing function file...
		
		
		$.get("http://"+document.location.hostname+"/pricing-functions.php", {id:propid, from:fromstamp, to:tostamp}, function(data){
																															   
		function copyprices() {
		$('.pricediv').html(data);	
		}
		setTimeout(copyprices, 500);
		});
		
		
		
		}			
		
		else {
			alert ("Sorry, but the range you selected is not available due to a scheduling conflict.");
		}
		
		
		});
		
		// display the booking form on booking click
		
		$('#dropform').live('click', function() {
												
		 $('.bookform').slideToggle('slow');
		});
		
		// validate the forms
		
		$("#bookingform").validate();
		
		$("#newsletterform").validate(function() {
		$(".changer").val('Loading...');
		});
		
		$("#brochureform").validate(function() {
		$(".changer").val('Loading...');
		});
		
		$("#contactform").validate(function() {
		$(".changer").val('Loading...');
		});
		
		

		
		$("#newsletterform").ajaxForm(function() { 
		$("#newsletter").html("<p class='title'>Thanks for subscribing!</p><p>Your details have been added to our mailing list.</p>"); 	
	});
		
		$("#brochureform").ajaxForm(function() { 
		$(".brochure").html("<p class='title'>Thanks for your request.</p><p>Your brochure will be mailed to you in the next five working days.</p><p>If you can't wait that long, why not download a copy using the link on the right?</p>"); 	
	});
		
		
		$("#contactform").ajaxForm(function() { 
		$(".contact").html("<p class='title'>Thanks for getting in touch with Discover Scotland.</p><p>We'll be back in touch as soon as possible.</p>"); 	
	});
		
	
	
	// datepicker on form front page
	
	var currentTime 	= new Date();
	var month = currentTime.getMonth();
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	
	var currentDate = (year + "-" + month + "-" + day);
	
	
	$('#searchdate').DatePicker({
	date: 'currentDate',
	date: 'currentDate',
	format: 'd B Y',
	calendars: 1,
	mode: 'single',
	position: 'r',
	starts: 0,
	
	onChange: function(formated, dates){
		$('#searchdate').val(formated);
		$('#searchdate').DatePickerHide();
		}
	
	});
	
	// move to next/previous month	
		$('.move').click(function() {
		var moveamount = ($(this).attr("id"));
		// we've already defined the year above
		// now we need to check the month
		
		var calmonth = ($("#calmonth").val());
		var calyear = ($("#calyear").val());
		var propid = ($(".calendardiv").attr("id"));
		
		// case one: we're adding a month
		if (moveamount==1) {
			// check that we ain't changing year
			if (calmonth==12) {
				// check that we're not trying to get past the end of the next bookable year
				if ((parseFloat(calyear))==(parseFloat(year)+1)) {
					alert (' Sorry, but we cannot take bookings online beyond the end of next calendar year. \n Please contact Discover Scotland for more information.')
				} else {
				// add one to year, change month to 1, submit
				$(".calendardiv").html(loading);
				
				$('#calmonth').val('1');
				$('#calyear').val(parseFloat(year)+1);
				
				$.get("http://"+document.location.hostname+"/calendar-functions.php", {id:propid, month:1, year:(parseFloat(year)+1)}, function(data){																		
				setTimeout("$('.calendardiv').html('"+data+"')", 500);
				});
								
				}
			}
			
			else {
			// easy peasy: just add 1 to the month and resubmit
			$(".calendardiv").html(loading);
			
			// we have to bugger about a wee bit for leading zeros
			var newmonth = parseFloat(calmonth)+1;
			if (newmonth < 10) var newmonth = '0'+newmonth;
			
			$('#calmonth').val(newmonth);
							
			$.get("http://"+document.location.hostname+"/calendar-functions.php", {id:propid, month:(parseFloat(calmonth)+1), year:calyear}, function(data){																		
			setTimeout("$('.calendardiv').html('"+data+"')", 500);
			});
				
			}
			
			
		}
		
		// case two: we're adding a month
		if (moveamount== -1) {
			// check that we ain't changing year
			if (calmonth==1) {
				// check that we're not trying to get past the start of this year
				if ((parseFloat(calyear))==(parseFloat(year))) {
					alert (' Sorry, but we cannot display bookings for last year.')
				} else {
				// subtract one from year, change month to 12, submit
				$(".calendardiv").html(loading);
				
				$('#calmonth').val('12');
				$('#calyear').val(parseFloat(year)-1);
				
				$.get("http://"+document.location.hostname+"/calendar-functions.php", {id:propid, month:12, year:(parseFloat(year))}, function(data){																		
				setTimeout("$('.calendardiv').html('"+data+"')", 500);
				});
								
				}
			}
			
			else {
			// easy peasy: just add 1 to the month and resubmit
			$(".calendardiv").html(loading);
			
			// we have to bugger about a wee bit for leading zeros
			var newmonth = parseFloat(calmonth)-1;
			if (newmonth < 10) var newmonth = '0'+newmonth;
			
			$('#calmonth').val(newmonth);
							
			$.get("http://"+document.location.hostname+"/calendar-functions.php", {id:propid, month:(parseFloat(calmonth)-1), year:calyear}, function(data){																		
			setTimeout("$('.calendardiv').html('"+data+"')", 500);
			});
				
			}
			
			
		}
		
		
		
		});	
		
		// get and display payment details on form submit
		
		$("#paymentform").validate();
		
		var options = { 
        target:        '.contact-right',  
        clearForm: false  
 
    	};
		
		$('#paymentform').ajaxForm(options); 

	
		
	});
