﻿/* Brass Drop Down List
*************************************************************************************/
(function ($)
{
    $.fn.brassDropDownList = function (options)
    {
        // default configuration properties
        var defaults = {
            expanded: false,
            click: null,
            followLinks: true,
            selectedValue: ""
        };

        var options = $.extend(defaults, options);

        return this.each(function ()
        {
            var bdd = new brassDropDownList($(this), this);
            bdd.click = options.click;
            bdd.followLinks = options.followLinks;
            bdd.setSelectedItem(options.selectedValue);
        });
    };
})(jQuery);

function brassDropDownList(root)
{
    var _this = this;
    _this.element = root;
    _this.expanded = false;
    _this.text = "";
    _this.defaultText = "";
    _this.selectedValue = "";
    _this.click = null;
    _this.followLinks = true;

    _this.initialise();
}
brassDropDownList.prototype.initialise = function ()
{
    var _this = this;
    
    _this.defaultText = $(".Text", _this.element).text();
    _this.text = $(".Text", _this.element).text();

    // Header click
    $(".Head", _this.element).click(function (evt)
    {
        evt.preventDefault();
        if (!$(".Head", _this.element).hasClass("Head-Expanded"))
            _this.expand();
        else
            _this.collapse();
    });

    // Option click
    $(".Options a", _this.element).click(function (evt)
    {
        if (!_this.followLinks)
        {
            evt.preventDefault();

            if (_this.click)
                _this.click(this);
        }

        _this.setText($(this).text());
        _this.collapse();
    });
};
brassDropDownList.prototype.expand = function ()
{
    var _this = this;

    $(".Head", _this.element).addClass("Head-Expanded");

    $(".Options", _this.element).stop().animate({
        height: $(".Options div", _this.element).outerHeight()
    }, 750);
};
brassDropDownList.prototype.collapse = function ()
{
    var _this = this;

    $(".Head", _this.element).removeClass("Head-Expanded");

    $(".Options", _this.element).stop().animate({
        height: 0
    }, 750);
};
brassDropDownList.prototype.setText = function (text)
{
    var _this = this;
    if ($(".Text", _this.element).text() != text)
    {
        $(".Text", _this.element).fadeOut(250, function ()
        {
            $(this).html(text).fadeIn(250);
        });
    }
};
brassDropDownList.prototype.setSelectedItem = function (value)
{
    var _this = this;
    _this.selectedValue = value;
    if (_this.selectedValue != "")
    {
        $(".Options a", _this.element).each(function ()
        {
            if ($(this).attr("href").indexOf(_this.selectedValue) != -1)
            {
                _this.setText($(this).text());
                return false;
            }
        });
    }
}
brassDropDownList.prototype.reset = function (text)
{
    var _this = this;
    $(".Text", _this.element).html(_this.defaultText);
};