// holds the ajax object
var ajax = new sack();

// all timers needed for open or close a tab or open or close a content blck
var tOpen,tClose,tHigher,tShorter;
// indicates whenever an animation in proccess
var animating=false;

// holds the index of the content as in the TABS_CONTENTS_ID array
var selectedTabContentIndex=-1;
// holds the selected tab 
var selectedTab;
// holds the newly temporary selected tab - just when mouse got over him
var newSelectedTab;

var ANIMATION_DELTA=10;

// user with IE - make animation faster
if(navigator.userAgent.toLowerCase().indexOf('msie')>=0)
{
	ANIMATION_DELTA*=10;
	TO_ANIMATION/=5;
	TO_FADING/=1.3;
	
}else{

	// use catching...faster and anyways donesn't going to change so...
	ajax.encodeURIString = false;
}
/* 
 * Return the index of the current open tab content.
 * The index is reffering to his position in the TABS_CONTENTS_ID array.
 */
function getTabContentIndex(tabIndex)
{
	return parseInt((tabIndex-1)/3);
}
/* 
 * Return the current open tab content object.
 */
function getTabContent()
{
	return document.getElementById(TABS_ROWS[selectedTabContentIndex][0]);
}
/*
 * Select the specified tab with the default time out as in the TIME_OUT var.
 */
function tabSelect(tabIndex)
{
	tabSelectTimeout(tabIndex,TO_OPEN);
}
/*
 * Select the given tab with the given time out amount.
 * This method will open the desire tab, close the old one and will show the
 * relvent content - of the tab selected.
 */
function tabSelectTimeout(tabIndex,timeOut)
{
	objTab = document.getElementById('tmTab'+tabIndex);
	// cancel other openings
	cancelOpening();
	
	// if the desire tab is not the one already open and no animation is currectly active
	if(selectedTab!=objTab && !animating)
	{
		// save tab object for class modifing
		newSelectedTab=objTab;
		// open the current tab after a while...
		tOpen=setTimeout("openTab("+tabIndex+")",timeOut);
	}
}
/*
 * Open the selected tab, while unselecing the old selected tab.
 */
function openTab(tabIndex)
{	
	// change opened tab class
	if(selectedTab)
		selectedTab.className="tmItem";
	
	// change the new select tab line to a selected one
	newSelectedTab.parentNode.className="tmTabsLine";
	
	// save the old tab content index
	oldTabContentIndex = selectedTabContentIndex;
	// get the new tab content index
	newTabContentIndex = getTabContentIndex(tabIndex);

	// if tab content change, close the previous one
	if(oldTabContentIndex>=0 && newTabContentIndex!=oldTabContentIndex)
	{
		// close previous tab
		closeTab();
	}
	
	// update tab content index
	selectedTabContentIndex = newTabContentIndex;
	
	// open new tab content (with content)
	animateHeight(getTabContent(),180,ANIMATION_DELTA);	

	// update selected tab to newly opened tab
	selectedTab = newSelectedTab;
	
	// change new selected tab class
	if(selectedTab) /* IE BUG */
		selectedTab.className="tmItem tmItemSelected";
		
	loadTabContent(tabIndex);
}
/*
 * Loads the content of the given tab index.
 */
function loadTabContent(tabIndex)
{
	getTabContent().innerHTML="";

	tabsDesc = ['adv_join','adv_qualify','adv_earn','aff_join','aff_qualify','aff_earn'];
	
	ajax.method="GET";
	ajax.setVar('tab',tabsDesc[tabIndex-1]);
	//ajax.element = getTabContent().id;
	ajax.onCompletion=updateTabContent;
	ajax.requestFile = "tab_page.php";
	ajax.runAJAX();
}
function updateTabContent()
{
	tabContent = getTabContent();
	if(tabContent.style.visibility=="visible")
	{

		if(FADING_EFFECT)	changeOpac(0,tabContent.id);
		tabContent.innerHTML=ajax.response;
		if(FADING_EFFECT)	opacity(tabContent.id,0,100,TO_FADING);
		
	}else{
		setTimeout("updateTabContent()",100);
	}
}
/*
 * close the current open tab.
 */
function closeTab()
{
	// there is an open tab content
	if(selectedTabContentIndex>=0)
	{
		selectedTab.parentNode.className="tmTabsLine tmTabsLine_unselected";
		// get tab content
		selectedTabContent = getTabContent();
		// clear tab content
		selectedTabContent.innerHTML="";
		// close tab content
		animateHeight(selectedTabContent,0,-ANIMATION_DELTA);
		// reset selected tab
		selectedTabContentIndex=-1;
	}
}
/*
 * animate the height of the given object to the desire height by the
 * given delta and update his content after finish to the given one.
 * if Delta is negetive then it will shorten the desire object.
 */
function animateHeight(obj,height,delta)
{
	// animating is on
	animating=true;
	
	/* IE 6 BUG */
	obj.style.display="block";
	/* -------- */
	
	// hide the object content
	hideObjectContent(obj.id);

	// if delta is position - higher
	if(delta>0)
		tHigher = setInterval("_animateHeight('"+obj.id+"',"+height+","+delta+")",TO_ANIMATION);
	// delta is negetive - shorter
	else
		tShorter = setInterval("_animateHeight('"+obj.id+"',"+height+","+delta+")",TO_ANIMATION);
}
/*
 * This method is actually changing the height of the object recurcivly with a timer
 * until the desire height is reach. Then the content of the object gets visible again
 * unless the desire height is 0.
 */
function _animateHeight(objId,height,delta)
{	
	// get the desire object by his given id
	obj = document.getElementById(objId);
	// get his old height
	oldHeight = getNumericValue(obj.style.height);
	
	// get the new height as given from the old one and the delta
	newHeight = oldHeight+delta;
	if(delta>0)
		newHeight = Math.min(height,newHeight);
	else
		newHeight = Math.max(0,newHeight);

	// change object height to the new height
	obj.style.height=newHeight+"px";
	
	// if the object is in the desire height - we done
	if(newHeight==height)
	{
		// animating is off
		animating=false;
		// stop the heighing
		clearInterval(delta>0?tHigher:tShorter);
		
		// if new height is bigger then 0 (the object is visible)
		if(newHeight>0)
		{
			// show the object content
			showObjectContent(objId);
		}else{ /* IE 6 BUG */
			obj.style.display="none";
		}
	}
}
/*
 * hide the content of the given object depends on the FADING_EFFECT var.
 */
function hideObjectContent(objId)
{
	// hide the content of the obj
	document.getElementById(objId).style.visibility="hidden";
	if(FADING_EFFECT)
		changeOpac(0,objId);
}
/*
 * Show the content of the given object depends on the FADING_EFFECT var.
 */
function showObjectContent(objId)
{	
	// show the content of the object if were hiden
	obj = document.getElementById(objId);
	/*if(FADING_EFFECT)
		changeOpac(0,objId);*/
	obj.style.visibility="visible";
	
	/* IE 6 BUG */
	obj.style.display="block";
	/* -------- */
	
	// if fading effect is on do it with fading
	/*if(FADING_EFFECT)	
		opacity(objId,0,100,TO_FADING);*/
}
/*
 * Retrurn the numeric part of the given string as an integer.
 */
function getNumericValue(str)
{
	re = new RegExp("[^0-9]","g");
	str=str.replace(re,"");

	return parseInt(str==""?0:str);
}
/*
 * Cancel the closing of the current closing tab.
 */
function cancelClosing()
{
	clearTimeout(tClose);
}
/*
 * Cancel currently trying to open tabs.
 */
function cancelOpening()
{
	clearTimeout(tOpen);	
}
/*
 * Initialize the tab menu.
 */
function initTabsMenu()
{
	// add event listener to content that won't close when they are at focus
	for(i=0;i<TABS_ROWS.length;i++)
		addEventListener(document.getElementById(TABS_ROWS[i][0]),"mouseover",cancelOpening,false);
	
	// open the default tab
	openDefaultTab();
}
/*
 * Add the given handler as the event handler of the given evenName to the given
 * object with the capture propery (FF ONLY).
 * This method works for IE and FF.
 */
function addEventListener(obj, eventName, handler, capture)
{
	// FF
	if(obj.addEventListener)
		obj.addEventListener(eventName,handler,capture);
	// IE
	else if (obj.attachEvent)
		obj.attachEvent('on' + eventName, handler);
}
/*
 * Opens the default tab as specified in the DEFAULT_TAB_ID and the 
 * DEFAULT_TAB_INDEX vars.
 */
function openDefaultTab()
{
	// select the default tab with some time out big enuf to prevent bug related
	// to speed
	tabSelectTimeout(selectedTabContentIndex<0?DEFAULT_TAB_INDEX:TABS_ROWS[selectedTabContentIndex][1],TO_DEFAULT);
}