/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/

// 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 = [ 
			  
			  
		{
		"title" : "Nueva Era",
		"image" : "nuevaerac.jpg",
		"url" : "http://nuevaerashop.com/images/nuevaerac.jpg",
		"enlace": "http://nuevaerashop.com/snow/snow.php?option=Tablas",
		"firstline" : "Bienvenido a Nueva Era!",
		"secondline" : "ver bananas",
		"title2" : "Compra tu banana",
		"enlace2" : "http://nuevaerashop.com/snow/snow.php?option=Tablas"
	},{
		"title" : "Nike Zoom Kaiju QS",
		"image" : "Nike-Zoom-Kaiju-QS-promo.jpg",
		"url" : "http://nuevaerashop.com/images/Nike-Zoom-Kaiju-QS-promo.jpg",
		"enlace": "http://www.nuevaerashop.com/mostrar/mostrar.php?option=2080",
		"firstline" : "Nike Zoom Kaiju QS",
		"secondline" : "Edición exclusiva",
		"title2" : "Ver aquí",
		"enlace2" : "http://www.nuevaerashop.com/mostrar/mostrar.php?option=2080"
	},{
		"title" : "VOLCOM",
		"image" : "bannervolcom.jpg",
		"url" : "http://nuevaerashop.com/images/bannervolcom.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=VOLCOM",
		"firstline" : "VOLCOM",
		"secondline" : "Su nueva temporada",
		"title2" : "Ver aquí",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=VOLCOM"
	},{
		"title" : "Habitat",
		"image" : "bannerhabitat.jpg",
		"url" : "http://nuevaerashop.com/images/bannerhabitat.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=Habitat",
		"firstline" : "Habitat",
		"secondline" : "Sus nuevas zapas",
		"title2" : "Ver zapas",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=Habitat"
	},{
		"title" : "Koston Shoe",
		"image" : "KOSTON_SHOE_SPREAD.jpg",
		"url" : "http://nuevaerashop.com/images/KOSTON_SHOE_SPREAD.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=NIKE%20SB",
		"firstline" : "Koston Shoe",
		"secondline" : "Sus nuevas zapas",
		"title2" : "Ver zapas",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=NIKE%20SB"
	},{
		"title" : "Eric Koston ",
		"image" : "Eric_Koston_Wall.jpg",
		"url" : "http://nuevaerashop.com/images/Eric_Koston_Wall.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=NIKE%20SB",
		"firstline" : "Las nuevas zapas de",
		"secondline" : "ERIC KOSTON",
		"title2" : "Ver zapas",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=NIKE%20SB"
	},{
		"title" : "Etnies",
		"image" : "banneretnies4.jpg",
		"url" : "http://nuevaerashop.com/images/banneretnies4.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=ETNIES",
		"firstline" : "",
		"secondline" : "",
		"title2" : "",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=ETNIES"
	},{
		"title" : "És",
		"image" : "banneres2.jpg",
		"url" : "http://nuevaerashop.com/images/banneres2.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=ES",
		"firstline" : "",
		"secondline" : "",
		"title2" : "",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=ES"
	},{
		"title" : "Lakai",
		"image" : "lakaibanner1.jpg",
		"url" : "http://nuevaerashop.com/images/lakaibanner1.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=LAKAI",
		"firstline" : "las zapas del momento",
		"secondline" : "Lakai",
		"title2" : "Ver zapas Lakai",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=LAKAI"
	},{
		"title" : "Technine",
		"image" : "techninebanner1.jpg",
		"url" : "http://nuevaerashop.com/images/techninebanner1.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=TECHNINE",
		"firstline" : "",
		"secondline" : "",
		"title2" : "",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=TECHNINE"
	},{
		"title" : "Technine",
		"image" : "techninebanner2.jpg",
		"url" : "http://nuevaerashop.com/images/techninebanner2.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=TECHNINE",
		"firstline" : "",
		"secondline" : "",
		"title2" : "",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=TECHNINE"
	},{
		"title" : "Tablas Lib Tech",
		"image" : "libtech1banner.jpg",
		"url" : "http://nuevaerashop.com/images/libtech1banner.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=LIBTECHNOLOGIES",
		"firstline" : "las nuevas tablas",
		"secondline" : "Lib Tech",
		"title2" : "Ver tablas Lib Tech",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=LIBTECHNOLOGIES"
	},{
		"title" : "Danny Kass de GNU",
		"image" : "bannerDannyKassc.jpg",
		"url" : "http://nuevaerashop.com/images/bannerDannyKassc.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=GNU",
		"firstline" : "las tablas más piratas",
		"secondline" : "Danny Kass",
		"title2" : "Ver estás tablas GNU",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=GNU"
	},{
		"title" : "GNU",
		"image" : "bannerGNU2.jpg",
		"url" : "http://nuevaerashop.com/images/bannerGNU2.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=GNU",
		"firstline" : "Nuevas tablas",
		"secondline" : "GNU",
		"title2" : "Ver estás tablas GNU",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=GNU"
	},{
		"title" : "bannerGNU",
		"image" : "bannerGNUc.jpg",
		"url" : "http://nuevaerashop.com/images/bannerGNUc.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=GNU",
		"firstline" : "Nueva temporada 2011",
		"secondline" : "Las nuevas Bstreet",
		"title2" : "Ver estás tablas",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=GNU"
	},{
		"title" : "Lib Tech 2011",
		"image" : "bannerlibtechc.jpg",
		"url" : "http://nuevaerashop.com/images/bannerlibtechc.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=LIBTECHNOLOGIES",
		"firstline" : "Las mejores tablas",
		"secondline" : "Lib Tech",
		"title2" : "Ver bananas",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=LIBTECHNOLOGIES"
	}, {
		"title" : "Nike SB Paul Rodriguez IV",
		"image" : "bannerNikeSBPaulRodriguez4c.jpg",
		"url" : "http://nuevaerashop.com/Nueva2/bannerNikeSBPaulRodriguez4c.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=NIKE%20SB",
		"firstline" : "Paul Rodriguez IV",
		"secondline" : "Nike SB",
		"title2" : "Ver zapas",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=NIKE%20SB"
	}, {
		"title" : "Neff ",
		"image" : "BannerNeffc.jpg",
		"url" : "http://nuevaerashop.com/images/BannerNeffc.jpg",
		"enlace": "http://www.nuevaerashop.com/marcas/marcas.php?option=NEFF",
		"firstline" : "Nueva temporada de:",
		"secondline" : "NEFF",
		"title2" : "Ver su ropa",
		"enlace2" : "http://www.nuevaerashop.com/marcas/marcas.php?option=NEFF"
	}
];



$(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(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
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$("#headertxt").css({"display" : "none"});
		
		// Set the new header text
		$("#firstline").html(photoObject.firstline);
		$("#secondline")
			.attr("href", photoObject.enlace)
			.html(photoObject.secondline);
		$("#pictureduri")
			.attr("href", photoObject.enlace2)
			.html(photoObject.title2);
		
		
		// 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;
			}, 500);
		});
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		$("#control").css({ "background-image" : "url(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);
	
});
