// Speed of the automatic slideshow
var slideshowSpeed = 5000;
var photos = ['home','spa','porch','fireplace','greenlandscape'];

function moveSocialShare(){
	if($("body").width() > 1440){
		$("#socialShare").css("left",$(".sizeContainer").offset().left+"px");
	}else{
		$("#socialShare").css("left","0");
	}
}
$(document).ready(function() {	
	
	// moving social share to left of screen unless screen is bigger than background
	moveSocialShare();	
	$(window).resize(function(){moveSocialShare()});	
	
	// changing backgrounds depending on what page you're on
	if($("body").hasClass("page-our-story")){
		photos = ['fireplace','spa','porch','greenlandscape'];
	}else if($("body").hasClass("page-rivendell-suite")){
		photos = ['rivendell1', 'rivendell2', 'rivendell3', 'rivendell4', 'rivendell5', 'rivendell6', 'rivendell7'];
	}else if($("body").hasClass("page-shire-suite")){
		photos = ['shire1', 'shire2', 'shire3', 'shire4', 'shire5', 'shire6', 'shire7'];
	}else if($("body").hasClass("page-fanghorn-suite")){
		photos = ['fanghorn1', 'fanghorn2', 'fanghorn3', 'fanghorn4', 'fanghorn5'];
	}else if($("body").hasClass("page-location")){
		photos = ['kitchen', 'bbq', 'beach'];
	}else if($("body").hasClass("page-around-manawa-ridge")){
		photos = ['rivendell4', 'sunset', 'fireplace'];
	}else if($("body").hasClass("page-media")){
		photos = ['lounge'];
	}else if($("body").hasClass("page-guest-book")){
		photos = ['chair'];
	}else if($("body").hasClass("page-news")){
		photos = ['horse'];
	}else if($("body").hasClass("page-rooms")){
		photos = ['door', 'spa', 'chair', 'greenlandscape2', 'rivendell4'];
	}else if($("body").hasClass("page-one-night-luxury")){
		photos = ['shire1', 'beach', 'roof'];
	}else if($("body").hasClass("page-romance-package")){
		photos = ['rivendell2', 'sunset', 'lounge'];
	}else if($("body").hasClass("page-two-nights")){
		photos = ['kitchen', 'horse', 'fanghorn1'];
	}else if($("body").hasClass("page-three-nights")){
		photos = ['shire1', 'beach', 'roof'];
	}else if($("body").hasClass("page-four-nights")){
		photos = ['shire1', 'lounge', 'kitchen', 'sunset'];
	} 
	
	// rotating background
	var interval;	
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	function navigate(switchTo) {
		if(switchTo == currentImg){
			return;
		}
		switchTo = switchTo || '';
		/*if(animating) {
			return;
		}*/
		
		// Check which current image we need to show
		currentImg++;
		if(currentImg == photos.length + 1) {
			currentImg = 1;
		}
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}		
		
		if(switchTo != ''){
			currentImg = switchTo;
			if(photos[switchTo - 1]){				
			}else{
				switchTo = 0;
			}
			showImage(photos[switchTo - 1], currentContainer, activeContainer);	
		}else{
			if(photos[currentImg - 1]){
			}else{
				currentImg = 0;
			}
			showImage(photos[currentImg - 1], currentContainer, activeContainer);	
		}
	}
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;		
		// Make sure the new container is always on the background
		currentZindex--;		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			//"background-image" : "url(images/" + photoObject + ")",
			"display" : "block",
			"z-index" : currentZindex
		}).attr("class","img-"+photoObject);		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 2500);
		});
	};	
	// We should statically set the first image
	navigate();	
	// Start playing the animation
	if( photos.length > 1 ){
		interval = setInterval(function() {
			navigate();
		}, slideshowSpeed);
	}	
	
	$(".page-rivendell-suite .image1").click(function(){ navigate(2); });
	$(".page-rivendell-suite .image2").click(function(){ navigate(4); });
	$(".page-rivendell-suite .image3").click(function(){ navigate(6); });	
	$(".page-shire-suite .image1").click(function(){ navigate(1); });
	$(".page-shire-suite .image2").click(function(){ navigate(3); });
	$(".page-shire-suite .image3").click(function(){ navigate(6); });
	$(".page-fanghorn-suite .image1").click(function(){ navigate(1); });
	$(".page-fanghorn-suite .image2").click(function(){ navigate(5); });
	$(".page-fanghorn-suite .image3").click(function(){ navigate(3); });
	
	// jquery scrollpane
	$(".scrollable").jScrollPane({showArrows:true, verticalDragMinHeight:25, verticalDragMaxHeight:25, scrollbarWidth:25});

    if (document.images)
    {
      preload_image_object = new Image();
      // set image url
      image_url = new Array();
	  
	  image_url[0] = "/wp-content/themes/manawaridge/images/bg-home.jpg";	
      image_url[1] = "/wp-content/themes/manawaridge/images/bg-spa.jpg";
      image_url[2] = "/wp-content/themes/manawaridge/images/bg-porch.jpg";
	  image_url[3] = "/wp-content/themes/manawaridge/images/bg-greenlandscape.jpg";
	  image_url[4] = "/wp-content/themes/manawaridge/images/bg-door.jpg";
	  image_url[5] = "/wp-content/themes/manawaridge/images/bg-fireplace.jpg";
      image_url[6] = "/wp-content/themes/manawaridge/images/shire7.jpg";
	  image_url[7] = "/wp-content/themes/manawaridge/images/bg-greenlandscape2.jpg";
	  image_url[8] = "/wp-content/themes/manawaridge/images/bg-horse.jpg";
	  image_url[9] = "/wp-content/themes/manawaridge/images/bg-kitchen.jpg";
      image_url[10] = "/wp-content/themes/manawaridge/images/bg-livingroom.jpg";
      image_url[11] = "/wp-content/themes/manawaridge/images/bg-beach.jpg";
      image_url[12] = "/wp-content/themes/manawaridge/images/bg-roof.jpg";
      image_url[13] = "/wp-content/themes/manawaridge/images/bg-bbq.jpg"; 
      image_url[14] = "/wp-content/themes/manawaridge/images/bg-sunset.jpg";
      image_url[15] = "/wp-content/themes/manawaridge/images/fanghorn1.jpg";
      image_url[16] = "/wp-content/themes/manawaridge/images/fanghorn2.jpg";
      image_url[17] = "/wp-content/themes/manawaridge/images/fanghorn3.jpg";
      image_url[18] = "/wp-content/themes/manawaridge/images/fanghorn4.jpg";
      image_url[19] = "/wp-content/themes/manawaridge/images/fanghorn5.jpg";
      image_url[20] = "/wp-content/themes/manawaridge/images/rivendell1.jpg";
      image_url[21] = "/wp-content/themes/manawaridge/images/rivendell2.jpg";
      image_url[22] = "/wp-content/themes/manawaridge/images/rivendell3.jpg";
      image_url[23] = "/wp-content/themes/manawaridge/images/rivendell4.jpg";
      image_url[24] = "/wp-content/themes/manawaridge/images/rivendell5.jpg";
      image_url[25] = "/wp-content/themes/manawaridge/images/rivendell6.jpg";
      image_url[26] = "/wp-content/themes/manawaridge/images/rivendell7.jpg";
      image_url[27] = "/wp-content/themes/manawaridge/images/shire1.jpg";
      image_url[28] = "/wp-content/themes/manawaridge/images/shire2.jpg";
      image_url[29] = "/wp-content/themes/manawaridge/images/shire3.jpg";
      image_url[30] = "/wp-content/themes/manawaridge/images/shire4.jpg";
      image_url[31] = "/wp-content/themes/manawaridge/images/shire5.jpg";
      image_url[32] = "/wp-content/themes/manawaridge/images/shire6.jpg";

       var i = 0;
       for(i=0; i<=3; i++) 
         preload_image_object.src = image_url[i];
    }
});
