/**
*  BRF 2006-2007 para R Cable y Telecomunicaciones
*
*-------------------------------------------------
* funciones basadas en LighBox.JS (www.huddletogether.com/projects/lightbox/)
* y sobre todo subModal (http://www.subimage.com/dhtml/subModal/), de donde se han
* tomado algunas funciones auxiliares
*-------------------------------------------------
* lanza una capa centrada sobre todo el web oscureciendo lo que hay detr�s (semitransparente)
* y encima un Flash, una imagen, un iFrame, un VideoLAN/PCTV, etc.
*------------------------------------------------
* interfaz:
*	function abrirPopUpMultimedia(ancho, alto, tipo, url)
*	function cerrarPopUpMultimediaUrl(url)
*	function cerrarPopUpMultimedia()
*	var gPUMVersion
*	var gPUMPrefijoIMX
*------------------------------------------------
*  v1.1
* 8/2/2007
*		opcion tamano de popup liquido especificando ancho o alto 0
*		centrado ANTES de mostrar
*		anadida variable de prefijo para carpeta imx de popupmultimedia
**/

var gPUMVersion = "1.1"; // versi�n de popUpMultimedia
var PUMmargenPopContenido = 20; // margen alrededor del multimedia
var PUMmargenPopBordeVentana = 40; // margen que se deja al borde de ventana en caso de tamano liquido

/**
* localizaci�n de las im�genes de popUpMultimedia (un PNG gris, un t.gif y un bot�n de cerrar)
* esta variable puede cambiarse (siempre antes de llamar a  abrirPopUpMultimedia() para indicar d�nde se
* encuentran las im�genes que necesita popUpMultimedia (p.e. /imx/ si all� se han copiado t.gif, cerrar.gif y )
*/
var gPUMPrefijoIMX = "img/popUpMultimedia/";

/**
*	la funci�n principal, abre un popup multimedia
*
*	ancho = ancho del cotenido a mostrar en p�xeles; si se pone 0, el ancho ser� l�quido (el ancho de la ventana menos un margen)
*	alto = alto del cotenido a mostrar en p�xeles; si se pone 0, el alto ser� l�quido (el alto de la ventana menos un margen)
*	tipo =  puede ser:
*			'flash' y el par�metro URL es el SWF m�s los par�metros de SWF
*			'iframe' y el par�metro URL es el contenido del iframe
*			'imagen' y el par�metro URL es la URL de la imagen
*			'videolan' y el par�metro URL es el v�deo de videolan a mostrar
*	url = el contenido a mostrar en el popupmultimedia seg�n su tipo
*/

function abrirPopUpMultimedia(ancho, alto, tipo, url) {

        gPUMHideSelects = false;

    aPUMTamanoEspecificado = new Array(ancho, alto);
    bPUMAnchoLiquido=(ancho == 0);
    if (bPUMAnchoLiquido) {
        // cogemos tama�o de ventana visible
                var fullWidth = PUMgetViewportWidth();
                // el ancho especificado ser� el ancho de pantalla menos los bordes, menos el margen alrededor del multimedia
        ancho = fullWidth - (PUMmargenPopBordeVentana*2) - (PUMmargenPopContenido*2)
    }

    bPUMAltoLiquido=(alto == 0);
    if (bPUMAltoLiquido) {
        // cogemos tama�o de ventana visible
                var fullHeight = PUMgetViewportHeight();
                // el ancho especificado ser� el ancho de pantalla menos los bordes, menos el margen alrededor del multimedia
        alto = fullHeight - (PUMmargenPopBordeVentana*2) - (PUMmargenPopContenido*2)
    }

        var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
        if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
                gPUMHideSelects = true;
        }

        if(gPUMHideSelects){
                PUMHideSelectBoxes();
        }

        if (!bPUMPopupAbierto)
        {
                        var body = document.getElementsByTagName('body')[0];

                        if (PUMPopMask==null)
                        {
                                // creamos el DIV con el fondo
                                var lPopMask = null;
                                lPopMask = document.createElement('div');
                                lPopMask.style.display = 'none';
                                lPopMask.id = 'mascaraPopUp';
                                lPopMask.style.position = 'absolute';
                                lPopMask.style.zIndex = '200';
                                lPopMask.style.top = '0px';
                                lPopMask.style.left = '0px';
                                lPopMask.style.width = '100%';
                                lPopMask.style.height = '100%';
                                lPopMask.style.opacity = '.6';
                                lPopMask.style.filter = 'alpha(opacity=60)';
                                lPopMask.style.backgroundColor = '#666666';
                                lPopMask.style.backgroundImage = 'none';
                                lPopMask.style.backgroundRepeat =  'repeat';
                                body.appendChild(lPopMask);

                                PUMPopMask = document.getElementById('mascaraPopUp');
                        }

                        if (PUMPopContenido==null)
                        {
                                // preparamos el contenido del <div>
                                var sContenido;

                                if (tipo == 'flash')
                                {
                                        // es un flash, con el ancho y alto especificado y como movie el par�metro URL
                                        sContenido = '<object width="' + (bPUMAnchoLiquido ? '100%' : ancho) + '" height="' + (bPUMAltoLiquido ? '100%' : alto) + '"><param name="wmode" value="transparent"><param name="movie" value="' + url + '"></param><embed src="' + url + '" type="application/x-shockwave-flash" width="' + (bPUMAnchoLiquido ? '100%' : ancho) + '" height="' + (bPUMAltoLiquido ? '100%' : alto) + '"></embed></object>';
                                }
                                else if (tipo == 'iframe')
                                {
                                        // es un iframe, con el ancho y alto especificado y que carga como contenido lo indicado en el par�metro URL
                                        sContenido = '<iframe border="0" width="' + (bPUMAnchoLiquido ? '100%' : ancho) + '" height="' + (bPUMAltoLiquido ? '95%' : alto)  + '" src="' + url + '"><img align="center" alt="" src="' + gPUMPrefijoIMX + 'espera.gif"></iframe>';
                                }
                                else if (tipo == 'imagen')
                                {
                                        sContenido = '<img id="iPopImagenMultimedia" align="center" alt="" src="' + gPUMPrefijoIMX + 'espera.gif">';
                                        // y seguimos m�s abajo...
                                }
                                else if (tipo == 'videolan')
                                {
                                        sContenido = '<OBJECT classid="clsid:E23FE9C6-778E-49D4-B537-38FCDE4887D8" codebase="http://downloads.videolan.org/pub/videolan/vlc/latest/win32/axvlc.cab" ' +
                                                ' width="' + (bPUMAnchoLiquido ? '100%' : ancho) + '" height="' + (bPUMAltoLiquido ? '100%' : alto) + '" id="PCTVplayer" events="True">' +
                                                '<param name="Src" value="' + url + '" />'  +
                                                '<param name="ShowDisplay" value="True" /><param name="AutoLoop" value="True" /><param name="AutoPlay" value="True" />' +
                                                '<embed name="PCTVplayer" autoplay="yes" loop="yes" target="' + url + '" type="application/x-vlc-plugin" width="' + ancho + '" height="' + alto + '"></embed></OBJECT>';
alert(sContenido);
                                }
                                else
                                        // esto debe ser un error
                                        sContenido = '<img id="iPopImagenMultimedia" align="center" alt="" src="' + gPUMPrefijoIMX + 'espera.gif">';

                                // creamos el DIV con el contenido
                                var lPopContenido = null;
                                lPopContenido = document.createElement('div');
                                lPopContenido.id = 'contenidoPopUp';
                                lPopContenido.style.position = 'absolute';
                                lPopContenido.style.zIndex = '201';

                                lPopContenido.style.top = '0px';
                                lPopContenido.style.left = '0px';
                                lPopContenido.style.width = (ancho + PUMmargenPopContenido*2) + 'px';
                                lPopContenido.style.height = (alto + PUMmargenPopContenido*2) + 'px';
                                lPopContenido.style.backgroundColor = '#FFFFFF';
                                lPopContenido.style.display = 'none';
                                body.appendChild(lPopContenido);

                                PUMPopContenido = document.getElementById('contenidoPopUp');
                                PUMPopContenido.innerHTML =
                                                '<table cellpadding="0" cellspacing="0" height="100%" width="100%" border="0">' +
                                                '<tr><td><img src="' + gPUMPrefijoIMX + 't.gif" width="' + PUMmargenPopContenido + 'px" height="' + PUMmargenPopContenido + 'px"></td><td align="right"></td><td align="center"><a href="javascript:cerrarPopUpMultimedia();"><img border="0" src="' + gPUMPrefijoIMX + 'close.gif"></a></td></tr>' +
                                                '<tr><td></td><td valign="middle" align="center" height="95%" width="100%">' + sContenido + '</td><td></td></tr>' +
                                                '<tr><td colspan="2"></td><td><img src="' + gPUMPrefijoIMX + 't.gif" width="' + PUMmargenPopContenido + 'px" height="' + PUMmargenPopContenido + 'px"></td></tr>' +
                                                '</table>';

                                PUMaddEvent(PUMPopMask, "click", cerrarPopUpMultimedia);
                        }

                        bPUMPopupAbierto = true;
                        PUMcentrar(ancho + PUMmargenPopContenido*2, alto + PUMmargenPopContenido*2);
                        PUMPopMask.style.display = 'block';
                        PUMPopContenido.style.display = 'block';

                        window.onscroll = PUMcentrar;
                        PUMaddEvent(window, "resize", PUMcentrar);

                        // terminamos en caso de que sea imagen, cargando en background la que sustituir� a la de "wait"
                        // lo hacemos al final no vaya a ser que no existiera a�n nada y terminara de cargar la imagen

                        if (tipo=='imagen')
                        {
                                PUMImagenMultimedia = new Image;
                                PUMImagenMultimedia.src = url;
                                PUMImagenMultimedia.onload = PUMswapImagenes; // ya nos avisar� cuando termine
                        }
        }
}

/**
* funciones que cierran el popup; lo hace al pulsar el "cerrar" o al clickar fuera, en la zona gris
*
* la versi�n con URL se puede invocar de forma manual (p.e. desde un bot�n de "cerrar" dentro del iframe) y provoca que al cerrarse el popup
* se redirige al usuario a la URL especificada
*/

function cerrarPopUpMultimediaUrl(url){
        if(url!=null){
            window.parent.location = url;
        }
}

function cerrarPopUpMultimedia() {

        if(gPUMHideSelects){
                PUMdisplaySelectBoxes();
        }

        //document.listavideos.lista.style.visibility="visible";

        if (PUMPopMask!=null)
        {
                if (bPUMPopupAbierto) {
                        bPUMPopupAbierto = false;
                        PUMPopContenido.style.display = 'none';
                        PUMPopMask.style.display = 'none';

                        window.onscroll = null;
                        PUMremoveEvent(window, "resize", PUMcentrar);

                        // nos cargamos tambi�n del HTML el objeto
                        var body = document.getElementsByTagName('body')[0];
                        body.removeChild(PUMPopContenido);
                        PUMPopContenido = null;
                        body.removeChild(PUMPopMask);
                        PUMPopMask = null;
                }
        }
}


/**
* funciones y variables globales auxiliares (NO PUBLICAS) ==================================================
*/

var PUMPopMask = null;
var PUMPopContenido = null;
var PUMImagenMultimedia = null;
var bPUMPopupAbierto = false;
var bPUMAnchoLiquido = false;
var bPUMAltoLiquido = false;
var aPUMTamanoEspecificado;
var gPUMHideSelects = false;


/**
* funci�n de centrado de las capas para la primera vez que aparecen, para cuando se termina
* de cargar la imagen (si es de tipo "imagen"), cuando se hace scroll o cuando el usuario
* cambia la ventana del navegador
*/

function PUMcentrar(width, height) {

        if (bPUMPopupAbierto) {

        // cogemos tama�o de ventana visible
                var fullWidth = PUMgetViewportWidth();
                var fullHeight = PUMgetViewportHeight();

        // el fondo de pantalla gris ocupa todo el BODY
                var scTop = parseInt(PUMgetScrollY(),10);
                var scLeft = parseInt(PUMgetScrollX(),10);
                PUMPopMask.style.height = fullHeight + "px";
                PUMPopMask.style.width = fullWidth + "px";
                PUMPopMask.style.top = scTop + "px";
                PUMPopMask.style.left = scLeft + "px";

        // calculamos ancho del popup a mostrar
        if (bPUMAnchoLiquido) {
          width = fullWidth - (PUMmargenPopBordeVentana*2);
        }
        else
          if (width == null || isNaN(width)) {
              width = PUMPopContenido.offsetWidth;
          }

        if (bPUMAltoLiquido) {
          height = fullHeight - (PUMmargenPopBordeVentana*2);
        }
        else
                  if (height == null || isNaN(height)) {
                          height = PUMPopContenido.offsetHeight;
                  }

                PUMPopContenido.style.width = width + "px";
                PUMPopContenido.style.height = height + "px";
                PUMPopContenido.style.top = (scTop + ((fullHeight - height) / 2)) + "px";
                PUMPopContenido.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
        }
}


// esta funci�n se llama cuando se carga la imagen referenciada en PUMImagenMultimedia

function PUMswapImagenes() {

                // swapeamos la imagen existente ("wait") por �sta
                var lImagen = document.getElementById("iPopImagenMultimedia");
                lImagen.src = PUMImagenMultimedia.src;

                // redimensionamos las capas para que quepa la imagen
                //PUMcentrar(PUMImagenMultimedia.width + PUMmargenPopContenido*2, PUMImagenMultimedia.height + PUMmargenPopContenido*2);
}

// funciones de gesti�n de eventos de objetos

function PUMaddEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function PUMremoveEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

// funciones de c�lculo del tama�o de la p�gina visible

function PUMgetViewportHeight() {
        if (window.innerHeight!=window.undefined) return window.innerHeight;
        if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
        if (document.body) return document.body.clientHeight;
        return window.undefined;
}
function PUMgetViewportWidth() {
        if (window.innerWidth!=window.undefined) return window.innerWidth;
        if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
        if (document.body) return document.body.clientWidth;
        return window.undefined;
}

function PUMgetScrollY() {
    var pos = 0;
    if (window.pageYOffset) {
        pos = window.pageYOffset
    } else if (document.documentElement && document.documentElement.scrollTop) {
      pos = document.documentElement.scrollTop
    } else if (document.body) {
        pos = document.body.scrollTop
    }
    return pos;
}

function PUMgetScrollX() {
    var pos = 0;
    if (window.pageXOffset) {
        pos = window.pageXOffset
    } else if (document.documentElement && document.documentElement.scrollLeft) {
      pos = document.documentElement.scrollLeft
    } else if (document.body) {
        pos = document.body.scrollLeft
    }
    return pos;
}

/**
* ocultar selects (un bug de IE hace que salgan siempre encima de cualquier capa)
*/
function PUMHideSelectBoxes() {

        for(var i = 0; i < document.forms.length; i++) {
                for(var e = 0; e < document.forms[i].length; e++){
                        if(document.forms[i].elements[e].tagName == "SELECT") {
                                document.forms[i].elements[e].style.visibility="hidden";
                        }
                }
        }
}

/**
* mostrar otra vez los selects ocultos (un bug de IE hace que salgan siempre encima de cualquier capa)
*/
function PUMdisplaySelectBoxes() {
        for(var i = 0; i < document.forms.length; i++) {
                for(var e = 0; e < document.forms[i].length; e++){
                        if(document.forms[i].elements[e].tagName == "SELECT") {
                        document.forms[i].elements[e].style.visibility="visible";
                        }
                }
        }
}

