/******************************************************************************
*  homepage-tabs.js
* 	
*   Displays tabs in the homepage...
******************************************************************************/

/*
 * Set GLOBAL variables
 */
var _curTab = 1;
var _maxTabs = 4;
var _t;
var _clicked = false;
var _tabDelay = 15000;
var _tabDelayMin = 500;

/*
 * Core fucntions & Methods
 */
$(document).ready(function() {
    //alert("in ready");
    $('#tabs ul li a').click(function(c) {
        var tabNum = $(this).attr("rel");
        ShowTab(tabNum);

        // clear the timeout now so we don't keep switching tabs automatically
        clearTimeout(_t);
        _clicked = true;

        c.stopPropagation();

        return false;
    });

    $('.txt-feature').mouseenter(function() {
        if (!_clicked) clearTimeout(_t);
    });

    $('.txt-feature').mouseleave(function() {
        if (!_clicked) _t = window.setTimeout(function() { ShowTab(_curTab, true) }, _tabDelayMin);
    });

    // show the first tab...
    ShowTab(1, true);
});

function ShowTab(num, onload) { 
    
    // loop over all the document divs and hide them
    $('.top').children().each(function() {
        $(this).hide();
    });
    // reset all tab classes
    $('#tabs ul li a').each(function() {
        $(this).removeClass('active');
    });

    // show the one we just clicked on
    $('#data-' + num).fadeIn(200);

    // select the tab
    $('.tab-' + num + ' a').addClass('active');

    // we want to show the tabs automatically onload
    // if someone clicks one of the tabs, the auto-show
    // will stop (clearTimeout)
    if (onload) {
        _curTab++;
        _t = window.setTimeout(function() { ShowTab(_curTab, true) }, _tabDelay);
        if (_curTab == parseInt(_maxTabs)+1) _curTab = 1; // reset the counter since we only support 4 tabs
    } else {
        _curTab=num;
    }

    return false;
}

/*
 * Mimics the trim function in most languages...
 */
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
