var selectedTab;
var displayedTabID;

var tabcontentIDs=new Object()

function changeTab(linkobj){
	// Ensure Selected Tab Is Not Chosen
	if(displayedTabID != linkobj.getAttribute("rel")){
		selectedTab = linkobj;
		fadeTabOut();
	}
}

function showTab(p_linkobj, p_init){
	
	var linkobj = p_linkobj //(arguments.length == 1) ?  : selectedTab;	// Determine If Set Hard OR Came From Focus Out		
	var ulid = linkobj.parentNode.parentNode.id //id of UL element
	var resourceContainerID;

	var ullist = document.getElementById(ulid).getElementsByTagName("a")

	for (var i=0; i<ullist.length; i++){
				
		if(ullist[i] == linkobj){
			resourceContainerID = i;
		}
				
		ullist[i].parentNode.className= "tabInactive"  //deselect all tabs
		
		if (typeof tabcontentIDs[ulid][i]!="undefined"){
			document.getElementById(tabcontentIDs[ulid][i]).style.display = "none" //hide all tab contents
		}
	}
	
	// Set Tab Nav Background : on UL
	linkobj.parentNode.parentNode.className = linkobj.getAttribute("rel") + "Selected";
	linkobj.parentNode.className = "tabActive";

	document.getElementById(linkobj.getAttribute("rel")).style.display = "block" //expand corresponding tab content

	displayedTabID = linkobj.getAttribute("rel") // Save Tab ID
	setCookie(ulid, linkobj.getAttribute("rel")) // Save Cookie Value
	
	
	if(!p_init) fadeTabIn();

	// Update Resources Right Column If Needed
	if(typeof($.showResource) == "function"){
		$.showResource(resourceContainerID);
	}
}

function pushTabOpen(tabcontentid, tabnumber){ //interface for selecting a tab outside .js  (plus expand corresponding content)
	var thetab=document.getElementById(tabcontentid).getElementsByTagName("a")[tabnumber]
	
	if (thetab.getAttribute("rel")){
		changeTab(thetab);
	}
}

function savetabcontentids(ulid, relattribute){// save ids of tab content divs
	if (typeof tabcontentIDs[ulid]=="undefined"){ //if this array doesn't exist yet
		tabcontentIDs[ulid]=new Array()
	}
	tabcontentIDs[ulid][tabcontentIDs[ulid].length]=relattribute
}

function getullistlinkbyId(ulid, tabcontentid){ //returns a tab link based on the ID of the associated tab content
	var ullist=document.getElementById(ulid).getElementsByTagName("li")

	for (var i=0; i<ullist.length; i++){
		if (ullist[i].getElementsByTagName("a")[0].getAttribute("rel")==tabcontentid){
			return ullist[i].getElementsByTagName("a")[0]
			break
		}
	}
}

function initTabControl(){

	for (var i=0; i<arguments.length; i++){ //loop through passed UL ids
	
		//if (getCookie(arguments[i])!=""){ //clean up cookie if persist=off
		//	setCookie(arguments[i], "")
		//}
		
		var clickedontab = getCookie(arguments[i]) //retrieve ID of last clicked on tab from cookie, if any
	
		var ulobj = document.getElementById(arguments[i])
		var ulist = ulobj.getElementsByTagName("li") //array containing the LI elements within UL

		for (var x=0; x<ulist.length; x++){ //loop through each LI element
			var ulistlink = ulist[x].getElementsByTagName("a")[0]
			
			// Default Tab To Not Show
			document.getElementById(ulistlink.getAttribute("rel")).style.display = "none"
			
			if (ulistlink.getAttribute("rel")){
				savetabcontentids(arguments[i], ulistlink.getAttribute("rel")) //save id of each tab content as loop runs
				
				ulistlink.onclick=function(){
					changeTab(this)
					return false
				}

				if (ulist[x].className=="selected" && clickedontab==""){ //if a tab is set to be selected by default
					showTab(ulistlink, true) //auto load currenly selected tab content
				}
			}
		} //end inner for loop
				
		if (clickedontab!=""){ //if a tab has been previously clicked on per the cookie value
			var culistlink=getullistlinkbyId(arguments[i], clickedontab)
			
			if (typeof culistlink!="undefined"){ //if match found between tabcontent id and rel attribute value
				showTab(culistlink, true)
				setCookie(ulobj.id, culistlink.getAttribute("rel")) // Save Cookie Value	
			}else{ //else if no match found between tabcontent id and rel attribute value (cookie mis-association)
				showTab(ulist[0].getElementsByTagName("a")[0], true) //just auto load first tab instead
			}
		}
	} //end outer for loop
}

//*******************************************************************************************
// Browser safe opacity handling function
function setTabOpacity( p_value, p_incrament, p_direction ) {
	
	if(isIE){
		document.getElementById(displayedTabID).style.filter = 'alpha(opacity=' + p_value * 10 + ')'; // For IE	
	}else{
		document.getElementById(displayedTabID).style.opacity = p_value / 10;	
	}

	if(p_incrament == 100 && p_direction == "out"){
		showTab(selectedTab);
	}else if(p_incrament == 100){
		if(isIE){
			// We have to remove the "filter" attribute in IE to enable clearType font again.  !Stupid!
			document.getElementById(displayedTabID).style.removeAttribute('filter');		
		}
	}
}

function fadeTabIn() {	
	setTabOpacity( 0 );
	clearAllTimeouts();
	for( var i = 0 ; i <= 100 ; i++ ){
		timeOuts["time" + i] = setTimeout( 'setTabOpacity(' + ( i / 10) + ',' + i + ', "in"' + ')' , 4 * i);
	}
}

function fadeTabOut() {
	clearAllTimeouts();
	for( var i = 0 ; i <= 100 ; i++ ) {
		timeOuts["time" + i] = setTimeout( 'setTabOpacity(' + (10 - i / 10) + ',' + i + ', "out"' + ')' , 2 * i);
	}
}