//*******************************************
//libreria compatible manipulacion de LAYERS
//*******************************************
//funciones en documento y ventana:

var JVLayers=false
var isDOM
var isNC4
var isNC
var isIE
var isNC6
var isMozilla
var isOpera

var pageLeft
var pageTop
var imagePreloaderCount
var imagePreloaderArray
var imageRef
var styleSwitch
var layerPostfix
var layerRef

//getWindowLeft y getWindowTop estan basados en las funciones del navegador 
//puede no funcionar correctamente en algunas versiones antiguas de IE4

function getWindowLeft(){
  if(isOpera) return -1 // no en Opera
  if(isNC) return top.screenX
  if(isIE) return top.screenLeft
}

function getWindowTop(){
  if(isOpera) return -1 // no en Opera
  if(isNC) return top.screenY
  if(isIE) return top.screenTop
}

function getWindowWidth(w){
  if(!w) w=window
  if(isIE) return w.document.body.clientWidth
  if(isNC) return w.innerWidth
}

function getWindowHeight(w){
  if(!w) w=window
  if(isIE) return w.document.body.clientHeight
  if(isNC) return w.innerHeight
}

function setWindowLeft(x){
  if(!isOpera)top.moveBy(x-getWindowLeft(),0)
}

function setWindowTop(y){
  if(!isOpera)top.moveBy(0,y-getWindowTop())
}

function setWindowWidth(x){
  if(isIE) top.resizeBy(x-getWindowWidth(),0); else
  if(isNC) top.innerWidth=x
}

function setWindowHeight(y){
  if(isIE) top.resizeBy(0,y-getWindowHeight()); else
  if(isNC) top.innerHeight=y
}

function getScrollX(w){
  if(!w) w=window
  if(isIE) return w.document.body.scrollLeft
  if(isNC) return w.pageXOffset
}

function getScrollY(w){
  if(!w) w=window
  if(isIE) return w.document.body.scrollTop
  if(isNC) return w.pageYOffset
}

function getDocumentWidth(w){
  if(!w) w=window
  if(isIE) return w.document.body.scrollWidth
  if(isOpera) return -1 // no en Opera
  if(isNC) return w.document.width
}

function getDocumentHeight(w){
  if(!w) w=window
  if(isIE) return w.document.body.scrollHeight
  if(isOpera) return -1 // no en Opera
  if(isNC) return w.document.height
}

var LAYER=0
var IMAGE=1

function findObject(where, what, type){
  var i,j,l,s
  var len=eval(where+".length")
  for(j=0;j<len;j++){
    s=where+"["+j+"].document.layers"
    if(type==LAYER){
      l=s+"[\""+what+"\"]"
    }
    if(type==IMAGE){
      i=where+"["+j+"].document.images"
      l=i+"[\""+what+"\"]"
    }
    if(eval(l)) return l
    l=findObject(s,what,type)
    if(l!="false") return l
  }
  return "false"
}

function getLayerPath(name,parent){
  var l=((parent && isNC4)?(parent+"."):(""))+layerRef+name+layerPostfix
  if(eval(l))return l
  if(!isNC4){
    return "false"
  }else{
    return findObject("document.layers",name,LAYER)
  }
}

function getImagePath(name,parent){
  var l=((parent && isNC4)?(parent+"."):(""))+imageRef+name+layerPostfix
  if(eval(l))return l
  if(!isNC4){
    return "false"
  }else{
    return findObject("document.layers",name,IMAGE)
  }
}

// class "JVLayer":

JVLP=JVLayer.prototype

JVLP.isExist=JVL_exists
JVLP.getLeft=JVL_left
JVLP.getTop=JVL_top
JVLP.getWidth=JVL_width
JVLP.getHeight=JVL_height
JVLP.getZIndex=JVL_zIndex
JVLP.moveX=JVL_moveX
JVLP.moveY=JVL_moveY
JVLP.move=JVL_move
JVLP.moveZ=JVL_moveZ
JVLP.setZIndex=JVL_moveZ
JVLP.setVisibility=JVL_setVisibility
JVLP.show=JVL_show
JVLP.hide=JVL_hide
JVLP.isVisible=JVL_visible
JVLP.setBgColor=JVL_setBgColor
JVLP.clip=JVL_clip
JVLP.scroll=JVL_scroll
JVLP.scrollByOffset=JVL_scrollByOffset
JVLP.scrollByPercentage=JVL_scrollByPercentage
JVLP.write=JVL_write
JVLP.add=JVL_add

function JVLayer(name,parent){
  this.path=getLayerPath(name,parent)
  this.object=eval(this.path)
  this.css=eval(this.path+styleSwitch)
  this.id=name
}

function layer(name){
  var x=new JVLayer(name,false)
  return (x.path)?x:false
}

function layerFrom(name,parent){
  var x=new JVLayer(name,layer(parent).path)
  return (x.path)?x:false
}

function JVL_exists(){
  return (this.object)?true:false
}

function JVL_left(){
  var o=this.object
  if(isStandard) return o.offsetLeft-pageLeft
  if(isNC4) return o.pageX-pageLeft
}

function JVL_top(){
  var o=this.object
  if(isStandard) return o.offsetTop-pageTop
  if(isNC4) return o.pageY-pageTop
}

function JVL_width(){
  var o=this.object
  if(isOpera) return this.css.pixelWidth
  if(isStandard) return o.offsetWidth
  if(isNC4) return o.document.width
}

function JVL_height(){
  var o=this.object
  if(isOpera) return this.css.pixelHeight
  if(isStandard) return o.offsetHeight
  if(isNC4) return o.document.height
}

function JVL_zIndex(){
  return this.css.zIndex
}

function JVL_moveX(x){
  this.css.left=x+pageLeft
}

function JVL_moveY(y){
  this.css.top=y+pageTop
}

function JVL_move(x,y){
  this.moveX(x)
  this.moveY(y)
}

function JVL_moveZ(z){
  this.css.zIndex=z
}

function JVL_setVisibility(v){
  this.css.visibility=(v)?(isNC4?"show":"visible"):(isNC4?"hide":"hidden")
}

function JVL_show(){
  this.setVisibility(true)
}

function JVL_hide(){
  this.setVisibility(false)
}

function JVL_visible(){
  return (this.css.visibility.toLowerCase().indexOf("h")==0)?false:true
}

function JVL_setBgColor(color){
  if(isStandard){
  	this.css.backgroundColor=color
  }
  if(isNC4){
  	this.css.bgColor=color
  }
}

function JVL_clip(top, right, bottom, left){
  if(isIE || isNC6 || isMozilla){
    this.css.clip="rect("+top+"px "+right+"px "+bottom+"px "+left+"px)"
  }
  if(isNC4 || isOpera){
    this.css.clip.top=top
    this.css.clip.right=right
    this.css.clip.bottom=bottom
    this.css.clip.left=left
  }
}

function JVL_scroll(windowLeft,windowTop,windowWidth,windowHeight,scrollX,scrollY){
  if(scrollX<0)scrollX=0
  if(scrollY<0)scrollY=0
  if(scrollX>this.getWidth()-windowWidth) scrollY=this.getWidth()-windowWidth
  if(scrollY>this.getHeight()-windowHeight) scrollY=this.getHeight()-windowHeight
  var top=0
  var right=windowWidth
  var bottom=windowHeight
  var left=0
  left=left+scrollX
  right=right+scrollX
  top=top+scrollY
  bottom=bottom+scrollY
  this.move(windowLeft-scrollX,windowTop-scrollY)
  this.clip(top,right,bottom,left)
}

function JVL_scrollByOffset(windowLeft,windowTop,windowWidth,windowHeight,scrollX,scrollY){
  var X=-parseInt(this.css.left)+windowLeft+scrollX
  var Y=-parseInt(this.css.top)+windowTop+scrollY
  this.scroll(windowLeft,windowTop,windowWidth,windowHeight,X,Y)
}

function JVL_scrollByPercentage(windowLeft,windowTop,windowWidth,windowHeight,scrollX,scrollY){
  var X=(this.getWidth()-windowWidth)*scrollX/100
  var Y=(this.getHeight()-windowHeight)*scrollY/100
  this.scroll(windowLeft,windowTop,windowWidth,windowHeight,X,Y)
}

function JVL_write(str){
  if(isOpera)return // no en Opera
  var o=this.object
  if(isStandard){
    o.innerHTML=str
  }else
  if(isNC4){
    var a=o.document
    a.open()
    a.write(str)
    a.close()
  }
}

function JVL_add(str){
  if(isOpera)return // no en Opera
  var o=this.object
  if(isStandard){
    o.innerHTML+=str
  }else
  if(isNC4){
    var a=o.document
    a.write(str)
  }
}

// class "JVImage"

JVIP=JVImage.prototype

JVIP.isExist=JVI_exists
JVIP.getSrc=JVI_src
JVIP.setSrc=JVI_setSrc

function JVImage(name){
  this.path=getImagePath(name)
  this.object=eval(this.path)
}

function image(name){
  var x=new JVImage(name,false)
  return (x.path)?x:false
}

function imageFrom(name,layerName){
  var x=new JVImage(name,layer(layerName).path)
  return (x.path)?x:false
}

function JVI_exists(){
  return (this.object)?true:false
}

function JVI_src(){
  return this.object.src
}

function JVI_setSrc(file){
  this.object.src=file
}

function preloadImage(imageFile){
  imagePreloaderArray[imagePreloaderCount]=new Image()
  imagePreloaderArray[imagePreloaderCount++].src=imageFile
}

// init /para iniciar:

function initJVLayers(){
  isDOM=(document.getElementById)?true:false
  isNC4=(document.layers)?true:false
  isIE=(document.all && document.all.item)?true:false
  isOpera=isDOM && window.opera
  isNC6=isDOM && !isIE && !isOpera
  isMozilla=isNC6
  isNC=isNC4 || isNC6 || isMozilla || isOpera
  isStandard=isIE || isNC6 || isMozilla || isOpera
    
  if(!isDOM && !isNC4 && !isNC6 && !isIE && !isOpera && !isMozilla){
    JVLayers=false
    return JVLayers
  }

  pageLeft=0
  pageTop=0
  imagePreloaderCount=0
  imagePreloaderArray=new Array()

  imageRef="document.images[\""
  styleSwitch=".style"
  layerPostfix="\"]"

  if(isNC4){
    layerRef="document.layers[\""
    styleSwitch=""
  }

  if(isIE){
    layerRef="document.all[\""
  }

  if(isDOM){
    layerRef="document.getElementById(\""
    imageRef=layerRef
    layerPostfix="\")"
  }
  JVLayers=true
  return JVLayers
}

initJVLayers()
