var activeProductTab = null;

jQuery().ready(function(){
    if ($("#tab_audios").length==0) {
        $("#prodfunc_audios").remove();
    }
    else {
        $("#prodfunc_audios").show();
    }
    if(!description_available) {
        $("#tab_description").remove();
    }
    if ( !window.isPrintView ) {
        $(".productblock[id^=tab_]").hide();
    }
    else {
        $(".productblock[id^=tab_]").show();
        return;
    }
    var jTabList = $(".productblock[id^=tab_]").filter("[id!=tab_sample_pages]").filter("[id!=tab_audios]");
    var numTabs = jTabList.size();
    var divider = $("<div class='divider'>|</div>");
    var hook = $("#productsubcats");
    var firstTabId;
    var i=0;
    jTabList.each(function(){
        // Create an entry in "productsubcats" for each tab
        var curTabId = this.id;
        firstTabId = firstTabId ? firstTabId : curTabId;
        var linktext = $(this).children("p.header:first").text();
        var anchorname = $(this).children("a.pb_anchor:first").attr("name");
        var aTag = $(
            "<div class='productsubcat' id='a_" + anchorname.substring(5) + "'>" + 
                "<a href='#" + anchorname + "'>" + linktext + "</a>" + 
            "</div>"
        );
        hook.append(aTag);
        aTag.children("a:first").click(function(eventObj){
            eventObj.preventDefault();
            var oldProductTab = activeProductTab;
            var newTabId = "tab_" + $(this).parent().attr("id").substring(2);
            setActiveProductTab(newTabId);
            if (oldProductTab.attr("id") == curTabId) {
                document.location.href = "#" + anchorname;
            }
        });
        i++;
        if (i<numTabs) {
            hook.append(divider.clone());
        }
    });
    var nextTabId = firstTabId;
    var scrollUp = false;
    if (window.location.hash && window.location.hash.substring(0,6)=="#show_") {
        nextTabId = "tab_" + window.location.hash.substring(6);
        scrollUp = true;
    }
    setActiveProductTab(nextTabId);
    if ($(".productblock[id^=tab_]").size()>1) {
        hook.show();
    }
    if (scrollUp) {
        document.location.href="#top";
    }
});
    
function setActiveProductTab(tabId) {
    if(activeProductTab) {
        var a_id_old = "a" + activeProductTab.attr("id").substring(3);
        $("#" + a_id_old).removeClass("productsubcatselected").addClass('productsubcat');
        activeProductTab.hide();
    }
    var a_id_new = "a" + tabId.substring(3);
    $("#" + a_id_new).removeClass('productsubcat').addClass('productsubcatselected');
    activeProductTab = $("#" + tabId);
    activeProductTab.fadeIn();
}