var ns = (document.layers)? true:false
var ie = (document.all)? true:false
var atmr = new Array()

function reloadPage(init) {
  if (init==true) {
        document.pgW=ie?top.document.body.clientWidth:innerWidth;
        document.pgH=ie?top.document.body.clientHeight:innerHeight;
        onresize=reloadPage;
  }
  else if ((ie?top.document.body.clientWidth:innerWidth)!=document.pgW || (ie?top.document.body.clientHeight:innerHeight)!=document.pgH)
      location.reload();
}

function preloadImages(){
    var d=document;
    if ( ! d.aimages )
        d.aimages = new Array();
    for ( var i=0;i<preloadImages.arguments.length;i++ ){
        d.aimages[ i ] = new Image();
        d.aimages[ i ].nombre = preloadImages.arguments[ i ]
        d.aimages[ i ].src = preloadImages.arguments[ i ]
    }
}

function toObj( obj ){
    if ( typeof( obj ) == "object" )
        return obj
    else
        return findObject( obj )
}

function findObject( cObj ){
    var o
    if ( ie )
            o = eval( "document.all." + cObj )
    else    {
        if ( eval( "document.layers." + cObj ) ){
            o = eval( "document.layers." + cObj ) }
        else{
            if( eval( "document." + cObj ) )
                 o = eval( "document." + cObj )
            else
                if ( document.layers.length != 0 ){
                for ( var i=0;i<document.layers.length;i++)
                        if ( eval( "document.layers[ " + i + "].document." + cObj ) != null ){
                                o = eval( "document.layers[ " + i + "].document." + cObj )
                        }
                }
        }
    }
    return ( o )

}

function cambiaImg( imagen, cambiopor ){
    var d=document;
    for ( var i=0;i<d.aimages.length;i++ )
        if ( d.aimages[ i ].nombre == cambiopor )
             toObj( imagen ).src = d.aimages[ i ].src
}

// Funciones para el movimiento del menú
function selElement( cobj ){
    var o = ie?toObj( cobj ).style:toObj( cobj )
    return ( o )
}


function muestra( o, lMostrar ){
    if ( lMostrar )
        ie?o.visibility = "":o.visibility = "show";
    else
        ie?o.visibility = "hidden":o.visibility = "hide";
    return( false );
}

function slide( nameDiv, toX, toY, nSpeed, lfrenada, ntmr, cevent ){
    if( atmr[ ntmr ] != null )
      clearInterval( atmr[ ntmr ] )
    if ( ! nSpeed )
           nSpeed=20
    if ( ! lfrenada && lfrenada != false )
           lfrenada=true
    if ( ! ntmr )
           ntmr=0
    if ( ! cevent )
           cevent=""
    var o = selElement( nameDiv );
    var nleft = ie?o.pixelLeft:o.left
    var ntop = ie?o.pixelTop:o.top
    if ( ! toX && toX != 0)
          toX = nleft
    if ( ! toY && toY != 0)
          toY = ntop
    var dirH = ( nleft > toX )?-1:1
    var dirV = ( ntop > toY )?-1:1
    var ntimes = Math.sqrt(Math.pow(Math.abs( toX - nleft ),2) + Math.pow(Math.abs( toY - ntop ),2))/nSpeed
    if ( ntimes == 0 )
        ntimes = 1
    var nincremH = Math.max( Math.abs( toX - nleft ) / ntimes, 1 )
    var nincremV = Math.max( Math.abs( toY - ntop ) / ntimes, 1 )
    slideMove( nameDiv, toX, toY, nincremH, nincremV, lfrenada, dirH, dirV, ntmr, cevent )
}

function slideMove( nameDiv, toX, toY, nincremH, nincremV, lfrenada, dirH, dirV, ntmr, cevent ){
    var o = selElement( nameDiv );
    var nleft = ie?o.pixelLeft:o.left
    var ntop = ie?o.pixelTop:o.top
    var nmovH = lfrenada?Math.min( nincremH, Math.max( ( Math.abs( nleft - toX  )/ 7 ), 1  ) ):nincremH
    var nmovV = lfrenada?Math.min( nincremV, Math.max( ( Math.abs( ntop - toY  )/ 7 ), 1  ) ):nincremV
    var lfinH = false
    var lfinV = false
    nleft = nleft + ( nmovH * dirH );
    ntop = ntop + ( nmovV * dirV );
    if ( ( dirH == -1 && nleft <= toX ) || ( dirH == 1 && nleft >= toX ) ){
        nleft = toX
        o.left = nleft
        lfinH = true ;
    }
    if ( ( dirV == -1 && ntop <= toY ) || ( dirV == 1 && ntop >= toY ) ){
        ntop = toY
        o.top = ntop
        lfinV = true ;
    }
    if( lfinH && lfinV )
        eval( cevent );
    else{
        o.top = ntop
        o.left = nleft
        atmr[ ntmr ] = setTimeout( 'slideMove( "'+ nameDiv + '", ' + toX + ',' + toY + ',' + nincremH + ',' + nincremV + ',' + lfrenada + ',"' + dirH +  '","' +dirV +  '",' + ntmr + ',"' + cevent+  '")', 3 ) ;
    }
}

// Funciones para movimiento
function posValues(o,which) {
    var obj = toObj( o )
    if (which=="t") return Number(ie?obj.style.pixelTop:obj.top)
    if (which=="l") return Number(ie?obj.style.pixelLeft:obj.left)
    if ( ie ){
        if (which=="b") return Number( obj.style.pixelTop + obj.style.pixelHeight)
        if (which=="r") return Number( obj.style.pixelLeft + obj.style.pixelWidth )
    }
}

function posTo(o,x,y) {
    var obj = toObj( o )
    if ( ie ){
        obj.style.left = x
        obj.style.top = y
    }
    else{
        obj.left = x
        obj.top = y
    }
}

function posBy(o,x,y ) {
    var obj = toObj( o )
    if ( ie ){
        obj.style.left = ( obj.style.pixelLeft + x )
        obj.style.top = ( obj.style.pixelTop + y )
    }
    else{
        obj.left = Number( obj.left ) + x
        obj.top = Number( obj.top ) + y
    }
}

// Funciones para CLIP
function clipTo(o,t,r,b,l) {
    var obj = toObj( o )
    if (ns) {
            obj.clip.top = t
            obj.clip.right = r
            obj.clip.bottom = b
            obj.clip.left = l
    }
    else if (ie) obj.style.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)"
}

function clipBy(o,t,r,b,l) {
    var obj = toObj( o )
    if (ns) {
            obj.clip.top = clipValues(obj,'t') + t
            obj.clip.right = clipValues(obj,'r') + r
            obj.clip.bottom = clipValues(obj,'b') + b
            obj.clip.left = clipValues(obj,'l') + l
    }
    else if (ie) obj.style.clip = "rect("+(this.clipValues(obj,'t')+t)+"px "+(this.clipValues(obj,'r')+r)+"px "+Number(this.clipValues(obj,'b')+b)+"px "+Number(this.clipValues(obj,'l')+l)+"px)"
}

function clipValues(o,which) {
    var obj = toObj( o )
    if ( ie ) {
            var clipv = obj.style.clip.split("rect(")[1].split(")")[0].split("px")
            if (which=="t") return Number(clipv[0])
            if (which=="r") return Number(clipv[1])
            if (which=="b") return Number(clipv[2])
            if (which=="l") return Number(clipv[3])
    }
    else {
            if (which=="t") return obj.clip.top
            if (which=="r") return obj.clip.right
            if (which=="b") return obj.clip.bottom
            if (which=="l") return obj.clip.left
    }
}

// Función para filtros
function filtro( cdiv, lshow,ntrans, ndurac ) {
    if ( ! ntrans )
        ntrans = 12
    if ( ! ndurac )
        ndurac = 0.5
    var o
    if ( ie ){
          o = document.all( cdiv )
          o.filters[0].Apply();
          o.filters[0].duration = ndurac;
          o.filters[0].transition = ntrans;
          if ( lshow )
              o.style.visibility = "visible";
          else
              o.style.visibility = "hidden";
          o.filters[0].Play();
    }
    else{
      o = selElement( cdiv  )
      if( lshow )
          muestra( o, true )
      else
          muestra( o, false )
    }
}

// Escribe el contenido de una capa
function writeCapa( capa, texto ){
     capa = toObj( capa )
     if ( ie )
          capa.innerHTML = texto
     else {
          capa.document.open()
          capa.document.write( texto )
          capa.document.close()
     }
}


// **** SCROLL HORIZONTAL ****
// Funciones para scrolls horizontales de capas
var tmrscrollh
var nAnchoscrollh
var nspeedh = 5

// En el "onload" hay que llamar a esta función pasandole la capa y el ancho deseado
function initscrollh( o, w ){
   var obj = toObj( o )
   nAnchoscrollh = (ie)?obj.clientWidth:obj.clip.right
   var nAlto = (ie)?obj.offsetHeight:obj.clip.bottom
   clipTo( obj, 0, w, nAlto, 0 )
}

// Movimiento del scroll. Parámetros:
// o -> capa
// dir -> dirección ("izda" o "dcha" )
// lmov -> true para iniciar movimiento y false para pararlo
// nspeedini -> velocidad del scroll (opcional)
//
// ej:   onmouseover="movscrollh( 'capa', 'izda', true )" onmouseout="movscroll( 'capa', 'izda', false )"

function movscrollh( o, dir, lmov, nspeedini ){
    clearInterval( tmrscrollh )
    if ( ! nspeedini )
            nspeedini = 5
    var obj = toObj( o )
    if ( lmov )
         nspeedh = nspeedini
    else
         nspeedh --
    if ( nspeedh > 0 ) {
         if ( dir == 'izda' ) {
            if ( clipValues( obj, 'r' ) <= nAnchoscrollh ){
                posBy( obj, -1 * nspeedh, 0 )
                clipBy( obj, 0, nspeedh, 0, nspeedh )
                tmrscrollh = setTimeout('movscrollh(\'' + o + '\',\'' + dir + '\',' + lmov + ')',3)
            }
        }
        else {
            if ( clipValues( obj, 'l' ) > 0 ){
                posBy( obj, nspeedh, 0 )
                clipBy( obj, 0, -1 * nspeedh, 0, -1 * nspeedh )
                tmrscrollh = setTimeout('movscrollh(\'' + o + '\',\'' + dir + '\',' + lmov + ')',3)
            }
        }
     }
}

// **** SCROLL VERTICAL ****

var tmrscrollv
var nAltoscrollv
var nspeedv = 5

// En el "onload" hay que llamar a esta función pasandole la capa y el ancho deseado
function initscrollv( o, h ){
   var obj = toObj( o )
   nAltoscrollv = (ie)?obj.clientHeight:obj.clip.bottom
   var nAncho = (ie)?obj.offsetWidth:obj.clip.right
   clipTo( obj, 0, nAncho, h, 0 )
}

// Movimiento del scroll. Parámetros:
// o -> capa
// dir -> dirección ("up" o "dwn" )
// lmov -> true para iniciar movimiento y false para pararlo
// nspeedvini -> velocidad del scroll (opcional)
//
// ej:   onmouseover="movscrollv( 'capa', 'up', true )" onmouseout="movscrollv( 'capa', 'up', false )"

function movscrollv( o, dir, lmov, nspeedvini ){
    clearInterval( tmrscrollv )
    if ( ! nspeedvini )
            nspeedvini = 5
    var obj = toObj( o )
    if ( lmov )
         nspeedv = nspeedvini
    else
         nspeedv --
    if ( nspeedv > 0 ) {
         if ( dir == 'up' ) {
            if ( clipValues( obj, 'b' ) <= nAltoscrollv ){
                posBy( obj, 0, -1 * nspeedv )
                clipBy( obj, nspeedv, 0, nspeedv, 0 )
                tmrscrollv = setTimeout('movscrollv(\'' + o + '\',\'' + dir + '\',' + lmov + ')',3)
            }
        }
        else {
            if ( clipValues( obj, 't' ) > 0 ){
                posBy( obj, 0, nspeedv )
                clipBy( obj, -1 * nspeedv, 0, -1 * nspeedv, 0 )
                tmrscrollv = setTimeout('movscrollv(\'' + o + '\',\'' + dir + '\',' + lmov + ')',3)
            }
        }
     }
}

// Función para la barra de estado.

function PonerTexto( StrMsg){
    status = StrMsg;
}
