
function CargaVariables()
{
      velocidad= 100;       //velocidad en milisegundos
      cambio= 200;          //tiempo en que tarda en cambiar la velocidad en milisegundos. 
      xmax=20;              //pixels maximos de cambio derecha-izquierda        
      ymax=20;              //pixels maximos de cambio arriba-abajo

      incrx=2;
      incry=2;
      xdir=true;
      ydir=true;
 }    
 
function inicia()
{
   if (document.all)
       window.onresize=ajusta;
	CargaVariables();
	ajusta();
	mueveImagen(w_x/2,w_y/2);
	if(document.all)
	    document.all('img1').style.visibility="visible";
	else if(document.layers)
	    document.layers['img1'].visibility="show";
	else if(document.getElementById)
	    document.getElementById('img1').style.visibility="visible";
	anima();
	cambia();
	recarga2();
}
      
/**
* funcion para centrar la imagen en pantalla cuando se resicea la ventana del navegador
*/
function ajusta()
{
    if(document.all)
    {
        w_x=document.body.clientWidth;
        w_y=document.body.clientHeight;
    }
    else if(document.layers)
    {
        w_x=window.innerWidth-document.img1.document.width;
        w_y=window.innerHeight-document.img1.document.height;
    }
    else if(document.getElementById && !document.all)
    {
       
        w_x=window.innerWidth;
        w_y=window.innerHeight;
    }          
}

function cambia()
{
    xdir=(Math.floor(Math.random()*2)==0);
    ydir=(Math.floor(Math.random()*2)==0);
    incrx=Math.floor(Math.random()*xmax);
    incry=Math.floor(Math.random()*ymax);
    setTimeout('cambia()',cambio);
}

function anima()
{
    if(document.all)
    {
        tx=document.all('img1').style.pixelLeft;
        ty=document.all('img1').style.pixelTop;
        xo=document.body.scrollLeft;
        yo=document.body.scrollTop;
    }
    else if(document.layers)
    {
        tx=document.layers['img1'].left;
        ty=document.layers['img1'].top;
        xo=pageXOffset;
        yo=pageYOffset;
    }
    else if(document.getElementById && !document.all)
    {
        tx=parseInt(document.getElementById('img1').style.left);
        ty=parseInt(document.getElementById('img1').style.top);
        xo=pageXOffset;
        yo=pageYOffset;
    }          
    if(ydir)
    {
        if((ty)>(w_y-75))
        {
            ydir=false;
            mueveImagen(0,-incry);
        }
        else
        {
            mueveImagen(0,incry);
        }
    }
    else
    {
        if((ty)<75)
        {
            ydir=true; 
            mueveImagen(0,incry);
        }
        else
        {
            mueveImagen(0,-incry);
        }
    }
    if(xdir)
    {
        if((tx)>(w_x-75))
        {
            xdir=false; mueveImagen(-incrx,0);
        }
        else
        {
            mueveImagen(incrx,0);
        }
    }
    else
    {
        if((tx)<75)
        {
            xdir=true; mueveImagen(incrx,0);
        }
        else
        {
            mueveImagen(-incrx,0);
        }
    }
    setTimeout('anima()',velocidad);
}

function mueveImagen(dx,dy)
{
    if(document.all)
    {
        document.all('img1').style.pixelTop+=dy;
        document.all('img1').style.pixelLeft+=dx;
    }
    if(document.layers)
    {
        document.img1.moveBy(dx,dy);
    } 
    else if(document.getElementById && !document.all)
    {              
        document.getElementById('img1').style.top=parseInt(document.getElementById('img1').style.top)+dy;
        document.getElementById('img1').style.left=parseInt(document.getElementById('img1').style.left)+dx;  
    }          
}

function esconde()
{
    if (document.all)
        document.all('img1').style.visibility="hidden"
    else if (document.layers)
        document.layers['img1'].visibility="hide"
    else if (document.getElementById)
        document.getElementById('img1').style.visibility="hidden"
}

function recarga()
{
    window.location.reload()
}
function recarga2()
{
    if (document.layers)
        setTimeout("window.onresize=recarga",400)
}
