
var browser = new BrowserCheck()

// create a simple layer object
function Layer(
	id // layer's id as declared in the div tag
	,nestref //  parent layer reference - for ns4
	)
{
	this.id = id
	this.obj = id + 'Object'
	eval(this.obj + '=this')
	if (document.layers) {
		this.elm = nestref? eval('document.' + nestref + '.document.' + id): document.layers[id]
		this.css = this.elm
		this.doc = this.elm.document
		this.x = this.css.left
		this.y = this.css.top
		this.w = this.css.clip.width
		this.h = this.css.clip.height
	}
	else if (document.all || document.getElementById) {
		this.elm = document.all? document.all[id]: document.getElementById(id)
		this.css = this.elm.style
		this.doc = document
		this.x = this.elm.offsetLeft
		this.y = this.elm.offsetTop
		this.w = (browser.ie4)? this.css.pixelWidth: this.elm.offsetWidth
		this.h = (browser.ie4)? this.css.pixelHeight: this.elm.offsetHeight
	}
	if (browser.ns61)
	{ // get nested divs - unfortuantely this way nested divs are taken recursively
		this.layers = new Array()
		var nestedDivs = this.elm.getElementsByTagName('DIV')
		for (var i = 0; i < nestedDivs.length; i++)
				this.layers[i] = new Layer(nestedDivs.item(i).id)
	}
	this.event = this.elm

	if (browser.ns4) {
		this.event.captureEvents(Event.MOUSEDOWN)
		this.event.captureEvents(Event.MOUSEUP)
		this.event.captureEvents(Event.MOUSEMOVE)
		// the onclick is a property of the layers' document
		this.event.onclick = null
		this.event.watch('onclick',
			function(property,oldval,newval)
			{
				this.document.onclick = newval
			}
		)
	}
	this.opacity = 100
}

// set layer's visibility
function LayerShow()
{
	this.css.visibility = browser.ns4? 'show': 'visible'
}
function LayerHide()
{
	this.css.visibility = browser.ns4? 'hide': 'hidden'
}
Layer.prototype.hide = LayerHide
Layer.prototype.show = LayerShow

// check layer visibility
function LayerIsVisible()
{
	return this.css.visibility == (browser.ns4? 'show': 'visible')
}
Layer.prototype.isVisible = LayerIsVisible

// set layer's position
function LayerMoveTo(x,y)
{
	if (x != null) {
		this.x = x
		if (browser.ns4) this.css.left = this.x
		else if (browser.ie) this.css.pixelLeft = this.x
		else if (browser.ns && browser.version >= 5) this.css.left = Math.floor(this.x) + 'px'
	}
	if (y != null) {
		this.y = y
		if (browser.ns4) this.css.top = this.y
		else if (browser.ie) this.css.pixelTop = this.y
		else if (browser.ns && browser.version >= 5) this.css.top = Math.floor(this.y) + 'px'
	}
}
function LayerMoveBy(x,y)
{
	this.moveTo(this.x + x,this.y + y)
}
Layer.prototype.moveTo = LayerMoveTo
Layer.prototype.moveBy = LayerMoveBy

// write into layer
function LayerWrite(html)
{
	if (browser.ns4) {
		this.doc.open()
		this.doc.write(html)
		this.doc.close()
	}
	else
		this.elm.innerHTML = html
}
Layer.prototype.write = LayerWrite

// set layer's event handler
function LayerSetEventHandler(eventName,handler)
{
	// check if handler is a string to be evaluated or a function object
	var func = typeof(handler) == 'function'? handler: new Function(handler)
	switch(eventName) {
	case 'onclick':
		browser.ns4? this.doc.onclick = func: this.event.onclick = func
		break
	case 'onmouseout':
		this.event.onmouseout = func
		break
	case 'onmouseover':
		this.event.onmouseover =  func
		break
	case 'onmousedown':
		if (browser.ns4) this.elm.captureEvents(Event.MOUSEDOWN)
		this.event.onmousedown = func
		break
	case 'onmouseup':
		if (browser.ns4) this.elm.captureEvents(Event.MOUSEUP)
		this.event.onmouseup = func
		break
	case 'onmousemove':
		if (browser.ns4) this.elm.captureEvents(Event.MOUSEMOVE)
		this.event.onmousemove = func
		break
	default: break
	}
	return this
}
Layer.prototype.setEventHandler = LayerSetEventHandler

// set layer's bg
function LayerSetBg(color)
{
	if (browser.ns4)
		this.css.bgColor = color
	else
		this.css.backgroundColor = color
}
Layer.prototype.setbg = LayerSetBg

// change layer's image's source
function LayerChangeImage(imgName,imgObj)
{
	this.doc.images[imgName].src = imgObj.src
}
Layer.prototype.changeImage = LayerChangeImage

// set layer's opacity in percentage not ns4 compatible
function LayerSetOpacity(prcnt)
{
	if (browser.ie && browser.version >= 5) {
		if (this.css.filter)
			this.css.filter = this.css.filter.replace('alpha(opacity=' + this.opacity + ')','alpha(opacity=' + prcnt + ')')
		else
			this.css.filter = 'alpha(opacity=' + prcnt + ')'
	}
	else if (browser.ns && browser.version >= 5)
		this.css.MozOpacity = prcnt/100
	else if (prcnt < 50)
		this.hide()
	else
		this.show()
	this.opacity = prcnt
}
Layer.prototype.setOpacity = LayerSetOpacity

// set layer's clip values
// init for ie
function DynLayerClipInit(clipTop,clipRight,clipBottom,clipLeft)
{
	if (browser.ie || browser.ns5) {
		if (arguments.length == 4) this.clipTo(clipTop,clipRight,clipBottom,clipLeft)
		else if (browser.ie4) this.clipTo(0,this.css.pixelWidth,this.css.pixelHeight,0)
	}
}
function DynLayerClipTo(t,r,b,l)
{
	if (t == null) t = this.clipValues('t')
	if (r == null) r = this.clipValues('r')
	if (b == null) b = this.clipValues('b')
	if (l == null) l = this.clipValues('l')
	if (browser.ns4) {
		this.css.clip.top = t
		this.css.clip.right = r
		this.css.clip.bottom = b
		this.css.clip.left = l
	}
	else if (browser.ie || browser.ns5) this.css.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)"
}
function DynLayerClipBy(t,r,b,l)
{
	this.clipTo(this.clipValues('t')+t,this.clipValues('r')+r,this.clipValues('b')+b,this.clipValues('l')+l)
}
function DynLayerClipValues(which)
{
	if (browser.ie || browser.ns5)
		var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px")
	if (which == "t") return (browser.ns4)? this.css.clip.top : Number(clipv[0])
	if (which == "r") return (browser.ns4)? this.css.clip.right : Number(clipv[1])
	if (which == "b") return (browser.ns4)? this.css.clip.bottom : Number(clipv[2])
	if (which == "l") return (browser.ns4)? this.css.clip.left : Number(clipv[3])
}
Layer.prototype.clipInit = DynLayerClipInit
Layer.prototype.clipTo = DynLayerClipTo
Layer.prototype.clipBy = DynLayerClipBy
Layer.prototype.clipValues = DynLayerClipValues

// wipe effect method
function DynLayerWipeInit(clipTop,clipRight,clipBottom,clipLeft)
{
	if (arguments.length==4) this.clipInit(clipTop,clipRight,clipBottom,clipLeft)
	else this.clipInit()
}
function DynLayerWipeTo(
	endt,endr,endb,endl // top, right, bottom, left final values
	,num		// number of iterations
	,timeval	// time interval for timeout - 30 by default
	,fn			// function string to be evaluated at wipe end
	)
{
	var distt = (endt != null)? endt - this.clipValues('t'): 0
	var distr = (endr != null)? endr - this.clipValues('r'): 0
	var distb = (endb != null)? endb - this.clipValues('b'): 0
	var distl = (endl != null)? endl - this.clipValues('l'): 0
	if (timeval == null) timeval = 30
	this.wipeStart(distt,distr,distb,distl,endt,endr,endb,endl,num,timeval,fn)
}
function DynLayerWipeBy(distt,distr,distb,distl,num,speed,fn)
{
	this.wipeStart(distt,distr,distb,distl,distt+this.clipValues('t'),distr+this.clipValues('r'),distb+this.clipValues('b'),distl+this.clipValues('l'),num,speed,fn)
}
function DynLayerWipeStart(
	distt,distr,distb,distl // top, right, bottom, left distances
	,endt,endr,endb,endl	// -- " -- final values
	,num		// number of iterations
	,timeval	// time interval for timeout
	,fn			// function string to be evaluated at wipe end
	)
{
	if (this.wipeActive) return
	this.wipeActive = true
	this.timeoutId = 0
	this.iter = 0
	if (!fn) fn = null
	this.wipe(distt/num,distr/num,distb/num,distl/num,endt,endr,endb,endl,this.clipValues('t'),this.clipValues('r'),this.clipValues('b'),this.clipValues('l'),num,timeval,fn)
}
function DynLayerWipe(dt,dr,db,dl,endt,endr,endb,endl,st,sr,sb,sl,num,timeval,fn)
{
	if (!this.wipeActive) return
	var i
	if (this.iter++ < num) {
		i = this.iter
		if (browser.ns61) { // hide and show nested divs in each iteration for NS6.1 (NS6.2?)
			for(var j in this.layers) {
				this.layers[j].hide()
				this.layers[j].show()
			}
		}
		this.clipTo(st + i*dt, sr + i*dr, sb + i*db,sl + i*dl)
		this.timeoutId = setTimeout(this.obj+".wipe("+dt+","+dr+","+db+","+dl+","+endt+","+endr+","+endb+","+endl+","+st+","+sr+","+sb+","+sl+","+num+","+timeval+",\""+fn+"\")",timeval)
	}
	else {
		this.wipeActive = false
		this.iter = 0
		this.clipTo(endt,endr,endb,endl)
		eval(fn)
	}
}
function LayerWipeStop()
{
	if (!this.wipeActive) return
	clearTimeout(this.timeoutId)
	this.wipeActive = false
}
function LayerWipeContinue(
	endt,endr,endb,endl	// -- " -- final values
	,num		// number of iterations
	,timeval	// time interval for timeout
	,fn			// function string to be evaluated at wipe end
	)
{
	var distt = (endt != null)? endt - this.clipValues('t'): 0
	var distr = (endr != null)? endr - this.clipValues('r'): 0
	var distb = (endb != null)? endb - this.clipValues('b'): 0
	var distl = (endl != null)? endl - this.clipValues('l'): 0
	if (timeval == null) timeval = 30
	if (this.wipeActive) return
	this.wipeActive = true
	this.timeoutId = 0
	if (!fn) fn = null
	this.wipe(distt/num,distr/num,distb/num,distl/num,endt,endr,endb,endl,this.clipValues('t'),this.clipValues('r'),this.clipValues('b'),this.clipValues('l'),num,timeval,fn)
}
Layer.prototype.wipeInit = DynLayerWipeInit
Layer.prototype.wipeTo = DynLayerWipeTo
Layer.prototype.wipeBy = DynLayerWipeBy
Layer.prototype.wipeStart = DynLayerWipeStart
Layer.prototype.wipe = DynLayerWipe
Layer.prototype.wipeStop = LayerWipeStop
Layer.prototype.wipeContinue = LayerWipeContinue

function LayerFadeTo(
	prcnt		// opacity percentage - integer 0-100
	,num		// number of iterations
	,timeval	// time interval for timeout
	,fn			// function string to be evaluated at wipe end
	)
{
	if (LayerFadeTo.arguments.length < 2 || isNaN(prcnt) || isNaN(num) || prcnt < 0 || prcnt > 100)
		return
	var diff = prcnt - this.opacity
	if (timeval == null) timeval = 30
	this.fadeStart(diff,num,timeval,fn)
}
function LayerFadeBy(diff,speed,timeval,fn)
{
	this.fadeStart(diff,num,timeval,fn)
}
function LayerFadeStart(
	diff 		// opacity difference
	,num		// number of iterations
	,timeval	// time interval for timeout
	,fn			// function string to be evaluated at wipe end
	)
{
	if (this.fadeActive) return
	this.fadeActive = true
	this.timeoutId = 0
	this.iter = 0
	if (!fn) fn = null
	this.fade(diff/num,this.opacity+diff,this.opacity,num,timeval,fn)
}
function LayerFade(dopac,endopac,stopac,num,timeval,fn)
{
	if (!this.fadeActive) return
	if (this.iter++ < num) {
		this.setOpacity(stopac + this.iter*dopac)
		this.timeoutId = setTimeout(this.obj + '.fade(' + dopac + ',' + endopac + ',' + stopac + ',' + num + ',' + timeval + ',"' + fn + '")',timeval)
	}
	else {
		this.fadeActive = false
		this.iter = 0
		this.setOpacity(endopac)
		eval(fn)
	}
}
function LayerFadeStop()
{
	if (!this.fadeActive) return
	clearTimeout(this.timeoutId)
	this.fadeActive = false
}
function LayerFadeContinue(
	endt,endr,endb,endl	// -- " -- final values
	,num		// number of iterations
	,timeval	// time interval for timeout
	,fn			// function string to be evaluated at wipe end
	)
{
	if (LayerFadeTo.arguments.length < 2 || isNaN(prcnt) || isNaN(num) || prcnt < 0 || prcnt > 100)
		return
	var diff = prcnt - this.opacity
	if (timeval == null) timeval = 30
	if (this.fadeActive) return
	this.fadeActive = true
	this.timeoutId = 0
	if (!fn) fn = null
	this.fade(diff/num,this.opacity+diff,this.opacity,num,timeval,fn)
}
Layer.prototype.fadeTo = LayerFadeTo
Layer.prototype.fadeBy = LayerFadeBy
Layer.prototype.fadeStart = LayerFadeStart
Layer.prototype.fade = LayerFade
Layer.prototype.fadeStop = LayerFadeStop
Layer.prototype.fadeContinue = LayerFadeContinue
