var expandFirstItemAutomatically = 1;	// Para expandir el primer menu automaticamente
var initMenuIdToExpand = false;	// Si se quiere colocar Id del primer <li> a expandir.
var expandMenuItemByUrl = true;	

var initialMenuItemAlwaysExpanded = true;	// NOT IMPLEMENTED YET

var dhtmlgoodies_slmenuObj;
var divToScroll = false;
var ulToScroll = false;	
var divCounter = 1;
var otherDivsToScroll = new Array();
var divToHide = false;
var parentDivToHide = new Array();
var ulToHide = false;
var offsetOpera = 0;
if(navigator.userAgent.indexOf('Opera')>=0)offsetOpera=1;	
var slideMenuHeightOfCurrentBox = 0;
var objectsToExpand = new Array();
var initExpandIndex = 0;
var alwaysExpanedItems = new Array();

var sliderTime = 15;
	
function popMenusToShow(){
	var obj = divToScroll;
	var endArray = new Array();
	while(obj && obj.tagName!='BODY'){
		if(obj.tagName=='DIV' && obj.id.indexOf('slideDiv')>=0){
			var objFound = -1;
			for(var no=0;no<otherDivsToScroll.length;no++){
				if(otherDivsToScroll[no]==obj){
					objFound = no;		
				}					
			}	
			if(objFound>=0){
				otherDivsToScroll.splice(objFound,1);	
			}		
		}	
		obj = obj.parentNode;
	}	
}

function showSubMenu(e,inputObj){
	if(this && this.tagName)inputObj = this.parentNode;
	if(inputObj && inputObj.tagName=='LI'){
		divToScroll = inputObj.getElementsByTagName('DIV')[0];
		for(var no=0;no<otherDivsToScroll.length;no++){
			if(otherDivsToScroll[no]==divToScroll)return;
		}			
	}
	hidingInProcess = false;
	if(otherDivsToScroll.length>0){
		if(divToScroll){				
			if(otherDivsToScroll.length>0){
				popMenusToShow();
			}
			if(otherDivsToScroll.length>0){	
				autoHideMenus();
				hidingInProcess = true;
			}
		}	
	}		
	if(divToScroll && !hidingInProcess){
		divToScroll.style.display='';
		otherDivsToScroll.length = 0;
		otherDivToScroll = divToScroll.parentNode;
		otherDivsToScroll.push(divToScroll);	
		while(otherDivToScroll && otherDivToScroll.tagName!='BODY'){
			if(otherDivToScroll.tagName=='DIV' && otherDivToScroll.id.indexOf('slideDiv')>=0){
				otherDivsToScroll.push(otherDivToScroll);
									
			}
			otherDivToScroll = otherDivToScroll.parentNode;
		}			
		ulToScroll = divToScroll.getElementsByTagName('DIV')[0];
		if(divToScroll.style.height.replace('px','')/1<=1)scrollDownSub();
	}	
	

}



function autoHideMenus(){
	if(otherDivsToScroll.length>0){
		divToHide = otherDivsToScroll[otherDivsToScroll.length-1];
		parentDivToHide.length=0;
		var obj = divToHide.parentNode.parentNode.parentNode;
		while(obj && obj.tagName=='DIV'){			
			if(obj.id.indexOf('slideDiv')>=0)parentDivToHide.push(obj);
			obj = obj.parentNode.parentNode.parentNode;
		}
		var tmpHeight = (divToHide.style.height.replace('px','')/1 - slideMenuHeightOfCurrentBox);
		if(tmpHeight<0)tmpHeight=0;
		if(slideMenuHeightOfCurrentBox)divToHide.style.height = tmpHeight  + 'px';
		ulToHide = divToHide.getElementsByTagName('DIV')[0];
		slideMenuHeightOfCurrentBox = ulToHide.offsetHeight;
		scrollUpMenu();		
	}else{
		slideMenuHeightOfCurrentBox = 0;
		showSubMenu();			
	}
}


function scrollUpMenu(){
	var height = divToHide.offsetHeight;
	height-=15;
	if(height<0)height=0;
	divToHide.style.height = height + 'px';

	for(var no=0;no<parentDivToHide.length;no++){	
		parentDivToHide[no].style.height = parentDivToHide[no].getElementsByTagName('DIV')[0].offsetHeight + 'px';
	}
	if(height>0){
		setTimeout('scrollUpMenu()', sliderTime);
	}else{
		divToHide.style.display='none';
		otherDivsToScroll.length = otherDivsToScroll.length-1;
		autoHideMenus();			
	}
}	

function scrollDownSub(){
	if(divToScroll){			
		var height = divToScroll.offsetHeight/1;
		var offsetMove =Math.min(15,(ulToScroll.offsetHeight - height));
		height = height +offsetMove ;
		divToScroll.style.height = height + 'px';
		
		for(var no=1;no<otherDivsToScroll.length;no++){
			var tmpHeight = otherDivsToScroll[no].offsetHeight/1 + offsetMove;
			otherDivsToScroll[no].style.height = tmpHeight + 'px';
		}			
		if(height<ulToScroll.offsetHeight)setTimeout('scrollDownSub()', sliderTime); else {
			divToScroll = false;
			ulToScroll = false;
			if(objectsToExpand.length>0 && initExpandIndex<(objectsToExpand.length-1)){
				initExpandIndex++;
				
				showSubMenu(false,objectsToExpand[initExpandIndex]);
			}
		}
	}
}
	
function initSubItems(inputObj,currentDepth){		
	divCounter++;		
	var div = document.createElement('DIV');	// Creating new div		
	div.style.overflow = 'hidden';	
	div.style.position = 'relative';
	div.style.display='none';
	div.style.height = '1px';
	div.id = 'slideDiv' + divCounter;
	div.className = 'slideMenuDiv' + currentDepth;		
	inputObj.parentNode.appendChild(div);	// Appending DIV as child element of <LI> that is parent of input <DIV>		
	div.appendChild(inputObj);	// Appending <DIV> to the div
	var menuItem = inputObj.getElementsByTagName('LI')[0];
	while(menuItem){
		if(menuItem.tagName=='LI'){
			var aTag = menuItem.getElementsByTagName('A')[0];
			aTag.className='slMenuItem_depth'+currentDepth;	
			var subUl = menuItem.getElementsByTagName('DIV');
			if(subUl.length>0){
				initSubItems(subUl[0],currentDepth+1);					
			}
			aTag.onclick = showSubMenu;				
		}			
		menuItem = menuItem.nextSibling;						
	}		
}

function initSlideDownMenu(){	
	var URL = document.location.href;
	if(URL=='http://www.cubika.com/' || URL=='http://www.cubika.com/index.php' || URL=='http://www.cubika.com/index.php#')
	{
		iniciar('textMenu0,textMenu1,textMenu2,textMenu3,textMenu4,textMenu5');
	}
	else if(URL=='http://www.cubika.com/inicio.html' || URL=='http://www.cubika.com/inicio.html#')
	{
		iniciar('textMenu0,textMenu1,textMenu2,textMenu3,textMenu4,textMenu5,textMenu6');	
	}
	dhtmlgoodies_slmenuObj = document.getElementById('slide_menu');
	dhtmlgoodies_slmenuObj.style.visibility='visible';
	var mainUl = dhtmlgoodies_slmenuObj.getElementsByTagName('DIV')[0];		
	var mainMenuItem = mainUl.getElementsByTagName('LI')[0];
	mainItemCounter = 1;
	while(mainMenuItem){			
		if(mainMenuItem.tagName=='LI'){
			var aTag = mainMenuItem.getElementsByTagName('A')[0];
			aTag.className='slMenuItem_depth1';	
			var subUl = mainMenuItem.getElementsByTagName('DIV');
			if(subUl.length>0){
				mainMenuItem.id = 'mainMenuItem' + mainItemCounter;
				initSubItems(subUl[0],2);
				aTag.onclick = showSubMenu;
				mainItemCounter++;
			}				
		}			
		mainMenuItem = mainMenuItem.nextSibling;	
	}		
	
	if(location.search.indexOf('mainMenuItemToSlide')>=0){
		var items = location.search.split('&');
		for(var no=0;no<items.length;no++){
			if(items[no].indexOf('mainMenuItemToSlide')>=0){
				values = items[no].split('=');
				showSubMenu(false,document.getElementById('mainMenuItem' + values[1]));	
				initMenuIdToExpand = false;				
			}
		}			
	}else if(expandFirstItemAutomatically>0){
		if(document.getElementById('mainMenuItem' + expandFirstItemAutomatically)){
			showSubMenu(false,document.getElementById('mainMenuItem' + expandFirstItemAutomatically));
			initMenuIdToExpand = false;
		}
	}

	if(expandMenuItemByUrl)
	{
		var aTags = dhtmlgoodies_slmenuObj.getElementsByTagName('A');
		for(var no=0;no<aTags.length;no++){
			var hrefToCheckOn = aTags[no].href;				
			if(location.href.indexOf(hrefToCheckOn)>=0 && hrefToCheckOn.indexOf('#')<hrefToCheckOn.length-1){
				initMenuIdToExpand = false;
				var obj = aTags[no].parentNode;
				while(obj && obj.id!='slide_menu'){
					if(obj.tagName=='LI'){							
						var subUl = obj.getElementsByTagName('DIV');
						if(initialMenuItemAlwaysExpanded)alwaysExpanedItems[obj.parentNode] = true;
						if(subUl.length>0){								
							objectsToExpand.unshift(obj);
						}
					}
					obj = obj.parentNode;	
				}
				showSubMenu(false,objectsToExpand[0]);
				break;					
			}			
		}
	}
			
	if(initMenuIdToExpand)
	{
		objectsToExpand = new Array();
		var obj = document.getElementById(initMenuIdToExpand)
		while(obj && obj.id!='slide_menu'){
			if(obj.tagName=='LI'){
				var subUl = obj.getElementsByTagName('DIV');
				if(initialMenuItemAlwaysExpanded)alwaysExpanedItems[obj.parentNode] = true;
				if(subUl.length>0){						
					objectsToExpand.unshift(obj);
				}
			}
			obj = obj.parentNode;	
		}
		
		showSubMenu(false,objectsToExpand[0]);

	}		
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Generador de efecto
var browser=navigator.appName;
var cantidadFr=0;
	
function iniciar(parametros)
{
	var truncate;
	truncate = parametros.split(',');
	var b_version=navigator.appVersion;
	var version=b_version.split(';');
	if (browser=="Microsoft Internet Explorer")
	{
		var numero;
		numero = version[1].split('MSIE ');
		var nuevo_num;
		nuevo_num = numero[1].split('.');
		if(nuevo_num[0]<8)
		{
			var navVersion = 'IE6';
		}
		else
		{
			var navVersion = 'IE8';		
		}
	}
	else
	{
			var navVersion = 'IE8';		
	}

	if (navVersion== 'IE6')
  	{
		for(var o=0; o<truncate.length; o++)
		{
			document.getElementById(truncate[o]).style.background = "white";
			document.getElementById(truncate[o]).style.zIndex = "2";
			document.getElementById(truncate[o]).style.position = "absolute";
			document.getElementById(truncate[o]).style.display = "none";
			document.getElementById(truncate[o]).style.marginLeft = "50px";
			document.getElementById(truncate[o]).style.marginTop = "-10px";
		}
		cantidadFr=o;
		

		document.getElementById('textMenuInicio').style.background = "white";
		document.getElementById('textMenuInicio').style.zIndex = "2";
		document.getElementById('textMenuInicio').style.position = "absolute";
		document.getElementById('textMenuInicio').style.marginLeft = "50px";		
		document.getElementById('textMenuInicio').style.marginTop = "-10px";		
  		crearDivsIE();		
  	}
	else
	{
		for(var o=0; o<truncate.length; o++)
		{
			document.getElementById(truncate[o]).style.background = "white";
			document.getElementById(truncate[o]).style.zIndex = "2";
			document.getElementById(truncate[o]).style.position = "absolute";
			document.getElementById(truncate[o]).style.display = "none";
			document.getElementById(truncate[o]).style.marginLeft = "250px";
			document.getElementById(truncate[o]).style.marginTop = "-10px";
		}		
		cantidadFr=o;
		
		document.getElementById('textMenuInicio').style.background = "white";
		document.getElementById('textMenuInicio').style.zIndex = "2";
		document.getElementById('textMenuInicio').style.position = "absolute";
		document.getElementById('textMenuInicio').style.marginLeft = "250px";		
		document.getElementById('textMenuInicio').style.marginTop = "-10px";				

		crearDivs();	
	}
	
	document.getElementById('textMenuInicio').style.display = "block";
//crearDivs();
}

function muestra(parametroBase)
{
	document.getElementById(parametroBase).style.display = "block";
}

function oculta(parametroInicio)
{
	document.getElementById(parametroInicio).style.display = "none";	
}

function reindex(parametroIndexa,index)
{
	document.getElementById(parametroIndexa).style.zIndex = index;	
}

function limpiar()
{
	for(var b=0; b<=cantidadFr; b++)
	{
		document.getElementById('textMenu' + b).style.background = "white";
		document.getElementById('textMenu' + b).style.zIndex = "1";
		document.getElementById('textMenu' + b).style.position = "absolute";
		document.getElementById('textMenu' + b).style.display = "none";
		document.getElementById('textMenu' + b).style.marginLeft = "250px";
	}
	document.getElementById('textMenuInicio').style.background = "white";
	document.getElementById('textMenuInicio').style.zIndex = "0";
	document.getElementById('textMenuInicio').style.position = "absolute";
	document.getElementById('textMenuInicio').style.display = "none";
	document.getElementById('textMenuInicio').style.marginLeft = "250px";
	
}

function limpiarIE()
{
	for(var b=0; b<=cantidadFr; b++)
	{
		document.getElementById('textMenu' + b).style.background = "white";
		document.getElementById('textMenu' + b).style.zIndex = "1";
		document.getElementById('textMenu' + b).style.position = "absolute";
		document.getElementById('textMenu' + b).style.display = "none";
		document.getElementById('textMenu' + b).style.marginLeft = "50px";
	}
	document.getElementById('textMenuInicio').style.background = "white";
	document.getElementById('textMenuInicio').style.zIndex = "0";
	document.getElementById('textMenuInicio').style.position = "absolute";
	document.getElementById('textMenuInicio').style.display = "none";
	document.getElementById('textMenuInicio').style.marginLeft = "50px";	
}

function mostrarOculto(parametro2,parametro3)
{
//parametro2: nombre del partron del div ocultador
//parametro3: nombre del div que se va a mostrar
	var b_version=navigator.appVersion;
	var version=b_version.split(';');
	if (browser=="Microsoft Internet Explorer")
	{
		var numero;
		numero = version[1].split('MSIE ');
		var nuevo_num;
		nuevo_num = numero[1].split('.');
		if(nuevo_num[0]<8)
		{
			var navVersion = 'IE6';
		}
		else
		{
			var navVersion = 'IE8';		
		}
	}
	else
	{
			var navVersion = 'IE8';		
	}

	if (navVersion== 'IE6')
	{
		document.getElementById('flotador0').style.zIndex = "5";	
		setTimeout('muestra("flotador0")',100);
		setTimeout('limpiarIE()',100);
		/*
		setTimeout('oculta("textmenu0")', 200)
		setTimeout('oculta("textmenu1")', 200)
		setTimeout('oculta("textmenu2")', 200)
		setTimeout('oculta("textmenu3")', 200)
		setTimeout('oculta("textmenu4")', 200)
		setTimeout('oculta("textmenu5")', 200)
		*/
		setTimeout('muestra(\'' + parametro3 + '\')', 300);
		setTimeout('reindex(\'' + parametro3 + '\', 4)', 350);	
		setTimeout('reindex("flotador0", 0)', 400);
		setTimeout('oculta("flotador0")', 400);
	}
	else
	{
		var tiempo = 100;
		for(var a=0; a<=5; a++)
		{
			tiempo += 100;
			setTimeout('reindex(\'' + parametro2 + a + '\', 5)', tiempo);
			setTimeout('muestra(\'' + parametro2 + a + '\')', tiempo);
		}

		setTimeout('limpiar()',tiempo);
		setTimeout('muestra(\'' + parametro3 + '\')', tiempo);
		setTimeout('reindex(\'' + parametro3 + '\', 4)', tiempo);	

		for(var a=5; a>=0; a--)
		{
			tiempo += 100;
			setTimeout('reindex(\'' + parametro2 + a + '\', 0)', tiempo);
			setTimeout('oculta(\'' + parametro2 + a + '\')', tiempo);
		}
	}	
}

function crearDivs()
{
var nombreFloat;
var a = 0.0;
	for(var z=0; z<=5; z++)
	{
		document.getElementById('contenedor').innerHTML = document.getElementById('contenedor').innerHTML + '<div id="flotador' + z +'" style="height:160px; width:400px;"><br></div>';
		nombreFloat= 'flotador' + z;
		document.getElementById(nombreFloat).style.background = "white";
		document.getElementById(nombreFloat).style.opacity = a;
		document.getElementById(nombreFloat).style.display = "block";
		document.getElementById(nombreFloat).style.position = "absolute";
		document.getElementById(nombreFloat).style.zIndex = "0";	
		document.getElementById(nombreFloat).style.marginLeft = "250px";	
		document.getElementById(nombreFloat).style.marginTop = "-10px";
		a = a + 0.5;
	}	
}

function crearDivsIE()
{
var nombreFloatIE;
var n = 0;
	document.getElementById('contenedor').innerHTML = document.getElementById('contenedor').innerHTML + '<div id="flotador0" style="height:160px; width:400px;"><br></div>';
	nombreFloatIE= 'flotador0';
	document.getElementById(nombreFloatIE).style.background = "white";
	document.getElementById(nombreFloatIE).style.filters = "alpha(opacity=100)";
	document.getElementById(nombreFloatIE).style.display = "block";
	document.getElementById(nombreFloatIE).style.position = "absolute";
	document.getElementById(nombreFloatIE).style.zIndex = "0";	
	document.getElementById(nombreFloatIE).style.marginLeft = "50px";	
	document.getElementById(nombreFloatIE).style.marginTop = "-10px";	
	n = n + 5;
}



window.onload = initSlideDownMenu;
