/*
 * @Revision: #1
 * @Owner: eado-nasa-dev@
 * @Description: Contains Javascript for displaying tabs for product information on the detail page.
 * Copyright (c)2009 Amazon.com, Inc.
 * All rights reserved.
 * @Author: vinit
*/

jQuery.fn.featureTabs = function(tabNavDivName, tabContentDivName) {
   
    var tabs = new function() {};
    
    tabs.jObj_tabWrapperDivList = undefined,
    tabs.jObj_contentWrapperDivList = undefined,
    tabs.selectedTabIndex = 0,

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

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

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

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

    // set the height of the tabs to a constant
    tabs.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);
    }
 
    if( tabNavDivName == undefined || tabContentDivName == undefined )
        tabs.initialize('featureTabsWrapperDiv', 'featureTabsContentWrapperDiv'); 
    else
        tabs.initialize(tabNavDivName, tabContentDivName);
};        
