jQuery('document').ready(function() {
    featureTabs.initialize('featureTabsWrapperDiv', 'featureTabsContentWrapperDiv');
});

var featureTabs = {
   
    jObj_tabWrapperDivList : undefined,
    jObj_contentWrapperDivList : undefined,
    selectedTabIndex : 0,

    initialize : function(tabWrapperDiv, contentWrapperDiv) {
        var parentThis = this;
        // cache jQuery objects
        this.jObj_tabWrapperDivList = jQuery('#'+tabWrapperDiv).children('div');
        this.jObj_contentWrapperDivList = jQuery('#'+contentWrapperDiv).children('div');

        // hide/show divs
        this.jObj_contentWrapperDivList.
            each(function() {
                   var jObj = jQuery(this);
                   if (jObj.hasClass('tab'))
                     jObj.hide();
                   else
                     jObj.show();
                });

        // attach handlers
        this.jObj_tabWrapperDivList.click(function() {
            parentThis.tabClicked(this);
        });

        this.jObj_tabWrapperDivList.keypress(function(e) {
	    if (e.which == 13 ) {
	            parentThis.tabClicked(this);
	    }
        });
       
        this.setTabHeight(); 
    },
    // event handler for tab click event
    tabClicked : function (domElem) {
        var jObj = jQuery(domElem);
        var tabIndex = this.jObj_tabWrapperDivList.index(domElem);
        if (tabIndex == this.selectedTabIndex) {
            return;
        }
        this.hideDiv   (this.jObj_tabWrapperDivList.get(this.selectedTabIndex), this.jObj_contentWrapperDivList.get(this.selectedTabIndex)); 
        this.showDiv   (this.jObj_tabWrapperDivList.get(tabIndex), this.jObj_contentWrapperDivList.get(tabIndex));
        this.selectedTabIndex = tabIndex;
    },
    // show content div and highlight corresponding tab
    showDiv : function (tabDomElem, contentDomElem) {
        jQuery(tabDomElem).attr('class', 'tabHeaderSelected');
        jQuery(contentDomElem).show();
    },
    // hide content div and unhighlight corresponding tab
    hideDiv : function (tabDomElem, contentDomElem) {
        jQuery(tabDomElem).attr('class', 'tabHeader');
        jQuery(contentDomElem).hide();
    },

    // set the height of the tabs to a constant
    setTabHeight : function () {
        var contentWrapper = jQuery("#featureTabsContentWrapperDiv");
        var tabs = contentWrapper.find(".tabContentBody");
        var maxHeight = 0;
    
        tabs.each(function(){
             var thisTabHeight = jQuery(this).height();
             if (thisTabHeight>maxHeight) {
                maxHeight = thisTabHeight;
             }
        });
         
        tabs.height(maxHeight);
    }
};        
