// Speed of the automatic slideshow
var slideshowSpeed = 6000;

$(document).ready(function() {
						   
	$("#banner1").click(function() {
		navigate("banner1");
		restart(); 
	});
	
	$("#banner2").click(function() {
		navigate("banner2");
		restart(); 
	});
	
	$("#banner3").click(function() {
		navigate("banner3");
		restart(); 
	});
	
	$("#banner4").click(function() {
		navigate("banner4");
		restart(); 
	});
		
	// Backwards navigation
	$("#back").click(function() {
		navigate("back");
		restart(); 
	});	
	
	// Forward navigation
	$("#next").click(function() {
		navigate("next");
		restart(); 
	});
	
	var interval;
	$("#control").toggle(function(){
		stopAnimation();
	}, function() {
		// Change the background image to "pause"
		$(this).css({ "background-image" : "url(catalog/view/theme/tntspeedshop/image/banner/btn/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;
		}
		
		if(direction == "banner1") {
			currentImg = 1;
		}
		
		if(direction == "banner2") {
			currentImg = 2;
		}
		
		if(direction == "banner3") {
			currentImg = 3;
		}
		
		if(direction == "banner4") {
			currentImg = 4;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		}
		
		if(direction == "back") {
			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, currentImg);
		
	};
	
	
	var currentZindex = 5000;
	var showImage = function(photoObject, currentContainer, activeContainer, currentImg) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		$("#headernav-outer").css({	"z-index" : currentZindex + 1 });
		
		document.getElementById('headerimg1_link').href = photoObject.url;
		document.getElementById('headerimg2_link').href = photoObject.url;
			
		switch(currentImg) // seta em quais banners o 'destaque' ira aparecer
		{
			case 1: document.getElementById('header-destaque').style.display = 'none'; break;
			case 2: document.getElementById('header-destaque').style.display = 'none'; break;
			case 3: document.getElementById('header-destaque').style.display = 'none'; break;
			case 4: document.getElementById('header-destaque').style.display = 'none'; break;
		}
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			
			"background-image" : "url(/image/"+photoObject.image+")",
			"display" : "block",
			"background-position" : "center",
			"z-index" : currentZindex
		});		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				animating = false;
			}, 10);
		});
		
		for (i=1;i<=4;i++){
			if (i == photoObject.id){
				$("#banner"+i).css({"background-image" : "url(catalog/view/theme/tntspeedshop/image/banner/btn/btn_banner"+i+"_sel.png)"});
			}else{
				$("#banner"+i).css({"background-image" : "url(catalog/view/theme/tntspeedshop/image/banner/btn/btn_banner"+i+".png)"});
			}
		}
	
		
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		$("#control").css({ "background-image" : "url(catalog/view/theme/tntspeedshop/image/banner/btn/btn_play.png)" });		
		// Clear the interval
		clearInterval(interval);
	};
	
	var restart = function() {
		clearInterval(interval);
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
		
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});
