var openChild = false;
var sliderStarted = false;
var sliderOptions = {
	effect: 'fade',
	pauseOnHover: true,
	captionOpacity: 1,
	pauseTime: 10000,
	animSpeed: 900
};
var fadingSpeed = 500;
var homeSliderTrans = 1000;
var homeSliderWait1 = 2000;
var homeSliderWait2 = 3000;
var activeHomeSlides = false;

function getPage(slug, children){
	
	if(activeHomeSlides){
		return false;
	}
	
	
	if(children === "false"){
			
		$.post(AJAX_PATH+"/get-page.php", {slug: slug}, function(data){
			
			if(data === null){
				return false;
			}
			
			// If the user has selected the page that they
			// are currently on, ignore
			if(data.docId === curDocId){
				return;
			}
			
			
			if(data.home){
				
				// If the user has landed on a inner page first, we
				// need to set the slider going
				if(sliderStarted === false){
					$('#slider').nivoSlider(sliderOptions);
					sliderStarted = true;
				}
				
				// Fade out inner page info
				$("#inner-page-wrap").fadeOut(fadingSpeed);
				
				// Bring home page info in
				$("#slider").children(".nivo-caption").css("display", "block");
				$("#home-page-wrap").fadeIn(fadingSpeed);
				$('#slider').data('nivoslider').start();
				
				homeOrInnerState = 'home';
				
			} else {
				
				if(homeOrInnerState === 'home'){
					
					// Stop the slider
					$('#slider').data('nivoslider').stop();
					
					// Update the content image, header and content
					$("#inner-page-wrap > img").attr("src", imagePath+"/"+data.image);
					$("#si-content h1").html(data.heading);
					$("#si-content .content").html(data.content);
					
					// Fade out home page info
					$("#home-page-wrap").fadeOut(fadingSpeed);
					
					// Set content box to display so that it will come
					// in with the #inner-page-wrap below
					$("#si-content").css("display", "block");
					
					// Bring inner page info in
					$("#inner-page-wrap").fadeIn(fadingSpeed);
					
				} else {
					
					// We are already seeing an inner page and have
					// selected to see a different inner page
					
					$("#inner-page-wrap")
						.clone().insertAfter("#inner-page-wrap").attr("id", "inner-page-wrap-new").css("display", "none");
					
					// Update the content image, header and content
					$("#inner-page-wrap-new > img").attr("src", imagePath+"/"+data.image);
					$("#inner-page-wrap-new #si-content h1").html(data.heading);
					$("#inner-page-wrap-new #si-content .content").html(data.content);
					
					// Display content
					$("#inner-page-wrap-new #si-content").removeClass("hidden").css("display", "block");
					
					
					// Bring inner page info in
					$("#inner-page-wrap").fadeOut(fadingSpeed);
					
					// Bring inner page info in
					$("#inner-page-wrap-new").fadeIn(fadingSpeed, function(){
						
						// Remove old inner page
						$("#inner-page-wrap").remove();
					
						// Change id of the new inner wrap
						$("#inner-page-wrap-new").attr("id", "inner-page-wrap");
						
					});
					
				}
				
				homeOrInnerState = 'inner';
			}
			
			
			// Close open navigation
			$("#"+openChild).children("ul").fadeOut("fast");
			openChild = false;
			
			// fade in booking bar
			$("#booking-wrap").fadeIn("fast");
			


			// Update curDocId
			curDocId = data.docId;
			
		}, "json");
		
	} else {
		
		// Hide content box
		if(homeOrInnerState === 'home'){
			
			$("#slider").children(".nivo-caption").fadeOut("fast");
			$("#booking-wrap").fadeOut("fast");
			
			// Stop the slider so that the content doesn't come back on the next slide
			try{
			$('#slider').data('nivoslider').stop();
			}catch(e){}
			
		} else {
			
			$("#si-content, #booking-wrap").fadeOut("fast");
		}
	}

	return false;
}

$(function(){
	
	$(".datepicker").datepicker({
		dateFormat: 'dd/mm/yy',
		minDate: 0,
		showButtonPanel: true,
		changeMonth: true,
		changeYear: true 
	}).attr("readonly","readonly");

	
	if(homeOrInnerState === 'home'){
		
		activeHomeSlides = true;
		
		// Fade home sliders in and out and show nivo slider
		setTimeout(function(){
			
			$("#slide-1").fadeOut(homeSliderTrans);
			
			$("#slide-2").fadeIn(homeSliderTrans, function(){
				
				setTimeout(function(){
					
					$("#home-slides-wrap").fadeOut(homeSliderTrans, function(){
						
						activeHomeSlides = false;
						
					});
					
					$('#slider').nivoSlider(sliderOptions);
					sliderStarted = true;
					
				}, homeSliderWait2);
				
			});
			
		}, homeSliderWait1);
	}
	
	
	$(document).click(function(event){
		
		// If the user clicks elsewhere on the page while a
		// child navigation is open, close it and display the content
		if(openChild){
			
			var $parentId = $(event.target).parent().parent().parent().attr("id");
			
			// if the click was from within the parent that's open, ignore
			if($parentId === openChild){
				return;	
			}
			
			// fade out open list
			$("#"+openChild).children("ul").fadeOut("fast");
			
			// Show content box
			if(homeOrInnerState === 'home'){
				
				$("#slider").children(".nivo-caption").fadeIn("fast");
				$("#booking-wrap").fadeIn("fast");
				
				// Start the slider again
				$('#slider').data('nivoslider').start();
				
			} else {
				$("#si-content, #booking-wrap").fadeIn("fast");
			}
			
			openChild = false;
		}
		
	});
	
	$("#main_nav > li:not('.ignore') > a, #bottom-nav > li:not('.ignore') > a").click(function(){
		
		// If we are going through the home slides ignore
		if(activeHomeSlides){			
			return false;
		}
		
		// Check if the click is the parent of the same dropdown, 
		// if so, close dropdown
		if(openChild === $(this).parent().attr("id")){
			
			// fade out open list
			$(this).siblings("ul").fadeOut("fast");
			
			// Show content box
			if(homeOrInnerState === 'home'){
				
				$("#slider").children(".nivo-caption").fadeIn("fast");
				$("#booking-wrap").fadeIn("fast");
				
				// Start the slider again
				$('#slider').data('nivoslider').start();
				
			} else {
				$("#si-content, #booking-wrap").fadeIn("fast");
			}
			
			// Update openChild state
			openChild = false;
			
			return false;
		}
		
		// if there is no dropdown open, open as normal
		if(openChild === false){
			
			$(this).siblings("ul").fadeIn("fast");
			openChild = $(this).parent().attr("id");
			
			return false;
			
		} else { // If there is a dropdown open, close it
			
			// Close child curently open
			$("#"+openChild).children("ul").fadeOut("fast");
			
			// Open new child
			$(this).siblings("ul").fadeIn("fast");
			openChild = $(this).parent().attr("id");
			
			return false;
		}
		
	});

});
