$(document).ready(function() {

    ////////////////////////////////////////////
    // Search.ascx
    ////////////////////////////////////////////
    $('div.howTo').hide();

    $('div.howToArea .heading').live('click', function() {
        $('div.howTo').slideToggle(500, function() {
            var plus = "/Templates/Styles/Images/Plus.png";
            var minus = "/Templates/Styles/Images/Minus.png";
            var imageSrc = $('div.howToArea .heading').css('background-image');

            if (imageSrc.match(plus)) {
                $('div.howToArea .heading').css('background-image', 'url(' + minus + ')');
            }
            else if (imageSrc.match(minus)) {
                $('div.howToArea .heading').css('background-image', 'url(' + plus + ')');
            }
        });
    });

    ////////////////////////////////////////////
    // QuickSearch.ascx
    ////////////////////////////////////////////
    $('.searchButton, .searchWindow').mouseenter(function() {
        $('.searchWindow').fadeIn('fast');
        $('.searchButton').addClass('active');
        $(this).data('in', true); $('.searchWindow').data('hidden', false);
    }).mouseleave(function() {
        $(this).data('in', false); setTimeout(hideSearchMenu, delay);
    });
    var delay = 400;
    function hideSearchMenu() {
        if (!$('.searchButton').data('in') && !$('.searchWindow').data('in') && !$('.searchWindow').data('hidden')) {
            $('.searchWindow').fadeOut('fast');
            $('.searchButton').removeClass('active');
            $('.searchWindow').data('hidden', true);
        }
    }

    ////////////////////////////////////////////
    // TipAndPrint.ascx
    ////////////////////////////////////////////
    $('.tipButton, .tipMenu').mouseenter(function() {
        $('.tipMenu').fadeIn('fast');
        $('.tipButton').addClass('active');
        $(this).data('in', true); $('.tipMenu').data('hidden', false);
    }).mouseleave(function() {
        $(this).data('in', false); setTimeout(hideTipMenu, delay);
    });

    function hideTipMenu() {
        if (!$('.tipButton').data('in') && !$('.tipMenu').data('in') && !$('.tipmenu').data('hidden')) {
            $('.tipMenu').fadeOut('fast');
            $('.tipButton').removeClass('active');
            $('.tipMenu').data('hidden', true);
        }
    }

    $('.emailTip').click(function() {
        $('.tipWindow').fadeIn('fast');
        return false;
    });

    $('.tipClose').click(function() {
        $('.tipWindow').fadeOut('fast');
    });

    ////////////////////////////////////////////
    // Stolen boats
    ////////////////////////////////////////////
    $("div.stolenList table tbody tr td a").live("click", openDetail);

    function openDetail(e) {
        e.preventDefault();

        var params = getUrlParams($(this).attr("href"));
        var type = params['type'];
        var id = params['itemid'];
        getExternalContent(type, id);

        $('div.stolenDetail').modal();
        //$('.helpPadding').html(content);

        return false;
    }

    function getExternalContent(type, id) {
        return $.ajax({
            type: "POST",
            url: "/Templates/Pages/Atlantica/StolenBoats.aspx/GetDetails",
            data: "{ type: '" + type + "', itemid: '" + id + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                $('.helpPadding').html(data.d);
            }
        });
    }

    function getUrlParams(url) {
        var vars = [], hash;
        var hashes = url.slice(url.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }

    ////////////////////////////////////////////
    // Menu
    ////////////////////////////////////////////
    $('#MainMenu ul li.childs, #MainMenu ul li.selectedChilds').bind('mouseover', DropDownShow)
    $('#MainMenu ul li.childs, #MainMenu ul li.selectedChilds').bind('mouseout', DropDownTimer)

    $('#MainMenu .dropDownMenu .dropDownBorder ul li').bind('mouseover', DropDownItemShow)
    $('#MainMenu .dropDownMenu .dropDownBorder ul li').bind('mouseout', DropDownItemTimer)

    var selectedMenuItem = $("#MainMenu ul li.selected");
    var selectedDropDownMenuItem = $("#MainMenu ul li.selectedDropDown");
    var menuItem;

    var dropDownTimeout = 400;
    var dropDownCloseTimer = 0;
    var dropDownItemTimeout = 400;
    var dropDownItemCloseTimer = 0;

    function DropDownShow() {
        menuItem = $(this);

        //Show the image of the selected menuitem if it's in the dropdownmenu
        menuItem.find("li.selected").children(".dropDownImg").show();

        //Set classes on the dropdownmenuitem so it stays hovered
        menuItem.children("a").addClass("hover");
        menuItem.addClass("liHover")

        //Show the dropdownmenu on hover
        menuItem.children(".dropDownMenu").show();

        //Remove selected style from the menuitem that is selected while hovering the dropdownmenu
        selectedMenuItem.removeClass("selected");

        DropDownCancelTimer();
    }

    function DropDownHide() {
        if (menuItem) {
            menuItem.children("a").removeClass("hover");
            menuItem.removeClass("liHover")
            menuItem.children(".dropDownMenu").hide();

            selectedMenuItem.addClass("selected");
        }
    }

    function DropDownTimer() {
        dropDownCloseTimer = window.setTimeout(DropDownHide, dropDownTimeout);
    }

    function DropDownCancelTimer() {
        if (dropDownCloseTimer) {
            window.clearTimeout(dropDownCloseTimer);
            dropDownCloseTimer = null;
        }
    }

    function DropDownItemShow() {
        //Hide all images
        $(".dropDownImg").hide();
        //Show the hovered image
        $(this).children(".dropDownImg").show();

        DropDownItemCancelTimer();
    }

    function DropDownItemHide() {
        //Hide all images
        $(".dropDownImg").hide();
        //Show the image of the selected dropdownmenuitem
        selectedDropDownMenuItem.children(".dropDownImg").show();
    }

    function DropDownItemTimer() {
        dropDownItemCloseTimer = window.setTimeout(DropDownItemHide, dropDownItemTimeout);
    }

    function DropDownItemCancelTimer() {
        if (dropDownItemCloseTimer) {
            window.clearTimeout(dropDownItemCloseTimer);
            dropDownItemCloseTimer = null;
        }
    }
});