//Control de velocidad del telón
var GlobalRefreshStep;
GlobalRefreshStep = new Number(100);

//Funciones del menú
var globalId;
var mov=-2;
var IdCapa;
var Delay;
var xpos=-30;
var IdClass;
var divButtons = new Array();
//var ArrayCount;


var GlobalRand;

function MinHeight(LayerName)
{	
	var MinH;
	var Mintemp;
	if (!LayerName)
		LayerName=DivSelected;
	IdCapa="tablaClass_"+LayerName;
	IdTabla="itemsClass_"+LayerName;
	if (!document.getElementById(IdCapa))
		return;
	if (parseInt(document.getElementById(IdTabla).style.height) > 3)
	{
		MinH=document.getElementById(IdCapa).offsetHeight;
		for ( i=0; i<MinH; i=i+2) 
		{
			MinglobalId="document.getElementById('"+IdTabla+"').style.height='"+parseInt(MinH-i)+"px';";
			Mintemp=parseInt(i*15);
			setTimeout(MinglobalId,Mintemp);
		}
	}
	//document.getElementById(SpanMin).className = "LeftMenu";
//	if (document.getElementById(IdTabla).style.height >= 2 )
}
function MaxHeight(LayerName)
{	
	var h;
	var temp;
	//logVars=logVars+" MaxH:"+id;
	//document.getElementById('LinkInicio').className = "LeftMenu";
	IdCapa="tablaClass_"+LayerName;
	IdTabla="itemsClass_"+LayerName;
	//SpanMax="span_"+LayerName;
	if (!document.getElementById(IdCapa))
		return;
	if (parseInt(document.getElementById(IdTabla).style.height) < 3)
	{	
		//alert(document.getElementById(IdTabla).style.height);
		//if (isIE)
			//alert(document.getElementById(IdCapa).offsetHeight);
		h=document.getElementById(IdCapa).offsetHeight;
		for ( i=0; i<h; i=i+2) 
		{
			globalId="document.getElementById('"+IdTabla+"').style.height='"+parseInt(i)+"px';";
			temp=parseInt(i*15);
			Delay=temp;
			setTimeout(globalId,temp);
		}
		clearTimeout();
		DivSelected = LayerName;
	}
		
	/*if (LayerName==DivSelected && InitPos==1)
		InitPos = 0;
	else
	{
		DivSelected=undefined;
		return;
	}*/
	//document.getElementById(SpanMax).className = "LeftMenuON";	
	//h= document.getElementById(IdCapa).style.height
	//alert(h);
}

function LateralMove()
{
	//document.getElementById(IdCapa).style.visibility="visible";
	IdCapa="tablaClass_"+IdClass;
	IdTabla="itemsClass_"+IdClass;
	xpos=xpos+2*mov;
	if(xpos <- 50) {xpos=-50};
	if(xpos > 10) {xpos=10};
	document.getElementById(IdCapa).style.left=xpos+"px";
	//alert(document.getElementById(IdCapa).style.left);		
	if (xpos <=-200 || xpos >=10) {
		window.clearTimeout() 
	}
	else {
		window.setTimeout("LateralMove();",100);
	}
} 

function MarkPosition(Num)
{
	var ind;
	for (ind in divButtons)
	{
		if (ind == Num)
		{
			if (divButtons[ind] == 1)
				divButtons[ind] = 2;
			else
				divButtons[ind] = 1;
		}
		else
			divButtons[ind]=1;
	}
	MoveMenu(Num)
}

function MoveMenu(Num)
{
	//NumTemp="itemsClass_"+Num;
	IdTabla="itemsClass_"+Num;
	//IdCapa="tablaClass_"+Num;
	for (var j in document.getElementsByTagName("div"))
	{	
		if (document.getElementsByTagName("div")[j].id !== undefined)
		{
			//alert(j+document.getElementsByTagName("div")[j].id);
			nomDiv = document.getElementsByTagName("div")[j].id;
			ArrayCapa = nomDiv.split("_");
			if ((ArrayCapa[0]=="itemsClass"))
			{
				IdClass = ArrayCapa[1];
				if (divButtons[ArrayCapa[1]]==2 )	
				{
					mov=2;
					MaxHeight(ArrayCapa[1]);	
					setTimeout("LateralMove();",Delay);
				}
				else					//tiene que estar
				{
					mov=-2;
					LateralMove();
//					MinHeight(ArrayCapa[1]);		
				}
			}
		}
	}
}
//var ImgBgndArray = new Array();
//ImgBgndArray[3] = '/userfiles/contenido/class/3.jpg';
//ImgBgndArray[5] = '/userfiles/contenido/class/5.jpg';
//ImgBgndArray[10] = '/userfiles/contenido/class/10.jpg';
//onload='MM_preloadImages()'

var StatusCurtain;
var GlobalUrlDest;
var GlobalContenLayer;
var GlobalEntryId;
var GlobalEntryCode;
var GlobalItemId;
var GlobalHeightSize;
var GlobalStep;
var CurtainSTC;
var GlobalTmpLayer = 'TmpLayer';
var GlobalTmp2Layer = 'Tmp2Layer';
var GlobalWidthImage;
var GlobalHeightImage;
var ImageAnimation = false;
var MOST;
var AnchorsArray = new Array();
var InitAArray = 0;
var TimeOutMiliSecDef = 45;
var TimeOutMiliSecImgDef = 45;

GlobalStatusCurtain = 'open';
GlobalHeightSize = new Number(400);
GlobalOpRefreshStep = GlobalRefreshStep;
GlobalStep = new Number(40);
GlobalCurtainLayer = 'CurtainLayer';
GlobalImageStep = new Number(80);
GlobalWidthImage = new Number(600);
GlobalHeightImage = new Number(400);


function ProcessInfo(LayerName,URLDest, EntryId, EntryCode, ItemId)
{
	var ElementName;
	ImageSrc = '?GetClassImage=1&ClassId=';
	GlobalUrlDest = URLDest;
	GlobalContenLayer = LayerName;
	GlobalEntryId = EntryId;
	GlobalEntryCode = EntryCode;
	GlobalItemId = ItemId;
	//alert("Paso");
	UseFlash=0;
	
	ImgObj1 = document.getElementById('MarginImgTop');
	ImgObj2 = document.getElementById('MarginImgLeft');
	ImgObj3 = document.getElementById('MarginImgRight');
	ImgObj4 = document.getElementById('MarginImgBottom');
	
	
	if (GlobalStatusCurtain == 'open')
	{
		switch(URLDest)
		{ 
			case '/index.php?ClassCode=STATIC':
				ElementName = 'LinkInicio';
				URLDest = URLDest+"&Ajax=1";
				ImageSrc = FondoArray[1];
				ChangeItemShow('0');
				UseFlash=1;				
			break;
			case '/formulario/index.php':
				ElementName = 'LinkForm';
				URLDest = URLDest+"?Ajax=1";
				ImageSrc = FondoArray[5];
			break;
			default:
				if (EntryId)
				{	
					ElementName = 'span_'+EntryId;
					URLDest = URLDest+"&Ajax=1";
				}
				if (EntryId!="10"){
					opacityValue = 0;
					setOpacity(ImgObj1,opacityValue);
					setOpacity(ImgObj2,opacityValue);
					setOpacity(ImgObj3,opacityValue);
					setOpacity(ImgObj4,opacityValue);					
					ChangeItemShow(ItemId, EntryId);
					UseFlash=1;
				}
			break;
		}
		SelectedMenu(ElementName, "SpanItem_"+ItemId);
		
		if (EntryId)
			ImageSrc = ImageSrc+EntryId;
		TableObj = document.getElementById('PageTable');
	//	TableObj.style.backgroundImage = 'url('+ImgBgndArray[EntryId]+')';
		TableObj.style.backgroundImage = 'url('+ImageSrc+')';

		if (UseFlash==1)
			return;
		ProcessMargin();
		StopImage = 1;
		
		document.getElementById(GlobalTmpLayer).innerHTML = 'false';
		RefreshAjaxDiv(GlobalTmpLayer, URLDest);

		var Rand = Math.floor(Math.random()*(FondoArray.length));
		while (Rand==GlobalRand)
		{
			Rand = Math.floor(Math.random()*(FondoArray.length));
		}
		if(Rand==FondoArray.length)
			Rand=FondoArray.length-1;
		TableObj.style.backgroundImage = 'url('+FondoArray[Rand]+')';
		GlobalRand = Rand;
	
		GCLObj = document.getElementById(GlobalCurtainLayer);
		//alert(GCLObj.style.backgroundImage);
		GCLObj.style.height = 1;
			if(GCLObj.filters)
				GCLObj.filters.alpha.opacity = 0;
			else
				GCLObj.style.MozOpacity = 0;
		
		//CloseCurtain(GlobalCurtainLayer, GlobalStep);
		ShowCurtain(GlobalCurtainLayer, GlobalStep);		
		return;
	}
	LayerContent = document.getElementById(GlobalTmpLayer).innerHTML;
	if (LayerContent != 'false')
	{
		//alert(LayerContent);
		LayerContentObj = document.getElementById(GlobalContenLayer);
		LayerContentObj.innerHTML = LayerContent;
		document.getElementById(GlobalTmpLayer).innerHTML = 'false';
		//alert("aca");
		//OpenCurtain(GlobalCurtainLayer, GlobalStep);
		HideCurtain(GlobalCurtainLayer, GlobalStep);		
		if (EntryId)
		{
			StopImage = null;
			document.getElementById(GlobalTmp2Layer).innerHTML = 'false';
			UrlJS = '?ProcessPhotos=1&ClassId='+EntryId;
			if (ItemId)
				UrlJS = UrlJS + '&Id='+ItemId;
			CallAjaxJs(GlobalTmp2Layer,UrlJS);		
		}
		if (EntryCode)
		{
			StopImage = null;
			document.getElementById(GlobalTmp2Layer).innerHTML = 'false';
			CallAjaxJs(GlobalTmp2Layer,'?ProcessPhotos=1&ClassCode='+EntryCode);		
		}

	}
	else
		ContentRefreshST=setTimeout('ProcessInfo("'+LayerName+'","'+URLDest+'","'+EntryId+'","'+EntryCode+','+ItemId+'")',GlobalRefreshStep);	
}

function ProcessMargin()
{
	ImgObj1 = document.getElementById('MarginImgTop');
	ImgObj2 = document.getElementById('MarginImgLeft');
	ImgObj3 = document.getElementById('MarginImgRight');
	ImgObj4 = document.getElementById('MarginImgBottom');
		
	if (document.getElementById('TransitionImg'))
	{
		TimeOutMiliSec = document.getElementById('TransitionImg').value;
		TimeOutMiliSecImg = document.getElementById('TransitionImg').value;
	}else
	{
		TimeOutMiliSec = TimeOutMiliSecDef;
		TimeOutMiliSecImg = TimeOutMiliSecImgDef;
	}
	opacityValue = getOpacity(ImgObj1);

	if (opacityValue > 0)
	{
		opacityValue-=3;
		setOpacity(ImgObj1,opacityValue);
		setOpacity(ImgObj2,opacityValue);
		setOpacity(ImgObj3,opacityValue);
		setOpacity(ImgObj4,opacityValue);
		MOST=window.setTimeout("ProcessMargin();",TimeOutMiliSecImg);
	}
	else
	{
		if (ImgObj1) ImgObj1.width = 1;
		if (ImgObj2) ImgObj2.height = 1;
		if (ImgObj3) ImgObj3.height = 1;
		if (ImgObj4) ImgObj4.width = 1;
		if(MOST)
			clearTimeout(MOST);
		ExpandTopWidthImage('MarginImgTop');
	}

}

function HideCurtain(LayerName, Step)
{
	ImageObj = document.getElementById(LayerName);
	OpacityValue = getOpacity(ImageObj);
	if (OpacityValue > 0)
	{
		OpacityValue-=Step;
		setOpacity(ImageObj, OpacityValue);
		CurtainSTHC=setTimeout('HideCurtain("'+LayerName+'",'+Step+')',GlobalOpRefreshStep);
	}
	else
	{
		LayerObj = document.getElementById(LayerName);
		LayerObj.style.display='none';
		LayerObj.style.height = 1;
			if(LayerObj.filters)
				LayerObj.filters.alpha.opacity = 0;
			else
				LayerObj.style.MozOpacity = 0;
		GlobalStatusCurtain = 'open';
		clearTimeout(CurtainSTHC);
	}

}


function OpenCurtain(LayerName, Step)
{
	LayerObj = document.getElementById(LayerName);

	if (LayerObj)
	{
		LayerHeight = LayerObj.style.height;
		//alert("Abre"+LayerHeight);
		LayerHeight = new Number(LayerHeight.replace('px',''));
		if (LayerHeight > 1)
		{
			LayerObj.style.height = LayerHeight - Step;
			CurtainSTOP=setTimeout('OpenCurtain("'+LayerName+'",'+Step+')',GlobalRefreshStep);
		}
		else
		{
			GlobalStatusCurtain = 'open';
			clearTimeout(CurtainSTOP);
			LayerObj.style.display = 'none';
			/*LayerObj.style.height = 1;
			if(LayerObj.filters)
				LayerObj.filters.alpha.opacity = 0;
			else
				LayerObj.style.MozOpacity = 0;*/

		}
	}
}

function ShowCurtain(LayerName, Step)
{
	LayerObj = document.getElementById(LayerName);
	LayerObj.style.display='inline';
	LayerObj.style.height = '400px';
	ImageObj = document.getElementById(LayerName);
//alert(ImageObj);
	OpacityValue = getOpacity(ImageObj);
//alert(OpacityValue);
//return;
	if (OpacityValue < 100)
	{
		OpacityValue+=Step;
		setOpacity(ImageObj, OpacityValue);
		CurtainSTSC=setTimeout('ShowCurtain("'+LayerName+'",'+Step+')',GlobalOpRefreshStep);
	}
	else
	{
		GlobalStatusCurtain = 'close';
		ProcessInfo(GlobalContenLayer,GlobalUrlDest, GlobalEntryId, GlobalEntryCode, GlobalItemId);		
		clearTimeout(CurtainSTSC);
	}
	
}

function CloseCurtain(LayerName, Step )
{
	LayerObj = document.getElementById(LayerName);
	if (LayerObj)
	{
		//LayerObj.style.height = GlobalHeightSize;
		alert("Cierra"+LayerObj.style.height);
		LayerHeight = LayerObj.style.height;
		LayerHeight = new Number(LayerHeight.replace('px',''));
		LayerObj.style.display = 'inline';
		if (LayerHeight < GlobalHeightSize)
		{
			LayerObj.style.height = LayerHeight + Step;
			CurtainSTCC=setTimeout('CloseCurtain("'+LayerName+'",'+Step+')',GlobalRefreshStep);
		}
		else
		{
			GlobalStatusCurtain = 'close';
			ProcessInfo(GlobalContenLayer,GlobalUrlDest, GlobalEntryId, GlobalEntryCode, GlobalItemId);
			clearTimeout(CurtainSTCC);
		}
	}
}

function SetImagesMeasures()
{
	ImageAnimation = true;	
}

function ExpandTopWidthImage(ImageName)
{
	if(ImageAnimation==false)
		SetImagesMeasures();
		
	ImageObj = document.getElementById(ImageName);
	if (ImageObj)
	{
		setOpacity(ImageObj, 100);
		ExpandWidthSTC=setTimeout('ExpandTopWidthImage("'+ImageName+'")',GlobalRefreshStep);
		ActualWidth = new Number();
		ActualWidth = ImageObj.width;
		if (ActualWidth == 1)
			ActualWidth = 0;
			
		if (ActualWidth < GlobalWidthImage)	
		{
			if ((ActualWidth + GlobalImageStep) >  GlobalWidthImage )
				ImageObj.width = GlobalWidthImage;
			else
				ImageObj.width = ActualWidth + GlobalImageStep;
		}
		else
		{
			ExpandHeightImage('MarginImgLeft', 'MarginImgRight');
			clearTimeout(ExpandWidthSTC);
		}
	}
}

function ExpandHeightImage(ImageName, ImageName2)
{
	ImageObj = document.getElementById(ImageName);
	ImageObj2 = document.getElementById(ImageName2);
	if (ImageObj && ImageObj2)
	{
		setOpacity(ImageObj, 100);
		setOpacity(ImageObj2, 100);
		ExpandHeightSTC=setTimeout('ExpandHeightImage("'+ImageName+'","'+ImageName2+'")',GlobalRefreshStep);
		ActualHeight = new Number();
		ActualHeight = ImageObj.height;
		if (ActualHeight == 1)
			ActualHeight = 0;
		if (ActualHeight < GlobalHeightImage)
		{
			ImageObj.height = ActualHeight + GlobalImageStep;
			ImageObj2.height = ActualHeight + GlobalImageStep;
		}
		else
		{
			ExpandBottomWidthImage('MarginImgBottom');
			clearTimeout(ExpandHeightSTC);
		}
	}
}

function ExpandBottomWidthImage(ImageName)
{
	ImageObj = document.getElementById(ImageName);
	if (ImageObj)
	{
		setOpacity(ImageObj, 100);
		ExpandBottomWidthSTC=setTimeout('ExpandBottomWidthImage("'+ImageName+'")',GlobalRefreshStep);
		ActualWidth = new Number();
		ActualWidth = ImageObj.width;
		if (ActualWidth == 1)
			ActualWidth = 0;
		if (ActualWidth < GlobalWidthImage)
		{
			if ((ActualWidth + GlobalImageStep) >  GlobalWidthImage )
				ImageObj.width = GlobalWidthImage;
			else
				ImageObj.width = ActualWidth + GlobalImageStep;
		}
		else
		{
			clearTimeout(ExpandBottomWidthSTC);
			ImageAnimation = false;
		}
	}
}


//Funciones del album de fotos
var TimeOutMiliSec = 40;
var TimeOutMiliSecImg = 40;
var BackGroundImageInit = "url(";
var BackGroundImageEnd =  ") no-repeat center center";
var CallJSST;
var ArrayLength;
var ItemsArray = new Array();
var ID2;
var StopImage = 0;

function setOpacity(ImageObject, NewValue)
{
	if (!ImageObject) return;
	if(ImageObject.filters)
		ImageObject.filters.alpha.opacity = NewValue;
	else
		ImageObject.style.MozOpacity = NewValue / 100;
}

function getOpacity(ImageObj)
{
	if (!ImageObj) return;
	if(ImageObj.filters)
		opacityValue = ImageObj.filters.alpha.opacity;
	else
		opacityValue = ImageObj.style.MozOpacity*100;
	return opacityValue;
}

function LoadNextImage(ImageObject)
{
	ActualImageId = document.getElementById('TdProducto').ImageId;
	ActualImageId++;
	if(ActualImageId >= (ArrayLength))
	{
		//alert(ItemsArray[0]);
		ImageObject.src = ItemsArray[0];
		ImageObject.ImageId = 0;
	}
	else
	{
		ImageObject.src = ItemsArray[ActualImageId];
		ImageObject.ImageId = ActualImageId;
	}
}

function OpacityOn()
{
	MainImage = document.getElementById('FilterImg');
	opacityValue = getOpacity(MainImage);
	if (opacityValue < 70 )
	{
		opacityValue+=6;
		setOpacity(MainImage,opacityValue);
		ID2=window.setTimeout("OpacityOn();",TimeOutMiliSecImg);
	}
	else
		ShowNextImage();
}

function OpacityOff()
{
	MainImage = document.getElementById('FilterImg');
	opacityValue = getOpacity(MainImage);
	if (opacityValue > 0)
	{
		opacityValue-=3;
		setOpacity(MainImage,opacityValue);
		ID2=window.setTimeout("OpacityOff();",TimeOutMiliSecImg);
	}
	else
	{
		if (CountImg>1)
			ID2=window.setTimeout("OpacityOn();",TimeOutMiliSec);
	}
}

function ShowNextImage()
{
	MainImage = document.getElementById('TdProducto');
	AuxImage = document.getElementById('ImgAuxProducto');
	if (!MainImage || !AuxImage)
		return;
	MainImage.style.background = BackGroundImageInit + AuxImage.src + BackGroundImageEnd;
	MainImage.ImageId = AuxImage.ImageId;
	LoadNextImage(AuxImage);
	if ( StopImage == 1)
		clearTimeout(ID2);	
	else
		OpacityOff();	
}

function CallAjaxJs(LayerName, Url)
{
	LayerObj = document.getElementById(LayerName);
	//alert(Url);	
	if (LayerObj)
		LayerObj.innerHTML = '<iframe src="/index.php'+Url+'"></iframe>';
//	RefreshAjaxDiv(LayerName, Url, "", "", "", "", "", 1 );
}

var ActualRootSel = 'LinkInicio';
var ActualSubRootSel;
var RootSelClassName = 'LeftMenuON';
var RootStdClassName = 'LeftMenu';
var SubRootSelClassName = 'LeftSubMenuOn';
var SubRootStdClassName = 'LeftSubMenu';

function SelectedMenu(ElementId, SubMenuId)
{
	SelectedElement = document.getElementById(ElementId);
	
	if (SelectedElement)
		SelectedElement.className = RootSelClassName;
	if (ActualRootSel && ActualRootSel != ElementId )
	{
		ActualSelected = document.getElementById(ActualRootSel);
		ActualSelected.className = RootStdClassName;
	}
	ActualRootSel = ElementId;
	
	if (SubMenuId && document.getElementById(SubMenuId))
	{
		SelectedElement = document.getElementById(SubMenuId);
		SelectedElement.className = SubRootSelClassName;
	}
	if (ActualSubRootSel && (SubMenuId != ActualSubRootSel) && document.getElementById(ActualSubRootSel))
	{
		ActualSelected = document.getElementById(ActualSubRootSel);
		ActualSelected.className = SubRootStdClassName;
	}
	ActualSubRootSel = SubMenuId;
}
