//INICIO DE SCREENSAVER
//Variables de configuración
var josInterval = 5000;//Interv. de movimiento de la img en miliseg.
var josWait = 150;//300 //Segundos a esperar para el screensaver
var imgWidth = 240;//163;
var imgHeight = 50;//34; 
//Variables internas
var run = false;
var objImage;
var objOverlay;
var curpos;
var xRange;
var yRange;
var josDate = new Date();
var josTime2Run = josDate.getTime();
var ars;

function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}

function josShowHide (st) 
{
    var tags = new Array("applet", "iframe", "select");
    for (var k = tags.length; k > 0; ) 
    {
        var ar = document.getElementsByTagName(tags[--k]);
        var cc = null;
        for (var i = ar.length; i > 0;) 
        {
            cc = ar[--i];
            cc.style.visibility = st;
        }
    }
}
function getPageScroll()
{
    var yScroll;
    if (self.pageYOffset) 
    {
        yScroll = self.pageYOffset;
    } 
    else if (document.documentElement && document.documentElement.scrollTop)
    {	 // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
    } 
    else if (document.body) 
    {// all other Explorers
        yScroll = document.body.scrollTop;
    }
    arrayPageScroll = new Array("", yScroll ) 
    return arrayPageScroll;
}
function getPageSize()
{
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) 
    {	
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } 
    else if (document.body.scrollHeight > document.body.offsetHeight)
    { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } 
    else 
    { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) 
    {	// all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } 
    else if (document.documentElement && document.documentElement.clientHeight) 
    { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } 
    else if (document.body) 
    { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }	
    if(yScroll < windowHeight)
    {
        pageHeight = windowHeight;
    } 
    else 
    { 
        pageHeight = yScroll;
    }
    if(xScroll < windowWidth)
    {	
        pageWidth = windowWidth;
    } 
    else 
    {
        pageWidth = xScroll;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
    return arrayPageSize;
}
function josStart() 
{
    var ar = getPageSize();
    ars = getPageScroll();
    if (document.all) 
    {
        objOverlay.style.width = ar[0] + "px";
    } 
    else 
    {
    objOverlay.style.width = "100%"; 
    }
    if ((document.all) && ars[1]>10) 
    {
        objOverlay.style.height = (ar[1]+25) + "px";
    } 
    else 
    {
        objOverlay.style.height = ar[1] + "px";
    }
    xRange = ar[2] - objImage.width;
    yRange = ar[3] - objImage.height;
    objOverlay.style.display = "block";
    objImage.style.visibility ="visible";
    run = true;
    josStartRandomPosition();
    josShowHide("hidden");
}
function josEnd() 
{
    if (run) 
    {
        if (document.all) 
        {
            if ((curpos == (event.screenX + ":" + event.screenY)) && (event.type=="mousemove")) 
                return false;
        }
        objImage.style.visibility ="hidden";
        objImage.style.top = "0px";
        objImage.style.left = "0px";
        objOverlay.style.display = "none";
        objOverlay.style.width = "1px";
        objOverlay.style.height = "1px";
        josShowHide("visible");
    }
    run = false;
    var dt = new Date();
    josTime2Run = dt.getTime();
}
function josStartRandomPosition() 
{
    if (run) 
    {
        var x = Math.round(Math.random() * xRange);
        var y = Math.round(Math.random() * yRange) + ars[1];
        objImage.style.left = x + "px";
        objImage.style.top = y + "px";
        window.setTimeout("josStartRandomPosition();", josInterval);
    }
}
function josWatcher() 
{
    var dt = new Date();
    if ( (run) ) 
    {
        window.setTimeout("josWatcher();", 1000);
        return true;
    }
    if ((dt.getTime() - josTime2Run) > 1000*josWait) 
        josStart();
    window.setTimeout("josWatcher();", 1000);
}
/*
//Para poner al final de la web e iniciar el salvapantallas
objOverlay = findObj('josOverlay');
objOverlay.style.background="black";
objImage = findObj("ss");
objImage.style.height = imgHeight + "px";
objImage.style.width = imgWidth + "px";
		
document.onclick = josEnd;
objOverlay.onclick = josEnd;
document.onmousemove = function () {
if (document.all) {
if (window.event.srcElement.id != "josOverlay") {
curpos = event.screenX + ":" + event.screenY;}}
josEnd();
};
document.onkeypress = josEnd;
window.onresize = josEnd;
josWatcher();  
*/
//FIN
