///////////////////////////////////////////////////
//EXPANDABLE MENU
///////////////////////////////////////////////////

//Init Vars
var activeSmenus = new Array();
var expandSpeed = 3.1;
var removeActiveSmenu = new Boolean(false);
var animating = new Boolean (false);
var sMenu = new Object();

//Encuentro los menus Secundarios y obtengo su altura inicial
startDerMenus =  function (derMenu) {
	
	if (document.getElementById(derMenu) != null) {
		
		var derMenu = document.getElementById(derMenu);
		var divTags = derMenu.getElementsByTagName("div");
		
		//alert(divTags.length);
		
		//Busco los DIV con clase "derSubM"
		for (var i = 0; i<divTags.length; i++) {
			
			if(divTags[i].className == "derSubM") {
				
				//coloco Estilos CSS y hago visibles los submenus (Ocultados por defecto)
				divTags[i].style.height = "auto";
				divTags[i].style.width = "auto";
				divTags[i].style.visibility = "visible";
				
				//Doy propiedades a los objetos
				divTags[i].id = "subM" + i;
				divTags[i].expanded = false;
				divTags[i].currentSize = 0; //estan contraidos siempre al principio			
				divTags[i].targetSize = divTags[i].offsetHeight;
				divTags[i].contractedSize = divTags[i].currentSize;
				divTags[i].expandedSize = divTags[i].targetSize;
				divTags[i].moving = false;
				
				/*
				alert("subM.Id = " + divTags[i].id);
				alert("subM.currentSize = " + divTags[i].currentSize);
				alert("subM.targetSize = " + divTags[i].targetSize);
				alert("subM.expandedSize = " + divTags[i].expandedSize);
				*/
				
				//alert(divTags[i].parentNode.firstChild.tagName);
				
				//oculto todos los submenus
				//divTags[i].style.height = "0px";
				

				if(divTags[i].parentNode.firstChild.className == "current") {
					//divTags[i].expanded = true;
					divTags[i].expanded = true;
					divTags[i].currentSize = divTags[i].offsetHeight; //estan expandidos siempre al principio			
					divTags[i].targetSize = 0;
					divTags[i].moving = false;	
					divTags[i].style.height = divTags[i].currentSize + "px";
					
					sMenu = divTags[i];
					activeSmenus.push(sMenu);
				}
				else {
					//oculto todos los submenus
					divTags[i].style.height = "0px";
				}
				
		
				//divTags[i].style.display = "none";	
				
			}	
		}
		
		//alert(subMenus[2][0]);
	}
	
	//recoloco footer en EXPLORER
	/*
	if (navigator.appName.indexOf("Explorer") > 0) {
		tagReloc("footer");
	}
	*/
	
}

//Animacion del subMenu
animateSubmenus = function(selectedAnchor) {
	
	
	if(animating == false) {//Si no se esta animando(Espero a que termine para animar de nuevo)
		
		animating = true;
		
		//Si existia un sMenu que se ha contraido lo borro del array activeSmenus
		if(removeActiveSmenu == true) {
			activeSmenus.shift();
			//alert("removing ActiveSmenu");
		}
		
		stoppingCount = 0;
		
		//Encuentro el submenu "div class = subM" correpondiente al anchor seleccionado
		var liParentChilds = selectedAnchor.parentNode.childNodes;
		//alert(liParentChilds.length);
		
		for (var i = 0; i<liParentChilds.length; i++) {
			if(liParentChilds[i].tagName == "DIV") {
				sMenu = liParentChilds[i];
				sMenuId = liParentChilds[i].id;
				
				//alert("sMenu = " + sMenuId + ":: sMenu currentSize = " + sMenu.currentSize);
				
				//Aņado sMenus al array activeSmenus solo si se va a expandir
				if(sMenu.targetSize != 0) {
					activeSmenus.push(sMenu);
				}
   	
   	
				if(activeSmenus.length > 1){//Si la longitud del activeSmenus es mayor a 1 se debe remover un valor del mismo
					removeActiveSmenu = true;
					//alert("Dos items");
				}
				else if (activeSmenus.length == 1 && sMenu.targetSize == 0) {//Si hay solo 1 valor pero es el mismo SMenu contrayendose se remueve tambien 
					removeActiveSmenu = true;
					//alert("1 items y se va a cerrar");
				}
				else {//Ni es el mismo contrayendose
					//alert("1 items y se va a abrir");
					removeActiveSmenu = false;
				}
   	
				//alert("activeSmenus.length = " + activeSmenus.length + " ::: removing = " + removeActiveSmenu);
				
				for (var j=0; j<activeSmenus.length; j++) {
					//alert("activeSmenus = " + activeSmenus[j].id + " || targetSize = " + activeSmenus[j].targetSize);
					activeSmenus[j].moving = true;
					activeSmenus[j].resizing = new sizeIncrement(activeSmenus[j].currentSize, activeSmenus[j].targetSize);
				}
   	
				
			}
		}
		
		sizeInterval = setInterval( function() { updateSize(); }, 50 );
	}
		
}

//recolocacion del fondo
setBackgroundPos = function (activeSmenu,pos) {

	//Encuentro los hijos del Nodo padre
	//para el link y para el subnenu es el LI contenedor
	var liParentChilds = activeSmenu.parentNode.childNodes;
	//alert(liParentChilds.length);
	
	//si uno de los hijos es "A" busco el hijo "SPAN"
	for (var i = 0; i<liParentChilds.length; i++) {
		if(liParentChilds[i].tagName == "A") {
			var parentSmenu = liParentChilds[i];
			//alert(parentSmenu.tagName + "||" + parentSmenu.className);
			if (parentSmenu.className != "current") {
				parentSmenu.style.color = pos;
			}
		}
	}
}

//recolocacion del elementos (Explorer)
tagReloc = function(relocItem) {

	itemReloc = document.getElementById(relocItem);
	itemReloc.style.bottom = "-1px";
	itemReloc.style.bottom = "0px";
	//alert("relocating");
}

//Incremento de Movimiento
function sizeIncrement (currentSize,targetSize) {
	
	this.resizeIt = function(){
		
		if(currentSize < targetSize) {//Expando
			currentSize += (targetSize - currentSize) / expandSpeed;
			currentSize = Math.ceil(currentSize);
		}
		else if (targetSize < currentSize) {//Contraigo
			currentSize += (targetSize - currentSize) / expandSpeed;
			currentSize = Math.floor(currentSize);
		}
		
		return [currentSize,targetSize];   
		
	}
	
	
}

//Actualizacion de tamaņo del subMenu
function updateSize(){
	
	for (var i=0; i<activeSmenus.length; i++) {
		activeSmenus[i].currentSize = activeSmenus[i].resizing.resizeIt()[0];
		activeSmenus[i].style.height = activeSmenus[i].currentSize + "px";
		
		//Si completo la expansion o la contraccion del subMenu
		if (activeSmenus[i].currentSize == activeSmenus[i].targetSize && activeSmenus[i].moving == true) {
			//alert(sMenu.id + " -> finished!!");
			stoppingCount += 1;
			
			//alert("stoppingCount = " + stoppingCount);
			//alert("activeSmenus.length = " + activeSmenus.length);
			
			if(activeSmenus[i].targetSize == activeSmenus[i].expandedSize) {//Se estaba expandiendo
				activeSmenus[i].targetSize = activeSmenus[i].contractedSize;
				activeSmenus[i].moving = false;
				//setBackgroundPos(activeSmenus[i],"#000000");
				activeSmenus[i].expanded = true;
				//alert(activeSmenus[i].id + " targetSize = " +  activeSmenus[i].targetSize);
			}
			else {//Se estaba contrayendo
				activeSmenus[i].targetSize = activeSmenus[i].expandedSize;
				activeSmenus[i].moving = false; 
				//setBackgroundPos(activeSmenus[i],"#ffffff");
				activeSmenus[i].expanded = false;
				//alert(activeSmenus[i].parentNode.className);
				//alert(activeSmenus[i].id + " targetSize = " +  activeSmenus[i].targetSize);
				//alert("contracting:" + activeSmenus[i].id);
			}
			
			//testVars
			//testVars(activeSmenus[0].id,activeSmenus[0].currentSize,activeSmenus[0].targetSize);
			
			if (stoppingCount == activeSmenus.length) {
				//alert("full STOP!!");
				animating = false;
				clearInterval(sizeInterval);
			}
			
		}
		
		
	}
	
	//recoloco footer en EXPLORER
	/*
	if (navigator.appName.indexOf("Explorer") > 0) {
		tagReloc("footer");
	}
	*/

}



///////////////////////////////////////////////////
//RECOLOCO FONDO EN EXPLORER
///////////////////////////////////////////////////

//var explorerBkg = new Boolean(false);

adjustBackground = function () {
	
	if (navigator.appName.indexOf("Explorer") > 0 && (navigator.userAgent.indexOf("MSIE 6") > 0) || navigator.userAgent.indexOf("MSIE 7") > 0) {     
		
		//alert(navigator.userAgent);
		
		var settingUpBackground = false;
		docStyles = document.getElementsByTagName("link");
		//alert("settingUpBackground = " + settingUpBackground);
		for (var i=0; i<docStyles.length; i++) {
			if (docStyles[i].href.indexOf("ciria_principal") > 0 ) {
				settingUpBackground = true;
			}
		}
		//alert("settingUpBackground = " + settingUpBackground);
   	
   	
		if (settingUpBackground == true) {

			var backgroundContainer = "principal";
			var imageWidth = 1024;
			var imageHeight = 542;
			var minWidth = 968;
			var minHeight = 542;
			
			itemReshape = document.getElementById(backgroundContainer);
			itemReshapeWidth = itemReshape.offsetWidth;
			itemReshapeHeight = itemReshape.offsetHeight;
			
			if(document.body.offsetWidth < minWidth) {
				itemReshapeWidth = minWidth + "px";
				itemReshapeWidth = itemReshape.offsetWidth;
			}
			else if (document.body.offsetWidth > minWidth) {
				itemReshape.style.width = document.body.offsetWidth + "px";
			}
			
			if(document.body.offsetHeight < minHeight) {
				itemReshape.style.height = minHeight + "px";
				itemReshapeHeight = itemReshape.offsetHeight;
			}
			else if (document.body.offsetHeight > minHeight) {
				itemReshape.style.height = document.body.offsetHeight + "px";
			}
			
			newX = Math.round(itemReshapeWidth/2) - (imageWidth/2);
			newY = Math.round(itemReshapeHeight/2) - (imageHeight/2);
			itemReshape.style.backgroundPosition = newX + "px" + " " + newY + "px";
			//itemReshape.style.top = newY;
			
			//testVars(document.body.offsetWidth, itemReshapeWidth, document.body.offsetHeight, itemReshapeHeight, newX, newY);
			
		}
		
	}

}

window.onresize = adjustBackground;



//Initial setUp
setUpPage = function() {
	//alert ("settingUp!!");
	
	//Menus Expandibles
	startDerMenus("sMenu");
	
	//adjust Background Image     
	adjustBackground();
	
} 

window.onload = setUpPage;



//Test expandable Menu
function testVars(documentWidth, itemReshapeWidth, documentHeight, itemReshapeHeight,newX,newY) {
	
	var outPutVars = ""
	+ '<strong>ADJUSTBACKGROUND</strong><br />'
	+ '<strong>'
	+ 'documentWidth=' + documentWidth
	+ '<br />'
	+ 'itemReshapeWidth=' + itemReshapeWidth
	+ '<br />'
	+ 'documentHeight=' + documentHeight
	+ '<br />'
	+ 'itemReshapeHeight=' + itemReshapeHeight
	+ '<br />'
	+ 'newX=' + newX
	+ '<br />'
	+ 'newY=' + newY
	+ '<br />'
	+ '<br /></strong>';
	
	
	document.getElementById("testVars").innerHTML = outPutVars;

}
