// Variables globales

// Javascript para vista mensual.
var mouseX=0, mouseY=0;
var MOUSE_DIST_X = 15;
var MOUSE_DIST_Y = 25;
var MSIE = false;
var GECKO = false;
var popupActivo = null;
var popupVisible = false;
var anchoCapa = 360;
var altoCapa = 130;

/*
Observación:
	Para HTML usar document.body
	Para XHTML usar document.documentElement
*/

/* 
Función que se ejecutará al cargar la página.
*/
function init (){
    //focusInputs();
    //loadExternalUrl();
    initPopup();    
}

if( navigator.userAgent.indexOf('MSIE')!=-1 ){
   ie = true;
   ns = false;
   MSIE = true;
   window.attachEvent('onload', init );
}       
if( navigator.userAgent.indexOf('Gecko')!=-1 ){		
   ie = false;
   ns = true;
   GECKO = true;
   window.addEventListener( 'load', init, false );
}

function initPopup(){                            
		
    // Capturamos los eventos del ratón
    if( MSIE ){
    	document.onmousemove = mouseMove;
        
    }else if( GECKO ){
        document.onmousemove = mouseMove;
        document.captureEvents(Event.MOUSEMOVE);
    }                                
}                        

function getOffsetLeft (el) {
    var ol = el.offsetLeft;
    while ((el = el.offsetParent) != null){
        ol += el.offsetLeft;
    }
    return ol;
}

function getOffsetTop (el) {
    var ot = el.offsetTop;
    while((el = el.offsetParent) != null){
        ot += el.offsetTop;
    }
    return ot;
}      

function getOrigenX(){
    if( GECKO ){                
        return window.pageXOffset;                    
    } else if( MSIE ){
        return document.body.scrollLeft;
    }
}
function getOrigenY(){
    if( GECKO ){
        return window.pageYOffset;
    } else if( MSIE ){                    
        return document.body.scrollTop;
    }
}            

// Funciones para posicionar el layer.
function mouseMove (e) {
    if( popupVisible ){
        if( GECKO ){
            mouseX= parseInt(e.pageX,10);
            mouseY= parseInt(e.pageY,10);                                        
        } else if( MSIE ){
            mouseX= event.clientX + getOrigenX();
            mouseY= event.clientY + getOrigenY();
        }

        
        
        if( GECKO ){
            anchoDoc = window.innerWidth;
            altoDoc = window.innerHeight;
        } else if( MSIE ){
            anchoDoc = document.body.clientWidth;
            altoDoc = document.body.clientHeight;
        }
        anchoDoc += getOrigenX();
        altoDoc += getOrigenY();
    
        var obj = document.getElementById(popupActivo);                    
    
        // Obtenemos el rectángulo del area cliente (el area visible).
        clientLeft = getOrigenX();
        clientTop = getOrigenY();
        clientRight = anchoDoc - 20;    // Asumimos 20 pixels de ancho de las barras de scroll.
        clientBottom = altoDoc - 20;                                                                                        
                
        // Posicionamos la capa inicialmente alejada del ratón.
        capaX = mouseX + MOUSE_DIST_X;
        capaY = mouseY + MOUSE_DIST_Y;
                                
        if( capaX+anchoCapa > clientRight ){
             capaX = clientRight - anchoCapa;
        }
        if( capaY+altoCapa > clientBottom ){
            capaY = clientBottom - altoCapa;
        }
        if( capaY < clientTop ){
            capaY = clientTop;
        }                  
        if( capaX < clientLeft ){
            capaX = clientLeft;
        }                        
        
        // Si al final el puntero del ratón nos queda dentro de la capa, la movemos para no interferir en el rollover.
        if( mouseX > capaX && mouseX < capaX+anchoCapa && mouseY > capaY && mouseY < capaY+altoCapa ){
            espacio=new Array();
            espacio[0] = clientBottom - mouseY; // abajo
            espacio[1] = mouseY - clientTop;    // arriba
            espacio[2] = mouseX - clientLeft;   //izq
            espacio[3] = clientRight - mouseX;  //der
            
            max = pos = 0;                                
            for( i=0; i<4; i++ ){                      
                if( espacio[i] > max ){
                    max = espacio[i];
                    pos = i;
                }
            }
            switch( pos ){
                case 0:capaY = mouseY + MOUSE_DIST_Y; break;
                case 1:capaY = mouseY - altoCapa - MOUSE_DIST_Y*2; break;
                case 2:capaX = mouseX - anchoCapa - MOUSE_DIST_X*2; break;
                case 3:capaX = mouseX + MOUSE_DIST_X; break;
            }
        }                
        obj.style.left = capaX + "px";
        obj.style.top = capaY + "px";
        obj.style.display = "block";
        
    }
}                            

function mostrarPopup(id){
    //popupActivo = "popupEvento_"+id;
    popupActivo = id;
    var obj = document.getElementById(popupActivo);                                
    popupVisible = true;        
}

function ocultarPopup(){                
    popupVisible = false;
    var obj = document.getElementById(popupActivo);
    if( obj ){
        obj.style.display = "none";
        obj.style.left="0px";
        obj.style.top="0px";
    }
}
            
// Abir una ventana popup (no resizable)
function abrirPopup( url ,f_amp, f_alt, nom_finestra, scroll ){
	ancho=screen.width;				//ample pantalla
	alto=screen.height;				//alt pantalla
	v_top=(alto-f_alt)/2;	
	v_left=(ancho-f_amp)/2;
	if (typeof(v_fin)!="undefined"){
		v_fin.close();
	}
	v_fin=window.open(url,nom_finestra,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+(scroll?'yes':'no')+",resizable=no,width="+f_amp+",height="+f_alt+",top="+v_top+",left="+v_left);

}

