﻿//Premium Boat
$(document).ready(function() {
    reactivateBoat();
});

function reactivateBoat() {
    $("div.errorMessage").scrollTop(300);

    $().unbind("ajaxStart", $.blockUI);

    // ---------------------------
    // BOAT QUICK SEARCH
    // ---------------------------
    $("input.boatQuickSearch").autocomplete("/PremiumUI/Handlers/QuickSearchHandler.ashx",
    {
        width: 260,
        delay: 250,
        minChars: 3,
        cacheLength: 1,
        max: 100,
        selectFirst: true,
        mouseDownSelect: true
    });

    $("input.boatQuickSearch").result(function(event, data, formatted) {
        var hidden = $(this).siblings("input");
        hidden.val(data[1]);
        var button = $('input.modelSearchButton');
        if (button) {
            button.click();
        }
    });

    $("ul.boatModels li").live("click", activateRow);

    $("ul.boatModels li > input:checked").parent().addClass('selected');

    // ---------------------------
    // FIND TENDERS POPUP
    // ---------------------------
    // hide the popup if no search has been done yet
    if ($(".findTendersPopup input[type=hidden]").length == 0 || $(".findTendersPopup input[type=hidden]").val() == "hide") {
        $(".findTendersPopup").hide();
    }

    $(".findTendersPopup .findTendersTextBox").click(emptySearchBox);

    // show/hide popup
    $(".findTenders a, .closeFindTenders").click(function() {
        // show the popup when the link is clicked
        if ($(".findTendersPopup").is(':hidden')) {
            $(".findTendersPopup").show();
            //setOverlay();
        }
        else {
            // hide the popup when the link is clicked again
            if ($(".findTendersPopup input[type=hidden]").length != 0) {
                $(".findTendersPopup input[type=hidden]").val("hide");
            }
            $(".findTendersPopup").hide();
            //resetOverlay();
        }
        return false;
    });

    $(".closeFindTenders").click(function() {
        // hide the popup when the link is clicked again
        if ($(".findTendersPopup input[type=hidden]").length != 0) {
            $(".findTendersPopup input[type=hidden]").val("hide");
        }
        $(".findTendersPopup").hide();
        //resetOverlay();
    });

    // do not let the overlay click event be triggered
    $('.findTendersPopup').click(function(e) {
        if (!$(e.target).hasClass('findTendersButton')) {
            e.stopPropagation();
        }
    });

    // When Securmark is selected, display the help popup.
    $('span[id$="rdoSecurMark"] input[value="1"]').click(function() {
        $('[id$="ucHelpSecurmarkWarning_aHelp"]').click();
    });
    //initTabs();
    
}

// ---------------------------
// FIND TENDERS POPUP
// ---------------------------
// if anything else then the popup is clicked, hide the popup
function setOverlay() {
    $("body").live("click", function() {
        $('.findTendersPopup').hide();
        if ($(".findTendersPopup input[type=hidden]").length != 0) {
            $(".findTendersPopup input[type=hidden]").val("hide");
        }
        resetOverlay();
    });
}

// kill the overlay click event
function resetOverlay() {
    $("body").die("click");
}

function emptySearchBox() {
    if ($(this).val() == $(this).attr("title")) {
        $(this).val("");
    }
}

// ---------------------------
// BOAT QUICK SEARCH
// ---------------------------
function activateRow() {
    if ($(this).hasClass('selected')) {
        return;
    }
    $(this).siblings("li").removeClass('selected');

    $(this).addClass('selected');
}
// ---------------------------
// TENDER TABS
// ---------------------------
function initTabs(){
    var activeTab;
    if(window.location.hash != "")
    {
        activeTab = window.location.hash;
        if($("div" + activeTab).size() > 0){
            setActiveTab(activeTab);
            $("a[href='" + activeTab + "']").parent("li").addClass("selected");
        }
    }
    $(".tab a").click(function(){
        //setActiveTab($(this).attr("href"));
        $(this).parent("li").addClass("selected");

    });
}
function setActiveTab(tab){
    $(".tabbedContent").removeClass("active");
    $(".tab").removeClass("selected");
    $(tab).addClass("active");
}
