$(document).ready(function() {

	// check element ID exists before creating the slideshow
	if ($("#slideshow").length) {
		$('#slideshow').cycle({
			fx: 'fade',
			timeout: 7500,
			speed: 750
		});
	}

	// Set date format for availability form
	if ($(".datepicker").length) {
		$('.datepicker').datePicker({
		dateFormat: 'dd-mm-yy',
		clickInput:true,
		renderCallback:function($td, thisDate, month, year)
			{
				var day = thisDate.getDay();
				if (day == 0 || day == 1 || day == 2 || day == 3 || day == 4) {
				$td.addClass('disabled');
				}
			}
		});
	}

	// Bookmark site
	if(window.opera) {
		if ($("a.bookmark").attr("rel") != ""){
			$("a.bookmark").attr("rel","sidebar");
		} 
	}

	$("a.bookmark").click(function(event){
		event.preventDefault();
		var url = this.href;
		var title = this.title;

		if (window.sidebar) { 
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { 
			window.external.AddFavorite( url, title);
		} else if(window.opera) { 
			return false; 
		} else { 
			 alert('Sorry, your browser does not support the requested action,'
			 + ' please add this page manually to your bookmarks.');
		}
	});

	// headline scroll content
	var headline_count;
	var headline_interval;
	var old_headline = 0;
	var current_headline = 0;
	headline_count = $("div.headline").size();
	$("div.headline:eq("+current_headline+")").css('top','5px');
	headline_interval = setInterval(headline_rotate,12000); //time in milliseconds
	
	function headline_rotate() {
		current_headline = (old_headline + 1) % headline_count; 
		$("div.headline:eq(" + old_headline + ")").animate({top: -205},"slow", function() {
		$(this).css('top','210px');
		});
		$("div.headline:eq(" + current_headline + ")").show().animate({top: 5},"slow");  
		old_headline = current_headline;
	}


	if ($('#blog-panel').length) {
		$('#blog-panel').prependTo($('#panel'))
	}

	// subscribe button click (validation)
	$('#subscribe').click(function() {
		if ($("#subscription-form").valid()) {
			$.post('subscribe.asp', {
				xxControl:'sign up',
				emailaddress:$('#email-address').val(),
				action:'subscribe'
			}, function(response){
				if (response != '') { 
					$('.inner-content').empty();
					$('#email-address').val('');
					$('.inner-content').append(response);
				}
			})
		}
		return false;
	});

	// unsubscribe button click (validation)
	$('#unsubscribe').click(function() {
		if ($("#unsubscribe-form").valid()) {
			$.post('subscribe.asp', {
				xxControl:'unsubscribe',
				emailaddress:$('#Email').val(),
				action:'remove'
			}, function(response){
				if (response != '') { 
					$('.inner-content').empty();
					$('#Email').val('');
					$('.inner-content').append(response);
				}
			})
		}
		return false;
	});

	// contact button click (validation)
	$('#contact').click(function(e) {
		e.preventDefault();
		if ($('#contact-form').valid()) {
			$.post('contact.asp', {
				xxControl:'Send enquiry',
				Name:$('#name').val(),
				Email:$('#email').val(),
				Telephone:$('#telephone').val(),
				Enquiry:$('#enquiry').val()
			}, function(response){
				if (response == 'sent') { 
					window.location.href='/contact.asp?action=sent'
				}
			})
		}
		return false;
	});


	$('#showMore').bind('click', function(e) {
		e.preventDefault();
		if ($('#showMore').text() == '+ More options') {
			$('#showMore').text('- Less options');
		} else {
			$('#showMore').text('+ More options');
		}
		$('#moreOptions').slideToggle('slow');
	});

	$('#bookingFormSubmit').click(function() {
		if ($("#booking-form").valid()) {
			if (!( $('#ConfirmEmail').val() == $('#CheckEmail').val() )) {
				alert('Email address and Confirmation must be the same');
				return false;
			}
			return true;
		}
		$(".error:visible:enabled:first").focus();
		if (!( $('#ConfirmEmail').val() == $('#CheckEmail').val() )) {
			alert('Email address and Confirmation must be the same');
		}
		return false;
	});

	$('#xxcopy').click(function() {
		if ($("#xxcopy").is(":checked")) {
			$('#copyto-fields input', ':visible', document.body).each(function(i) { 
				$(this).val( $('#address-fields input').eq(i).val() );
			});
			$('#copyto-fields textarea', ':visible', document.body).each(function(i) { 
				$(this).val( $('#address-fields textarea').eq(i).val() );
			});

		} else {
			$('#copyto-fields input', ':visible', document.body).each(function(i) { 
				$(this).val(""); 
			});
			$('#copyto-fields textarea', ':visible', document.body).each(function(i) { 
				$(this).val(""); 
			});
		}
	});

	$('.lightbox').lightBox({fixedNavigation:true});

	// scroll properties
	function mycarousel_initCallback(carousel) {
		// Disable autoscrolling if the user clicks the prev or next button.
		carousel.buttonNext.bind('click', function() {
			carousel.startAuto(0);
		});

		carousel.buttonPrev.bind('click', function() {
			carousel.startAuto(0);
		});

		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	};

	$('#mycarousel').jcarousel({
		scroll: 1,
		auto: 4,
		wrap: 'last',
		initCallback: mycarousel_initCallback
	});

	// if IE6 set image src to use thumbnails
	if ($.browser.msie && $.browser.version == '6.0') {
		$(".feature a img").each(function() {
			$(this).attr('src', $(this).attr('src').replace('.jpg','t.jpg'));
		});
	}

	$('#submitForm').click(function() {
		if ($(".validate").valid()) {
			return true;
		}
		return false;
	});

	ddsmoothmenu.init({
		mainmenuid: "smoothmenu1", 
		orientation: 'h', 
		classname: 'ddsmoothmenu', 
		contentsource: "markup" 
	});

});
