$(document).ready(function () {

    // Main nav glow effect
    $('.main-navigation ul li a').each(function () {
        $(this).bind('mouseover focus', function () {
            pos = $(this).position();
            wid = $(this).width();
            currPos = Math.round(pos.left) + Math.round(wid / 2);
            glowPos = currPos - 75;
            // Prevent horisontal scrollbar
            if (($(document).width() < 1047) && (glowPos > 628)) {
                glowPos = 628;
            }
            $('.main-navigation .glow').css('left', glowPos).show();
        }).bind('mouseout blur', function () {
            $('.main-navigation .glow').hide();
        });
    });

    // Left menu folding
    $('ul.leftmenu li ul').hide();
    $('ul.leftmenu li.current > ul').show();
    $('ul.leftmenu li.parent > span').click(function () {
        var el = $(this).parent();
        if (el.hasClass('expanded')) {
            if (!el.hasClass('current')) {
                el.removeClass('expanded');
                el.find('ul:first').slideUp(500);
            }
        }
        else {
            el.addClass('expanded');
            el.find('ul:first').slideDown(500);
        }
        return false;
    });

    // Box hover effects
    $('.focusblock').hover(function () {
        $(this).find('.popup').stop().animate({ top: '0' }, { queue: false, duration: 500 });
        $(this).find('.focusblockheader').addClass('hover');
    }, function () {
        $(this).find('.popup').stop().animate({ top: '-6.25em' }, { queue: false, duration: 500 });
        $(this).find('.focusblockheader').removeClass('hover');
    });

    // Help Webkit (and some versions of Opera) to respect focus when using skip links
    //EDITED: Runar
    // this was making chrome crash (not showing) content because og error on line targetMainContent.href = "#mainContent";
    //	if($.browser.webkit || $.browser.opera) {
    //		var targetMainContent = document.getElementById("mainContent");
    //		targetMainContent.href = "#mainContent";
    //		targetMainContent.setAttribute("tabindex","0");
    //		$("#jumpToMainContent").click(function(){
    //			$("#mainContent").focus();
    //		});
    //	}

    // Table sorting
    // $('.zebrastripes tr:nth-child(odd)').addClass('odd');
    $('table tbody tr:nth-child(odd)').addClass('odd');

    // Table cell hover
    $('table td').mouseenter(function () {
        $(this).parent().addClass('hover');
    }).mouseleave(function () {
        $(this).parent().removeClass('hover');
    });

    // Tab content
    $('.tabs a').mouseenter(function () {
        $(this).parent().addClass('hover');
    }).mouseleave(function () {
        $(this).parent().removeClass('hover');
    });

    // jQuery Uniform
    $(".filters input").uniform();

    // Tooltips for downloads
    $(".downloads td a").each(function () {
        $(this).bind('mouseover focus', function () {
            var apos = $(this).position();
            var tabLeft = Math.round($('.wide .tabcontent').position().left);
            tipLeft = Math.round(apos.left + 36) - tabLeft;
            if (($(document).width() < 1062) && (tipLeft > 690)) {
                tipLeft = 690;
            }
            var atop = apos.top;
            if ($.browser.msie && $.browser.version.substr(0, 3) < "8") {
                atop = atop - 10;
            }
            var type = $(this).html();
            $('#tooltip').css({
                'top': Math.round(atop) + 'px',
                'left': (tipLeft + tabLeft) + 'px'
            }).find('span').html(type);
            $('#tooltip').show();
        }).bind('mouseout blur', function () {
            $('#tooltip').hide();
        });
    });

    // Browser dependent classes
    if ($.browser.msie) {
        $('body').addClass('msie');
    }
    if ($.browser.webkit) {
        $('body').addClass('webkit');
    }
    if ($.browser.opera) {
        $('body').addClass('opera');
    }

});

