///////////////////////////////////////////////////
//MAIN MENU
///////////////////////////////////////////////////

//Init Vars
var holdTime = new Number(500);
var topSMenuTimer;

//Encuentro los menus Secundarios y aņado eventos y submenu al anchor correspondiente
startMainMenu = function (mainMenu) {
	if (document.getElementById(mainMenu) != null) {
		var mainMenu = document.getElementById(mainMenu);
		var ATags = mainMenu.getElementsByTagName("A");
		
		for (var i = 0; i<ATags.length; i++) {
			var parentLi = ATags[i].parentNode;
			var parentLitags = parentLi.childNodes;
			
			for (var j = 0; j<parentLitags.length; j++) {
				
				if(parentLitags[j].nodeName == "DIV") {
					//alert("top subMenu found");
					ATags[i].topSMenu = parentLitags[j];
					setUpTopMenuEvents(ATags[i],ATags[i].topSMenu);
					
					var divChilds = parentLitags[j].childNodes;
					
					for (var k = 0; k<divChilds.length; k++) {
						sMenuElementEvents(divChilds[k],ATags[i].topSMenu);
					}
					
				}
			}	
		}
		
	}
}

//Aņado eventos a los Anchors y a los subMenus
setUpTopMenuEvents = function (topMenuItem,topSMenu) {
	//alert(topMenuItem);
	
	
	//ANCHOSR
	//MOUSEOVER
	topMenuItem.onmouseover = function () {
		
		//si es el mismo smenu Activo lo borro
		if (typeof(activeTopSMenu) != 'undefined') {
			if (topSMenu == activeTopSMenu) {
				delete activeTopSMenu;
			}
		}
		
		//muestro smenu, si ya existe uno activo le oculto
		topSMenu.className = "subMenu currentSMenu";
		if (typeof(activeTopSMenu) != 'undefined') {
			activeTopSMenu.className = "subMenu";
		}
		
		/*
		document.onmousemove = function(topSMenu) {
			x = topSMenu.mouseX;
			testVars (x);
		}
		*/
		
		//Defino que hay un menu activo
		activeTopSMenu = topSMenu;
		clearTimeout(topSMenuTimer);
		
	}
	
	//MOUSEOUT
	topMenuItem.onmouseout = function () {
		topSMenuTimer = setTimeout( function() { hideTopSMenu(topSMenu); }, holdTime );
		//sMenu.className = "subMenu";
	}
	

	
}

//Aņado eventos a cada elemento de los submenus
sMenuElementEvents = function (sMenuElement,topSMenu) {
	//alert(topMenuItem);
	
	
	//MOUSEOVER
	sMenuElement.onmouseover = function () {
		clearTimeout(topSMenuTimer);
		//testVars ("over");
	}
	
	//MOUSEOUT
	sMenuElement.onmouseout = function () {
		topSMenuTimer = setTimeout( function() { hideTopSMenu(topSMenu); }, holdTime );
		//testVars ("out");
	}
	

	
}


//Oculto submenu
hideTopSMenu = function (topSMenu) {
	topSMenu.className = "subMenu";
}

function doSomething(e) {
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	if (tg.nodeName != 'DIV') {
		
		alert("DIV");
		return;
	}
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	while (reltg != tg && reltg.nodeName != 'BODY') {
		reltg= reltg.parentNode
	}
	if (reltg == tg) {
		return;
	}
	// Mouseout took place when mouse actually left layer
	alert("OUT!!!!!!!!");
}


///////////////////////////////////////////////////
//EXPANDABLE MENU
///////////////////////////////////////////////////

//Init Vars
var activeSmenus = new Array("empty");
var expandSpeed = 3.1;
var removeActiveSmenu = new Boolean(false);
var animating = new Boolean (true);

//Encuentro los menus Secundarios y obtengo su altura inicial
startIzqMenus =  function (izqMenu) {
	
	if (document.getElementById(izqMenu) != null) {
		
		var izqMenu = document.getElementById(izqMenu);
		var divTags = izqMenu.getElementsByTagName("UL");
		var labelTags = izqMenu.getElementsByTagName("LABEL");
		
		//alert(divTags.length);
		
		//Doy propiedades a los objetos UL
		for (var i = 0; i<divTags.length; i++) {

			divTags[i].id = "subM" + i;
			divTags[i].expanded = true;
			divTags[i].currentSize = divTags[i].offsetHeight; //estan expandidos siempre al principio
			divTags[i].targetSize = 0;
			divTags[i].contractedSize = divTags[i].targetSize;
			divTags[i].expandedSize = divTags[i].currentSize;
			divTags[i].moving = false;
			divTags[i].style.height = divTags[i].currentSize + "px";

		}
		
		//Busco los LABEL con clase "kind" y les aņado Eventos
		for (var i = 0; i<labelTags.length; i++) {
			
			
			
			if(labelTags[i].className == "kind") {
				//alert("LABEL found!!");
				setUpIzqmenuEvents(labelTags[i]);
				
			}

		}
		
	}
	
	//recoloco footer en EXPLORER
	if (navigator.appName.indexOf("Explorer") > 0) {
		tagReloc("footer");
	}
	
	//Permito animacion
	animating = false;
	
}

//Animacion del subMenu
animateSubmenus = function(selectedAnchor) {
	if(animating == false) {//Si no se esta animando(Espero a que termine para animar de nuevo)
		
		animating = true;
		
		//alert(">> " + activeSmenus.length);
		
		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 == "UL") {
				
				sMenu = liParentChilds[i];
				sMenuId = liParentChilds[i].id;
				
				//alert("sMenu = " + sMenuId + ":: sMenu currentSize = " + sMenu.currentSize);
				
				//Aņado sMenus
				//remuevo activeSmenu y luego aņado (el array siempre contiene solo un valor)
				//alert(activeSmenus[0]);
				activeSmenus.shift();
				activeSmenus.push(sMenu);
   
   	
				//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 );
		
		//testVars
		//alert(activeSmenus.length, activeSmenus);
		//testVars(activeSmenus.length, activeSmenus);
		
	}
		
}


/*onmouseover="moseOverIzqmenu(this);" onmouseout="moseOutIzqmenu(this);" onclick="animateSubmenus(this); moseClickIzqmenu(this); return false;"*/

//Aņado eventos a los Anchors de los izqMenu
setUpIzqmenuEvents = function (izqMenuItem) {
	//alert(menuItem + "||" + tab);
	
	izqMenuItem.onmouseover = function () {
		//alert("OVER!!");
		//Encuentro el submenu "UL" correpondiente al anchor seleccionado
		var liParentChilds = izqMenuItem.parentNode.childNodes;
		//alert(liParentChilds.length);
    	
		for (var i = 0; i<liParentChilds.length; i++) {
			if(liParentChilds[i].tagName == "UL") {
				if(liParentChilds[i].currentSize == liParentChilds[i].expandedSize  && liParentChilds[i].expanded == true){
					setBackgroundPos(liParentChilds[i],"-30px 4px","#2779A8");
				}
				else {
					setBackgroundPos(liParentChilds[i],"-412px 4px","#2779A8");
				}
			}
		}
	}
	
	
	izqMenuItem.onmouseout = function () {
		//alert("OUT!!");
		//Encuentro el submenu "div class = subM" correpondiente al anchor seleccionado
		var liParentChilds = izqMenuItem.parentNode.childNodes;
		//alert(liParentChilds.length);
			
		for (var i = 0; i<liParentChilds.length; i++) {
			if(liParentChilds[i].tagName == "UL") {
				if(liParentChilds[i].currentSize == liParentChilds[i].contractedSize  && liParentChilds[i].expanded == false){
					setBackgroundPos(liParentChilds[i],"-221px 4px","#E56D00");
				}
				else {
					setBackgroundPos(liParentChilds[i],"161px 4px","#E56D00");
				}
			}
		}
	}
	
	
	izqMenuItem.onclick = function () {
		//alert("CLICK!!"); 
		//Encuentro el submenu "div class = subM" correpondiente al anchor seleccionado
		var liParentChilds = izqMenuItem.parentNode.childNodes;
		//alert(liParentChilds.length);
    	
		for (var i = 0; i<liParentChilds.length; i++) {
			if(liParentChilds[i].tagName == "UL") {
				if(liParentChilds[i].currentSize == liParentChilds[i].expandedSize  && liParentChilds[i].expanded == true){
					setBackgroundPos(liParentChilds[i],"-30px 4px","#2779A8");
				}
				else {
					setBackgroundPos(liParentChilds[i],"-412px 4px","#2779A8");
				}
			}
		}
		
		animateSubmenus(izqMenuItem);

		/*
		if("undefined" != typeof(event) ) {
			event.returnValue = false;
		}
		return false;
		*/
	}
}


//recolocacion del fondo
setBackgroundPos = function (activeSmenu,pos,color) {
	
	//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 == "LABEL") {
			var parentSmenu = liParentChilds[i];
		}
		
	}
	//alert(activeSmenu.currentSize);
	//pongo el fondo de submenu Expandido en el SPAN
	parentSmenu.style.backgroundPosition = pos;
	parentSmenu.style.backgroundColor = color;
}

//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],"161px 4px","#E56D00");
				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],"-221px 4px","#E56D00");
				activeSmenus[i].expanded = false;
				//alert(activeSmenus[i].parentNode.className);
				//alert(activeSmenus[i].id + " targetSize = " +  activeSmenus[i].targetSize);
				//alert("contracting:" + activeSmenus[i].id);
			}
			
			if (stoppingCount == activeSmenus.length) {
				//alert("full STOP!!");
				animating = false;
				clearInterval(sizeInterval);
			}
			
		}
		
		//testVars
		//testVars(activeSmenus[0].id,activeSmenus[0].currentSize,activeSmenus[0].targetSize);
		
		
	}
	
	//recoloco footer en EXPLORER
	if (navigator.appName.indexOf("Explorer") > 0) {
		tagReloc("footer");
	}

}


///////////////////////////////////////////////////
//MENU FICHA EN EXPLORER
///////////////////////////////////////////////////

startList = function() {
	if (document.all&&document.getElementById && document.getElementById("menuficha") != null) {
		navRoot = document.getElementById("menuficha");
		/*alert (document.getElementById("menuficha"));*/
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
					//testVars(this.className);
				}
					node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
					//testVars(this.className);
				}
				//alert (i);
			}
		}
	}
}



///////////////////////////////////////////////////
//FOLDERS Y MENU-FOLDERS
///////////////////////////////////////////////////
fichaMenuSetUp = function (menuID, tabClassN) {

	//alert("preparing Menu");

	if (document.getElementById(menuID) != null) {
	
		menuItems = new Array();
		tabItems = new Array();
		
		
		//Obtengo los items del Menu
		menu = document.getElementById(menuID);
		menuNodes = menu.childNodes;
		//alert(menuNodes);
		
		for (var i=0; i<menuNodes.length; i++) {
			if (menuNodes[i].nodeName == "LI") {
				//alert(menuNodes[i].nodeName);
				menuItems.push(menuNodes[i]);
				
				//Defino el Menu Actual
				if(menuNodes[i].className == "current") {
					//alert("we got a winner!!");
					currentMenu = menuNodes[i];
				}
			}
			
		}
		
    	
		//alert (menuItems);
    	
		//Obtengo los Tabs
		docDivs = document.getElementsByTagName("div");
		//alert(docDivs.length);
		
		for (var i=0; i<docDivs.length; i++) {
			//docParagraphs[i].style.background = "#00ff00";
			if (docDivs[i].className == tabClassN) {
				var currentTab = docDivs[i].id;
				tabItems.push(currentTab);
				//alert (currentTab);
			}
		}
		
		//Hago visible el primer tab por defecto
		var defaultTab = document.getElementById(tabItems[0]);
		defaultTab.className = defaultTab.className + " currentTab";
		
		
		//aņado eventos a los Anchors
		//Si los Estilos estan activados
    	
		if(stylesDisabled == false) {
			
			for (var i=0; i<menuItems.length; i++) {
				
				var LIAnchors = menuItems[i].childNodes;
				for (var j=0; j<LIAnchors.length; j++) {
					if (LIAnchors[j].nodeName == "A") {
						//alert("anchor");
						var tab = tabItems[i];
						setUpMenuEvents(LIAnchors[j],tab);
					}
				}
			}
		}
		
		
		//Se define el Footer SOLO para Explorer 6
		if (navigator.appName.indexOf("Explorer") > 0 && (navigator.userAgent.indexOf("MSIE 6") > 0 || navigator.userAgent.indexOf("MSIE 7") > 0)) {
			pageFooter = document.getElementById("footer");
		}
		
	}
	
}

fichaMenuDisplay = function (selectedTab,selectedAnchor) {
	
	//Redefino el Menu Actual
	currentMenu = selectedAnchor.parentNode;
	selectedMenu = selectedAnchor.parentNode;
	
	//Manipulo los items del Menu
	//alert("tab = " + selectedTab + "|| menu = " + selectedMenu + "||" + "menuItems = " + menuItems);

		for (var i=0; i<menuItems.length; i++) {

			if(menuItems[i] == selectedMenu) {
				//alert("selectedMenu = " + menuItems[i].className);
				if (menuItems[i].className != "last" && menuItems[i].className != "current last" && menuItems[i].className != "last over" && menuItems[i].className != "current last over") {
					menuItems[i].className = "current";
					//setAttribute Explorer
					if (navigator.appName.indexOf("Explorer") > 0 && (navigator.userAgent.indexOf("MSIE 6") > 0 || navigator.userAgent.indexOf("MSIE 7") > 0)) {
						menuItems[i].setAttribute('className','current');
					}
				}
				else if (navigator.appName.indexOf("Explorer") > 0 && navigator.userAgent.indexOf("MSIE 6.0") > 0) {
					//alert("dam!!");
					menuItems[i].className = "current last over";
				}
				else {
					//alert("last!!");
					menuItems[i].className = "current last";
				}
			}
			else {
				
				if (menuItems[i].className == "current") {
					menuItems[i].removeAttribute('class');
					//removeAttribute Explorer
					if (navigator.appName.indexOf("Explorer") > 0 && (navigator.userAgent.indexOf("MSIE 6") > 0 || navigator.userAgent.indexOf("MSIE 7") > 0)) {
						menuItems[i].removeAttribute('className');
					}
				}
				else if (menuItems[i].className == "current last") {
					menuItems[i].className = "last";
				}
			}
		
		}

	
	//alert(selectedTab);
	
	//Manipulo los Tabs
	for (var i=0; i<tabItems.length; i++) {
		
		var currentTab = document.getElementById(tabItems[i]);
		//alert("currentTab = "  + currentTab.id);
		if(stylesDisabled == false) {
			if (tabItems[i] == selectedTab) {	
				currentTab.className = "fichaTab currentTab";
			}
			else {
				currentTab.className = "fichaTab";
			}
		}
	}
	

	//Se acomoda el Footer SOLO en Explorer 6 y 7
	if (navigator.appName.indexOf("Explorer") > 0 && (navigator.userAgent.indexOf("MSIE 6") > 0 || navigator.userAgent.indexOf("MSIE 7") > 0)) {
		pageFooter.style.bottom = "1px";
		pageFooter.style.bottom = "0px";
	}

	
}

//Aņado eventos a los Anchosr de los Menus
setUpMenuEvents = function (menuItem,tab) {
	//alert(menuItem + "||" + tab);
	
	menuItem.onmouseup = function () {
		//alert("UP!!");
		//return false;
	}
	menuItem.onclick = function () {
		fichaMenuDisplay(tab,menuItem);
		if("undefined" != typeof(event) ) {
			event.returnValue = false;
		}
		return false;
	}
}


//verify CSS is active ("FUNCION GENERICA")
function testCSS() {
	var cssdisabled = false; // must be proven otherwise
	var testcss = document.createElement('div');
	
	testcss.innerHTML = "testing...";
	
	
	testcss.style.position = "absolute";
	document.getElementsByTagName("body")[0].appendChild(testcss);
	if (testcss.currentStyle) {
		var currstyle = testcss.currentStyle["position"];
		//alert(currstyle);
	}
	else if (window.getComputedStyle) {
		//alert(currstyle);
		var currstyle = document.defaultView.getComputedStyle(testcss, null).getPropertyValue("position");
		
	}
	var cssdisabled = (currstyle == "static") ? true : false;
	document.getElementsByTagName("body")[0].removeChild(testcss);
	//alert(cssdisabled);
	
	return cssdisabled;
}


//Test expandable Menu
function testVars(mouseEvent) {
//function testVars(activeSmenus,currentSize,targetSize) {
//function testVars(tabClass) {


	var outPutVars = ""
	

	+ '<strong>TOP MENU</strong><br />'
	+ '<strong>'
	+ 'mouseEvent=' + mouseEvent
	+ '<br />'
	+ '<br /></strong>';

	
	/*
	+ '<strong>EXPANDABLE MENU</strong><br />'
	+ '<strong>'
	+ 'activeSmenus=' + activeSmenus
	+ '<br />'
	+ 'currentSize=' + currentSize
	+ '<br />'
	+ 'targetSize=' + targetSize
	+ '<br />'
	+ '<br /></strong>';
	*/
	
	/*
	+ '<strong>TAB CLASSES</strong><br />'
	+ '<strong>'
	+ 'tabClass=' + tabClass
	+ '<br /></strong>';
	*/
	
	document.getElementById("testVars").innerHTML = outPutVars;

}

///////////////////////////////////////////////////
//ZOOM IMAGENES
///////////////////////////////////////////////////

function zoomImages(zoomUrl,ancho,alto) {
	
	porperties = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=" + ancho + ",height=" + alto + ",left=0,top=0";
	mywin = window.open(zoomUrl,"win",porperties);
	mywin.self.focus();
}



//Initial setUp
setUpPage = function() {
	//alert ("settingUp!!");
	
	//Menus Principal
	startMainMenu("mainMenu");
	
	//Menus Expandibles
	startIzqMenus("izqMenu");
	
	//Menu Ficha en Explorer
	if (navigator.appName.indexOf("Explorer") > 0 && navigator.userAgent.indexOf("MSIE 6.0") > 0) {
		startList();
	}
	
	//Folders y menu-Folders              
	stylesDisabled = testCSS();
	fichaMenuSetUp("menuFicha", "fichaTab");
	
} 


window.onload = setUpPage;
