(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  jQuery.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

var slideshowSpeed = 10000;

jQuery(document).ready(function() {
	
	if(jQuery(window).width() > 1280){repFonds='1920';}else{repFonds='1280';}	
	
	// Backwards navigation
	jQuery("#back").click(function() {
		stopAnimation();
		navigate("back");
	});
	
	// Forward navigation
	jQuery("#next").click(function() {
		stopAnimation();
		navigate("next");
	});
	
	var interval;
	jQuery("#control").toggle(function(){
		stopAnimation();
	}, function() {
		// Change the background image to "pause"
		jQuery(this).css({ "background-image" : "url(img/template_2011/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
		jQuery("#background").hide();
		jQuery("#background").css({
			"background-image" : "url('/common/img/fonds/"+repFonds+"/" + photoObject.image + "')",
			"background-position":"center top",
			"background-repeat":"no-repeat"
		});
		jQuery("#background").fadeIn(750);
		jQuery(".promo strong.l1").hide().html("<a href=\""+photoObject.url + "\">" +  photoObject.firstline + "</a>");
		jQuery(".promo strong.l2").hide().html("<a href=\""+photoObject.url + "\">" +  photoObject.secondline + "</a>");
		jQuery(".promo strong.l1").delay(500).fadeIn("slow");
        	jQuery(".promo strong.l2").delay(1000).fadeIn("slow");
				
		// Fade out the current container
		// and display the header text when animation is complete
		jQuery("#aheaderimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				jQuery("#headertxt").css({"display" : "block"});
				animating = false;
			}, 500);
		});
		animating = false;
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		jQuery("#control").css({ "background-image" : "url(common/img/template_2011/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);
	
	/*
	repFonds='1920';	
	jQuery.preLoadImages("common/img/fonds/"+repFonds+"/G2010-1023.jpg","common/img/fonds/"+repFonds+"/G2011-0176.jpg","common/img/fonds/"+repFonds+"/G2007-0908.jpg","common/img/fonds/"+repFonds+"/G2010-0902.jpg","common/img/fonds/"+repFonds+"/G2010-0208.jpg","common/img/fonds/"+repFonds+"/G2010-1018.jpg","common/img/fonds/"+repFonds+"/G2010-1022.jpg");
	repFonds='1024';
	jQuery.preLoadImages("common/img/fonds/"+repFonds+"/G2010-1023.jpg","common/img/fonds/"+repFonds+"/G2011-0176.jpg","common/img/fonds/"+repFonds+"/G2010-0121-.jpg");		
	repFonds='1280';
	jQuery.preLoadImages("common/img/fonds/"+repFonds+"/G2010-1023.jpg","common/img/fonds/"+repFonds+"/G2011-0176.jpg","common/img/fonds/"+repFonds+"/G2010-0121-.jpg");
	*/
	
});
