/*
slideshow.js
	Creates the images slideshow stacking
	other images under the one already
	present in the markup.
By Luca De Angelis
	lucaDOTdeangelisATgmailDOTcom
	For Elettra Progetti e Servizi S.p.A.
*/

var imagesPath, interval, duration, slideshow;

$(document).ready(function(){

	// Settings
	imagesPath = "Img/Sfondi/";
	interval = 4000;
	duration = 2000;
	slideshow = $('#slideshow');

	// Check if there is an image in the slideshow div
	// to prevent loading the script in pages where there are no background images
	if (slideshow.find('img').length) {

		// Global array
		// Inserting the src attribute of the found image in the array
		imgArray = [];

		// Local variable to test the page's name
		var url = window.location.href;

		// Populating the array with the images
		if (url.match(/404/)) {
			imgArray.push("Elicottero.jpg",
				"Piattaforme.jpg");
		} else if (url.match(/Contacts/)) {
			imgArray.push("Telefono.jpg",
				"Filo_telefono.jpg");
		} else if (url.match(/Engineering-and-Procurement/)) {
			imgArray.push("Nave.jpg",
				"Quadro_01.jpg",
				"Ciminiera.jpg",
				"Piattaforme.jpg",
				"Cantiere_180.jpg");
		} else if (url.match(/Field-and-Engineering-Services/)) {
			imgArray.push("Piping.jpg",
				"Piattaforme.jpg",
				"Ciminiera.jpg",
				"Elicottero.jpg",
				"Piattaforma.jpg");
		} else if (url.match(/History/)) {
			imgArray.push("EPS_Lights.jpg");
		} else if (url.match(/Job/)) {
			imgArray.push("Elicottero.jpg",
				"Piattaforme.jpg",
				"Cantiere_180.jpg",
				"Piattaforma.jpg",
				"Sfondo_home_01.jpg",
				"Sfondo_home_02.jpg",
				"Sfondo_home_03.jpg");
		} else if (url.match(/Profile/)) {
			imgArray.push("Elicottero.jpg",
				"Piattaforme.jpg",
				"Cantiere_180.jpg",
				"Piattaforma.jpg",
				"Sfondo_home_01.jpg",
				"Sfondo_home_02.jpg",
				"Sfondo_home_03.jpg");
		} else if (url.match(/References/)) {
			imgArray.push("Elicottero.jpg",
				"Piattaforme.jpg",
				"Cantiere_180.jpg",
				"Piattaforma.jpg",
				"Sfondo_home_01.jpg",
				"Sfondo_home_02.jpg",
				"Sfondo_home_03.jpg");
		} else if (url.match(/Search/)) {
			imgArray.push("Elicottero.jpg",
				"Piattaforme.jpg",
				"Cantiere_180.jpg",
				"Piattaforma.jpg",
				"Sfondo_home_01.jpg",
				"Sfondo_home_02.jpg",
				"Sfondo_home_03.jpg");
		} else {
			// Homepage could be with or without the page name
			imgArray.push("Elicottero.jpg",
				"Piattaforme.jpg",
				"Cantiere_180.jpg",
				"Piattaforma.jpg",
				"Sfondo_home_01.jpg",
				"Sfondo_home_02.jpg",
				"Sfondo_home_03.jpg");
		}

		// Check if the array has been filled and start the slideshow
		// Otherwise show an error alert (just not to forget to fill the array in case of new pages)
		if (imgArray.length) {
			// Inserting the other images into the slideshow div
			$.each(imgArray, function(k, v){
				slideshow.prepend('<img src="' + imagesPath + v + '" alt="" />');
			});
			slideshowTimer();
		} else {
			alert('Error: Slideshow\'s array to fill.\nPlease report this error to the webmaster from the contacts page.');
		}

	}

});

function slideshowTimer(){
	// Timer that calls the slideshowDo() function
	slideshowTimerVar = setTimeout('slideshowDo()', interval);
}

function slideshowDo(){
	// Function that handles the show/hide state of the images
	if (slideshow.find('img:visible').length > 1) {
		// Not at the last image: fading out the last one
		slideshow.find('img:visible').last().fadeOut(duration);
		slideshowTimer();
	} else {
		// Last image: fading in the first and then setting to visible all the others
		slideshow.find('img').last().fadeIn(duration, function(){
			slideshow.find('img:not(:visible)').show();
			slideshowTimer();
		});
	}
}

