var global_current_menu_tab_id = "";

function decSelMenuTab(id){
	global_current_menu_tab_id = id;
}

function activeTab(me, fn){
	var table=me.parentNode;
	var cells=table.getElementsByTagName('td');
	for(var i=0; i<cells.length; i++){
		cells[i].className="menu_generaltabs_alltabs_table_td";
	}
	me.className="menu_generaltabs_alltabs_table_td_active";
	if(fn) eval(fn);
}

var menu_tabs=new Array();

function readListToMenuTab(){
	var mainobj=document.getElementById('principal_contentmenu');
	var uls=mainobj.getElementsByTagName('ul');
	var tabs_table_tr=document.getElementById('menu_table_tabs').getElementsByTagName('tr')[0];
	for(var i=0; i<uls.length; i++){
		var ul_id=uls[i].id;
		var lis=uls[i].getElementsByTagName('li');
		var new_td_id="menutab_"+ul_id.substring(ul_id.lastIndexOf('_')+1, ul_id.length);
		var td = document.createElement("TD");
		td.className="menu_generaltabs_alltabs_table_td";
		td.setAttribute('id', new_td_id);
		var content_cell=extractName(lis[0]);
		td.innerHTML=content_cell;
		td.title=content_cell;
		td.onclick=function(){
			decSelMenuTab(this.id);
			activeTab(this);
			getTabAction(this);
			window.location = '#tab='+this.id;
		}
		tabs_table_tr.appendChild(td);
	}
	startTab();
}
var currentTab_id;

function getTabAction(me){
	var ul_id="menu_tab_"+me.id.substring(me.id.lastIndexOf('_')+1, me.id.length);
	var settime_topopen=0;
	if(currentTab_id==me.id){
		return false;
	} else {
		currentTab_id=me.id;
	}
	var content_li=document.getElementById(ul_id).getElementsByTagName('li');
	var tags_a=content_li[0].getElementsByTagName('a');
	var obj_onoptions=document.getElementById('menu_boxtoviewcontenttab_0');
	if(tags_a.length==1 && content_li.length==1){
		window.open(tags_a[0].href, tags_a[0].target);
		if(obj_onoptions.style.display=='block') abriuTab();
	} else if(content_li.length>1) {
		//if(obj_onoptions.style.display=='block') abriuTab();
		var box_onoptions_tab=document.getElementById('menu_contentabs');
		var total_options="<nobr>";
		for(var r=1; r<content_li.length; r++){
			total_options+=content_li[r].innerHTML;
			if(r+1<content_li.length)
				total_options+="<span>|</span>";
		}
		total_options+="</nobr>";
		box_onoptions_tab.innerHTML=total_options;
		var getlinks = box_onoptions_tab.getElementsByTagName('a');
		for(var pi=0; pi<getlinks.length; pi++){
			getlinks[pi].href = getlinks[pi].href+"#tab="+global_current_menu_tab_id;
		}
		if(obj_onoptions.style.display!='block') abriuTab();
	} else {
		if(obj_onoptions.style.display=='block') abriuTab();
	}
}

function extractName(tag){
	var atags=tag.getElementsByTagName('a');
	if(atags.length==1){
		var content=atags[0].innerHTML;
	} else {
		var content=tag.innerHTML;
	}
	return trim(content);
}

function extractURL(tag){
	var atags=tag.getElementsByTagName('a');
	var href=atags[0].getAttribute("href");
	return href;
}	

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function abriuTab(tip){
	document.getElementById('menu_boxcontent_subitems_0').style.top = 0 - document.getElementById('menu_boxtoviewcontenttab_0').offsetHeight + 'px'; 
	showHideContent(tip);
}

var dhtmlgoodies_slideSpeed = 2;	// Higher value = faster
var dhtmlgoodies_timer = 10;	// Lower value = faster

var objectIdToSlideDown = false;
var dhtmlgoodies_activeId = false;
var dhtmlgoodies_slideInProgress = false;

function showHideContent(){
	if(dhtmlgoodies_slideInProgress)return;
	dhtmlgoodies_slideInProgress = true;
	var inputId='menutab_0';
	var numericId = inputId.replace(/[^0-9]/g,'');
	var answerDiv = document.getElementById('menu_boxtoviewcontenttab_' + numericId);
	objectIdToSlideDown = false;
	
	if(!answerDiv.style.display || answerDiv.style.display=='none'){
		if(dhtmlgoodies_activeId &&  dhtmlgoodies_activeId!=numericId){			
			objectIdToSlideDown = numericId;
			slideContent(dhtmlgoodies_activeId,(dhtmlgoodies_slideSpeed*-1));
		}else{
			
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			
			slideContent(numericId,dhtmlgoodies_slideSpeed);
		}
	}else{
		slideContent(numericId,(dhtmlgoodies_slideSpeed*-1));
		dhtmlgoodies_activeId = false;
	}	
}

function slideContent(inputId,direction){
	
	var obj =document.getElementById('menu_boxtoviewcontenttab_' + inputId);
	var contentObj = document.getElementById('menu_boxcontent_subitems_' + inputId);
	height = obj.clientHeight;
	if(height==0)height = obj.offsetHeight;
	height = height + direction;
	rerunFunction = true;
	if(height>contentObj.offsetHeight){
		height = contentObj.offsetHeight;
		rerunFunction = false;
	}
	if(height<=1){
		height = 1;
		rerunFunction = false;
	}

	obj.style.height = height + 'px';
	var topPos = height - contentObj.offsetHeight;
	if(topPos>0)topPos=0;
	contentObj.style.top = topPos + 'px';
	if(rerunFunction){
		setTimeout('slideContent(' + inputId + ',' + direction + ')',dhtmlgoodies_timer);
	}else{
		if(height<=1){
			obj.style.display='none'; 
			if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
				document.getElementById('menu_boxtoviewcontenttab_' + objectIdToSlideDown).style.display='block';
				document.getElementById('menu_boxtoviewcontenttab_' + objectIdToSlideDown).style.visibility='visible';
				slideContent(objectIdToSlideDown,dhtmlgoodies_slideSpeed);				
			}else{
				dhtmlgoodies_slideInProgress = false;
			}
		}else{
			dhtmlgoodies_activeId = inputId;
			dhtmlgoodies_slideInProgress = false;
		}
	}
}

function startTab(){
	var hv = getUrlHashVars();
	if(hv['tab']){
		var tab_id = hv['tab'];
		var objta = document.getElementById(tab_id);
		if(objta){
			decSelMenuTab(tab_id);
			activeTab(objta);
			getTabAction(objta);	
		}	
	}
}