$(document).ready(function() {

    ////////////////////////////////////////////
    // Search.ascx
    ////////////////////////////////////////////

    $('div.howTo').hide();

    $('div.howToArea .heading').live('click', function() {
        $('div.howTo').slideToggle(500, function() {
            var plus = "/Templates/Styles/Web/Images/Plus.png";
            var minus = "/Templates/Styles/Web/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');
    //    });

    // TODO: Move to atlantica script.js
    ////////////////////////////////////////////
    // 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/StolenBoatsService.asmx/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 currentMenuItem;

    var dropDownTimeout = 0;
    var dropDownCloseTimer = 0;
    var dropDownItemTimeout = 0;
    var dropDownItemCloseTimer = 0;

    function DropDownShow() {

        currentMenuItem = $(this);

        if (currentMenuItem != menuItem) {
            DropDownHide();
            menuItem = currentMenuItem;
        }

        //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;
        }
    }
    $("#NewsTicker").smoothDivScroll({ autoScroll: "always", autoScrollDirection: "endlessloopright", autoScrollStep: 1, autoScrollInterval: 45 });
    $(".fontSize").show();
    //    $(".fontSize a").live('click', function() {
    //        var size = $(this).attr('class').split(' ');
    //        
    //        Resize(size[0]);
    //    });
    ////////////////////////
    /// Exapanding table
    ////////////////////////
    $(".table-expander").live('click', function() {
        var target = $(this).attr("href");
        var heading = target.split("#");
        $("." + heading[1]).slideToggle('fast', 'linear');
        $("a.plusbutton[href='" + target + "']").toggleClass("minus");
        return false;
    });

    ////////////////////////
    /// INFO POPUP
    ///////////////////////

    $(".infoButton").live("hover", function() {
        $(this).children(".infoContent").toggle();
    });
});

////////////////////
//  FONTSIZE
////////////////////
function Resize(inSize) {
    //$.getJSON('/Templates/Handlers/FontSettings.ashx', { fontSettings: inSize }, function (data) {
    //if (data) 
    {
        switch (inSize) {
            case "medium":
                $(".resizable.active p, .resizable.active ul, .resizable.active ol, .resizable.active .introduction").css({ "font-size": "1.2em", "line-height": "1.6" });
                $(".fontSize a").removeClass("selected");
                $(".fontSize a.medium").addClass("selected");
                break;
            case "large":
                $(".resizable.active p, .resizable.active ul, .resizable.active ol, .resizable.active .introduction").css({ "font-size": "1.35em", "line-height": "1.6" });
                $(".fontSize a").removeClass("selected");
                $(".fontSize a.large").addClass("selected");
                break;
            default:
                $(".resizable.active p, .resizable.active ul, .resizable.active ol").css({ "font-size": "1em", "line-height": "1.4" });
                $(".resizable.active .introduction").css({ "font-size": "1.1em", "line-height": "1.4" });
                $(".fontSize a").removeClass("selected");
                $(".fontSize a.small").addClass("selected");
                break;
        }
    }
    //});

}
