/**
 * dlTabs
 * Version: 0.1
 * @requires jQuery v1.2.6
 *
 * Copyright (c) 2009 Seth House
 * Released under the GNU General Public License 3
 * http://www.gnu.org/licenses/gpl.html
 *
 *
 * @description Makes a tabbed interface from a definition list.
 *
 *
 * @example $('#somedl').dltabs();
 * @description Invoke the plugin
 *
 * @example $('#somedl').dltabs({ optionName: 'option-value'});
 * @description Invoke the plugin with a non-default option
 *
 * @exmaple $.fn.dltabs.defaults.selClass = 'yourClassName';
 * @description Override class name that is given to the shown <dd> element
 *
 *
 *
 * @param Object settings An object literal containing key/value pairs to provide optional settings.
 *
 * @option String selClass (optional)            Class name that is given to the shown <dd> element.
 *                                               Default value: 'dltabs-selected'
 *
 * @option String tabClass (optional)            Class name that is given to the <dl> element once it has been tabbed.
 *                                               Default value: 'dltabs-tabbed'
 *
 * @option Boolean fixHeight (optional)          Sets the height attribute of the <dl> to allow for horizontal layout (which is not possible with bare CSS)
 *                                               Default value: 'true'
 *
 *
 * @type jQuery
 *
 * @name dltabs
 *
 * @cat Plugins/Misc
 *
 * @author Seth House/seth@eseth.com
 */

(function($) {
    $.fn.dltabs = function(options) {
        // debug(this);

        var opts = $.extend({}, $.fn.dltabs.defaults, options);

        return this.each(function() {
            var $this = $(this);
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

            $this.addClass(o.tabClass);
            $this.find('dd').hide();
            $this.find('dt').click(function() {
                $.fn.dltabs.showTab($this, $(this), o);
            });
            $.fn.dltabs.showTab($this, $this.find('dt:first'), o);
        });
    };

    $.fn.dltabs.showTab = function($dl, $dt, o) {
        // FIXME: is there a better way to access o ?
        $dt.addClass(o.selClass).siblings('dt').removeClass(o.selClass);
        $dl.find('dd:visible').removeClass(o.selClass).hide();
        var dd_height = $dt.next('dd').addClass(o.selClass).show().height();

        // Optionally set the height of the <dl>
        if (o.fixHeight) {
            $dl.height($dt.height() + dd_height);
        };
    };

    // Private function for debugging
    function debug($obj) {
        if (window.console && window.console.log)
            window.console.log('Debugging output!');
    };

    // Defaults
    $.fn.dltabs.defaults = {
        selClass: 'dltabs-selected',
        tabClass: 'dltabs-tabbed',
        fixHeight: true
    };
})(jQuery);
