/*
	JJBM,	21/04/2008, elSlideShow.js

*/

function elSlideShow_GetAbsY( o ) {
	var y = o.offsetTop;
	while( ( o = o.offsetParent ) ) // && o.style.position == '' )
		y += o.offsetTop - o.scrollTop;
	return y;
}

function elSlideShow_GetAbsX( o ) {
	var x = o.offsetLeft;
	while( ( o = o.offsetParent ) ) //&& o.style.position == '' )
		x += o.offsetLeft - o.scrollLeft;
	return x;
}

function elSlideShow_fijar_objeto_final(e) {
	var s = "", n = parseInt( Math.random() * 10000 )

	var obj = e.BuscarObjeto( e.foto.activa )

	if( obj == null )
	    return

	switch( obj.tipo ) {
	case "Foto":
	    s += "<img src=\"" + obj.objeto + "\" width=\"" + e.foto_w + "\" height=\"" + e.foto_h + "\">";
	    break;
	case "YouTube":
		s += "<object width=\"" + e.foto_w + "\" height=\"" + e.foto_h + "\">"
		s += "	<param name=\"movie\" value=\"http://www.youtube.com" + obj.objeto + "&autoplay=1\"></param>"
		s += "	<param name=\"wmode\" value=\"transparent\"></param>"
		s += "	<embed src=\"http://www.youtube.com" + obj.objeto + "&autoplay=1\" type=\"application/x-shockwave-flash\""
		s += "		wmode=\"transparent\" width=\"" + e.foto_w + "\" height=\"" + e.foto_h + "\"></embed>"
		s += "</object>"
		break;
	case "QuickTime":
		s += "<OBJECT data=\"" + obj.objeto + "\" type=\"video/quicktime\" width=\"" + e.foto_w + "\" height=\"" + e.foto_h + "\"></OBJECT>";
		break;
	default:
		s += "<font color=red><b>Tipo de objeto desconocido.</b></font>"
		break;
	}

	e.foto.innerHTML = s
}

function elSlideShow_fade_OnTerminar() {
	var e = this.elss
	var img = this.elss_img

	e.fade = null

	e.foto.innerHTML = "<img src='" + img.src + "' width=" + e.foto_w + " height=" + e.foto_h + ">";
	e.foto.activa = img.src

    window.setTimeout( "elSlideShow_fijar_objeto_final(" + e.THIS + ")", 100 );

	img.style.display = 'none'

	this.Ocultar()

	e.fade = null
}

function elSlideShow_img_OnLoad(evt,img,e) {
	img.w = img.width
	img.h = img.height
	if( e.horiz ) {
		img.width = ( e.slide_w / e.nisw - 0.5 )
		img.height = e.slide_h
	} else {
		img.height = ( e.slide_h / e.nisw - 0.5 )
		img.width = e.slide_w
	}
}

function elSlideShow_img_OnClick(evt,img,e) {
	// si estamos en transición abortamos
	if( e.fade != null )
		return // e.fade.Ocultar()

	var obj = e.BuscarObjeto( img.src )

	e.Img.src = img.src
	if( obj != null ) {
	    e.Titulo.innerHTML = obj.titulo
	}

	// si había un objeto previo, lo quitamos y ponemos la foto (por tema de overlap)
	e.foto.innerHTML = "<img src='" + e.foto.activa + "' width=" + e.foto_w + " height=" + e.foto_h + ">";

	// creamos un fade

	// con resize
if( false ) {
	e.fade = new fadeBanner( e.THIS + ".fade", e.Img,
		elSlideShow_GetAbsX( img ) - ( window.Event ? e.slide.scrollLeft : 0 ), elSlideShow_GetAbsX( e.foto ),
		elSlideShow_GetAbsY( img ) - ( window.Event ? e.slide.scrollTop  : 0 ), elSlideShow_GetAbsY( e.foto ),

		img.offsetWidth, e.foto_w,
		img.offsetHeight, e.foto_h,

		0, 100, 50, 1000, elSlideShow_fade_OnTerminar )
} else {
	// sin resize
	e.fade = new fadeBanner( e.THIS + ".fade", e.Img,
		elSlideShow_GetAbsX( e.foto ), elSlideShow_GetAbsX( e.foto ),
		elSlideShow_GetAbsY( e.foto ), elSlideShow_GetAbsY( e.foto ),

		e.foto_w, e.foto_w,
		e.foto_h, e.foto_h,

		0, 100, 50, 1000, elSlideShow_fade_OnTerminar )
}


	e.fade.elss = e
	e.fade.elss_img = e.Img
	e.fade.Iniciar()

}

function elSlideShow_img_OnMouseOut(evt,img,e) {
	img.style.filter = "FILTER: Alpha(Opacity=80)"
	img.style.opacity = "0.80"

}

function elSlideShow_img_OnMouseMove(evt,img,e) {
	if( window.event )
		evt = window.event

	var ax = evt.clientX
	var ay = evt.clientY
	var cx = elSlideShow_GetAbsX( e.slide ) + e.slide_w / 2
	var cy = elSlideShow_GetAbsY( e.slide ) + e.slide_h / 2

	e.slide_move_x = Math.pow( ( ax - cx )  / ( 0.2 * e.slide_w ), 3 )
	e.slide_move_y = Math.pow( ( ay - cy ) / ( 0.2 * e.slide_h ), 3 )

	img.style.filter = ""
	img.style.opacity = "1.0"
}

        /****/
        function elSlideShow_Objeto( src, objeto, tipo, titulo ) {
            this.src = src
            this.objeto = objeto
            this.tipo = tipo
            this.titulo = titulo
        }
        /****/

	function elSlideShow_BuscarObjeto( src ) {
	    var i
	    for( i = 0; i < this.Objeto.length; i++ )
	        if( src == this.Objeto[i].src || unescape( src ).endsWith( this.Objeto[i].src ) )
	            return this.Objeto[i]
	    return null
	}

	function elSlideShow_Add( src, titulo, tipo, objeto ) {

	    // Insertamos información del objeto:
	    this.Objeto[this.Objeto.length] = new elSlideShow_Objeto( src, objeto, tipo, titulo )

		if( this.foto.innerHTML == '' ) {
			this.foto.innerHTML = "<img src='" + src + "' width=" + this.foto_w + " height=" + this.foto_h + ">";
			this.foto.activa = src
			this.Titulo.innerHTML = titulo
		}
		this.slide.innerHTML += "<img src='" + src +"'" +
			" alt='" + titulo + "'" +
			" style='FILTER: Alpha(Opacity=80); opacity: 0.80'" +
			" onload='elSlideShow_img_OnLoad(event,this," + this.THIS + ")'" +
			" onclick='elSlideShow_img_OnClick(event,this," + this.THIS + ")'" +
			" onmouseout='elSlideShow_img_OnMouseOut(event,this," + this.THIS + ")'" +
			" onmousemove='elSlideShow_img_OnMouseMove(event,this," + this.THIS + ")'>";
		if( !this.horiz )
			this.slide.innerHTML += "<br/>";
	}

	function elSlideShow_Start() {
		this.Img = document.getElementById( this.THIS + "_Img" );

		this.ImgA.style.filter = 'FILTER: Alpha(Opacity=65)'
		this.ImgA.style.opacity = "0.65"
		this.ImgA.style.position = "absolute"
		this.ImgA.style.zIndex = 5

		this.ImgB.style.filter = 'FILTER: Alpha(Opacity=70)'
		this.ImgB.style.opacity = "0.70"
		this.ImgB.style.position = "absolute"
		this.ImgB.style.zIndex = 5

		if( this.horiz ) {
			this.ImgA.src = "/img/ewt/elSlideShow/flecha-izquierda.gif";
			this.ImgB.src = "/img/ewt/elSlideShow/flecha-derecha.gif";
			this.ImgA.style.width = parseInt( this.slide_h / 3 ) + "px"
			this.ImgB.style.width = parseInt( this.slide_h / 3 ) + "px"
			this.ImgA.style.height = this.slide_h + "px"
			this.ImgB.style.height = this.slide_h + "px"
			this.ImgA.style.left = elSlideShow_GetAbsX( this.slide ) + "px"
			this.ImgA.style.top = elSlideShow_GetAbsY( this.slide ) + "px"
			this.ImgB.style.left = ( elSlideShow_GetAbsX( this.slide ) + this.slide_w - parseInt( this.slide_h / 3 ) ) + "px"
			this.ImgB.style.top = elSlideShow_GetAbsY( this.slide ) + "px"
			this.ImgA.style.zIndex = 1
			this.ImgB.style.zIndex = 1
		} else {
			this.ImgA.src = "/img/ewt/elSlideShow/flecha-arriba.gif";
			this.ImgB.src = "/img/ewt/elSlideShow/flecha-abajo.gif";
			this.ImgA.style.height = parseInt( this.slide_w / 3 ) + "px"
			this.ImgB.style.height = parseInt( this.slide_w / 3 ) + "px"
			this.ImgA.style.width = this.slide_w + "px"
			this.ImgB.style.width = this.slide_w + "px"
			this.ImgA.style.left = elSlideShow_GetAbsX( this.slide ) + "px"
			this.ImgA.style.top = elSlideShow_GetAbsY( this.slide ) + "px"
			this.ImgB.style.left = elSlideShow_GetAbsX( this.slide ) + "px"
			this.ImgB.style.top = ( elSlideShow_GetAbsY( this.slide ) + this.slide_h - parseInt( this.slide_w / 3 ) ) + "px"
			this.ImgA.style.zIndex = 1
			this.ImgB.style.zIndex = 1
		}

		this.Titulo.style.filter = 'FILTER: Alpha(Opacity=65)'
		this.Titulo.style.opacity = "0.65"
		this.Titulo.style.position = "absolute"
		this.Titulo.style.backgroundColor = "white"
		this.Titulo.style.textAlign = "center"
		this.Titulo.style.zIndex = 5
		this.Titulo.style.left = ( elSlideShow_GetAbsX( this.foto ) + this.foto_w / 4 ) + "px"
		this.Titulo.style.width = ( this.foto_w / 2 ) + "px"
		if( this.titulo == 1 ) {
			this.Titulo.style.top = elSlideShow_GetAbsY( this.foto ) + "px"
		} else {
			this.Titulo.style.top = ( elSlideShow_GetAbsY( this.foto ) + this.foto_h - 25 ) + "px"
			this.Titulo.style.height = "25px"
		}

	}
	function elSlideShow_SlideMove() {
		this.slide_x += this.slide_move_x
		this.slide_y += this.slide_move_y

		var prev_sl = this.slide.scrollLeft
		var prev_st = this.slide.scrollTop

		this.slide.scrollLeft = parseInt( this.slide_x )
		this.slide.scrollTop  = parseInt( this.slide_y )

		// ¿sin cambios?
		if( prev_sl == this.slide.scrollLeft ) this.slide_x = prev_sl
		if( prev_st == this.slide.scrollTop ) this.slide_y = prev_st
	}

function elSlideShow( THIS, obj_foto, obj_slide, num_img_slide_width, horizontal, titulo ) {
	this.THIS = THIS
	this.foto = obj_foto
	this.slide = obj_slide
	this.img = new Array()
	this.nisw = num_img_slide_width
	this.horiz = horizontal
	this.titulo = titulo

	this.Objeto = new Array()

	this.fade = null

	this.foto.activa = ""

	this.SlideMove = elSlideShow_SlideMove
	this.Add = elSlideShow_Add
	this.Start = elSlideShow_Start
	this.BuscarObjeto = elSlideShow_BuscarObjeto

	this.slide_x = 0.0
	this.slide_y = 0.0
	this.slide_move_x = 0
	this.slide_move_y = 0
	setInterval( this.THIS + ".SlideMove()", 20 )

	this.slide.innerHTML = "<img id=\"" + this.THIS + "_Img\" src=\"../img/ewt/clearpixel.gif\" style=\"position: absolute; width: 1; height: 1\" style=\"display: none\">"

	this.ImgA = document.createElement("IMG")
	this.ImgB = document.createElement("IMG")
	this.Titulo = document.createElement("DIV")

	document.body.appendChild( this.ImgA )
	document.body.appendChild( this.ImgB )

	if( titulo > 0 )
		document.body.appendChild( this.Titulo )

	this.foto_w = this.foto.offsetWidth
	this.foto_h = this.foto.offsetHeight

	this.slide_w = this.slide.offsetWidth
	this.slide_h = this.slide.offsetHeight
}

