function changeImg(next){
    $(".active_element").fadeOut("slow", function(){
        $(".active_element").removeClass('active_element');
        //console.log('next is:'+next);
        $("#" + next).fadeIn("slow");
        if (bkg_element[next] != '') {
            $("#body_page").css({
                'background-color': '#fff',
                'background-image': 'url(' + bkg_element[next] + ')',
                'backgroundPosition': 'top center',
                'backgroundRepeat': 'repeat-x'
            });
        }
        $("#" + next).addClass('active_element');
        
    });
}

function nextImg(){
    var active = parseInt($(".active_element").attr("id"));
    if (active == num_element) {
        var next = 1;
    }
    else 
        var next = active + 1;
    changeImg(next);
}

function prevImg(){
    var active = parseInt($(".active_element").attr("id"));
    
    if (active == 1) {
        var prev = num_element;
    }
    else 
        var prev = active - 1;
    
    changeImg(prev);
}

function toggleMenu(action, que) {
    que = que || '';
    if (action == 'show') {
        $('#nav').animate({
            'bottom': '-1px'
        }, 'fast');
    }
    else 
        if (action == 'hide' && que == 'que') {
            setTimeout(function(){
                $('#nav').animate({
                    'bottom': '-43px'
                }, 'medium');
            }, 1000);
        }
        else 
            if (action == 'hide' && que != 'que') {
                $('#nav').animate({
                    'bottom': '-43px'
                }, 'medium');
            }
    
}

function creaGallery(){
    $('#slide img:not(:first)').each(function(){
        var path = $(this).attr('src');
        var title = $(this).attr('title');
        var ids = $(this).attr('id');
        $('#gallery').append('<div class="thumbgal" rel="' + ids + '"><img src="' + path + '" width="260" /><div class="sfumel"><span class="titolo" id="tit' + ids + '">' + title + '</span></div></div>');
        
    });
}

$(document).ready(function(){
    $('#slide img:not(:first)').hide();
    $('#slide img, #nav_next').click(function(){
        nextImg();
    })
    $('#nav_prev').click(function(){
        prevImg();
    })
    $('#nav').hover(function(){
        toggleMenu('show');
    }, function(){
        toggleMenu('hide', 'que');
    });
    $('#coltuecscion').hover(function(){
        toggleMenu('hide');
    });
    $('#nav_gal').click(function(){
        var tops = $(window).scrollTop();
        $("#loader").css({
            backgroundImage: 'none',
            top: tops + 'px'
        }).fadeIn("medium", function(){
            $("#gallery").css({
                top: tops + 'px'
            }).show();
            $('body').css({
                overflow: 'hidden'
            });
            $(this).add("#gallery").click(function(){
                $("#loader, #gallery").fadeOut("medium");
                $('body').css({
                    overflow: 'auto'
                });
            })
        });
        
    });
    
    $('.thumbgal').live("click", function(){
        var ids = $(this).attr('rel');
        $('#gallery').fadeOut("fast", function(){
            $("#loader").fadeOut("fast", changeImg(ids));
            $('body').css({
                overflow: 'auto'
            });
        });
    });
});
$(window).load(function(){
    $("#loader, #gallery").hide();
    creaGallery();
    $('.thumbgal').mouseenter(function(){
        $(this).find('div.sfumel').show();
    }).mouseleave(function(){
        $(this).find('div.sfumel').hide();
    });
    if (jQuery.browser.msie && parseFloat(jQuery.browser.version) < 7) {
        messaggio = '<div id="stopfuckingie6">Il programma che state utilizzando per navigare &egrave; obsoleto. Per aumentare la sicurezza durante la navigazione e le funzionalit&agrave; vi consigliamo di aggiornare il vostro browser. <br />Passate gratuitamente a <a href="http://www.microsoft.com/italy/windows/products/winfamily/ie/default.mspx" target="_blank">Internet Explorer 8</a>, o <a href="http://www.mozilla-europe.org/it/firefox/" target="_blank">Firefox</a>, <a href="http://www.apple.com/it/safari/download/" target="_blank">Safari</a>, <a href="http://www.google.com/chrome/" target="_blank">Chrome</a>, <a href="http://www.opera.com/" target="_blank">Opera</a>.';
        $("body").prepend(messaggio);
        $('#stopfuckingie6').css({
            backgroundColor: '#f00',
            color: '#fff',
            fontFamily: 'Arial,sans-serif',
            position: 'absolute',
            padding: '8px',
            fontSize: '18px',
            borderBottom: 'solid 10px #f60',
            top: '0px',
            left: '0px',
            zIndex: '100'
        })
    };
    
    });
