/***************************** CONSTANTS *************************************/
GlobalId = new Array ();
runningGlobal = false;
LAYER = 0;
SlideMenuMAIN = 1;
TOP = 2;
LEFT = 3;
HEIGHT = 4;
WIDTH = 5;
SLIDE = 6;
ACTIVE = 7;
LASTMAIN = 8;
CLOSES = 9;
RUNNING = 10;
FIRST = 11;
THEREC = 12;
THERES = 13;
SECOND = 14;
THERECC = 15;
THERECS = 16;
SUBLEFT = 0; // Margen adicional en la parte izquierda de las subopciones.
DEFAULTWIDTH = 130; // Ancho mínimo por defecto.

function SlideMenurunMenu(myName,GlobalContent,newspeed){
	ieStep=0;
	GlobalContent[THERES]=false;
	GlobalContent[THEREC]=false;
	if(newspeed>0){mySpeed=newspeed;}
	GlobalContent[FIRST]=myName; // A partir de esta opcion hay q desplegar.
	if (GlobalContent[ACTIVE]==-1){		
		if(!GlobalContent[RUNNING]&&GlobalContent[SlideMenuMAIN][myName]){
			GlobalContent[RUNNING]=true;
			
			GlobalContent[LAYER][myName+1].clip= "rect(" + ("auto") +" "+ ("auto") +" "+ (0) +" "+ ("auto") +")";
			GlobalContent[LAYER][myName+1].visibility=VISIBLE;
			SlideMenuopenMenuS(myName,GlobalContent);
			SlideMenuopenMenuC(myName,GlobalContent);
		}
		return;
	}	
	if (GlobalContent[ACTIVE]>=0 && !(GlobalContent[ACTIVE]==myName)){	
		if(!GlobalContent[RUNNING]){
			// Se ejecuta para cerrar las opciones y luego abrir la nuevas seleccionadas.
			GlobalContent[RUNNING]=true;
			GlobalContent[SECOND]=GlobalContent[ACTIVE];				
				
			ieStep1=GlobalContent[HEIGHT][GlobalContent[SECOND]+1];
			GlobalContent[THERECS]=false;
			GlobalContent[THERECC]=false;
			SlideMenucloseMenuS(GlobalContent[SECOND],GlobalContent);
			SlideMenucloseMenuC(GlobalContent[SECOND],GlobalContent);
		}
	}
	if (GlobalContent[ACTIVE]>=0 && GlobalContent[ACTIVE]==myName){
		if(!GlobalContent[RUNNING]&&GlobalContent[CLOSES]){
			// Cierra la opcion que esta activa.
			GlobalContent[RUNNING]=true;
			GlobalContent[SECOND]=GlobalContent[ACTIVE];
				
			ieStep1=GlobalContent[HEIGHT][GlobalContent[SECOND]+1];
			GlobalContent[THERECS]=false;
			GlobalContent[THERECC]=false;
			SlideMenucloseMenuS(GlobalContent[SECOND],GlobalContent);
			SlideMenucloseMenuC(GlobalContent[SECOND],GlobalContent);
		}
		return;
	}
}


function SlideMenustopCloseS(myName,GlobalContent){
	GlobalContent[RUNNING]=false;
	GlobalContent[THERECS]=true;
	if(GlobalContent[CLOSES]&&GlobalContent[FIRST]==GlobalContent[ACTIVE]){
		GlobalContent[ACTIVE]=-1;
	}else {
		GlobalContent[ACTIVE]=-1;					   	
		SlideMenurunMenu(GlobalContent[FIRST],GlobalContent);
	}
}

function SlideMenustopOpenS(myName,GlobalContent){
	GlobalContent[RUNNING]=false;
	//GlobalContent[THERES]=true;
	GlobalContent[LAYER][myName+1].clip= "rect(" + ("auto") +" "+ ("auto") +" "+ ("auto") +" "+ ("auto") +")";
	GlobalContent[ACTIVE]=myName;
}

function SlideMenuopenMenuS(myName,GlobalContent){
	myStep=mySpeed;
	if(!GlobalContent[THERES]&&!(GlobalContent[FIRST]==GlobalContent[LASTMAIN])){
		if(parseInt(GlobalContent[LAYER][GlobalContent[FIRST]+2].top)+myStep>GlobalContent[SLIDE][GlobalContent[FIRST]+1]){
			myStep=GlobalContent[SLIDE][GlobalContent[FIRST]+1]-parseInt (GlobalContent[LAYER][GlobalContent[FIRST]+2].top);
		}
		for(i=GlobalContent[FIRST]+2; i<GlobalContent[LAYER].length; i+=2){
			GlobalContent[LAYER][i].top=parseInt (GlobalContent[LAYER][i].top)+myStep;
		}		
		
		if(parseInt (GlobalContent[LAYER][GlobalContent[FIRST]+2].top)==GlobalContent[SLIDE][GlobalContent[FIRST]+1]){
			SlideMenustopOpenS(GlobalContent[FIRST],GlobalContent)
		}
		p = GlobalContent;
				
		if(GlobalContent[RUNNING])setTimeout('SlideMenuopenMenuS(p[FIRST],p)',5);
	}
}

function SlideMenuopenMenuC(myName,GlobalContent){
	myStep=mySpeed;		
	if(!GlobalContent[THEREC]){
		ieStep+=myStep;		
		GlobalContent[LAYER][myName+1].clip= "rect(" + ("auto") +" "+ ("auto") +" "+ (ieStep) +" "+ ("auto") +")";
		if(ieStep>=GlobalContent[HEIGHT][GlobalContent[FIRST]+1]){
			GlobalContent[THEREC]=true;
			GlobalContent[ACTIVE]=GlobalContent[FIRST];

			SlideMenustopOpenS(GlobalContent[FIRST],GlobalContent)
		}
		p2 = GlobalContent;
		if(GlobalContent[RUNNING])setTimeout('SlideMenuopenMenuC(p2[FIRST],p2)',5);
	}
}

function SlideMenucloseMenuS(myName,GlobalContent){
	myStep=mySpeed;
	if(!GlobalContent[THERECS]&&!(GlobalContent[SECOND]==GlobalContent[LASTMAIN])){
		if(parseInt(GlobalContent[LAYER][GlobalContent[SECOND]+2].top)-myStep<GlobalContent[TOP][GlobalContent[SECOND]+2]){
			myStep=parseInt (GlobalContent[LAYER][GlobalContent[SECOND]+2].top)-GlobalContent[TOP][GlobalContent[SECOND]+2];
		}
		for(i=GlobalContent[SECOND]+2; i<GlobalContent[LAYER].length; i+=2){
			GlobalContent[LAYER][i].top=parseInt (GlobalContent[LAYER][i].top)-myStep;	
		}		
		if(parseInt (GlobalContent[LAYER][GlobalContent[SECOND]+2].top)==GlobalContent[TOP][GlobalContent[SECOND]+2]){
			SlideMenustopCloseS(GlobalContent[SECOND],GlobalContent);
		}
		p = GlobalContent;
		if(GlobalContent[RUNNING])setTimeout('SlideMenucloseMenuS(p[SECOND],p)',5);
	}
}

function SlideMenucloseMenuC(myName,GlobalContent){
	myStep=-mySpeed;
	ieStep1-=mySpeed;
	if(!GlobalContent[THERECC]){
		if(ieStep1<=0){
			GlobalContent[LAYER][myName+1].clip= "rect(" + ("auto") +" "+ ("auto") +" "+ (0) +" "+ ("auto") +")";
			GlobalContent[THERECC]=true;
			if(GlobalContent[SECOND]==GlobalContent[LASTMAIN]) SlideMenustopCloseS(GlobalContent[SECOND],GlobalContent);
		}else {
			GlobalContent[LAYER][myName+1].clip= "rect(" + ("auto") +" "+ ("auto") +" "+ (ieStep1) +" "+ ("auto") +")";

		}
		p = GlobalContent;
		if(GlobalContent[RUNNING])setTimeout('SlideMenucloseMenuC(p[SECOND],p)',5);
	}
}

function runInitMenu (myName,GlobalContent){
	if (myName != -1){
		GlobalContent[FIRST]=myName;
		GlobalContent[LAYER][myName+1].clip= "rect(" + ("auto") +" "+ ("auto") +" "+ (0) +" "+ ("auto") +")";
		GlobalContent[LAYER][myName+1].visibility=VISIBLE;		
		if(!GlobalContent[THERES]&&!(GlobalContent[FIRST]==GlobalContent[LASTMAIN])){		
			for(i=GlobalContent[FIRST]+2; i<GlobalContent[LAYER].length; i+=2){
				GlobalContent[LAYER][i].top=parseInt (GlobalContent[LAYER][i].top)+GlobalContent[HEIGHT][GlobalContent[FIRST]+1];
			}						
		}
		GlobalContent[LAYER][myName+1].clip= "rect(" + ("auto") +" "+ ("auto") +" "+ (GlobalContent[HEIGHT][GlobalContent[FIRST]+1]) +" "+ ("auto") +")";
		GlobalContent[LAYER][myName+1].clip= "rect(" + ("auto") +" "+ ("auto") +" "+ ("auto") +" "+ ("auto") +")";
		GlobalContent[ACTIVE]=myName;
	}
}						

function getSlideOptionsPosition (myTop,myLeft,myHeight,myWidth,mySlide,myLayer,suboptions,SlideMenumain,maxSizes,slideId){
	var minTop = suboptions[0].offsetTop;
	var minLeft = suboptions[0].offsetLeft;	
	for(i=0; i<myLayer.length; i++){
		if(SlideMenumain[i]){
			myWidth[i]=suboptions[i].offsetWidth;
			if (maxSizes [1] > myWidth[i]){
				myWidth[i] = maxSizes [1];
			}		
			if(i<=0){
				myLeft[i]=suboptions[i].offsetLeft - minLeft;
				myTop[i]=suboptions[i].offsetTop - minTop;
			} else {										
				myLeft[i]=myLeft[i-2];
				myTop[i]=myTop[i-2]+myHeight[i-2];								
			}
			if (parseInt (myLayer[i].height)>suboptions[i].offsetHeight){
				myHeight[i]=parseInt (myLayer[i].height);
			} else {
				myHeight[i]=suboptions[i].offsetHeight;
			}
				
			myLayer[i].left=myLeft[i];
			myLayer[i].top=myTop[i];
			myLayer[i].visibility=VISIBLE;
		}
		if(!SlideMenumain[i]) {
			myWidth[i]=suboptions[i].offsetWidth;
			if (maxSizes [1] > myWidth[i]){
				myWidth[i] = maxSizes [1];
			}		
			myTop[i]=myTop[i-1]+myHeight[i-1];
			myLeft[i]=myLeft[i-1];									
			myHeight[i]=suboptions[i].offsetHeight;
			myLayer[i].left=myLeft[i]+SUBLEFT;
			myLayer[i].top=myTop[i];
			mySlide[i]=myTop[i]+myHeight[i];
			if (myHeight[i] > maxSizes [0]){
				maxSizes [0] = myHeight[i];
			}				
		}		
		if (document.getElementById('tdSlideContainer'+slideId).offsetWidth > maxSizes [1]){
			maxSizes [1] = document.getElementById('tdSlideContainer'+slideId).offsetWidth;
		}		
		myLayer[i].position = 'absolute';		
	}

}

function SlideMenuinitMenu(slideId,numberOptions,activePosition,hasSidebar){
	var content = new Array (); //This array is the container of the parameters.
	HIDDEN = 'hidden';
	VISIBLE = 'visible';
	var myLayer = new Array();
	var suboptions = new Array ();
	var SlideMenumain=new Array(); // Tell me which option is main or suboption.
	var myTop=new Array(); // Array with top values.
	var myLeft=new Array(); // Array with left values.
	var myHeight=new Array(); // Array with height values.
	var myWidth=new Array(); // Array with width values.
	var mySlide=new Array(); // Array with slide values.
	var maxSizes=new Array(); // Array with container sizes
	
	var closes = true;
	var maxSubOptionsHeight = 0;
	var maxOptionsWidth = DEFAULTWIDTH;
	var running=false; //running tell me if the option is slide.																
	
	for(i=0; i<numberOptions; i++){
		myLayer[i] = document.getElementById ("SlideMenu" + slideId + i).style;	
		suboptions [i] = document.getElementById ("SlideMenu" + slideId + i);				
	}
	
	if (hasSidebar){
		myLayer[myLayer.length]= document.getElementById ("sidebarContainer" + slideId).style;		
		suboptions [suboptions.length] = document.getElementById ("sidebarContainer" + slideId);
		myLayer[myLayer.length]= document.getElementById ("sidebar2Container" + slideId).style;
		suboptions [suboptions.length] = document.getElementById ("sidebar2Container" + slideId);		
	}
	
	for(i=0; i<myLayer.length; i++){
		var mainORsub= i % 2;
		SlideMenumain[i] = mainORsub ? 0:1;
	}
	
	var lastMain=myLayer.length-2; //Tell me which option is the last.
	maxSizes[0] = maxSubOptionsHeight;
	maxSizes[1] = maxOptionsWidth;
	getSlideOptionsPosition (myTop,myLeft,myHeight,myWidth,mySlide,myLayer,suboptions,SlideMenumain,maxSizes,slideId);
		
	document.getElementById ("slideContainer" + slideId).style.height = myTop[myTop.length -1] + maxSizes[0];													
	document.getElementById ("slideContainer" + slideId).style.width = maxSizes[1];													
	document.getElementById ("slideContainer" + slideId).style.position = "relative";
	document.getElementById ("imgSlideContainer" + slideId).style.width = maxSizes[1];
	
	getSlideOptionsPosition (myTop,myLeft,myHeight,myWidth,mySlide,myLayer,suboptions,SlideMenumain,maxSizes,slideId);	
	
	content [LAYER] = myLayer;
	content [SlideMenuMAIN] = SlideMenumain;
	content [TOP] = myTop;
	content [LEFT] = myLeft;
	content [HEIGHT] = myHeight;
	content [WIDTH] = myWidth;
	content [SLIDE] = mySlide;
	content [ACTIVE] = activePosition;
	content [LASTMAIN] = lastMain;
	content [CLOSES] = closes;
	content [RUNNING] = running;
	content [FIRST] = -1;
	content [THEREC] = false;
	content [THERES] = false;
	content [SECOND] = -1;	
	
	runInitMenu (content[ACTIVE],content);	
	document.getElementById ("imgSlideContainer" + slideId).style.display = 'none';
	return content;
}
