// last active tab object
var objLastActiveTab;

// tab images
var imgLeft = '<img src="'+img_basedir+'images/tabs/background-left.gif" alt="background-left" class="tab_background_left" width="1" height="29">';
var imgRight = '<img src="'+img_basedir+'images/tabs/background-right.gif" alt="background-left" class="tab_background_right" width="3" height="29">';

/**
 * Tab constructor which goes through an unordered list, and saving
 * the contents into a dhtml structure.
 *
 * @return			True or false
 */
function tabsConstr() {
	
	// object containing the unordered list
	var objTab = ge('tabs');

	if (objTab !== null) {
	
		// get unordered list of headers and content
		var objUnorderedList = objTab.getElementsByTagName('ul');
		
		// check if unordered list exists
		if (objUnorderedList.length > 0) {

			// the first unordered list must be the tabs
			var objUL = objUnorderedList[0];
			
			// get all list elements
			var objListItems = objUL.getElementsByTagName('li');
			var objLI = new Array();
			
			// only get the list items with the tab unordered list as it's parent
			for (var i=0; i<objListItems.length; i++) {
				if (objListItems[i].parentNode == objUL) { // list element
					objLI.push(objListItems[i]);
				}
			}
			
			// split of the headers and the tab contents
			var tabHeaders = new Array();
			var tabContents = new Array();
			
			var curTabText;
			var curTabContent;
			

			for (var i=0; i<objLI.length; i++) {
				// get header
				var curHeader = objLI[i].getElementsByTagName('h3');
				
				// headers are required for tabs
				if (curHeader.length > 0) {
					// remove the header from the content area
					curTabText = curHeader[0].innerHTML;
					var searchText = new RegExp('<h3>'+curTabText+'</h3>',['i']);
					curTabContent = objLI[i].innerHTML.replace(searchText,'');
					
					// separate headers and contents
					tabHeaders.push(curTabText);
					tabContents.push(curTabContent);
				}
			}
			
			var objTabLinks = ge('tab_links');
			var objTabContent = ge('tab_content');
			for (var i=0; i<tabHeaders.length; i++ ) {
				objTabLinks.innerHTML += imgLeft +'<h1 id="tab_swap_' + i + '">' +  tabHeaders[i]  + '</h1>' + imgRight;
				objTabContent.innerHTML += '<div id="tab_content_area_' + i + '">' + tabContents[i] + '</div>';
			}
			var objTabSwap;
			for (var i=0; i<tabHeaders.length; i++) {
				
				objTabSwap = ge('tab_swap_' + i);
				objTabSwap.setAttribute('tab_offset', i);
				objTabContentArea = ge('tab_content_area_' + i);
				objTabContentArea.setAttribute('tab_offset', i);
				
				// attach click handlers
				if (window.addEventListener) {
					objTabSwap.addEventListener('click', tabSwap, false);
					objTabSwap.addEventListener('mouseover', tabOver, false);
					objTabSwap.addEventListener('mouseout', tabOut, false);
				}
				else {
					objTabSwap.attachEvent('onclick', tabSwap);
					objTabSwap.attachEvent('onmouseover', tabOver);
					objTabSwap.attachEvent('onmouseout', tabOut);
				}
			}
			if (tabHeaders.length > 0) { 
				tabActivate(0);
				ge("tabHTML").innerHTML = "";
			}
			//objTab.style.display = 'none';
			return true;
		}
		else {
			//tabs do not exist, do not do anything
			return false;
		}
	}
}

/**
 * Changes the color of the tab, swaps the content
 * area with the new tab's text, hides the old content
 * and disables the old tab (if it were selected).
 *
 * @param	offset	Tab array reference to activate
 */
function tabActivate(offset) {
	if (objLastActiveTab != null) {
		var objLastTab = ge('tab_swap_'+objLastActiveTab.getAttribute('tab_offset'));
		objLastTab.className = 'tab_inactive';
		objLastActiveTab.style.display = 'none';
	}
	objLastActiveTab = ge('tab_content_area_' + offset);
	objLastActiveTab.style.display = 'block';
	ge('tab_swap_' + objLastActiveTab.getAttribute('tab_offset')).className = 'tab_active';
}

/**
 * Event listener for clicking on a tab
 *
 * @param	e	Event object
 */
function tabSwap(e) {
	var objNewContent;
	var tabOffset;
	e[e_moz] ? tabOffset = e[e_moz].getAttribute('tab_offset') : tabOffset = e[e_ie]['tab_offset'];
	tabActivate(tabOffset);
}

/**
 * Event listener for hovering over an inactive tab
 *
 * @param	e	Event object
 */
function tabOver(e) {
	var tabOffset;
	e[e_moz] ? tabOffset = e[e_moz].getAttribute('tab_offset') : tabOffset = e[e_ie]['tab_offset'];
	
	// make sure this is not already active
	if (tabOffset != objLastActiveTab.getAttribute('tab_offset')) {
		//change color
		var objCurrentTab = ge('tab_swap_'+tabOffset);
		objCurrentTab.className = 'tab_hover';
	}	
}
/**
 * Event listener for hovering off an inactive tab
 *
 * @param	e	Event object
 */
function tabOut(e) {
	var tabOffset;
	e[e_moz] ? tabOffset = e[e_moz].getAttribute('tab_offset') : tabOffset = e[e_ie]['tab_offset'];
	
	// make sure this is not already active
	if (tabOffset != objLastActiveTab.getAttribute('tab_offset')) {
		//change color
		var objCurrentTab = ge('tab_swap_'+tabOffset);
		objCurrentTab.className = 'tab_inactive';
	}
}