
//------------------------------- Background slideshow ------------------------------//

// Speed of the automatic slideshow
var slideshowSpeed = 6000;

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {

		"image" : "bg1.jpg"

	}, {
		"image" : "bg2.jpg"

	}, {
		"image" : "bg3.jpg"

	}, {
		"image" : "bg4.jpg"

	}, {
		"image" : "bg5.jpg"

	}, {
		"image" : "bg6.jpg"

	}, {
		"image" : "bg7.jpg"

	}
];



$(document).ready(function() {
	

		
	// Backwards navigation
	$("#back").click(function() {
		stopAnimation();
		navigate("back");
	});
	
	// Forward navigation
	$("#next").click(function() {
		stopAnimation();
		navigate("next");
	});
	
	var interval;
	$("#control").toggle(function(){
		stopAnimation();
	}, function() {
		// Change the background image to "pause"
		$(this).css({ "background-image" : "url(http://www.mamylouise.be/images/btn_pause.png)" });
		
		// Show the next image
		navigate("next");
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		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
		$("#bgndimg" + activeContainer).css({
			"background-image" : "url(http://www.mamylouise.be/images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#bgndimg" + currentContainer).fadeOut(2000, function() {
			setTimeout(function() {
				//$("#headertxt").css({"display" : "block"});
				animating = false;
			});
		});
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		$("#control").css({ "background-image" : "url(http://www.mamylouise.be/images/btn_play.png)" });
		
		// Clear the interval
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
	
	
	
	//-------------------------------- Menu animation ------------------------------//
	
	
	//Padding, mouseover
	var padLeft = '20px';
	
	//Default Padding
	var defpadLeft = $('#menu li a').css('paddingLeft');
	
		
	//Animate the LI on mouse over, mouse out
	$('a[rel=panel]').mouseover(function (){
		
		//mouse over LI and look for A element for transition
		$(this).animate( { paddingLeft: padLeft}, { queue:false, duration:100 } )

	}).mouseout(function () {
		
		//Check if A has class "selected" 
	if($(this).hasClass('selected')) {
		
 		//If A has class "selected" look for A element and leave it as it is.
		$(this).animate( { paddingLeft: padLeft}, { queue:false, duration:100 } )
	}
	else {
	//mouse oout LI and look for A element and discard the mouse over transition
		$(this).animate( { paddingLeft: defpadLeft}, { queue:false, duration:100 } )
	}
	
	});	
	
	
	//-----------------------------Accordion Menu----------------------------------//
	
	
	$('p.accordionButton').click(function() {
		$('p.accordionContent').slideUp('normal');	
		$(this).next().slideDown('normal');
	});
	

	//CLOSES ALL DIVS ON PAGE LOAD

	$("p.accordionContent").hide();
	
	
	/*function initMenu() {
  $('p.accordionContent').hide();
  $('p.accordionButton').click(
    function() {
        $(this).next().slideToggle('normal');	
      }
    );
  }
$(document).ready(function() {initMenu();});*/
	
	//------------------------------- Content slider ------------------------------//
	
	//Intro text fade out//
	$('a[rel=panel]').click(function() {
		$('#introtxt').fadeOut(600, function(){ 
    		$(this).remove();
   		});
	});
	
	//Close btn //
	$('a[rel=panel_closer]').click(function() {
	$('#close_btn,#panel').fadeOut(600);
	$('a[rel=panel]').removeClass('selected');
	$('a[rel=panel]').animate( { paddingLeft: defpadLeft}, { queue:false, duration:100 } )
	});
	
	//Get the height of the first item
	//$('#mask').css({'height':$('#panel-1').height()});	
	
	//Calculate the total width - sum of all sub-panels width
	//Width is generated according to the width of #mask * total of sub-panels
	$('#panel').width(parseInt($('#mask').width() * $('#panel div').length));
	
	//Set the sub-panel width according to the #mask width (width of #mask and sub-panel must be same)
	$('#panel div').width($('#mask').width());
	
	//Get all the links with rel as panel
	$('a[rel=panel]').click(function () {
		
		//Display the panels and the close_btn on first click
		$('#panel').fadeIn('slow', function(){});
		$('#close_btn').fadeIn('slow', function(){});
		
		//Get the height of the sub-panel
		//var panelheight = $($(this).attr('href')).height();
		
		//Set class for the selected item
		$('a[rel=panel]').removeClass('selected');
		$('a[rel=panel]').animate( { paddingLeft: defpadLeft}, { queue:false, duration:100 } )
		$(this).addClass('selected');
		$(this).animate( { paddingLeft: padLeft}, { queue:false, duration:100 } )
	
		//Resize the height
		//$('#mask').animate({'height':panelheight},{queue:false, duration:500});			
		
		//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
		$('#mask').scrollTo($(this).attr('href'), 800);		
		
		//Discard the link default behavior
		return false;
	});
	
});
