$(function() {
    init_mainmenu();
    init_search();
    init_infodesk_tabs();
    init_banner_skyscraper();
});
 
function init_mainmenu() {
    var allItems = $("#mainmenu li");
    var firstItem = allItems.first();
    var lastItem = allItems.last();

    if (lastItem.is(":hidden")) {
        firstItem.nextAll(":last").addClass("last");
    }
    
    firstItem.addClass("first");
    lastItem.addClass("last");

    if (firstItem.hasClass("active")) {
        firstItem.addClass("first_active");
    }
    if (lastItem.hasClass("active")) {
        lastItem.addClass("last_active");
    }

    firstItem.mouseenter(function() {
        if ($(this).hasClass('first_active') == false) {
            $(this).addClass('first_hover');
        }
    }).mouseleave(function() {
        $(this).removeClass('first_hover');
    });
    

    lastItem.mouseenter(function() {
        if ($(this).hasClass('last_active') == false) {
            $(this).addClass('last_hover');
        }
    }).mouseleave(function() {
        $(this).removeClass('last_hover');
    });

    allItems.mouseenter(function() {
        if ($(this).hasClass('active') == false) {
            $(this).addClass('hover');
        }
    }).mouseleave(function() {
        $(this).removeClass('hover');
    });

    // remove arrows from non-expandable items
    $("#mainmenu li").each(function(i) {
        if ($(this).find('ul').length == 0) {
            $(this).addClass('non_expandable');
        }
    })
}

function init_search() {
    var input = $("#fulltext_query:first");
    if (input.length == 1) {
        var defaultText = input.attr('title');
        if (defaultText.length > 0) {
            if (input.val() == "") {
                input.val(defaultText);
            }
            // hooks
            input.click(function() {
                if (input.val() == defaultText) {
                    input.val("");
                }
            }).blur(function() {
                if (input.val() == "") {
                    input.val(defaultText);
                }
            });
        }
    }
}

function init_infodesk_tabs() {
    var infodesk = $("#infodesk_tabs");
    if (infodesk.length == 1) {
        infodesk.tabs();
    }
}

function init_banner_skyscraper() {
    globalBannerId = '#banner_skyscraper';
    globalBannerTop = 10;
    globalFooterId = '#layout_footer';
    $(window).scroll(function(){
        var windowTop = $(window).scrollTop();
        var bannerTop = $(globalBannerId).attr('offsetTop');
        if(windowTop > bannerTop) {
            var maxScrollTop = $(globalFooterId).attr('offsetTop') - $(globalBannerId).attr('offsetHeight');
            if($(window).scrollTop() < maxScrollTop) {
                $(globalBannerId).animate({
                    top:$(window).scrollTop()+'px'
                },{
                    queue: false,
                    duration: 350
                });
            } else {
                $(globalBannerId).animate({
                    top:maxScrollTop+'px'
                },{
                    queue: false,
                    duration: 350
                });
            }
        } else if(bannerTop > globalBannerTop) {
            if($(window).scrollTop() < globalBannerTop) {
                $(globalBannerId).animate({
                    top:globalBannerTop+'px'
                },{
                    queue: false,
                    duration: 350
                });
            } else {
                $(globalBannerId).animate({
                    top:$(window).scrollTop()+'px'
                },{
                    queue: false,
                    duration: 350
                });
            }
        }
    });
}
