﻿var tabs = function(list, o) {
    this.init(list, o);
}
jQuery.extend(tabs.prototype, {
    tabAnimating: false,
    tabLinks: null,
    tabWrapper: null,
    options: null,

    init: function(tabbableContent, o) {
        // Default configuration properties
        var defaults = { visibilityStatusChanged: null, animateSwitch: false };

        this.options = jQuery.extend({}, defaults, o || {});

        // Check there is tabable content
        if (jQuery(tabbableContent).length) {

            // Wrap the tabbable content in a tabbedContent Wrapper
            tabbableContent.wrapAll('<div class="tabbedContent"/>');
            this.tabWrapper = jQuery('.tabbedContent');

            // Create the tab navigation
            jQuery('div.tabbedContent').prepend('<ul class="tab-nav tablinks"/>')
            this.tabLinks = jQuery('.tab-nav', this.tabWrapper);

            var _self = this;

            jQuery('div.tabbedContent .tabHeading').each(function(index) {
                // Extract the text from each heading and add that to the "tab"
                var heading = jQuery(this);

                // Get the content specific css class. This will be attached to the tab link
                var tabbedContentClass = heading.parent('.tabContent').attr('class').replace('tabContent', '');

                jQuery('<li><a>' + heading.text() + '</a></li>').appendTo('div.tabbedContent .tab-nav')
                                                            .data('relatedContent', heading.parent())
                                                            .addClass(jQuery.trim(tabbedContentClass))
                                                            .bind('click', _self, _self._tabClick)

            });

            // Set the first tab as the current one
            this.setTabAsCurrent(jQuery('div.tabbedContent .tab-nav li:first'));
        }
    },

    setTabAsCurrent: function(newTabLink) {
        // Hide the selected tab snd display the newly chosen one
        var currentlySelectedTabLink = jQuery('.selected', this.tabLinks)
        currentlySelectedTabLink.removeClass('selected');

        var oldHeight, newHeight;

        // Hide currently visible tab
        var currentlySelectedTabbedContent = jQuery('div.tabbedContent .tabContent:not(.tab-hide)');
        var nextTabbedContent = jQuery(newTabLink).data('relatedContent');

        currentlySelectedTabbedContent.addClass('tab-hide');
        nextTabbedContent.removeClass('tab-hide');

        if (this.options.animateSwitch && !this.tabAnimating) {
            this.tabAnimating = true;

            oldHeight = currentlySelectedTabbedContent.height();
            newHeight = nextTabbedContent.height();

            nextTabbedContent.height(oldHeight);
        }

        if (this.options.visibilityStatusChanged) {
            this.options.visibilityStatusChanged(false, currentlySelectedTabLink, currentlySelectedTabbedContent)
        }

        // Show current tab and link
        jQuery(newTabLink).addClass('selected');

        if (nextTabbedContent != undefined) {

            // the animate option causes problems with IE6 when the left-hand column extends below the bottom of the tab (see DE418)
            if (this.options.animateSwitch && !isIE6) {
                nextTabbedContent.animate({ height: newHeight }, 'slow', 'swing', function() {
                    //set height to auto once anim is finished to stop it overriding display:none style
                    nextTabbedContent.removeAttr("style");
                    currentlySelectedTabbedContent.removeAttr("style");
                });
            } else { //fix for ie6 bug
                nextTabbedContent.removeAttr("style");
                currentlySelectedTabbedContent.removeAttr("style");
            }
        }

        if (this.options.visibilityStatusChanged) {
            this.options.visibilityStatusChanged(true, newTabLink, nextTabbedContent)
        }

        this.tabAnimating = false;
    },

    _tabClick: function(event) {
        var _self = event.data;

        _self.setTabAsCurrent(this);

        event.preventDefault();
        event.stopPropagation();
        return false;
    }
});

if (!window.Nova) { window['Nova'] = {}; };
if (!window.Nova.Controls) { window['Nova']['Controls'] = {}; };
window['Nova']['Controls']['Tabs'] = tabs;

