$(document).ready(function(){
	$("#gallery-button").hide(); // Temporary, until created gallery
	var stopped = "false";
	// Initial page load
	$("#content-inner").hide().fadeIn(1000); // removed the delay of 800
	$(".noscript").hide(); $("#extras").addClass("extras"); $("#main-menu .navigation-sub").removeClass("noscriptnav");
	$("#main-menu .navigation-sub").hide();
	if(!$("#wrapper").hasClass("homepage")){
		$("#content-inner").before("<span class='waiting'>Loading...</span>");
		$(".waiting").delay(200).fadeOut(1000, function(){ $(".waiting").remove(); });
		if(!$("#wrapper").hasClass("categorypage")){
			$(".nav-active").parents(".navigation-sub").show().delay(800).slideUp(800);
		}else{
			$(".nav-active").parents(".level-0").children(".navigation-sub").show().delay(800).slideUp(800);
		}
	}
	// Navigation
	$("#nav-explore").hover(function(){
		$("#nav-explore .navigation-sub").stop(true,true).slideDown("slow");
	},function(){
		if(stopped!="true"){
			$("#nav-explore .navigation-sub").stop(true,true).slideUp("slow");
		}
	});
	$("#nav-stay").hover(function(){
		$("#nav-stay .navigation-sub").stop(true,true).slideDown("slow");
	},function(){
		if(stopped!="true"){
			$("#nav-stay .navigation-sub").stop(true,true).slideUp("slow");
		}
	});
	$("#nav-booking").hover(function(){
		$("#nav-booking .navigation-sub").stop(true,true).slideDown("slow");
	},function(){
		if(stopped!="true"){
			$("#nav-booking .navigation-sub").stop(true,true).slideUp("slow");
		}
	});
	// Navigation Click
	$("#main-menu a").click(function(){
		stopped = "true";
		var href= $(this).attr('href');
		if($("#wrapper").hasClass("extrapage")){ document.location.href = href; }
		$(this).addClass("new-nav-active");
		$("#content-inner").before("<span class='waiting'>Loading...</span>");
		document.location.href = href; // used to be within the animation function
		/*
		$("#content-inner").animate({
			//left: '+=450',
			opacity: 0
  		}, 1000, function(){
  			document.location.href = href;
  			stopped=""; $(".navigation-sub:visible").slideUp(800);
  			$("#main-menu *").removeClass("new-nav-active");
  			$("#content .waiting").remove();
  			$("#content-inner").delay(1000).animate({//left: '-=450', 
  			opacity: 1}, 700);
  		});*/
	});
	// Logo animation
	$("#logo").hover(function(){
		$("#logo #logo-animation").stop(true,true).fadeIn("slow");
	},function(){
		$("#logo #logo-animation").stop(true,true).fadeOut(800);
	});
	// Special Offers Tab
	$("#tab-offers .tab-inner").load("http://www.atholl-lodge.com/special-offers.php #offered", function(){
		$(".tab-side .offer").click(function(){
			var thehtml = $(this).html();
			var temphtml = $("#offer-one").html();
			$(this).html(temphtml);
			$("#offer-one").html(thehtml);
		});
	});
	// Gallery
	if($("#wrapper").hasClass("minigallerypage")){
		$(".minigallerypage #gallery-content img").hide();
		var src = $(".minigallerypage #gallery-content img").attr("src");
		$(".minigallerypage #gallery-content .image").css("background","white url("+src+") no-repeat center center");
	}
	if($("#wrapper").hasClass("slidegallery")){
		$(".minigallerypage #gallery-content img").hide();
		var src = $(".slidegallery #gallery-content img").attr("src");
		$(".slidegallery #gallery-content .image").css("background","white url("+src+") no-repeat center center");
	}
	// Mini Slide Gallery
	var myimages = $("#autoscroll-gallery li");
	var interval = 6000;
	if(myimages.size()>1){
		$("#autoscroll-gallery li:first").clone().insertAfter("#autoscroll-gallery li:last").addClass("noprint");
		$("#autoscroll-gallery li").eq(1).clone().insertAfter("#autoscroll-gallery li:last").addClass("noprint");
		var containersize = myimages.size() * 761;
		containersize+=761;
		setInterval(
		function(){
			scrollPictures();
		}, interval
		);
	}
	function scrollPictures(myimages){
		var position = $("#autoscroll-gallery ul").position();
		var left = position.left - 761;
		if(containersize + left>5){
			$("#autoscroll-gallery ul").animate({
				"left": "-=761px"
			}, "slow", function(){
				if((containersize + left)-761 < 5){
					$("#autoscroll-gallery ul").css("left","0");
				}
			});
		}
	};
	// Tabs
	$(".tab-title").addClass("abs-tab");
	$(".tab").addClass("tabb-abs");
	$("#tab-location").addClass("active");
	$(".tab:not(.active) .tab-inner").hide();
	$(".tab").click(function(){
		$(".tab").removeClass("active");
		$(this).addClass("active");
		$(".tab:not(.active) .tab-inner").hide();
		$(".tab.active .tab-inner").show();
	});
	// Contact Form Tab
	$(".with-script").show(); $(".mini-contact-form h4").removeClass("with-float");
	$(".mini-contact-form fieldset:not(:first)").hide();
	$(".mini-contact-form .contact-title").eq(1).addClass("available-step").addClass("next-step");
	$(".mini-contact-form .contact-title.available-step").live("click", function(){
		var errorCheck = ""; if($(this).hasClass("next-step")){ errorCheck = errorChecking(); }
		if(errorCheck!="true" && $(this).next("fieldset").is(":hidden")){
			$(".mini-contact-form fieldset:visible").hide().removeClass("current-set");
			var which = $(this).attr("id");
			$(".contact-title").removeClass("available-step").removeClass("next-step");
			if(which=="contact-title-one"){ $("#contact-title-two").addClass("available-step").addClass("next-step"); }
			if(which=="contact-title-two"){ $(".contact-title").addClass("available-step"); $("#contact-title-three").addClass("next-step"); }
			$(this).removeClass("available-step").next("fieldset").addClass("current-set").show(function(){
				if($(".mini-contact-form fieldset:last").hasClass("current-set")){
					$(".mini-contact-form .communicate-message").html('<span class="waiting" style="margin-top:-30px;color:#8DAAD0;">{ SENDING }</span>');
					var scriptloc = $("form#mini-contact").attr("action");
					// Get / Prepare all of the data
					var name = $('input#username').val();
					var email = $('input#usermail').val();
					var subject = $('input#subject').val();
					var themessage = $('textarea#message').val();
					var message = encodeURIComponent(themessage);
					var page = $('input#page').val();
					var data = "page=" + page + "&username=" + name + "&usermail=" + email + "&subject=" + subject + "&message=" + message;
					//start the ajax
					$.ajax({
						url: scriptloc,	
						type: "GET",
						data: data,		
						cache: false,
						success: function (html) {				
						if (html==1) {
							$(".mini-contact-form .communicate-message").html("<p class='mymessage'>Your message has been sent. Thank you.</p>");
							$(".mini-contact-form input:text").val("");
							$(".mini-contact-form textarea").val("");
						}else{
							$(".mini-contact-form .communicate-message").html("<p class='mymessage'>Sorry, your message was unable to be sent.<br/>"+html+"</p>");
						}
						}			
					});
					$(".mini-contact-form *").removeClass("next-step","available-step","current-step");
					$("#contact-title-one").addClass("current-step").addClass("available-step");
				}
			});
		}else{ $("fieldset.current-set .error:first input:first").focus(); }
	});
	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}
	$(".mini-contact-form input.nextset").click(function(){
		$("fieldset.current-set").next(".contact-title").trigger("click");
	});
	function errorChecking(){
		var errors = ""; $(".mini-contact-form .input-block").removeClass("error");
		var currentSet = $("fieldset.current-set").attr("id");
		$("fieldset.current-set .val").each(function(){
			if(!$(this).val()){ $(this).parent(".input-block").addClass("error"); errors="true"; }
			if($(this).hasClass("val-email")){
				if(!isValidEmailAddress($(this).val())){ $(this).parent(".input-block").addClass("error"); errors="true"; }
			}
			if($(this).hasClass("val-digit")){
				if(!$(this).val().match(/^\d+$/)){ $(this).parent(".input-block").addClass("error"); errors="true"; }
			}
			if($(this).hasClass("val-capt")){
				if($(this).val()!="13"){ $(this).parent(".input-block").addClass("error"); errors="true"; }
			}
		});
		return errors;
	}
	
	// Panes Pages
	if($("#wrapper").hasClass("panespage")){
		var panescount = $(".pane").length; var pacl = 1;
		$("#content-main").append("<div class='nav-pane nprev' id='prev-pane'>PREV</div><div class='nav-pane nnext' id='next-pane'>MORE</div>");
		$("#prev-pane").hide(); $("#wrapper").addClass("js");
		$(".nav-pane").click(function(){
			var advance = false;
			if($(this).hasClass("nnext")){
				$("#prev-pane:hidden").show();
				if(pacl < panescount){ pacl++; advance=true; if(pacl==panescount){ $("#next-pane").hide(); } }
			}else{
				$("#next-pane:hidden").show();
				if(pacl > 1){ pacl=pacl-1; advance=true; if(pacl==1){ $("#prev-pane").hide(); } }
			}
			if(advance==true){
				$(".pane:visible").fadeOut();
				$(".pane").eq(pacl-1).fadeIn();
			}
		});
	}
	// Overlay setup
	$("#wrapper").after("<div id='dimmer'>&nbsp;</div><div id='theoverlay' tabindex='0'><div id='theoverlay-inner'><h3 id='overlay-title'>&nbsp;</h3><div id='overlay-content'>&nbsp;</div></div></div>");
	$(".overlaylink").click(function(){
		$("#dimmer").fadeTo("800",0.7);
		var target = $(this).attr("href");
		$("#wrapper").addClass("overlaid");
		if(target.search("driving.html")!=-1){
			$("#overlay-title").html("<h2>Driving Directions for Atholl Lodge</h2>");
			$("#overlay-content").html("<iframe id='mapped' src='"+target+"'></iframe>");
			$("#theoverlay-inner").prepend("<div id='close-overlay' title='close'>CLOSE</div>");
			$("#theoverlay").slideDown("1400").focus();
			$("#dimmer, #header, #availability, #close-overlay").click(function(){ $("#theoverlay").slideUp("800", function(){
				$("#overlay-title").html("&nbsp;"); $("#overlay-content").html("&nbsp;"); $("#wrapper").removeClass("overlaid");
				$("#close-overlay").remove();
			}); $("#dimmer").fadeOut(); });
		}else if(target.search("map.html")!=-1){
			$("#overlay-title").html("<h2>Map Our Location</h2>");
			$("#overlay-content").html("<iframe id='mapped' src='"+target+"'></iframe>");
			$("#theoverlay-inner").prepend("<div id='close-overlay' title='close'>CLOSE</div>");
			$("#theoverlay").slideDown("1400").focus();
			$("#dimmer, #header, #availability, #close-overlay").click(function(){ $("#theoverlay").slideUp("800", function(){
				$("#overlay-title").html("&nbsp;"); $("#overlay-content").html("&nbsp;"); $("#wrapper").removeClass("overlaid");
				$("#close-overlay").remove();
			}); $("#dimmer").fadeOut(); });
		}else{
			$("#overlay-title").load(target+" #page-title");
			$("#overlay-content").load(target+" #page-contentcontent", function(){
				$("#theoverlay-inner").prepend("<div id='close-overlay' title='close'>CLOSE</div>");
				$("#theoverlay").slideDown("1400").focus();
				$("#dimmer, #header, #availability, #close-overlay").click(function(){ $("#theoverlay").slideUp("800", function(){
					$("#overlay-title").html("&nbsp;"); $("#overlay-content").html("&nbsp;"); $("#wrapper").removeClass("overlaid");
					$("#close-overlay").remove();
				}); $("#dimmer").fadeOut(); });
			});
		}
	})
	// Rates Setup
	if($("#wrapper").hasClass("ratespage")){
		$("#rates").load("info/rates.php #rates");
	}
	// Eviivo
	// Put this in an iframe so that it loads at the last
	$("#nav-booking #availability-eviivo").html("<iframe id='eviivo-booking' src='http://www.atholl-lodge.com/info/online-booking.html'></iframe>");
 });
