﻿$(document).ready(function() {
    // preload some images
    headerActiveBg = new Image();
    headerActiveBg.src = "/themes/default/images/header_tab_active_bg.jpg";
    headerActiveLeft = new Image();
    headerActiveLeft.src = "/themes/default/images/header_tab_active_left.jpg";
    headerActiveRight = new Image();
    headerActiveRight.src = "/themes/default/images/header_tab_active_right.jpg";
    headerInactiveBg = new Image();
    headerInactiveBg.src = "/themes/default/images/header_tab_inactive_bg.jpg";
    headerInactiveLeft = new Image();
    headerInactiveLeft.src = "/themes/default/images/header_tab_inactive_left.jpg";
    headerInactiveRight = new Image();
    headerInactiveRight.src = "/themes/default/images/header_tab_inactive_right.jpg";
    smallArrowActive = new Image();
    smallArrowActive.src = "/themes/default/images/header_tab_arrow_active.gif";
    smallArrowInactive = new Image();
    smallArrowInactive.src = "/themes/default/images/header_tab_arrow_inactive.gif";
    smallArrowFocused = new Image();
    smallArrowFocused.src = "/themes/default/images/header_tab_arrow_focused.gif";

    $('.category-button-search').click(function() {
        $('.query-categories').slideToggle('normal');
        $('.category-button-search').hide();
        $('.container-slide').animate({ marginTop: '130px' }, 400);
    });
    $('.category-button-search-hide').click(function() {
        $('.query-categories').slideToggle('normal');
        $('.category-button-search').show();
        $('.container-slide').animate({ marginTop: '0px' }, 400);
    });

    $("#header .tab").each(function(i) {
        $(this).append("<div class='left'></div>");
        $(this).append("<div class='right'></div>");

        if ($(this).hasClass("has-dropdown"))
            $(this).append("<div class='arrow'></div>");

        if ($(this).hasClass("selected"))
            $(this).find(".arrow").addClass("arrow-selected");
    });

    $("#header .tab .left").click(function() {
        window.location = $(this).siblings("a:first").attr("href");
    });

    $("#header .tab").mouseenter(function() {
        $(this).addClass("hovered");
        $(this).find(".arrow").addClass("arrow-hovered");
    });

    $("#header .tab").mouseleave(function() {
        $(this).removeClass("hovered");
        $(this).find(".arrow").removeClass("arrow-hovered");
        $(this).find(".dropdown").hide();
    });

    var timerResearch;
    $("#drilldown-research").hover(function() {
            timerResearch = setTimeout(function() {
                $('#dropdown-research').slideDown('fast')
            }, 300);
        }, function() {
            $('#dropdown-research').slideUp('fast');
            if (timerResearch != '') {
                clearTimeout(timerResearch);
            }
        }
    );

    var timerHelp;
    $("#drilldown-help").hover(function() {
            timerHelp = setTimeout(function() {
                $('#dropdown-help').stop(true, true).slideDown('fast')
            }, 300);
        }, function() {
            $('#dropdown-help').slideUp('fast');
            if (timerHelp != "") {
                clearTimeout(timerHelp);
            }
        }
    );

    //    $("#header .tab .arrow").click(function() {
    //        $(this).siblings(".dropdown").slideToggle("fast");
    //    });

    $("#header .tab .arrow").mouseenter(function() {
        $(this).removeClass("arrow-selected");
        //$(this).addClass("arrow-focused");
    });

    $("#header .tab .arrow").mouseleave(function() {
        $(this).removeClass("arrow-focused");
        if ($(this).parent().hasClass("selected"))
            $(this).addClass("arrow-selected");
    });

    $("#header .tab .dropdown div:not(.separator)").click(function() {
        window.location = $(this).find("a:first").attr("href");
    });

    $("#header .tab .dropdown div:not(.separator)").mouseenter(function() {
        $(this).addClass("hovered");
    });

    $("#header .tab .dropdown div:not(.separator)").mouseleave(function() {
        $(this).removeClass("hovered");
    });

    $(".search-help .help-button").mouseenter(function() {
        $(".help-popup").show();
    });

    $(".search-help").mouseleave(function() {
        $(".help-popup").hide();
    });
    $("#search-tabs .tab").mouseenter(function() {
        $(this).addClass("hovered");
    });
    $("#search-tabs .tab").mouseleave(function() {
        $(this).removeClass("hovered");
    });
});

function selectTab(tabName) {
    $("#header .tab a").each(function(i) {
        if ($(this).text() == tabName) {
            $(this).parent().addClass("selected");
            $(this).siblings(".arrow").addClass("arrow-selected");
        }
    });
}

function switchAffTab(tab)
{ 
    $("#search-tabs a[id*='Tab']").removeClass("selected");
    $("div[id*='Result']").hide();
    $("#" + tab + "Result").show();
    $("#" + tab + "Tab").addClass("selected");
}

function checkBrowser() {
    var request = window.XMLHttpRequest;
    if (!request) {
        return true;
    }
    else {
        return false;
    }
}

//Fixes cut off webpage on small res
function AdjustBody(scr) {
    if (scr) {
        if (scr.width < 1000) {
            $('body').css({"position":"relative","width":"1000px"});
        }
    }
}

//Fix browser display
function fixDisplayWithCss() {
    if (checkBrowser()) {
        $('#content .shadow-bottom').hide();
        $('.bottom-ie').show();
        $('#content').css({ 'margin-left': '15px' });
        $('#paper').css({ 'width': '996px' });
        $('#content .shadow-right').css({ 'height': $('#paper').height() - 27, 'left': '100.1%' });
        $('#content .shadow-left').css({ 'height': $('#paper').height() - 27 });
    }
    else {
        $('#content .shadow-bottom').show();
        $('.bottom-ie').hide();
        $('#content').css({ 'margin-left': '0px' });
        $('#paper').css({ 'width': '981px' });
        $('#content .shadow-right').css({ 'height': '100%', 'left': '100%' });
        $('#content .shadow-left').css({ 'height': '100%' }); 
    }
}