// 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 = 560;
var altoCapa = 160;

/*
Observacion
	Para HTML usar document.documentElement
	Para XHTML usar document.documentElement
*/

/* 
Funcioue se ejecutara cargar la pagina.
*/
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 raton
    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.documentElement.scrollLeft;
    }
}
function getOrigenY(){
    if( GECKO ){
        return window.pageYOffset;
    } else if( MSIE ){                    
        return document.documentElement.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.documentElement.clientWidth;
            altoDoc = document.documentElement.clientHeight;

        }
        anchoDoc += getOrigenX();
        altoDoc += getOrigenY();
    
        var obj = document.getElementById(popupActivo);                    
    
        // Obtenemos el rectangulo 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 raton.
        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 raton 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";
		obj.style.visibility = 'visible';
        
    }
}                            


function mostrarPopup(id){
  //popupActivo = "popupEvento_"+id;
  popupActivo = id;
  
  // Cargamos la imagen del evento que se encuentra indicada en la propiedad rel.
  var rel;
  var obj = document.getElementById(popupActivo);
  var img = document.getElementById("fotoEvento_"+id);    
  if( img ){
      rel = img.getAttribute("title");
      if( rel != "" ){
        img.src = rel;
        img.setAttribute("title","");
      }
  }
  popupVisible = true;
}

function ocultarPopup(){                
    popupVisible = false;
    var obj = document.getElementById(popupActivo);
    if( obj ){
        //obj.style.display = "none";
		obj.style.visibility = 'hidden';
        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);

}
