function initAll() {
	initMenu();
	initBanner();
	initInputField();
	initTabs();
	initValidate();
}

function initMenu() {
	// setta voce attuale del menu
	if (!page[0]) return;
	// aggiuntas per nascondere testo certificazione in sezione AZIENDA
	if (page[0] == 'azienda') $('#header .certificazione span').show();
	$('#menu a').each(function(){
				if ($(this).hasClass(page[0])) {
					$(this).addClass('active');
				}
			});
	// setta voce attuale del sotto menu
	$('.menu-sezione a').each(function(){
				if ($(this).hasClass(page[1])) $(this).addClass('active');
			});
}

function initBanner() {
	var imgs = $('#banner .slide .item');
	if (imgs.length == 1) return;
	imgs.css({display: 'none'});
	
	var obj = '<div id="slidetabs" style="display:none">'
	for (i=0; i<imgs.length; i++) {
		obj += '<span>&nbsp;</span>';
	}
	obj += '</div>';
	$('#banner').prepend(obj);
	
	$("#slidetabs").tabs(imgs, {
	
		// enable "cross-fading" effect
		effect: 'fade',
		fadeOutSpeed: 800,
		fadeInSpeed: 800,
		// start from the beginning after the last tab
		rotate: true
		
		// use the slideshow plugin. It accepts its own configuration
	}).slideshow({
				autoplay: true,
				interval: 7000
			});
}

function initInputField() {
		var i = document.createElement('input');
		if('placeholder' in i) return;

		$("input[placeholder]").each(function(){
		if($(this).val() == ""){
			$(this).addClass('hasplaceholder');
			$(this).val($(this).attr("placeholder"));
			$(this).focus(function(){
				if($(this).val()==$(this).attr("placeholder")) $(this).val("");
				$(this).removeClass('hasplaceholder');
			});
			$(this).blur(function(){
				if($(this).val()==""){
					$(this).addClass('hasplaceholder');
					$(this).val($(this).attr("placeholder"));
				}
			});
		}
	});
	
	$('form').submit(function(evt){
		$('input[placeholder]').each(function(){
			if($(this).attr("placeholder") == $(this).val()) {$(this).val('');}
		});
	});
}

function initTabs() {
//	$('.tabs a').append('<span></span>');
	$(".tabs").tabs(".panes > div");
	
	// Accordion per Storia
	if($('.timeline').length > 0) {
		var p = $(".timeline p").hide();
		$('.timeline h4.titoletto').css({cursor: 'pointer'});
		$(".timeline").tabs(p, {tabs: 'h4.titoletto', effect: 'slide'});
	}
	
	// Accordion per prodotto
	if($('.on-off').length > 0) {
		var t = $('.on-off h4.articolo').css({cursor: 'pointer'});
		$('.on-off h4.articolo + div').hide();
		$('.on-off h4.articolo:first-child').next('div').show();
		t.click(function() {
			$(this).next('div').slideToggle();
		})
	}
	/*
	var p = $(".accordion h4.articolo + div").hide();
	$('.accordion h4.articolo').css({cursor: 'pointer'});
	$(".accordion").tabs(p, {tabs: 'h4.articolo', effect: 'slide'});
	*/
	
	
	$(".scrollable").scrollable({keyboard: false});
}

function initValidate() {
	var form1opt = { 
		url:     '/inc/asp/it/mail-contatti-invio.asp', 
		success: function(data) { 
			$('#page-contacts :input, #footer-contacts :input')
			 .not(':button, :submit, :reset, :hidden')
			 .val('')
			 .removeAttr('checked')
			 .removeAttr('selected');
			$.fancybox(data);
		} 
	}; 
	
	$("#footer-contacts").validate({
		rules: {
			email: {
				required: true,
				email: true
			},
			azienda: {
				required: true
			},
			nome: {
				required: true
			}
		},
		messages: {
			email: "scrivere il proprio indirizzo e-mail",
			azienda: "scrivere la ragione sociale dell'azienda",
			nome: "scrivere il proprio nome e cognome"
		},
		errorPlacement: function(error, element) {
			if (element.attr("type") == "checkbox") {
				error.insertAfter( element.next("br") );
			} else {
				error.appendTo( element.prev() );
			}
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit(form1opt);
		}
 });
		
	$("#page-contacts").validate({
		rules: {
			email: {
				required: true,
				email: true
			},
			nome: {
				required: true
			}
		},
		errorPlacement: function(error, element) {
			if (element.attr("type") == "checkbox") {
				error.insertAfter( element.next("br") );
			} else {
				error.appendTo( element.prev() );
			}
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit(form1opt);
		}
 });
}

$(document).ready(initAll);

