jQuery("document").ready(function() {
    featureTabs.initialize();
});

var featureTabs = {
    tabDivs : null,
    contentDivs : null,
    selectedTabIndex : 0,

    initialize : function() {
        var parentThis = this;
        this.tabDivs = jQuery("#featureTabsWrapperDiv").children("div");
        this.contentDivs = jQuery("#featureTabsContentWrapperDiv").children("div");

        // hide/show divs
        this.contentDivs.each(function() {
            var jObj = jQuery(this);
            if(jObj.hasClass("tab")) {
                jObj.hide();
            } else {
                jObj.show();
            }
        });

        // attach click and key handlers
        this.tabDivs.click(function() {
            parentThis.tabClicked(this);
        });

        this.tabDivs.keypress(function(e) {
		    if(e.which == 13 ) {
		        parentThis.tabClicked(this);
		    }
        });
        
        //Assign the product id to the contact us link
        //Can't do it in jsp because it's a CI include
        var productId = $("#productId").html();
        $("#emailDiv a").each(function() {
        	var html = $(this).html();
        	this.href = this.href + "?PRODUCT%3C%3Eprd_id=" + productId;
        	$(this).html(html);
		});
    },

    // event handler for tab click event
    tabClicked : function (tab) {
        var jObj = jQuery(tab);
        var tabIndex = this.tabDivs.index(tab);
        if (tabIndex == this.selectedTabIndex) {
            return;
        }

        this.hideDivs(this.selectedTabIndex);
        this.showDivs(tabIndex);
        this.selectedTabIndex = tabIndex;
    },

    // show content div and highlight corresponding tab
    showDivs : function (tabIndex) {
        var tab = this.tabDivs.get(tabIndex);
        var content = this.contentDivs.get(tabIndex);
        jQuery(tab).attr("class", "tabHeaderSelected");
        jQuery(content).show();
    },

    // hide content div and unhighlight corresponding tab
    hideDivs : function (tabIndex) {
        var tab = this.tabDivs.get(tabIndex);
        var content = this.contentDivs.get(tabIndex);
        jQuery(tab).attr("class", "tabHeader");
        jQuery(content).hide();
    }
};
