// Greg's layers support library
 var ie4=document.all
 var ns4=document.layers
 var ns6=document.getElementById & !document.all
// if(ns6) ns4=0

 // Define layers in document, returns new layer
 var layernum = 0 // static var
 function CreateLayer(img,x,y,w,h){
	var layer
	var tmp
	var lnk='<a href="#" onClick="doPic('+layernum+')"><img name="'+layernum+'" src="'+img+'" width='+w+' height='+h+' border=0></a>'
	if(ns4){
		tmp='<LAYER NAME="x'+layernum+'" LEFT='+x+' TOP='+y+' WIDTH='+w+' HEIGHT='+h+' VISIBILITY=SHOW CLIP="0,0,'+w+','+h+'"'
		if(img!="") document.write(tmp+'>'+lnk+'</LAYER>')
		else document.write(tmp+' bgcolor="#000000"></LAYER>')
		layer = document.layers[layernum]
		layer.imgnum=0
	}else if(ie4 || ns6){
		tmp='<div id="x'+layernum+'" style="position:absolute; left:'+x+'px; top:'+y+'px; width:'+w+'px; height:'+h+'px; visibility:visible'
		if(img!="") document.write(tmp+'">'+lnk+'</div>')
		else document.write(tmp+'; background-color:#000000; font-size:1px"></div>')
		layer = eval('document.all.x' + layernum + '.style')
		layer.imgnum=document.images.length-1
	}
	layer.x = x
	layer.y = y
	layernum++
	return layer
 }

 // set a layer's image (IMG within layer, not BG image)
 function SetLayerImage(layer,img){
	if (ns4) layer.document.images[0].src=img
	else document.images[layer.imgnum].src=img
 }

 // set a layer's image (IMG within layer, not BG image)
 function SetLayerImageLWH(layer,l,w,h){
	if (ns4){
		layer.document.images[0].clip.left=l
		layer.document.images[0].clip.right=l+w
	}else{
		document.images[layer.imgnum].style.clip="rect("+0+" "+(l+w)+" "+h+" "+l+")"
	}
 }

 // set the layer's width, height
 function SetLayerSize(layer, w, h){
	if(h<0) h = 0 // IE blows on negs!
	if(ns4){
		layer.clip.width=w
		layer.clip.height=h
	}else{
		layer.width = w
		layer.height = h
	}
 }

 // set the layer's visibility (true or false)
 function ShowLayer(layer, on){
	layer.visibility = on? ((ns4) ? true : "inherit"):((ns4) ? false : "hidden")
	layer.vis=on
 }

 // get document width
 function WinWidth(){
	if (ns4)  return window.innerWidth+2
	else return document.body.clientWidth-2
 }
 // get document height
 function WinHeight(){
	if (ns4)   return window.innerHeight+2
	else return document.body.clientHeight-2
 }

function SetLayerPos(layer,x,y){
	if(ns4){
		layer.top = y+ window.pageYOffset	// set new ball position
		layer.left= x+window.pageXOffset
	}else{
		layer.top = y+ document.body.scrollTop	// set new ball position
		layer.left= x+document.body.scrollLeft
	}
	layer.y = y		// set new ball position
	layer.x = x
}

// update X/Y position of layer
function UpdateLayer(layer)
{
	var x,y

	// limit the speeds
	if(layer.dy> 30) layer.dy= 30
	if(layer.dy<-25) layer.dy=-25
	if(layer.dx> 18) layer.dx= 18
	if(layer.dx<-18) layer.dx=-18

	y = layer.y + layer.dy
	x = layer.x + layer.dx

	// Ensure layers stay within window. handles window resize too
	if(x < 0){ x = 1; layer.dx = Math.abs(layer.dx)}
	if(y+h  > WinHeight()) y = WinHeight()-h
	if(x+w-2 > WinWidth()){ x = WinWidth()-w-2; layer.dx = -Math.abs(layer.dx)}
	SetLayerPos(layer,x,y)
}
