// JavaScript Document

//----------------------------------------------------------------------------//
// JQUERY SCRIPTED BY www.SERVUSTUO.com
// FOR http://www.italcomsrl.com/
// Contact me in English or Italian at servus@servustuo.com
//
// ENG ---
// Please let me know if you use all or parts of this code, I'd be really
// happy to hear that my work is appreciated and useful :)
//
// ITA ---
// Per favore contattatemi se usate tutto o una parte di questo codice,
// sarei felice di sentire che il mio lavoro e' apprezzato ed utilizzato :)
//----------------------------------------------------------------------------//


// FUNCTIONS ------------------------------------------------
function alignElementVertically(targetToAlign){
	var containerHeight = jQuery(targetToAlign).parent().outerHeight(true);
	var elementHeight = jQuery(targetToAlign).outerHeight(true);
	var newHeight = (containerHeight - elementHeight) / 2;
	jQuery(targetToAlign).css("margin-top",newHeight);
}

function preloadImages(imageContainer){
	var imageContainer = $(imageContainer);
	jQuery(imageContainer).css('display', 'none');
	var loading = $('<div class="loader"></div>').appendTo(imageContainer.parent());
	// Align the preloader vertically 
	alignElementVertically(loading);
	
	var interval = setInterval(function(){
		var images = imageContainer.find('img');
		var completed = 0;
		images.each(function(){
			if (this.complete) completed++;
		});
		if (completed == images.length){
			clearInterval(interval);
			setTimeout(function(){
				loading.fadeOut("fast", function(){
					loading.remove();
					// Conditionals
					if(imageContainer.attr("class") == "production_element"){
						jQuery(imageContainer[0]).fadeIn("slow", function(){
							scrollProduction(imageContainer[0], imageContainer[0], 3000);
						});
					}
					if(imageContainer.attr("class") == "products_element"){
						jQuery(imageContainer[0]).fadeIn("slow", function(){
							scrollProducts(imageContainer[0], imageContainer[0], 3500);
						});;
					}
					if(imageContainer.attr("class") != "production_element" && imageContainer.attr("class") != "products_element" ){
						jQuery(imageContainer).fadeIn("slow");
					}
				});
			}, 1000);
		}
	},100);
}

function scrollProduction(element, firstElement, time){
	var currentElement = element;
	var currentParent = $(currentElement).parent();
	var time = time;
	jQuery(currentElement).delay(time).fadeOut("fast", function(){
		currentElement = $(currentElement).next();
		if(currentElement.attr("class") != "production_element"){
			currentElement = firstElement;
		}
		jQuery(currentElement).fadeIn("slow", function(){
			scrollProduction(currentElement, firstElement, time);
		});
	});
}

function scrollProducts(element, firstElement, time){
	var currentElement = element;
	var currentParent = $(currentElement).parent();
	var time = time;
	jQuery(currentElement).delay(time).fadeOut("slow", function(){
		currentElement = $(currentElement).next();
		if(currentElement.attr("class") != "products_element"){
			currentElement = firstElement;
		}
		jQuery(currentElement).fadeIn("fast", function(){
			scrollProducts(currentElement, firstElement, time);
		});
	});
}

function scrollBanner(element, firstElement, time){
	var currentElement = element;
	var currentParent = $(currentElement).parent();
	var time = time;
	jQuery(currentElement).delay(time).fadeOut("fast", function(){
		currentElement = $(currentElement).next();
		if(currentElement.attr("class") != "banner_element"){
			currentElement = firstElement;
		}
		jQuery(currentElement).slideDown("fast", function(){
			scrollBanner(currentElement, firstElement, time);
		});
	});
}


// READY ----------------------------------------------------
jQuery(document).ready(function () {
	// HIDE ELEMENTS FOR JQUERY ANIMATION
	jQuery('#panel_1').css('display', 'none');
	jQuery('#enter').css('display', 'none');
	jQuery('#titles').css('display', 'none');
	jQuery('#title_1').css('display', 'none');
	jQuery('#title_2').css('display', 'none');
	jQuery('#production_scroll').css('display', 'none');
	jQuery('#products_scroll').css('display', 'none');
	jQuery('#slogan').css('display', 'none');
	
	
	// INTRO ANIMATION
	jQuery('#panel_1').delay(400).fadeIn("slow", function(){
		jQuery('#panel_1').delay(1200).fadeOut("slow", function(){
			jQuery('#panel_2').fadeIn("slow", function(){
				jQuery('#panel_2').delay(1000).fadeOut("slow", function(){
					jQuery('#panel_3').fadeIn("slow", function(){
						// DISPLAY BUG FIX
							alignElementVertically(".banner_element");
							jQuery('.banner_element').css('display', 'none');
							jQuery('.banner_element').css('opacity', '1');
						// ---------------
						jQuery('.banner_element:first').fadeIn("fast");
						scrollBanner(jQuery('.banner_element:first'), jQuery('.banner_element:first'), 3000);
						jQuery('#titles').delay(100).fadeIn("fast");
						jQuery('#title_1').delay(200).fadeIn("fast");
						jQuery('#title_2').delay(300).fadeIn("fast");
						jQuery('#production_scroll').delay(400).fadeIn("fast");
						jQuery('#products_scroll').delay(500).fadeIn("fast");
						preloadImages(".production_element");
						preloadImages(".products_element");
						jQuery('#enter').delay(700).fadeIn("fast");
					});					
				});
			});
		});
	});
	
	// ENTER ROLLOVER EFFECT
	jQuery('#welcome').mouseenter(function(){
		jQuery('#slogan').fadeIn("fast");
	}).mouseleave(function(){
		jQuery('#slogan').fadeOut("fast");
	});
	
	
	
	
});


// ON LOAD --------------------------------------------------
jQuery(window).bind("load", function() {
	
});


// ON RESIZE FINESTRA ---------------------------------------
jQuery(window).resize(function() {
	
});


