var tabs = new tabWidget();

function tabWidget() {
    this.contentDivID = '';
    this.baseUrl = '';
    this.startingPage = '';
    this.showTabs = true;
    this.currentPage = '';
    this.loadingContent = false;
	
	
    this.afterPageLoad = function(responseText) {
        var className = 'page_'+tabs.currentPage.replace(/\W/g, '_');

        if (typeof themeTabAfterLoadCallback == 'function') {
            themeTabAfterLoadCallback();
        }
        
        $j('body').removeClass();
        $j('body').addClass(className);
        $j('body').find('a').removeClass('mousewait');
        $j('body').children().removeClass('mousewait');
        $j('body').removeClass('mousewait');
        
        tabs.loadingContent = false;

        // When loading content via ajax some browsers do not reposition the page
        // back at the top of the screen, so we do this here.
        scrollTo(0,0);

        // Used to calculate page load times
        finishPageDebug(true);

        $j('#'+tabs.contentDivID).css('display', 'block');
        $j(document).append('<script defer type="text/javascript" src="/js/pngfix.js"></script>');
    }
    

    // Figures out which tab to highlight based on url hash value
    this.highlightTab = function(hash) {
        if (this.showTabs != true)
            return;

        $j('#header').show();
        $j('#'+tabs.contentDivID).removeClass();
        $j('#'+tabs.contentDivID).addClass(tabs.contentDivID);

        $j('ul.tabs > li > a').each(function(i) {
            var hrefHash = this.href.replace(/^.*#/, '');
            if (hrefHash == hash && !$j(this).parent().hasClass('current'))
                $j(this).parent().addClass('current');
            else if (hash.indexOf(hrefHash.replace(/index$/, '')) == 0 && hrefHash != "/index")
                $j(this).parent().addClass('current');
            else if (hrefHash != hash)
                $j(this).parent().removeClass('current');

            if (typeof themeTabHighlightCallback == 'function') {
                themeTabHighlightCallback();
            }
        });
    }
    

    // Called on first page load.  Figures out what content to display
    this.setStartTab = function() {
        var DivID = '#' + this.contentDivID;
        
        if (window.location.hash != '') {
            var hash = window.location.hash.replace(/^#/, '');
            this.currentPage = hash;
            $j(DivID).load(hash, function(){this.afterPageLoad()});
            if (this.showTabs == true)
                this.highlightTab(hash);
        }
        else if (this.showTabs == true && this.startingPage == '') {
            var first = $j('ul.tabs > li > a')[0];
            var firstHref = first.href.replace(/^.*#/, '');
            this.currentPage = firstHref;
            $j(DivID).load(firstHref, function(){this.afterPageLoad()});
            if (this.showTabs == true)
                $j(first).parent().addClass('current');
        }
        else {
            this.currentPage = this.startingPage;
            tabs.afterPageLoad();
            if (this.showTabs == true)
                this.highlightTab(this.startingPage);
        }
    }
    
    
    // Callback to load page contents when a link is clicked. The 'this' variable gets
    //   clobbered, so use 'tabs' global variable instead.
    this.pageload = function(hash) {
        // Used to calculate page load times
        startPageDebug(true);

        // Does the tab exist for this content?
        setTimeout(function() {
            if($j('.current').text() == '') {
                // If not, hide tabs and set up the content area
                $j('#header').hide();
                $j('#'+tabs.contentDivID).removeClass();
                $j('#'+tabs.contentDivID).addClass('inner-frame-no-tab');
            }
        }, 0);

        if (hash == '')
            hash = tabs.startingPage;
 
        tabs.currentPage = hash;
        $j('#'+tabs.contentDivID).load(hash, tabs.afterPageLoad);

        // track tab page views
        try {
            statsTrackPageView(hash.substr(1).replace(/\//g, ' / '));
        } catch(err) {}

        
        if (tabs.showTabs == true)
            tabs.highlightTab(hash);
    }
    
    
    this.init = function(options) {
        this.contentDivID = options.contentDivID;
        this.baseUrl = options.baseUrl;
        this.startingPage = options.startingPage;
        this.showTabs = options.showTabs;

        // Used to calculate page load times
        startPageDebug(true);

        $j.historyInit(this.pageload, this.baseUrl);
        
        var urlWithHash = this.baseUrl + '#/';		
        $j("a[href*='"+urlWithHash+"']").live('click', function() {
            if (tabs.loadingContent == true) { return false; }
            tabs.loadingContent = true;

            var hash = this.href;
            hash = hash.replace(/^.*#/, '');
            setTimeout(function() {
                $j('body').addClass('mousewait');
                $j('body').children().addClass('mousewait');
                $j('body').find('a').addClass('mousewait');
            }, 0);
            $j.historyLoad(hash);

            return false;
        });
        
        if (window.location.hash == '')
            this.setStartTab();
    }
}

