var subMenuTimer = -1;
var langMenuTimer = -1;
$().ready(function() {

    $.backstretch("/media/home-image/"+ HomeImage);
    adjustContentHeight();

    $(window).resize(adjustContentHeight);

    $('#hightlight_opener_link').toggle(function() {
        ToggleHightlight();
    }, function() {
        ToggleHightlight();
    });

    $(".withchild").click(function(event){

        if (subMenuTimer != -1)
            clearTimeout(subMenuTimer);

        closeSubMenu();

        var relMenu = $(this).attr("rel");
        var altezza = 0;

        $("#submenu_" + relMenu).show();
        altezza = $("#submenu_" + relMenu).height() + 91;

        $("#submenu_background_" + relMenu).css("height",altezza + 'px');
        $("#submenu_background_" + relMenu).show();

        event.preventDefault();
    });

    $(".open_submenu").hover(function(){

        if (subMenuTimer != -1)
            clearTimeout(subMenuTimer);

        closeSubMenu();

        var relMenu = $(this).attr("rel");
        var altezza = 0;

        $("#submenu_" + relMenu).show();
        altezza = $("#submenu_" + relMenu).height() + 91;

        $("#submenu_background_" + relMenu).css("height",altezza + 'px');
        $("#submenu_background_" + relMenu).show();

    }, function(){
        subMenuTimer = setTimeout("closeSubMenu()",200);
    });

    $(".submenu").hover(function(){
        clearTimeout(subMenuTimer);
    }, function(){
        subMenuTimer = setTimeout("closeSubMenu()",10);
    });

    $(".header_img_social").hover(function(){
        var img = $(this).attr("src");
        $(this).attr("src",img.replace("-off","-on"))
    },
    function(){
        var img = $(this).attr("src");
        $(this).attr("src",img.replace("-on","-off"))
    });

    $("#languages-list").hover(function(){
        clearTimeout(langMenuTimer);
    }, function(){
        langMenuTimer = setTimeout("closeSelectLanguage()",100);
    });

    //scrolling contenuti
    setContentScroll();

    var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
    var baseHref = '';
    if ($('base').attr('href') != undefined)
        baseHref = $('base').attr('href');
    $('a').each(function() {
        var href = $(this).attr('href');
        if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
            $(this).click(function() {
                var extLink = href.replace(/^https?\:\/\//i, '');
                _gaq.push(['_trackEvent', 'External', 'Click', extLink]);
                if ($(this).attr('target') != undefined && $(this).attr('target').toLowerCase() != '_blank') {
                    setTimeout(function() {location.href = href;}, 200);
                    return false;
                }
            });
        }
        else if (href && href.match(/^mailto\:/i)) {
            $(this).click(function() {
                var mailLink = href.replace(/^mailto\:/i, '');
                _gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
            });
        }
        else if (href && href.match(filetypes)) {
            $(this).click(function() {
                var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                var filePath = href;
                _gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);
                if ($(this).attr('target') != undefined && $(this).attr('target').toLowerCase() != '_blank') {
                    setTimeout(function() {location.href = baseHref + href;}, 200);
                    return false;
                }
            });
        }
    });
});

function setContentScroll()
{
    $('.scroll-pane-standard').each(
        function()
        {
            $(this).jScrollPane({showArrows: true, autoReinitialise: true});
            var api = $(this).data('jsp');
            var throttleTimeout;
            $(window).bind(
                    'resize',
                    function()
                    {
                        if ($.browser.msie) {
                            // IE fires multiple resize events while you are dragging the browser window which
                            // causes it to crash if you try to update the scrollpane on every one. So we need
                            // to throttle it to fire a maximum of once every 50 milliseconds...
                            if (!throttleTimeout) {
                                throttleTimeout = setTimeout(
                                    function()
                                    {
                                        api.reinitialise();
                                        throttleTimeout = null;
                                    },
                                    50
                                );
                            }
                        } else {
                            api.reinitialise();
                        }
                    }
                );
        }
    );
}

function closeSubMenu()
{
    $(".submenu:visible").hide();
    $(".submenu_background:visible").hide();
}

function ToggleHightlight()
{
    $('#hightlight_background').toggle();
    $('#hightlight').toggle();

    if ($('#hightlight:visible').length == 0)
    {

        $('#freccia_highlights').attr("src","/images/freccia-up.png");

        $('#hightlight_detail_background').hide();
        $('.hightlight_detail').hide();
        $('.box_hightlight_title a').removeClass("selected");
    }
    else
    {
        $('#freccia_highlights').attr("src","/images/freccia-down.png");
    }
}

function ShowHightlight()
{
    $('#hightlight_background').show();
    $('#hightlight').show();

    $('#freccia_highlights').attr("src","/images/freccia-down.png");
}

function HideHightlight()
{
    $('#hightlight_background').hide();
    $('#hightlight').hide();

    $('#freccia_highlights').attr("src","/images/freccia-up.png");

    $('#hightlight_detail_background').hide();
    $('.hightlight_detail').hide();
    $('.box_hightlight_title a').removeClass("selected");
}

function ShowHightlightDetail(id)
{
    $('#hightlight_detail_background').show();
    if ($('#dhl' + id + ':visible').length == 0)
    {
        $('.hightlight_detail').hide();
        $('#dhl' + id).show();

        $('.box_hightlight_title a').removeClass("selected");
        $('#hl' + id).children("a").addClass("selected");
    }
}

function HideHightlightDetail()
{
    $('.hightlight_detail').hide();
    $('#hightlight_detail_background').hide();
    $('.box_hightlight_title a').removeClass("selected");
}

function adjustContentHeight(fn)
{
    try {
        var h = $(window).height() - 57;

        if ($.browser.msie && parseInt($.browser.version) < 8)
            h = h +24;

        $("#centered").css("height",h +'px');

        h = h - 160;
        if ($.browser.msie && parseInt($.browser.version) < 8)
            h = h - 24;

        $("#content").css("height",h +'px');
        $("#contenitore-collection").css("height",h +'px');
        $("#mappa-google").css("height",h +'px');

        h = h - 250;

        if (h < 250)
            h = 250;

        $("#contenitore-testi").css("height",h +'px');

        h = h - 100;

        if (h < 150)
            h = 150;
        $("#contenitore-testi-piccolo").css("height",h +'px');

    } catch(err) {
        // IE7 seems to trigger _adjustBG before the image is loaded.
        // This try/catch block is a hack to let it fail gracefully.
    }

    // Executed the passed in function, if necessary
    if (typeof fn == "function") fn();

}

(function($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)

