/**
 * @author dvian
 */
$(document).ready(function(){
    /*$('h1').each(function(){
        $testo = $(this).text();
        $alt = $(this).height();
        $(this).before('<div class="tit_wrap" style="height:' + $alt + 'px"><div class="titsopra">' + $testo + '</div><div class="titsotto">' + $testo + '</div></div>');
        $(this).remove();
    })
    Cufon.replace('.titsopra', {
        color: '-linear-gradient(#fafafa, #c8c8c8)'
    });
    Cufon.replace('.titsotto', {
        color: '#333'
    });*/
    Cufon.replace('h1', {
        color: '-linear-gradient(#fafafa, #c8c8c8)', 
        textShadow: '#333 2px 2px'
    });
    $("#menu ul").css('display', 'none').addClass('submenu');
    $("#menu li:has(ul)").addClass('celho');
    
    $("#menu li").hover(function(){
        $("ul", this).fadeIn(400);
        $(this).addClass('over');
    }, function(){
        $("ul", this).fadeOut(400);
        $(this).removeClass('over');
    });
    // termini privacy
    $('span.terms').each(function(){
        $privacy = "Ai sensi del D.Lgs. 196/2003 si precisa che le informazioni inviate sono riservate ed a uso esclusivo del destinatario. Qualora il messaggio in parola Le fosse pervenuto per errore, La invitiamo ad eliminarlo senza copiarlo e a non inoltrarlo a terzi, dandocene gentilmente comunicazione. Grazie.";
        $css = "#termini{background:#fff;width:375px;height:108px;line-height:140%;padding:10px;color:#333}.close{color:#c81a11;text-decoration:none;font-weight:bold;text-transform:uppercase;float:right;width:10px}.window,#mask{position:absolute;left:0;top:0;display:none}.window{width:440px;height:200px;z-index:9999;padding:20px}#mask{background:#000;z-index:9000}";
        $('head').append('<style>' + $css + '</style>');
        $('body').append('<div id="termini" class="window"><a href="#" class="close">x</a>' + $privacy + '</div><div id="mask"></div>');
    });
    $('.terms').click(function(e){
        var id = '#' + $(this).attr('name');
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
        $('#mask').css({
            'width': maskWidth,
            'height': maskHeight
        });
        $('#mask').fadeIn(750);
        $('#mask').fadeTo(250, 0.8);
        var winH = $(window).height();
        var winW = $(window).width();
        $(id).css('top', winH / 2 - $(id).height() / 2);
        $(id).css('left', winW / 2 - $(id).width() / 2);
        $(id).fadeIn(1000);
    });
    $('.window .close').click(function(e){
        //Cancel the link behavior
        e.preventDefault();
        $('#mask').hide();
        $('.window').hide();
    });
    $('#mask').click(function(){
        $(this).hide();
        $('.window').hide();
    });
});
/**
 * Image Cycle
 */
//Settings
var faderSettings = {
	timing: 3000,
	fadeSpeed: 300,
	numberOfImages: 2,
	imagePrefix: "home",
	imageSuffix: ".png",
	imageDirectory: "slide/"
};
var displayImage = function(displayImage){
	var imageURL = faderSettings.imageDirectory + displayImage;
	$("#imagerota").fadeOut(faderSettings.fadeSpeed, function(){
		$(this).css({
			'backgroundImage': 'url('+ imageURL + ')'
		}).fadeIn(faderSettings.fadeSpeed);
	});
};
function outer(){
	var a = 1;
	function inner(){
		if(a==faderSettings.numberOfImages){
			a = 1;
		} else {a++;}
		var imageNeeded = faderSettings.imagePrefix + a + faderSettings.imageSuffix;
		displayImage(imageNeeded);
	}
	return inner;
}
var imageFade = outer();
var cycleMe = setInterval(imageFade, faderSettings.timing);
