
/********************************************************************/
//** 
//* Esta version 2.0 utiliza el applet fex.class (File EXist) que hay que añadir
//* en las paginas donde se utilice
//* El applet devuelve 'error' si no encuentra el fichero 
//* Si lo encuentra devuelve el tipo de fichero 



// Añadimos todas las utilidades para el Cross Browsing
document.write("\<script src=\"\/js/CBMutilities.js\"\> \<\/script\>");




function ImageSelector()
{
	
	

	this.img 	= new Array();
	this.base	= IS_getServer();
	this.srcOnLoad	= '';
	this.maxwidth	= 320;
	this.maxheight	= 240;
	this.srcK	= 1;
	this.imgactive	= -1;
	


	this.CheckImages	= __CheckImages;
	this.preloadImage	= __preloadImage;
	this.displayImage	= __displayImage;
	this.displayImageOnDiv	= __displayImageOnDiv;
	this.resizeImg		= __resizeImg;
	this.OnLoadWindow	= __onLoadWindow;
	this.overrideOnLoad	= __overrideOnLoad;
	this.fex		= __fex;
	this.args		= ImageSelector.arguments;
	
	this.overrideOnLoad();
}



function __fex( img )
{
	
	var ret = String( window.document.applets["fex"].fetchWebPage( img ));

	return  ( (String(ret)!='-1' && ret!='' ) ? true : false )

		

}
	
	


function __CheckImages(  )
{


	var args = this.args;


	
	for (var i=0; i<args.length; i++)
	{

		this.img[i]		= new Image();
		this.img[i].src		= args[i].indexOf('http')==-1 ? this.base + args[i] : args[i];	
		this.img[i].exist	= -1;

		this.img[i].exist = this.fex ( this.img[i].src ) ;
//alert(this.img[i].exist +'->'+this.img[i].src);
		// IE a veces no carga bien el tamaño de la imagen y devuelve 0,0
		// Para estos casos se le da a la imagen un tamaño fijo de 120,120
		//if (this.img[i].exist==true &&  (this.img[i].width==0 || this.img[i].height==0) )
		//	{ this.img[i].width=120; this.img[i].height=120 }
		
		if ( this.img[i].exist == true )
		{
			this.imgactive = i;
			this.preloadImage( this.imgactive );	
			this.displayImage();
			break;
		}
			
	}


return ;
}





function __preloadImage( ix )
{

	var args = this.args;
	
	var msghtml = '';

	this.img[ix].onload	= this.displayImage;

	msghtml += '<img src="' + this.img[ix].src + '"  >';

	var objDiv = CBM_getDocumentElement(this.divname);
	

 	switch  (CBM_typeofNavigator())
 	{
		
		case 'NN4':

			objDiv.document.open();
			objDiv.document.write ( msghtml );
			objDiv.document.close();
		break;
		
		
		default:
			objDiv.innerHTML 	=  msghtml  ;	
	}




}





function __resizeImg( ix )
{
	

	var cw = this.img[ix].width/this.maxwidth;
	var ch = this.img[ix].height/this.maxheight;
	if ( cw>1 || ch>1 )
		this.srcK = 1/Math.max(cw,ch) ;
		
	return this.img[ix];
			
}




/********************************************************************/
//** 
//* Esta funcion parsea la funcion onload de la ventana actual, si existe
//* y le añade la funcion onload propia del objeto ImageSelector para 
//* hacer un display automático sin necesidad de añadir en la funcion onload
//* ninguna llamada a la funcion displayImage.

//* En definitiva equivale a añadir a mano en el onload del body la llamada
//* objeto.displayImage();
//* ...
/********************************************************************/

function __overrideOnLoad()
{
	
	if ( typeof window.onload != 'undefined' && window.onload != null)  	// condicion para NS && condicion para IE
	{
		var str = String ( window.onload );
		this.srcOnLoad = str.slice ( str.indexOf('{') + 1, str.lastIndexOf('}') );
	}

	
	//*** 
	//* Se asigan al objeto ISobj el objeto actual ImageSelector para que la ventana
	//* actual guarde una referencia al mismo. Esto se utiliza para llamar a las funciones del propio
	//* objeto desde la ventana, como se hace en la funcion OnLoad.
	window.ISobj  = this;
	window.onload = this.OnLoadWindow;
	
}



function __displayImageOnDiv ( divname, maxwidth, maxheight )
{
	this.divname = divname;


	var objDiv = CBM_getDocumentElement(this.divname);
	

 	switch  (CBM_typeofNavigator())
 	{
		
		case 'NN4':
			if ( typeof objDiv == 'undefined' )
				this.divname = null;

		break;
		
		
		default:
			if ( typeof objDiv == 'undefined' )
				this.divname = null;
	}





	if ( this.divname == null )
		alert ( 'ImageSelector ERROR: La capa "' + divname +'" no existe o no tiene ningún estilo asignado.');


	this.maxwidth 	= typeof maxwidth!='undefined' ? maxwidth	: this.maxwidth;
	this.maxheight 	= typeof maxheight!='undefined' ? maxheight	: this.maxheight;
	
}	
	


function __displayImage ( ix )
{


	if ( window.ISobj.divname == null ) 
		return false;
	
	window.ISobj.resizeImg( window.ISobj.imgactive );



	
	if ( window.ISobj.imgactive != -1 )
	{
		var img = window.ISobj.img[ window.ISobj.imgactive ]; 
		var msghtml = '<table width=' + window.ISobj.maxwidth + ' height=' + window.ISobj.maxheight + ' border=0><tr><td align=center valign=middle><img src="' + img.src + '" width=' + Math.floor( window.ISobj.srcK*img.width ) + '   border="1"></td></tr></table>';
	}
	else
		var msghtml = '<table width=' + window.ISobj.maxwidth + ' height=' + window.ISobj.maxheight + ' border=1><tr><td align=center valign=middle>¡Imagen NO DISPONIBLE!</td></tr></table>';

//msghtml += '<table width=400><tr><td class=texto>' 
//for (var i=0; i<window.ISobj.img.length;i++) {msghtml += window.ISobj.img[i].exist + '(' + window.ISobj.img[i].width + ',' + window.ISobj.img[i].height +  ') // ' }
//msghtml += '</td></tr></table>'

	var objDiv = CBM_getDocumentElement(window.ISobj.divname);

 	switch  (CBM_typeofNavigator())
 	{
		
		case 'NN4':
			objDiv.document.open();
			objDiv.document.write ( msghtml );
			objDiv.document.close();
			objDiv.visibility="show";

		break;
		
		
		default:

			objDiv.innerHTML 	=  msghtml  ;	
			objDiv.style.visibility	=	"visible";


	}





return true;
}




 


/********************************************************************/
//** 
//* Esta funcion es especial porque al asignarse como override de la funcion
//* onload de la ventana, se convierte en hijo suyo y por tanto el puntero this
//* apunta al valor de window. De ahí que para referrir el objeto ImageSelector
//* se guardara como referencia en window.ISobj.
//* La asignacion se realiza en la funcion    __overrideOnLoad()
//*
//* @param <name> -> <valor>
//* @return <retorno>
/********************************************************************/

function __onLoadWindow ( event )
{
	

	eval ( window.ISobj.srcOnLoad );
	//window.ISobj.displayImage ( window.ISobj.divname );
	window.ISobj.CheckImages ();
	return true;
	
}



//-----------------------------------
// Obtiene el nombre y puerto del servidor actual
function IS_getServer() {




	ret = document.location.protocol + '//' + document.location.host;
	



	return ret;
	
}



