/********************************************************
	 TRATAMIENTO DE IMAGENES Y SUS EFECTOS
	 Necesita los includes de 
	 ManejoObjetos.js
	 Eventos.js
********************************************************/	 

//id de la imagen1, path imagen1 roll, etc...
function IMGPreloadImages()
{
var a=IMGPreloadImages.arguments;
var id;
	
	if(!document.IMGCache) document.IMGCache=new Array();
	for(var i=0; i<a.length; i+=2)
		{
		id=a[i];
		document.IMGCache[id]=new Array();
		document.IMGCache[id][0]=null;
		document.IMGCache[id][1]=new Image;
		document.IMGCache[id][1].src=a[i+1];
		};
};

function IMGRoll(id)
{
var im=document.getElementById(id);

	if (!im || !document.IMGCache|| !document.IMGCache[id]) return;

	if (document.IMGCache[id][0]==null) 
		{
		document.IMGCache[id][0]=new Image;
		document.IMGCache[id][0].src=im.src;
		};
	im.src=document.IMGCache[id][1].src;
};

function IMGNormal(id)
{
var im=document.getElementById(id);
	
	if (!im || !document.IMGCache|| !document.IMGCache[id]) return;

	if (document.IMGCache[id][0]!=null)
		im.src=document.IMGCache[id][0].src;
};


//Precarga de imagenes con 3 estados, normal, rollover y pulsado
//id,path normal, path rollover,path pulsado ...
function IMGPreloadImages3()
{
var a=IMGPreloadImages3.arguments;
var id,obj;
	
	if(!document.IMGCache3) document.IMGCache3=new Array();
	for(var i=0; i<a.length; i+=4)
		{
		id=a[i];
		obj=document.getElementById(id);
		obj.src=a[i+1];						//Cargamos la imagen normal en el image

		document.IMGCache3[id]=new Array();
		document.IMGCache3[id][0]=new Image;
		document.IMGCache3[id][0].src=a[i+1];
		document.IMGCache3[id][1]=new Image;
		document.IMGCache3[id][1].src=a[i+2];
		document.IMGCache3[id][2]=new Image;
		document.IMGCache3[id][2].src=a[i+3];
		
		};
		
};

//status 0-normal, 1-roll, 2-pulsado
function IMGPutStatus3(id,status)
{
var im=document.getElementById(id);

	im.src=document.IMGCache3[id][status].src;
};

//status 0-normal, 1-roll, 2-pulsado
function IMGGetStatus3(id)
{
var im=document.getElementById(id);
var i;

	for (i=0;i<=2;i++)
		if (im.src==document.IMGCache3[id][i].src) return i;
		
	return -1;
};


/*************************************************************
 Realiza el fadein de un objeto
 Si se le pasa una cadena en obj, la busca como id
 Si el objeto no tiene id, le asigna una
 mls son los milisegundos que tardará en hacer la transición
 Si mls<100, se tomará como 100
*************************************************************/
function IMGFadeIn(obj,mls)
{
var o;

	//Verifico si es un objeto, y si no presupongo que es una id
	if (!obj.tagName)
		o=document.getElementById(obj);
	else
		o=obj;

	//Y ahora si lo encontró, inicializamos el objeto y hacemos la primera llamada al timeout
	if (o)
		{
		//Si el objeto no tiene Id le asigna una
		if (o.id=='')
			{
			if(!document.IMGFadeIds) document.IMGFadeIds=0;	//Inicializamos una variable global
			o.id='IMGFade' + (document.IMGFadeIds++);
			};
		
		//Dependemos el navegador			
		if (navigator.appName=='Microsoft Internet Explorer')
			{
			//El explorer con los filtros
			if (!o.filters.alpha)
				{
				//Como no tiene el filtro, hay que crearlo
				o.style.filter='alpha(0)';
				}
			else
				{
				o.filters.alpha.Opacity=0;
				};
			}
		else
			{
			//Y para el resto dentro del stile, todos
			o.style.MozOpacity=0;
			o.style.KHTMLOpacity=0;
			o.style.opacity=0;
			};
		
		//Calculamos cada cuantos milisegundos hay que hacer la transición
		if (mls<100) mls=100;
		var time=Math.round(mls/100);
		if (time<1) time=1;
		
		//Y hacemos la primera llamada a la transición.
		IMGfade(o.id, 1, 0, time);
		};
};

/*************************************************************
 Realiza el fadeout de un objeto
 Si se le pasa una cadena en obj, la busca como id
 Si el objeto no tiene id, le asigna una
 mls son los milisegundos que tardará en hacer la transición
 Si mls<100, se tomará como 100
*************************************************************/
function IMGFadeOut(obj,mls)
{
var o;

	//Verifico si es un objeto, y si no presupongo que es una id
	if (!obj.tagName)
		o=document.getElementById(obj);
	else
		o=obj;

	//Y ahora si lo encontró, inicializamos el objeto y hacemos la primera llamada al timeout
	if (o)
		{
		//Si el objeto no tiene Id le asigna una
		if (o.id=='')
			{
			if(!document.IMGFadeIds) document.IMGFadeIds=0;	//Inicializamos una variable global
			o.id='IMGFade' + (document.IMGFadeIds++);
			};
		
		//Dependemos el navegador			
		if (navigator.appName=='Microsoft Internet Explorer')
			{
			//El explorer con los filtros
			if (!o.filters.alpha)
				{
				//Como no tiene el filtro, hay que crearlo
				o.style.filter='alpha(100)';
				}
			else
				{
				o.filters.alpha.Opacity=100;
				};
			}
		else
			{
			//Y para el resto dentro del stile, todos
			o.style.MozOpacity=1;
			o.style.KHTMLOpacity=1;
			o.style.opacity=1;
			};
		
		//Calculamos cada cuantos milisegundos hay que hacer la transición
		if (mls<100) mls=100;
		var time=Math.round(mls/100);
		if (time<1) time=1;
		
		//Y hacemos la primera llamada a la transición.
		IMGfade(o.id, 0, 100, time);
		};	
};


/************************************************************************************
 Realiza un fadein (dir=1) o un fadeout (dir=0) de un objeto referenciado por la id
*************************************************************************************/
function IMGfade(id,dir,opac,time)
{
	//Nos buscamos el objeto	
	obj=document.getElementById(id);
	
	//Calculamos el incremento, dependiendo del time
	var inc;
	switch(time)
		{
		case 1:
		case 2:
			inc=10;
			break;
			
		case 3:
		case 4:
			inc=8;
			break;
		
		case 5:
		case 6:
			inc=6;
			break;
			
		case 7:
		case 8:
		case 9:
			inc=4;
			break;
			
		default:
			inc=1;
			break;
		};
	
	//Dependiendo de si es fadein o fadeout cambiamos la opacidad
	if (dir==1)
		{
		//Fadein
		opac+=inc;
		if (opac>100) opac=100;
		}
	else
		{
		//Fadeout
		opac-=inc;
		if (opac<0)
			{
			opac=0;
			//Se acabo, la oculto
			obj.style.visibility='hidden';
			}
		};
	
	//Dependemos del navegador le metemos la 
	if(navigator.appName=='Microsoft Internet Explorer')
		{
		obj.filters.alpha.Opacity=opac;
		}
	else
		{
		obj.style.MozOpacity=(opac/100)-0.01;
		obj.style.KHTMLOpacity=(opac/100)-0.01;
		obj.style.opacity=(opac/100)-0.01;
		};
	
	//Si queda transición, lanzamos el timeout
	if (opac>0 && opac<100)
		setTimeout("IMGfade('" + obj.id + "'," + dir + "," + opac + "," + time + ")",time);
};


/****************************************************************
 
 Clase para la gestión de una secuencia de imágenes

 
****************************************************************/
function IMGSlideShow(objRef)
{
	/***********************************************************
				 Inicialización de la clase
	***********************************************************/
var obj;

	//Verifico si es un objeto, y si no presupongo que es una id
	if (!objRef.tagName)
		obj=document.getElementById(objRef);
	else
		obj=objRef;

	//Verificamos si el objeto existe
	if (!obj.tagName)
		{
		alert('Clase mal referenciada. Se le debe pasar o una id de un objeto o el objeto');
		return;
		};

//Variables globales
var clase=this;				//Nos referenciamos a nosotros mismos
var images=new Array();		//Matriz de objetos clImages
var top=OBJTop(obj);		//El top y el left del objeto referencia
var left=OBJLeft(obj);
var accion='stop';			//Si esta corriendo, play, si esta parado, stop
var idxImagen=0;			//El contador que lleva el control de las imagenes
var funVerificaRun=false;	//Indica si la función de verificacion de carga esta corriendo o no
var AllPicturesLoad=false;	//Indica si todas las imagenes están cargadas
var objPie=null;

//Declaramos los métodos
this.AddImage=mtAddImage;
this.AddImagePie=mtAddImagePie;
this.Play=mtPlay;
this.Stop=mtStop;
this.IsReady=mtIsReady;
this.StopAndHidden=mtStopAndHidden;
this.ShowOneImage=mtShowOneImage;
this.forceWidth='';		//Para forzar en el style de las imagenes
this.forceHeight='';

//Propiedades
this.IdPie=null;	//La id del objeto que va a contener los pies de foto
this.CenterImage=false;	//Si va a centrar las imagenes en relación al objeto "contenedor"

//Metodos internos
this.VerifyLoad=mtVerifyLoad;
this.PlayNext=mtPlayNext;

//Eventos que dispara la clase
this.EVTLoadEnd=null;	//Funcion que lanza la clase cuando se terminan de cargar todas las imagenes
this.EVTChange=null		//Nombre de la funcion que será llamada cuando haya un cambio de foto. Le añade los parametros de la foto, el pie de foto y el indice (objImg,pieFoto,index)

//Declaramos los eventos internos
this.Resize=evtResize;

	//Asignamos los eventos que necesitamos controlar
	EVTAsignaEvento(window, 'resize', clase.Resize);

	/**********************************************************
							 METODOS
	**********************************************************/
	//
	// Fuerza la transición a un número de foto concreto
	//
	function mtShowOneImage(idxNextImagen)
	{
		//Si no están toda las imagenes cargadas o esta funcionando, me piro
		if (!AllPicturesLoad || accion=='play') return;
	
		//Ajustamos, por si las moscas el indice de la siguiente imagen
		if (idxNextImagen>=images.length) idxNextImagen=images.length-1;
		if (idxNextImagen<0) idxNextImagen=0
		
		//Si es la que esta ya viendose, paso
		if (idxNextImagen==idxImagen) return;
		
		//Y mostramos la imagen con su transición
		mtShowImage(idxNextImagen);
	};

	//
	//Devuelve true si todas las fotos están cargadas
	//
	function mtIsReady()
	{
	//Nos buclamos por todas las imagenes verificando la carga. Si alguna no termino, nos piramos
	var i;
	for (i=0;i<images.length;i++)
		if (!images[i].image.complete)
			return false;

	//Ahora faltarán por cargar
	AllPicturesLoad=true;
		
	//Todas cargadas.
 	return true;
	};

	//
	//Añade una(s) imagen(s) al slide
	//path, mls de la transición (si 0 no hay transicion), mls que se muestra
	//
	function mtAddImage()
	{
	var i=images.length;
	var k;
	
		//Si esta en play, no puede cargar imagenes
		if (accion=='play') return;
	
		if (mtAddImage.arguments.length>0 && mtAddImage.arguments.length/3==Math.round(mtAddImage.arguments.length/3))
			{
			//Ahora faltarán por cargar
			AllPicturesLoad=false;
			
			//Nos creamos un nuevo elemento de la lista de imagenes por cada 3 parametros
			for (k=0; k<mtAddImage.arguments.length; k+=3)
				{
				images[i]=new clImages(mtAddImage.arguments[k],mtAddImage.arguments[k+1],mtAddImage.arguments[k+2],null);
				images[i].image.style.width=this.forceWidth;
				images[i].image.style.height=this.forceHeight;
				i++;
				};

			//Inicializamos el indice
			idxImagen=0;
			
			//Y llamamos a la función de verificación, si no esta corriendo
			if (!funVerificaRun) mtVerifyLoad();
			}
		else
			{
			alert('Nº de parámetros incorrectos.\nPor cada imagen:\npath, mls de la transición (si 0 no hay transicion), mls que se muestra');	
			};
	};

	//
	//Añade una(s) imagen(s) al slide con pie de foto
	//path, mls de la transición (si 0 no hay transicion), mls que se muestra, pie
	//
	function mtAddImagePie()
	{
	var i=images.length;
	var k;
	
		//Si esta en play, no puede cargar imagenes
		if (accion=='play') return;
	
		if (mtAddImagePie.arguments.length>0 && mtAddImagePie.arguments.length/4==Math.round(mtAddImagePie.arguments.length/4))
			{
			//Ahora faltarán por cargar
			AllPicturesLoad=false;
			
			//Nos creamos un nuevo elemento de la lista de imagenes por cada 3 parametros
			for (k=0; k<mtAddImagePie.arguments.length; k+=4)
				images[i++]=new clImages(mtAddImagePie.arguments[k],mtAddImagePie.arguments[k+1],mtAddImagePie.arguments[k+2],mtAddImagePie.arguments[k+3]);

			//Inicializamos el indice
			idxImagen=0;
			
			//Y llamamos a la función de verificación, si no esta corriendo
			if (!funVerificaRun) mtVerifyLoad();
			}
		else
			{
			alert('Nº de parámetros incorrectos.\nPor cada imagen:\npath, mls de la transición (si 0 no hay transicion), mls que se muestra, pie de foto');	
			};
	};


	//
	//Comienza el movimiento, que diver
	//
	function mtPlay()
	{
		//Nunca puede haber play si ya lo hay o no hay una imagenes, o no están todas cargadas
		if (accion=='play' || images.length<1  || !AllPicturesLoad) return;

		//Nos ponemos en play
		accion='play';
		//Y mostramos la que toque
		images[idxImagen].image.style.visibility='visible';
		//Si tenemos evento de cambio, lo lanzamos
		if (clase.EVTChange!=null) eval(clase.EVTChange + '(images[idxImagen].image,images[idxImagen].piedefoto,idxImagen);');
		//Y ponemos su pie
		mtPonPie();

		//Y Si hay más de una, configuramos el evento
		if (images.length>1)
			setTimeout(clase.PlayNext,images[idxImagen].waitShowMls);
	};

	//Se para el movimiento
	function mtStop()
	{
		accion='stop';
	};

	//Se para el movimiento y oculta las fotos
	function mtStopAndHidden()
	{
	var i;

		this.Stop();
		for (i=0;i<images.length;i++)
			images[i].image.style.visibility='hidden';
	};
	

	/**************************************************************
						 FUNCIONES INTERNAS
	**************************************************************/
	
	//
	//Verifica que todas las imagenes hayan cargado
	//
	function mtVerifyLoad()
	{
	var i;
	
	funVerificaRun=true;
	
	//Nos buclamos por todas las imagenes verificando la carga. Si alguna no termino, nos piramos
	//rellamandonos al medio segundo
	for (i=0;i<images.length;i++)
		if (!images[i].image.complete)
			{
			setTimeout(clase.VerifyLoad,500);
			return;
			};
	
	//Todas cargadas
	AllPicturesLoad=true;
	
	//Si necesitan centrado, llamo a resize
	if (clase.CenterImage) evtResize();
	
	//Todas cargadas. Si hay asignado evento lo lanzamos
 	if (clase.EVTLoadEnd) eval(clase.EVTLoadEnd);
 	funVerificaRun=false;
	};


	//
	//Carga la siguiente imagen que toque, con su transicion
	//	
	function mtPlayNext()
	{
	var idxNextImagen;
	var idxAntImagen;
	
		//Si lo pararon, aqui me quedo
		if (accion=='stop') return;
	
		//Calculamos el indice de la siguiente imagen
		idxNextImagen=idxImagen+1;
		if (idxNextImagen==images.length) idxNextImagen=0;
		
		//Antes de cambiarla me quedo con el indice como el anterior
		idxAntImagen=idxImagen;
		
		//La cambiamos. Esto cambia el indice
		mtShowImage(idxNextImagen);

		//Conectamos el timeout con el tiempo de espera, más el de la transicion
		setTimeout(clase.PlayNext,images[idxImagen].waitShowMls+images[idxAntImagen].transitionMls);
	};
	
	//Función que muestra la imagen que se le pasa.
	//No hace ningún tipo de comprobación, lanza lo que le viene
	function mtShowImage(idxNextImagen)
	{
		//Vemos si tenemos transicion con la imagen actual
		if (images[idxImagen].transitionMls!=0)
			{
			//La tenemos
			//Fadein de la entrante, fadeout de la saliente, con el tiempo de la saliente
			IMGFadeOut(images[idxImagen].image,images[idxImagen].transitionMls);
			IMGFadeIn(images[idxNextImagen].image,images[idxImagen].transitionMls);
			images[idxNextImagen].image.style.visibility='visible';
			}
		else
			{
			//No la tenemos. Ocultamos la actual, y mostramos la siguiente
			images[idxNextImagen].image.style.visibility='visible';
			images[idxImagen].image.style.visibility='hidden';
			};

		//Y dejamos el contador en su sitio
		idxImagen=idxNextImagen;

		//Si tenemos evento de cambio, lo lanzamos
		if (clase.EVTChange!=null) eval(clase.EVTChange + '(images[idxImagen].image,images[idxImagen].piedefoto,idxImagen);');

		//Ponemos el pie
		mtPonPie();
	};
	

	//Pone el pie de foto que toque
	function mtPonPie()
	{
		//Buscampos el objeto que hace de contenedor de los pies de página
		if (objPie==null && clase.IdPie!=null) objPie=document.getElementById(clase.IdPie);
		//Si tenemos objeto para los pies de página y la foto lo tiene, lo metemos
		if (objPie!=null)
			{
			if (images[idxImagen].piedefoto!=null)
				{
				objPie.innerHTML=images[idxImagen].piedefoto;
				}
			else
				{
				objPie.innerHTML='';
				};
			};
	};

	/**********************************************************
							 EVENTOS
	**********************************************************/

	//
	// Cuando hace resize el navegador, recolocamos las afotos
	//
	function evtResize()
	{
		var i;
		
		top=OBJTop(obj);		//Recalculamos el top y el left del objeto referencia
		left=OBJLeft(obj);
		
		//Y nos recorremos todas las imagenes, poniendoselo
		for(i=0;i<images.length;i++)
			{
			if (clase.CenterImage)
				{
				var xM=left + Math.round(obj.offsetWidth/2);
				var yM=top + Math.round(obj.offsetHeight/2);
				var xiM=Math.round(images[i].image.offsetWidth/2);
				var yiM=Math.round(images[i].image.offsetHeight/2);
				var posX=(xM-xiM);
				var posY=(yM-yiM);
				if (OBJQueNavegador()=='GC')
					{
					posX+=4;
					posY+=4;
					};
					
				images[i].image.style.top=posY + 'px';
				images[i].image.style.left=posX + 'px';
				}
			else
				{
				images[i].image.style.top=top + 'px';
				images[i].image.style.left=left + 'px';
				};
			};
	};

	
	/**********************************************************
					 CLASES INTERNAS
	**********************************************************/	
	//
	//Clase para contener los datos de las imagenes
	//y la imagen en si
	//
	function clImages(path,trMls,waitMls,pie)
	{
		this.image=document.createElement('IMG'); //Nos creamos un objeto image
		
		//Cargamos la imagen				
		this.image.src=path;
		
		//Configuramos la imagen para que no se vea y posicionarla
		this.image.style.visibility='hidden';
		this.image.style.position='absolute';
		this.image.style.top=top + 'px';
		this.image.style.left=left + 'px';

		//Y se la metemos al body		
		document.body.appendChild(this.image)
		
		//E inicializamos los tiempos
		this.transitionMls=trMls;		//Los milisegundos de transición con la siguiente
		this.waitShowMls=waitMls;		//Los milisegundos que se muestra
		this.piedefoto=pie;				//El pie de foto
	};
};



/****************************************************************
 
 
 
 
 
	 Clase para la gestión de un img como un boton
	 con n tipos y estados normal / roll y disabled




 
****************************************************************/
function IMGButton(objRef)
{
	/***********************************************************
				 Inicialización de la clase
	***********************************************************/
var obj;

	//Verifico si es un objeto, y si no presupongo que es una id
	if (!objRef.tagName)
		obj=document.getElementById(objRef);
	else
		obj=objRef;

	//Verificamos si el objeto existe
	if (!obj.tagName || obj.tagName!='IMG')
		{
		alert('Clase mal referenciada. Se le debe pasar o una id de un IMG o un objeto IMG');
		return;
		};
		

//Nos referenciamos a nosotros mismos
var clase=this;

/*****************************
		 Metodos
*****************************/
this.AddButton=mtAddButton;	//(id, imgNormal, [imgRoll], [imgDisabled])
this.SetButton=mtSetButton; //(id,[enabled/disabled])
this.SetButtonDisabled=mtSetButtonDisabled; //([id])
this.SetButtonEnabled=mtSetButtonEnabled; //([id])
this.GetStatus=mtGetStatus;	//([id]) Devuelve el estado (0-Normal, 1-Roll, 2-Disabled)
this.GetButton=mtGetButton; //() Devuelve el Id del button activo

/*****************************
		Eventos
*****************************/
this.EVTMouseOver=null;		//devuelve (evt,id)
this.EVTMouseOut=null;		//devuelve (evt,id)
this.EVTClick=null;			//devuelve (evt,id)
this.EVTDblClick=null;		//devuelve (evt,id)

/****************************
	 Variables globales
****************************/
var buttons=new Array();		//Matriz de objetos clButtons
var idxButton=null;				//indice de la matriz de botones del que esta activo en este momento


	/***************************************
				 Constructor
	***************************************/

	//Asignamos los eventos
	EVTAsignaEvento(obj, 'click', evtClick);
	EVTAsignaEvento(obj, 'dblclick', evtDblClick);
	EVTAsignaEvento(obj, 'mouseover', evtMouseOver);
	EVTAsignaEvento(obj, 'mouseout', evtMouseOut);



	/*********************************************
	 Clase clButtons
	 Contiene toda la información de cada boton
	*********************************************/
	function clButtons()
	{
		var claseB=this;
		
		//Propiedades
		this.images=new Array;		//Arra para las images precargadas
		this.images[0]=null;		//Imagen estado normal
		this.images[1]=null;		//Imagen rollover
		this.images[2]=null;		//Imagen disabled
		this.status=0;				//Estado del botón 0-Normal, 1-Roll, 2-Disabled.
		this.allImageLoad=false;	//Si todas las imagenes estan cargadas
		this.title=null;			//El title para el boton
		this.id=null;				//La id del boton
		
		//Metodos
		this.AddImages=mtAddImages;	//([normal], [rollover], [disabled])
		
		//Variables globales
		var intervalLoad=null;
		
		
		//Añade / cambia los gráficos del boton.
		//Todos los parametros son opcionales
		function mtAddImages(Normal,RollOver,Disabled)
		{
			var flag=false;
			
			if (Normal)
				{
				if (this.images[0]==null) this.images[0]=new Image
				this.images[0].src=Normal;
				flag=true;
				};
			
			if (RollOver)
				{
				if (this.images[1]==null) this.images[1]=new Image
				this.images[1].src=RollOver;
				flag=true;
				};
				
			if (Disabled)
				{
				if (this.images[2]==null) this.images[2]=new Image
				this.images[2].src=Disabled;
				flag=true;
				};
				
			if (flag)
				{ 
				//Disparamos el control de carga, si no es que esta activo
				this.allImageLoad=false;
				if (intervalLoad==null)	intervalLoad=setInterval(ControlCarga,100);
				};
		};
		
		//Funcion para el intervalo que se encarga de controlar la carga de las imagenes
		function ControlCarga()
		{
		var i;
		
			for (i=0;i<claseB.images.length;i++)
				if (claseB.images[i] && !claseB.images[i].complete) return;
	
			//Ya estan todas
			claseB.allImageLoad=true;
			clearInterval(intervalLoad);
		};
	};
	
	
	/*****************************
			 
			 
			 Metodos
			 
			 
	*****************************/
	
	//Crea un boton nuevo. El unico parametro obligatorio es la id
	function mtAddButton(id, title, imgNormal, imgRoll, imgDisabled)
	{
		var idx=buttons.length;
		buttons[idx]=new clButtons();
		buttons[idx].id=id;
		if (title) buttons[idx].title=title;
		buttons[idx].AddImages(imgNormal, imgRoll, imgDisabled);
	};
	
	//Conecta en funcionamiento el boton con id, IsEnabled es opcional
	function mtSetButton(id,IsEnabled)
	{
		var idx=GetIndex(id);
		if (idx==-1) 
			{
			alert('La id "' + id + '" no existe.');
			return;
			};
			
		//Ponemos el indice
		idxButton=idx;
		//Conectamos el estado normal
		obj.src=buttons[idxButton].images[0].src;
		obj.title=buttons[idxButton].title;
		buttons[idxButton].status=0;
		//Y si nos viene IsEnabled a false lo intentamos
		if (IsEnabled==false) mtSetButtonDisabled(id);
	};
	
	//Pone un boton en disabled. 
	//Si no se le pasa id pone el actual
	function mtSetButtonDisabled(id)
	{
	var idx;
	
		if (id)
			{
			idx=GetIndex(id);
			if (idx==-1) 
				{
				alert('La id "' + id + '" no existe.');
				return null;
				};
			}
		else
			{
			idx=idxButton;
			};
			
		if (idx==null) return null;
				
		if (buttons[idx].status!=2 && buttons[idx].images[2]!=null)
			{
			//Ponemos el disabled
			obj.style.cursor='default';
			obj.src=buttons[idx].images[2].src;
			buttons[idx].status=2;
			};		
	};
	
	//Pone un boton en enabled
	//Si no se le pasa id pone el actual
	function mtSetButtonEnabled(id)
	{
	var idx;
	
		if (id)
			{
			idx=GetIndex(id);
			if (idx==-1) 
				{
				alert('La id "' + id + '" no existe.');
				return null;
				};
			}
		else
			{
			idx=idxButton;
			};
			
		if (idx==null) return null;
		
		if (buttons[idx].status==2 && buttons[idx].images[0]!=null)
			{
			//Ponemos el enableded
			obj.style.cursor='default';
			obj.src=buttons[idx].images[0].src;
			buttons[idxButton].status=0;
			};		
	};
	
	//Devuelve el estado (0-Normal, 1-Roll, 2-Disabled)
	//Si no se le pasa id devuelve del actual, si no hay actual null
	//Si se le pasa id y no exuste devuelve null
	function mtGetStatus(id)
	{
		if (id)
			{
			var idx=GetIndex(id);
			if (idx==-1) 
				{
				alert('La id "' + id + '" no existe.');
				return null;
				};
			return buttons[idx].status;
			}
		else
			{
			if (idxButton!=null) 
				return buttons[idxButton].status;
			else
				return null;
			};
	};
	
	//Devuelve el Id del button activo	
	function mtGetButton()
	{
		if (idxButton!=null) return buttons[idxButton].id;
	};


	/*****************************
	
	
			Eventos 


	*****************************/
	
	//Funcion que recibe el mouse over del objeto image
	function evtMouseOver(evt)
	{
		if (idxButton==null) return;
		if (buttons[idxButton].status==0 && buttons[idxButton].images[1]!=null)
			{
			//Ponemos el rollover
			obj.style.cursor='pointer';
			obj.src=buttons[idxButton].images[1].src;
			buttons[idxButton].status=1;
			
			//Y si tenemos evento, lo disparamos
			//devuelve (evt,id)
			if (clase.EVTMouseOver!=null) eval(clase.EVTMouseOver + '(evt,buttons[idxButton].id);');
			};
	};
	
	//Funcion que recibe el mouse out del objeto image
	function evtMouseOut(evt)
	{
		if (idxButton==null) return;
		
		if (buttons[idxButton].status==1 && buttons[idxButton].images[0]!=null)
			{
			//Ponemos el normal
			obj.style.cursor='default';
			obj.src=buttons[idxButton].images[0].src;
			buttons[idxButton].status=0;
			
			//Y si tenemos evento, lo disparamos
			//devuelve (evt,id)
			if (clase.EVTMouseOut!=null) eval(clase.EVTMouseOut + '(evt,buttons[idxButton].id);');
			};
	};
	
	//Funcion que recibe el click del objeto image
	function evtClick(evt)
	{
		if (idxButton==null) return;
		
		if (buttons[idxButton].status!=2)
			{
			//Si tenemos evento, lo disparamos
			//devuelve (evt,id)
			if (clase.EVTClick!=null) eval(clase.EVTClick + '(evt,buttons[idxButton].id);');
			};
	};
	
	
	//Funcion que recibe el doble click del objeto image
	function evtDblClick(evt)
	{
		if (idxButton==null) return;
		
		if (buttons[idxButton].status!=2)
			{
			//Si tenemos evento, lo disparamos
			//devuelve (evt,id)
			if (clase.EVTDblClick!=null) eval(clase.EVTDblClick + '(evt,buttons[idxButton].id);');
			};
	};
	
	
	/*********************************
	
		 Funciones internas

	*********************************/
	
	//Devuelve el indice de la matriz buttons de una id
	function GetIndex(id)
	{
		for (i=0; i<buttons.length; i++)
			{
			if (buttons[i].id==id) return i;
			};
	
		return -1;
	};
};

