$(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/AtlanticaStolenBoats.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;
    }
});