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 ( 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 )
                    }
        }
        else
             o = eval( "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 = "hidden":o.visibility = "hide";
    else
        ie?o.visibility = "":o.visibility = "show";
    return( false );
}

function slide( nameDiv, toX, toY, nSpeed, lfrenada, ntmr, cevent ){
    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  )/ 12 ), 1  ) ):nincremH
    var nmovV = lfrenada?Math.min( nincremV, Math.max( ( Math.abs( ntop - toY  )/ 12 ), 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+  '")', 6 ) ;
    }
}

// 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 )
    }
}

function writeCapa( capa, texto ){
     if ( ie )
          document.all( capa ).innerHTML = texto
     else {
          var cap = findObject( capa )
          cap.document.open()
          cap.document.write( texto )
          cap.document.close()
     }
}


// Funciones para scrolls horizontales de capas
var tmrscroll
var nAnchosroll

// En el "onload" hay que llamar a esta función pasandole la capa y el ancho deseado
function initscroll( o, w ){
   var obj = toObj( o )
   nAnchosroll = (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="movscroll( 'capa', 'izda', true )" onmouseout="movscroll( 'capa', 'izda', false )"

function movscroll( o, dir, lmov, nspeedini ){
    clearInterval( tmrscroll )
    if ( ! nspeedini )
            nspeedini = 5
    var obj = toObj( o )
    if ( lmov )
         nspeed = nspeedini
    else
         nspeed --
    if ( nspeed > 0 ) {
         if ( dir == 'izda' ) {
         	if ( clipValues( obj, 'r' ) <=nAnchosroll  ){ // 730
         	posBy( obj, -1 * nspeed, 0 )
                clipBy( obj, 0, nspeed, 0, nspeed )
                tmrscroll = setTimeout('movscroll(\'' + o + '\',\'' + dir + '\',' + lmov + ')',3)
            }
        }
        else {
            if ( clipValues( obj, 'l' ) > 0 ){
                posBy( obj, nspeed, 0 )
                clipBy( obj, 0, -1 * nspeed, 0, -1 * nspeed )
                tmrscroll = setTimeout('movscroll(\'' + o + '\',\'' + dir + '\',' + lmov + ')',3)
            }
        }
     }
}
