$(document).ready(function(){
	var home_page_images = new Array("prestonfield-home-hall.jpg","prestonfield-home-bed.jpg","prestonfield-home-rhubarb.jpg","prestonfield-home-exterior.jpg","prestonfield-home-dining.jpg");
	if($('ul#bTabs').length > 0){
		$('ul#bTabs').tabify();
		$('#brochure-menu').hide();
		$('#brochure').hover(function(){
		$('#brochure-menu').fadeIn();
			},
			function(){
			$('#brochure-menu').hide();
			});
		}
	if ($('#image-rotate').length > 0){
		$('#image-rotate').rotator({images:home_page_images, time:'6000', path:'/images/'})
	}
	if ($("#mainimage").length > 0 && $(".thumbnail").length > 0){

		var images = new Array();
		$('.thumbnail img').each(function(){
			var $src = $(this).attr('src').split("&");
			images.push($src[0]);
		})
		$('.thumbnail').css({opacity:'0.5'});
		$('.thumbnail:first').addClass('active').css({opacity:'1'});
		
		$("#mainimage").rotator({path:'', images: images, time:'5000'});
	}
	$('.thumbnail img').click(function(){
		var imgsrc = $(this).attr('src').split("&");
		var i = new Image();
		$thumb = $(this).parents('.thumbnail');
     	$(i).load(function(){
				$thumb.prepend('<img src="images/ajax-loader.gif" class="ajaxloader" />');
				$('#mainimage').prepend(i);
				$('#mainimage img:gt(0)').fadeOut('3000', function(){
					$(this).remove();
					$('.ajaxloader').remove();
					$('.thumbnail').removeClass('active');
					$('a[href^='+imgsrc[0]+']').parent().addClass('active');
					$('.thumbnail').css({opacity:'0.3'});
					$('.thumbnail[class$="active"]').css({opacity:'1'});});
				}).attr('src', imgsrc[0])
				
		});
	if ($('#date_picker').length > 0){$('#date_picker').datepicker({
			showOn: "both",
			clearText: 'Clear',
			buttonImage:"/images/booking-calendar.gif",
			changeFirstDay: false,
			closeText: 'Close',
			closeAtTop: false,
			minDate: "0d",
			firstDay: 1,
			dateFormat: "d M, yy",
			altField: "#formatted-date",
			altFormat: "mm/dd/yy"
			}
		);
	}
	if ($('#date_picker-2').length > 0){$('#date_picker-2').datepicker({
		showOn: "both",
		clearText: 'Clear',
		buttonImage:"/images/booking-calendar.gif",
		changeFirstDay: false,
		closeText: 'Close',
		closeAtTop: false,
		minDate: "0d",
		firstDay: 1,
		dateFormat: "d M, yy",
		altField: "#formatted-date-2",
		altFormat: "mm/dd/yy"
		}
	);
	}

	
	$('#booking form #submit').click(function(){
		
		// used for alert only
		$link = "https://gc.synxis.com/rez.aspx&";
		$date = $('#formatted-date').val();
		$date_2 = $('#formatted-date-2').val();
		$non = $('#non').val();
		$nop = $('#nop').val();
		if ( $('#promo').val() == 'Promotional Code' ){
			$('#promo').val('');
		}
		$error = "";
		if($nop =="None Selected"){
			$error += "* Number of adults\n"
		}		
		if($non == "None Selected"){
			$error += "* Number of rooms\n";
		}
		if($date == ""){
			$error += "* Date of your arrival\n"; 
		}		
		if($date_2 == ""){
			$error += "* Date of your departure\n"; 
		}
		
		// check departure date is after arrival date		
		if( Date.parse($date) >= Date.parse($date_2) )
		{
			$error += "* Date of departure that occurs after date of arrival";
		}
		
		if($error.length>0){
			alert("Please enter:\n" + $error);
			return false;
		} else {
			$dates = $date.split("/");
			//$link = $link+'arrday='+$dates[0]+'&arrmon='+$dates[1]+'&non='+$non+'&ad='+$nop+'&agentref=&companyref=&refco=';
			$link = $link+'Arrive='+$dates[0]+'/'+$dates[1]+'/'+$dates[2]+'+&Rooms='+$non+'&Adult='+$nop;
			//alert($link);
			//$('#booking form').attr('action', $link)
		}
	})
	
});
