$(document).ready(function() {
	
	/*if($("#tabs").length > 0) { // If tabs exist
		$("#tabs").tabs({ 
			selected:0,
			cache: false,
			load: function() {
				$('#gallery1').galleria();
				$('#gallery2').galleria();
				$('#gallery3').galleria();
			}
		});
	}
	
	if((".gallery_thumbs").length > 0) {
		$("a.thumb").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	400, 
			'overlayShow'	:	false
		});
	}*/
	
	if($("select").length>0) {
		$('select').selectmenu({
			style:'popup',
			width: 220,
			menuWidth: 220
		});
	}

	
	if (!($.browser.msie && $.browser.version <= 6)) { //Galleria doesn't work with IE6
		if($(".gallery").length > 0) {
			$('#gallery1').galleria({ pause_on_interaction: true });
			$('#gallery2').galleria({ pause_on_interaction: true });
			$('#gallery3').galleria({ pause_on_interaction: true });
		}
	}
	
	if($("#tabs").length > 0) {
		$("#tabs").tabs({ 
			selected:0,
			cache: false
		});
	}
	
	if($("#contact_form").length > 0) { // If contact form exists
		var dates = $("#form_start_date, #form_end_date").datepicker({
			dateFormat: 'dd M yy',
			minDate: 1,
			showOn: 'both',
			buttonImage: '../css/pepper-grinder/images/calendar.png',
			buttonImageOnly: true,
			showButtonPanel: true
		});
	}
	
	if($("#page_about").length > 0) {
		$("#sidebar").css("height","580px");
	}
	  
	var total_images = $(".image_viewer .image_holder").size() - 1;
	var curr_image = 0;
	
	//Attach Image Navigation alter height of container
	$(".image_viewer").prepend("<a href='#' title='previous image' class='prev_img nav_btn'>Previous</a><a href='#' title='next image' class='next_img nav_btn'>Next</a>").css("height","400px");
	
	//Position individual image containers absolutely and add padding to place image above
	$(".image_viewer .image_holder").css({"position" : "absolute", "top" : "0", "padding-top" : "203px"}).hide();
	
	//Position each image absolutely so it slots into padding space
	$(".image_viewer .image_holder img").css({"position" : "absolute", "top" : "0"});
	
	//Align title text center and add margin to make way for nav buttons
	$(".image_viewer .img_link").css({"text-align" : "center", "width" : "180px", "margin" : "0 auto 12px auto"});
	
	//Show first image and info
	$(".image_viewer .image_holder:first").show();
	
	//Toggle image function
	function toggleImage() {
		$(".image_viewer .image_holder:eq(" + curr_image + ")").toggle();
	}
	
	//Function to check if first or last image. If so, fade respective button
	function imageStatus() {
		if(curr_image == total_images) {
			$("a.next_img").css("opacity",0.3);
		}else if(curr_image === 0) {
			$("a.prev_img").css("opacity",0.3);
		}else{
			$("a.next_img").css("opacity",1);
			$("a.prev_img").css("opacity",1);
		}
	}
	
	$("a.next_img").click(function() {
		if(curr_image < total_images) {
			toggleImage();
			curr_image++;
			toggleImage();
			imageStatus();
		}
		return false;
	});
	
	$("a.prev_img").css("opacity",0.3).click(function() {
		if(curr_image > 0) {
			toggleImage();
			curr_image--;
			toggleImage();
			imageStatus();
		}
		return false;
	});
	
	function autoFade() {
		if(curr_image < total_images) {
			toggleImage();
			curr_image++;
			toggleImage();
			
		}else{
			toggleImage();
			curr_image = 0;
			toggleImage();
			$("a.next_img").css("opacity",1);
		}
		imageStatus();
	}
	
	window.setInterval(autoFade, 12500);
	
	if($('#slideshow').length > 0) {
		$('#slideshow').cycle({
			delay:  2000,
			fx: 'fade'
		});
	}
	
	/* Homepage Slideshow
	var slideCount = $("#slideshow img").size() - 1;
	var currSlideImg = 0;
	
	$("#slideshow img").hide();
	$("#slideshow img:first").show();
	
	window.setInterval(changeSlide, 8000);
	
	function changeSlide() {
		$("#slideshow img:eq(" + currSlideImg + ")").toggleFade(1000);
		if(currSlideImg == slideCount) {
			currSlideImg = 0;
			$("#slideshow img:eq(" + currSlideImg + ")").toggleFade(1000);
			currSlideImg++;
		}else{
			currSlideImg++;
			$("#slideshow img:eq(" + currSlideImg + ")").toggleFade(1000);
		}
		
	};*/
	
	// Contact form validation and AJAX post
	
	$("#contact_form #form_submit").click(function() {
	
		//var form = $("#contact_form");
	  	
	  	//Hide any visible error messages and classes of inputs
	    $("label span.error").hide();
	    //form.find("input").removeClass("error");
		
		//Validate form values
		var name = $("#form_name").val();		
		if (name == "") {
			$(window).scrollTop(500);
			$("#form_name").focus().prev().find("span.error").show();
	    	return false;
	    }
	    
	    var email = $("#form_email").val();		
		if (email == "") {
			$(window).scrollTop(500);
			$("#form_email").focus().prev().find("span.error").show();
	    	return false;
	    }
	    
	    var contact_number = $("#form_contact_number").val();
	    var start_date = $("#form_start_date").val();
	    var end_date = $("#form_end_date").val();
	    var room = $("#room_choice").val();
	    var msg = $("#form_details").val();
	    
	    //Attach loading graphic to button
	    $(this).parent().append("<img class='loading' src='../css/img/icons/loader.gif' alt='Sending' width='20' height='20' />");
	    $("#contact_form #form_submit").attr('value','Sending Enquiry...');

	    
	    //Fade out form elements	
	    $("#contact_form").find("input[type='text'], label, span, textarea").animate( { opacity: 0.5 }, 400 );	    
	    
		var dataString = 'f1=' + name + '&f2=' + email + '&f3=' + contact_number + '&f4-1=' + start_date + '&f4-2=' + end_date + '&f5=' + room + '&f6=' + msg;
		
		//alert (dataString);
		
		$.ajax({
		    type: "POST",
		    url: "../inc/process.php",
		    data: dataString,
		    success: function($data) {
		    	$("#contact_form div").slideUp(1000);
		    	$("#contact_form #form_submit").fadeOut("slow", function() {
		        	$("#contact_form").append("<div class='message_sent'><h5>Message Sent.</h5><p>Thank you for your enquiry, we will shortly be in touch</p></div>")
		        		.find(".message_sent").hide().fadeIn();
		        		$(window).scrollTop(500);
		        });
		    }
		});
		
	    return false;
    
	});
	
	if($("#page_bookings").length > 0) {
		$(".info_text")
			.before("<a href='#' class='show_info'>Info (<span>+</span><span style='display:none'>&#8211;</span>)</a>")
			.css('display','none');
		$(".show_info").click(function() {
			$(this).parent().parent().siblings().find(".info_text:visible").hide().prev().find("span").toggle();
			$(this)
				.find("span").toggle()
				.end()
				.next().toggle();
			return false;
		})
	}
	
	//Toggle fade plugin
	$.fn.toggleFade = function(settings)
	{
		if(settings==undefined) {
			settings={ speedIn : 'slow'};
		}
		
		settings = jQuery.extend(
				{
					speedIn: "normal",
					speedOut: settings.speedIn
				}, settings
		);
		return this.each(function()
				{
			var isHidden = jQuery(this).is(":hidden");
			jQuery(this)[ isHidden ? "fadeIn" : "fadeOut" ]( isHidden ? settings.speedIn : settings.speedOut);
				});
	};

	
});
