function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

//global js here - common to all pages...

var setAdvancedSearchHref = function() {
    switch ($("#searchDropDown").val()) {
    case "vessels":
        $("#advancedSearch").attr("href", "/llint/vessels/search/general.htm");
        $("#advancedSearch").text("Vessels Advanced Search");
        break;
    case "companies":            
        $("#advancedSearch").attr("href", "/llint/companies/search/general.htm");
        $("#advancedSearch").text("Companies Advanced Search");
        break;
    case "reports":
        $("#advancedSearch").attr("href", "/llint/reports/search.htm");
        $("#advancedSearch").text("Credit Reports Advanced Search");
        break;
    case "places":
        $("#advancedSearch").attr("href", "/llint/places/search.htm");
        $("#advancedSearch").text("Places Advanced Search");
        break;
    case "casualties":
        $("#advancedSearch").attr("href", "/llint/law/law-casualties/serious-casualties.htm");
        $("#advancedSearch").text("Casualties Advanced Search");
        break;
    case "articles":
        $("#advancedSearch").attr("href", "/llint/article-search.htm?searchText=&action=articles&wordsOption=anyWords&advancedSearch=true");
        $("#advancedSearch").text("Articles Advanced Search");
        break;
    default:
        $("#advancedSearch").attr("href", "/llint/vessels/search/general.htm");
        $("#advancedSearch").text("Vessels Advanced Search");
    }

}

function attachOnChangeEventToQuickSearch() {
//      $("#quickSearch").change(setAdvancedSearchHref);
    $("#searchDropDown").bind($.browser.msie? 'propertychange': 'change', setAdvancedSearchHref);
}

function checkSectionAndDisplayAdvancedSearch() {
    /* maintains the advanced search context */
    $(".searchBox").hide();
    if (window.location.href.indexOf("vessels") > 0) {
        $("#searchDropDown").val("vessels");
        $("#advancedSearch").attr("href", "/llint/vessels/search/general.htm");
        $("#advancedSearch").text("Vessels Advanced Search");
        $("#vessels").show();
    } else if (window.location.href.indexOf("companies") > 0) {
        $("#searchDropDown").val("companies");
        $("#advancedSearch").attr("href", "/llint/companies/search/general.htm");
        $("#advancedSearch").text("Companies Advanced Search");
        $("#companies").show();
    } else if (window.location.href.indexOf("reports") > 0) {
        $("#searchDropDown").val("reports");
        $("#advancedSearch").attr("href", "/llint/reports/search.htm");
        $("#advancedSearch").text("Credit Reports Advanced Search");
        $("#reports").show();
    } else if (window.location.href.indexOf("places") > 0) {
        $("#searchDropDown").val("places");
        $("#advancedSearch").attr("href", "/llint/places/search.htm");
        $("#advancedSearch").text("Places Advanced Search");
        $("#places").show();
    } else if (window.location.href.indexOf("casualties") > 0) {
        $("#searchDropDown").val("casualties");
        $("#advancedSearch").attr("href", "/llint/law/law-casualties/serious-casualties.htm");
        $("#advancedSearch").text("Casualties Advanced Search");
        $("#casualties").show();
    } else if (window.location.href.indexOf("articles") > 0) {
        $("#searchDropDown").val("articles");
        $("#advancedSearch").attr("href", "/llint/article-search.htm?searchText=&action=articles&wordsOption=anyWords&advancedSearch=true");
        $("#advancedSearch").text("Articles Advanced Search");
        $("#articles").show();
    } else {
        $("#searchDropDown").val("vessels");
        $("#advancedSearch").attr("href", "/llint/vessels/search/general.htm");
        $("#advancedSearch").text("Vessels Advanced Search");
        $("#vessels").show();
    }
}

$(document).ready(function() {
    checkSectionAndDisplayAdvancedSearch();
    attachOnChangeEventToQuickSearch();

    var currentSubSelection = $("#navSecondLevel ul li a.selected");

    $("#container").hover(null,function() {
        closeAllDropDowns();
        switchBackSecondLevel(currentSubSelection);
    });

    $("#navigation").mouseover(function() {
        closeAllDropDowns();
        switchBackSecondLevel(currentSubSelection);
    });


    $('.clearField').clearField(); //clears fields of any form textfield having class 'clearField'

    $(".secondLevel").mouseover(function(){
        if($(this).attr("name") === "") {
            //if there is no sub drop down menu for this sub-link
            closeAllDropDowns();
        }
        switchBackSecondLevel(currentSubSelection);
        $(this).css("background", "#d4eaf3");
        $(this).css("color", "#666");
        if($(this).attr("name") !== "") {
        var theMenuPanel = $(this).attr("name");
        closeAllDropDowns();
        var xPos = $(this).position().left;
        var yPos = $(this).position().top;
        yPos = yPos + 30; // off-set
            if ($(this).hasClass("offSet.apex")) {
                $("#"+theMenuPanel+"Select").css("left", xPos-249); // off-set to the left for right-hand-most menu items
            } else if ($(this).hasClass("offSet.dailyLngTracker")) {
                $("#"+theMenuPanel+"Select").css("left", xPos-174);
            } else if ($(this).hasClass("offSet.monthlyLngTracker")) {
                $("#"+theMenuPanel+"Select").css("left", xPos-160);
            }else if ($(this).hasClass("offSet.creditReports")) {
                $("#"+theMenuPanel+"Select").css("left", xPos-160);
            } else if ($(this).hasClass("offSet.gasCreditReports")) {
                $("#"+theMenuPanel+"Select").css("left", xPos-195);
            } else if ($(this).hasClass("offSet.dryBulkCreditReports")) {
                $("#"+theMenuPanel+"Select").css("left", xPos-195);
            } else {
                $("#"+theMenuPanel+"Select").css("left", xPos);
            }
        $("#"+theMenuPanel+"Select").css("top", yPos);
        $("#"+theMenuPanel+"Select").bgiframe().show();
        $("#"+theMenuPanel+"Select").hover(function() {$(this).show()}, function() {$(this).hide();$(".secondLevel").css("background", "#235884");$(".secondLevel").css("color", "#A6D1E2");$(currentSubSelection).css("color", "#fff")});
        } else {
            $(this).mouseout(function() {
               closeAllDropDowns();
               switchBackSecondLevel(currentSubSelection);
            });
        }
    });
});

function closeAllDropDowns() {
    $(".menuDropDown").each(function(){$(this).hide()});
}

function switchBackSecondLevel(currentSubSelection) {
    $(".secondLevel").each(function() {$(this).css("background", "#235884"); $(this).css("color", "#A6D1E2")});
    $(currentSubSelection).css("color", "#fff");
}


//set up global events here...
//addLoadEvent(attachOnChangeEventToQuickSearch);

//some global stuff
function ajaxError(xhr, ajaxOptions, thrownError){
    alert('Sorry, we are unable to process your request at the moment. Please try again later.');
}

function isDefined(variable){
    return (typeof(variable) != 'undefined' && variable != null);
}

function hasText(variable){
    return isDefined(variable) && variable.length > 0;
}

