﻿var ajaxTarget = "tabMainArea";
var tabMode = "linkAll";
var selectedTab;
var tabLinks;

$j(document).ready(function() {

    tabLinks = $j('a.tabLink');

    tabLinks.click(function() {
        return openTab(this);
    });

    //get first tab
    if (showobject) {
        var firstTabIndex = 0;
        var firstTab = "";
        var tabName = "";
        var tabExists = true;

        //no direct tab linking for linkPrev mode
        if (tabMode == "linkAll") {
            //check for tab parameter - only if query plugin present

            if ($j.query) {
                tabName = $j.query.get('tab');
                var pathName = window.location.pathname;
                if (pathName.charAt(pathName.length - 1) != "/") {
                    pathName += "/";
                }
                firstTab = pathName + tabName;
            }
            if ($j('#tab-' + tabName).size() == 0) {
                tabExists = false;
                firstTab = $j('a.tabLink:first').attr("href");
            } else {
                firstTabIndex = tabLinks.index($j('#tab-' + tabName));
            }
        } else {
            firstTab = $j('a.tabLink:first').attr("href");
            disableNextTabs(tabLinks.get(firstTabIndex));
            tabExists = false;
        }
        //else open first tab
        showobject('#tabMainArea', firstTab, true);
        selectedTab = firstTabIndex;

        if (tabExists) {
            $j('#tab-' + tabName).parent().addClass('active');
            $j('#tab-' + tabName).each(highlightTab);
        } else {
            $j('a.tabLink:first').parent().addClass('active');
            $j('a.tabLink:first').each(highlightTab);
        }
    }
    
});

function highlightTab() {
    if (!this.tabSet) {
        var tabWidth = $j(this).outerWidth();
        if (window.XMLHttpRequest) {
            // Note, IE 6 7 and 8 all render this in completely different ways. only 8 does it in a standards compliant fashion, 6 is addressed below, 7 is addressed by a CSS addressing underlineStatusSelection
            $j(this).parent().append('<div id="underlineStatusSelection" style="width: ' + tabWidth + 'px;">\n' +
                '  <div style="margin-top:-24px;position: absolute; bottom: 0px; height: 4px; width: ' + tabWidth + 'px;">\n' +
                '    <div style="margin: 0 4px; background-color: #cc0000;"></div>\n' +
                '    <div style="margin: 0 2px; background-color: #cc0000;"></div>\n' +
                '    <div style="margin: 0 1px; background-color: #cc0000;"></div>\n' +
                '    <div style="margin: 0; background-color: #cc0000;"></div>\n' +
                '  </div>\n' +
                '</div>');
        } else {
            // IE 6 sucks

            $j(this).parent().append('<div style="left: -' + (tabWidth / 2) + ';height: 35px;  position: relative; width: ' + tabWidth + 'px;">\n' +
                '  <div style="margin-top:-24px;position: absolute; bottom: 0px; height: 4px; width: ' + tabWidth + 'px; font-size:0px;">\n' +
                '    <div style="margin: 0 4px; background-color: #cc0000; font-size:0px;"></div>\n' +
                '    <div style="margin: 0 2px; background-color: #cc0000; font-size:0px;"></div>\n' +
                '    <div style="margin: 0 1px; background-color: #cc0000; font-size:0px; "></div>\n' +
                '    <div style="margin: 0; background-color: #cc0000; font-size:0px;"></div>\n' +
                '  </div>\n' +
                '</div>');
        }
        this.tabSet = true;
    }
}

function disableNextTabs(tgtTab) {
    //$j(tgtTab).css("cursor", "default");
    //get index of <a>'s grandparent (<td>) within <tr>
    var tabIndex = $j(tgtTab).parent().parent().parent().children().index($j(tgtTab).parent().parent());
    //then disable links on all <td>'s after index
    $j(tgtTab).parent().parent().nextAll().andSelf().find("a").css("cursor", "default");
}


//use openTab($j('#tabname').get(),true) to call from page
function openTab(sourceLink,forceChange) {
    if (sourceLink == null) {
        sourceLink = this;
    }

    if (showobject) {
        var thisIndex = tabLinks.index($j(sourceLink));
        if (tabMode == "linkAll" || (tabMode == "linkPrev" && thisIndex < selectedTab) || forceChange) {
            selectedTab = thisIndex;
            tabLinks.parent().removeClass('active');
            showobject('#' + ajaxTarget, $j(sourceLink).attr("href"), true);
            $j(sourceLink).parent().addClass('active');
            $j(sourceLink).each(highlightTab);
            //enable tab link styles
            tabLinks.css("cursor", "pointer");
            //then selectively disable link style
            if (tabMode == "linkPrev") {
                disableNextTabs(sourceLink);
            }
        }
        return false;
    }
}

function showItem(className) {
    $j(className).show();
    $j('.lastPara').hide();
}


