﻿/* Brass Drop Down List
*************************************************************************************/
(function ($)
{
    $.fn.brassHeader = function (options)
    {
        // default configuration properties
        var defaults = {
            expanded: false
        };

        var options = $.extend(defaults, options);

        return this.each(function ()
        {
            var header = new brassHeader();
            header.expanded = options.expanded;
        });
    };
})(jQuery);

function brassHeader()
{
    var _this = this;
    _this.expanded = false;
    _this.contactUsHeight = 0;
    _this.mouseDown = false;
    _this.dragging = false;
    _this.mouseOverHandle = false;
    _this.mousePosition = 0;
    _this.keepHandleExpanded = false;

    _this.initialise();
}
brassHeader.prototype.initialise = function ()
{
    var _this = this;

    // Maintain dimensions throughout animation.
    $(".TopNavigation .ContactUs").css({
        "width": $(".TopNavigation .ContactUs").outerWidth(),
        "height": $(".TopNavigation .ContactUs").outerHeight()
    });

    // Max top margin for the header.
    _this.contactUsHeight = -1 * $(".ExpandableContent").outerHeight();

    // Hover
    $(".TopNavigation .ContactUs").hoverIntent(function ()
    {
        if (!$(this).hasClass("Expanded") && !_this.mouseDown)
            _this.expandHandle(this);
    },
    function ()
    {
        if (!$(this).hasClass("Expanded") && !_this.mouseDown && !_this.keepHandleExpanded)
            _this.collapseHandle(this);
    });

    // Contact us nav link click behaviour
    $(".TopNavigation .ContactUs, .TopNavigation .ContactUs .Handle, .TopNavigation .ContactUs .Default").click(function (evt)
    {
        evt.preventDefault();

        if (_this.dragging)
        {
            var exp = (-parseInt($(".ExpandableContent").css("marginTop").replace("px", "")) < -(_this.contactUsHeight / 2));
            if (exp)
            {
                _this.keepHandleExpanded = true;
                _this.expand();
            }
            else
            {
                _this.keepHandleExpanded = false;
                _this.collapse();
            }
        }
        else
        {
            if (!_this.expanded)
            {
                _this.keepHandleExpanded = true;
                _this.expand();
            }
            else
            {
                _this.keepHandleExpanded = false;
                _this.collapse();
            }
        }

        _this.mouseDown = false;
        _this.dragging = false;

        $(".TopNavigation .ContactUs .Handle").unbind("mousemove");
    }).hover(function ()
    {
        _this.mouseOverHandle = true;
    },
    function ()
    {
        _this.mouseOverHandle = false;
    });

    // Set the initial position. Change classes to disable non-js behaviour.
    $(".TopNavigation .ContactUs .Tab").css({
        "top": _this.contactUsHeight
    }).addClass("Handle").removeClass("Tab"); $(".TopNavigation .ContactUs .Link").addClass("Default").removeClass("Link");       // Change classes to disable non-js behaviour.

    // Drag behaviour for the tab.
    $(".TopNavigation .ContactUs .Handle a").disableSelection();    // Disable text selection on the handle.

    // Handle mouse down - set the default position and mouseDown to true.
    /*
    $(".TopNavigation .ContactUs .Handle").mousedown(function (e)
    {
        _this.mouseDown = true;
        _this.mousePosition = e.pageY;

        $(this).bind("mousemove", function ()
        {
            _this.dragging = true;
            $(this).unbind("mousemove");
        });

        return false;
    });
    */

    // Keep moving the header even when the mouse has left the tab.
    /*
    $("body").mousemove(function (e)
    {
        if (_this.mouseDown && _this.dragging)
        {
            var move = parseInt($(".ExpandableContent").css("marginTop").replace("px", "")) - (_this.mousePosition - e.pageY);

            if (move >= 0)
            {
                move = 0;
                _this.expanded = true;
                $(".ExpandableContent").addClass("Expanded");
            }
            else if (move <= _this.contactUsHeight)
            {
                move = _this.contactUsHeight;
                _this.expanded = false;
                $(".ExpandableContent").removeClass("Expanded");
            }

            $(".ExpandableContent").css({
                "margin-top": move
            });

            _this.mousePosition = e.pageY;
        }
    }).mouseup(function (e)
    {
        if ((_this.dragging && !_this.mouseOverHandle) || (_this.dragging && /Opera/.test(navigator.userAgent)))
            $(".TopNavigation .ContactUs").click();
    });
    */
    /*
    // Handle mouse down - set the default position and mouseDown to true.
    $(".TopNavigation .ContactUs .Handle").mousedown(function (e)
    {
    _this.mouseDown = true;
    _this.mousePosition = e.pageY;
    return false;
    });
    */
};
brassHeader.prototype.expand = function ()
{
    var _this = this;

    _this.expanded = true;
    $(this).addClass("Expanded");
    $(".ExpandableContent").stop().animate({ "margin-top": "0" }, 750, function()
    {
        _this.expandHandle(".TopNavigation .ContactUs");
    });
};
brassHeader.prototype.collapse = function ()
{
    var _this = this;

    _this.expanded = false;
    $(this).removeClass("Expanded");
    $(".ExpandableContent").stop().animate({ "margin-top": _this.contactUsHeight }, 750, function ()
    {
        _this.collapseHandle(".TopNavigation .ContactUs");
    });
};
brassHeader.prototype.expandHandle = function (element)
{
    $(".Default", element).fadeOut(150, function ()
    {
        $(".TopNavigation .ContactUs .Handle").stop().animate({
            top: 0
        }, 150);
    });
};
brassHeader.prototype.collapseHandle = function (element)
{
    $(".Handle", element).stop().animate({
        top: -$(element).outerHeight()
    }, 150, function ()
    {
        $(".TopNavigation .ContactUs .Default").fadeIn(150);
    });
};