﻿// jquery.tabs.js
// Built by Fullhouse 2009

/// <reference path="jquery-1.2.6-vsdoc.js"/>

(function($) {

    $.fn.tabs = function(options) {

        var opts = $.extend({}, $.fn.tabs.defaults, options);

        var isWorking = false;

        return this.each(function() {
            var context = $(this);

            $(opts.contentSelector, context).hide();

            var showTab = function(index, currentHeight) {
                var contentTab = $(opts.contentSelector + ':eq(' + index + ')', context);

                // get the height of the new content tab
                var height = $(contentTab).height();

                if (currentHeight) {
                    $(opts.containerSelector, context).height(currentHeight + 'px');
                }

                // hide the inner content of the new content tab
                $('> .Inner', contentTab).hide().parent().show();

                // animate the content container to change the height
                $(opts.containerSelector, context).animate({
                    height: height + 'px'
                }, opts.delay, function() {
                    $(opts.contentSelector + ':eq(' + index + ') > .Inner', context).fadeIn(opts.delay, function() {
                        $(opts.containerSelector, context).height('auto');
                        // hide all other tabs and show their inner div to restore effective height
                        $(opts.contentSelector, context).not(contentTab).hide().find('.Inner').show();
                        isWorking = false;
                    });
                });

                $(opts.tabSelector + ':eq(' + index + ')', context).addClass('Active');
            };

            var hideTab = function(index) {
                var height = $(opts.contentSelector + ':visible', context).height();
                $(opts.contentSelector + ':visible > .Inner', context).fadeOut(opts.delay, function() {
                    showTab(index, height);
                });
            };

            var closeTab = function(index) {
                $(opts.contentSelector + ':eq(' + index + ')', context).slideUp(opts.delay, function() {
                    $(opts.tabSelector + ':eq(' + index + ')', context).removeClass('Active');
                    isWorking = false;
                });
            };

            var i = 0;
            $(opts.tabSelector, context).each(function() {
                var tab = $(this);
                var index = i;
                $(tab).click(function(e) {
                    e.preventDefault();
                    if (isWorking) {
                        return;
                    }
                    isWorking = true;

                    if ($(tab).hasClass('Active')) {
                        closeTab(index);
                        return;
                    }

                    $(opts.tabSelector + '.Active', context).removeClass('Active');
                    if ($(opts.contentSelector + ':visible', context).length > 0) {
                        hideTab(index);
                    }
                    else {
                        showTab(index);
                    }
                });
                i++;
            });

            i = 0;
            $(opts.contentSelector, context).each(function() {
                var content = $(this);
                var index = i;
                $('a.Close', content).click(function(e) {
                    e.preventDefault();
                    closeTab(index);
                });
                i++;
            });
        });

    };

    // publicly accessible defaults
    $.fn.tabs.defaults = {
        tabSelector: '> div.Tabs a',
        containerSelector: '> div.Content',
        contentSelector: '> div.Content > div.Tab',
        delay: 250
    };

})(jQuery);
