var n_prevTab; 			// last active tab
var n_prevSubTab;		// last active subtab
var timeout;					// timeout have been assigned to a var so it can be cleared if a tab is clicked
var n_numTabs 	= 5; 	// number of Tabs
var a_priceSubs 	= new Array('stock', 'options', 'broker');

function changeTab(n_id, s_stop){
	switchTab(n_id, s_stop); // fade in new tab
	if (s_stop !== "TRUE") {
		if (n_id === 2) priceSubTab(0); // if on price tab, run through sub tabs
		(n_id === n_numTabs) ? n_nextId = 1 : n_nextId = n_id + 1; // figure out next tab to move to.  if current tab is the last tab then back to first.
		if (n_id !== 2) timeout = setTimeout("changeTab(" + n_nextId + ")", 7000); // If not set to stop then wait 7 second and call this function again to move to next tab.
	} else {
		clearTimeout(timeout);
	}
}

function getBlock(s_type, n_id) { 
	return document.getElementById("blockup" + s_type + n_id);
}

function switchTab(n_id, s_stop){	
	 // clear previous tab
	if (n_prevTab) {
		getBlock('InnerTabs', n_prevTab).className = 'tab' + n_id;
		getBlock('InnerContent', n_prevTab).style.display = 'none';	
		changeOpac(0, 'blockupInnerContent' + n_prevTab);
		getBlock('FooterContent', n_prevTab).style.display = 'none';
	}
	n_prevTab = n_id; //set previous tab var with the current id

	// show current tab
	getBlock('InnerTabs', n_id).className = 'tab' + n_id + ' current';
	getBlock('InnerContent', n_id).style.display = 'block';
	(s_stop !== "TRUE") ? opacity('blockupInnerContent' + n_id, 0, 100, 700) :	changeOpac(100, 'blockupInnerContent' + n_id);
	getBlock('FooterContent', n_id).style.display = 'block';
}

function priceSubTab(n_id){
	switchPriceSubTab(n_id); // hide old tab show new one
	n_id++; // increment to the next id
	
	// if we have reached the third sub tab then move onto the tools main tab otherwise grab next sub tab.
	if (n_id >= a_priceSubs.length) {
		timeout = setTimeout("changeTab(3)", 7000);
	} else {
		timeout = setTimeout("priceSubTab(" + n_id + ")", 7000);
	}
}

function switchPriceSubTab(n_id) {
	clearTimeout(timeout);
	// if previous sub tab hide
	if (a_priceSubs[n_prevSubTab]) {
		document.getElementById(a_priceSubs[n_prevSubTab] + 'PriceList').style.display = 'none';
		document.getElementById(a_priceSubs[n_prevSubTab] + 'PriceLink').className = '';
	}
	n_prevSubTab = n_id;

	// show current sub tab
	document.getElementById(a_priceSubs[n_id] + 'PriceList').style.display='block';
	document.getElementById(a_priceSubs[n_id] + 'PriceLink').className='current';
	document.getElementById('price-graphic').src='/Images/HomePage/price-'+ a_priceSubs[n_id] +'-trades.jpg';
}


/* OPACTITY FUNCTIONS */
function opacity(id, opacStart, opacEnd, millisec) {
    var speed = Math.round(millisec / 100);    //speed for each frame
    var timer = 0;

    //determines fade direction
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--, timer++) setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++, timer++) setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
    }
}

function changeOpac(opacity, id) {
    var el = document.getElementById(id).style;
    el.opacity = (opacity / 100);
    el.MozOpacity = (opacity / 100);
    el.KhtmlOpacity = (opacity / 100);
    el.filter = "alpha(opacity=" + opacity + ")";
}

/* ON WINDOW LOAD */
window.onload = function(){
	changeTab(1);
};