
// FUNCTION TO CREATE NEW DYNLAYER OBJECT
function DynLayer(id,nestlayer1,nestlayer2) {  //parameters passed are all strings of layer IDs
	if (!browser.ns5 && !DynLayer.set) DynLayerInit()
	if (browser.ns) {
		if (browser.ns4) {
			if (!DynLayerTest(id,nestlayer1,nestlayer2)) return;  //test function does nothing for now
			switch(arguments.length){ //depending on the number of parameters passed, form reference path to new DynLayer
				case 1:
					this.css = eval("document."+id)
					break;
				case 2:
					this.css = eval("document."+nestlayer1+".document."+id)
					break;
				case 3:
					this.css = eval("document."+nestlayer2+".document."+nestlayer1+".document."+id)
					break;
				default:
					alert("Creating a new DynLayer has failed");
					break;
			}
			this.elmt = this.event = this.css;  //elmt is the same as css for NS4 - it's the actual layer element
			this.doc = this.css.document;
		} else if (browser.ns5) {
			this.elmt = document.getElementById(id);
			this.css = this.elmt.style;
			this.doc = 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 (browser.ie) {
		this.elmt = this.event = document.all[id];
		this.css = document.all[id].style;
		this.doc = document;
		this.x = this.elmt.offsetLeft;
		this.y = this.elmt.offsetTop;
		this.w = (browser.ie4)? this.css.pixelWidth : this.elmt.offsetWidth;
		this.h = (browser.ie4)? this.css.pixelHeight : this.elmt.offsetHeight;
	}
	this.slideActive = false;
	this.id = id;
	this.obj = id + "DynLayer";
	eval(this.obj + "=this");

	//alert("\nthis.id: " + this.id + "\nthis.x: " + this.x + "\nthis.y: " + this.y + "\nthis.w: " + this.w + "\nthis.h: " + this.h);
}


// STANDARD FUNCTIONS TO MOVE AND CHANGE VISIBILITY
function DynLayerMoveTo(x,y) {
	if (x!=null) {
		this.x = x;
		if (browser.ns) this.css.left = this.x;
		else this.css.pixelLeft = this.x;
	}
	if (y!=null) {
		this.y = y;
		if (browser.ns) this.css.top = this.y;
		else this.css.pixelTop = this.y;
	}
}
function DynLayerMoveByOld(x,y) {
	this.moveTo(this.x+x,this.y+y)
}
function DynLayerMoveBy(x,y) {
	if (x!=null) {
		this.x += x;
		if (browser.ns) this.css.left = this.x;
		else this.css.pixelLeft = this.x;
	}
	if (y!=null) {
		this.y += y;
		if (browser.ns) this.css.top = this.y;
		else this.css.pixelTop = this.y;
	}
}
function DynLayerShow() {
	this.css.visibility = (browser.ns4)? "show" : "visible"
}
function DynLayerHide() {
	this.css.visibility = (browser.ns4)? "hide" : "hidden"
}
DynLayer.prototype.moveTo = DynLayerMoveTo
DynLayer.prototype.moveBy = DynLayerMoveBy
DynLayer.prototype.show = DynLayerShow
DynLayer.prototype.hide = DynLayerHide
DynLayerTest = new Function('return true')





// INITIALIZATION FUNCTION TO CALL DYNLAYER OBJECT CREATION FUNCTION
function DynLayerInit() {

	if (!DynLayer.set && keyDiv=="ds"){DynLayer.set = true}else{return false;}
	if (browser.ns) {
		// ONE LAYER DEEP
		for (var i=0; i<document.layers.length; i++) {
			var div1ID = document.layers[i].name;
			if (div1ID.indexOf("Div") > 0) { eval(div1ID.substr(0,div1ID.indexOf("Div"))+' = new DynLayer("'+div1ID+'")'); }

			// TWO LAYERS DEEP
			for (var j=0; j<eval("document."+div1ID+".document.layers.length"); j++) {
				var div2ID = eval("document."+div1ID+".document.layers[j].name");
				if (div2ID.indexOf("Div") > 0) { eval(div2ID.substr(0,div2ID.indexOf("Div"))+' = new DynLayer("'+div2ID+'","'+div1ID+'")'); }

				// THREE LAYERS DEEP
				for (var k=0; k<eval("document."+div1ID+".document."+div2ID+".document.layers.length"); k++) {
					var div3ID = eval("document."+div1ID+".document."+div2ID+".document.layers[k].name");
					if (div3ID.indexOf("Div") > 0) { eval(div3ID.substr(0,div3ID.indexOf("Div"))+' = new DynLayer("'+div3ID+'","'+div2ID+'","'+div1ID+'")'); }
				}
			}
		}
	}
	else if (browser.ie) {
		for (var i=0; i<document.all.tags("DIV").length; i++) {
			var divname = document.all.tags("DIV")[i].id;
			if (divname.indexOf("Div") > 0) eval(divname.substr(0,divname.indexOf("Div"))+' = new DynLayer("'+divname+'")');
		}
	}
	return true;
}
DynLayer.set = false






// SLIDE METHODS
function DynLayerSlideTo(endx,endy,inc,speed,fn) {
	if (endx==null) endx = this.x
	if (endy==null) endy = this.y
	var distx = endx-this.x
	var disty = endy-this.y
	this.slideStart(endx,endy,distx,disty,inc,speed,fn)
}
function DynLayerSlideBy(distx,disty,inc,speed,fn) {
	var endx = this.x + distx
	var endy = this.y + disty
	this.slideStart(endx,endy,distx,disty,inc,speed,fn)
}
function DynLayerSlideStart(endx,endy,distx,disty,inc,speed,fn) {
	if (this.slideActive) return
	if (!inc) inc = 10
	if (!speed) speed = 20
	var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/inc
	if (num==0) return
	var dx = distx/num
	var dy = disty/num
	if (!fn) fn = null
	this.slideActive = true
	this.slide(dx,dy,endx,endy,num,1,speed,fn)
}
function DynLayerSlide(dx,dy,endx,endy,num,i,speed,fn) {
	if (!this.slideActive) return
	if (i++ < num) {
		this.moveBy(dx,dy)
		this.onSlide()
		if (this.slideActive) setTimeout(this.obj+".slide("+dx+","+dy+","+endx+","+endy+","+num+","+i+","+speed+",\""+fn+"\")",speed)
		else this.onSlideEnd()
	}
	else {
		this.slideActive = false
		this.moveTo(endx,endy)
		this.onSlide()
		this.onSlideEnd()
		eval(fn)
	}
}
function DynLayerSlideInit() {}
DynLayer.prototype.slideInit = DynLayerSlideInit
DynLayer.prototype.slideTo = DynLayerSlideTo
DynLayer.prototype.slideBy = DynLayerSlideBy
DynLayer.prototype.slideStart = DynLayerSlideStart
DynLayer.prototype.slide = DynLayerSlide
DynLayer.prototype.onSlide = new Function()
DynLayer.prototype.onSlideEnd = new Function()

// MY PERSONAL SLIDE METHODS
function DynlayerEventSlideObj(dx,dy,freq){
	if (this.slideActive && (this.y<=0)){
		this.x += dx;
		this.y += dy;
		this.css.left = this.x;
		this.css.top = this.y;
		setTimeout(this.obj+".eventSlideObj("+dx+","+dy+","+freq+")",freq);
	} else { }
}
function DynlayerNewSlide(dx,dy,time) {
	if (endx==null) endx = this.x
	if (endy==null) endy = this.y
}
function DynlayerScrollDown(dy,freq){
	if (this.y > -(this.h-130)){
		this.y -= dy;
		this.css.top = this.y;
		if (this.slideActive) setTimeout(this.obj+".scrollDown("+dy+","+freq+")",freq);
	}
}
function DynlayerScrollUp(dy,freq){
	if (this.y < 0){
		this.y += dy;
		this.css.top = this.y;
		if (this.slideActive) setTimeout(this.obj+".scrollUp("+dy+","+freq+")",freq);
	}
}

DynLayer.prototype.eventSlideObj = DynlayerEventSlideObj
DynLayer.prototype.scrollUp = DynlayerScrollUp
DynLayer.prototype.scrollDown = DynlayerScrollDown






// CLIP METHODS
function DynLayerClipInit(clipTop,clipRight,clipBottom,clipLeft) {
	if (browser.ie) {
		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.ns) {
		this.css.clip.top = t
		this.css.clip.right = r
		this.css.clip.bottom = b
		this.css.clip.left = l
	}
	else if (browser.ie) 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) var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px");
	if (which=="t") return (browser.ns)? this.css.clip.top : Number(clipv[0])
	if (which=="r") return (browser.ns)? this.css.clip.right : Number(clipv[1])
	if (which=="b") return (browser.ns)? this.css.clip.bottom : Number(clipv[2])
	if (which=="l") return (browser.ns)? this.css.clip.left : Number(clipv[3])
}
DynLayer.prototype.clipInit = DynLayerClipInit
DynLayer.prototype.clipTo = DynLayerClipTo
DynLayer.prototype.clipBy = DynLayerClipBy
DynLayer.prototype.clipValues = DynLayerClipValues





// FUNCTION TO WRITE CONTENT OF LAYER
function DynLayerWrite(html) {
	if (browser.ns) {
		this.doc.open()
		this.doc.write(html)
		this.doc.close()
	}
	else if (browser.ie) {
		this.event.innerHTML = html
	}
}
DynLayer.prototype.write = DynLayerWrite






// CSS FUNCTION TO WRITE OUT CSS CODE FOR A NEW LAYER
function css(id,left,top,width,height,color,vis,z,other) {
	if (id=="START") return '<STYLE TYPE="text/css">\n'
	else if (id=="END") return '</STYLE>'
	var str = (left!=null && top!=null)? '#'+id+' {position:absolute; left:'+left+'px; top:'+top+'px;' : '#'+id+' {position:relative;'
	if (arguments.length>=4 && width!=null) str += ' width:'+width+'px;'
	if (arguments.length>=5 && height!=null) {
		str += ' height:'+height+'px;'
		if (arguments.length<9 || other.indexOf('clip')==-1) str += ' clip:rect(0px '+width+'px '+height+'px 0px);'
	}
	if (arguments.length>=6 && color!=null) str += (browser.ns)? ' layer-background-color:'+color+';' : ' background-color:'+color+';'
	if (arguments.length>=7 && vis!=null) str += ' visibility:'+vis+';'
	if (arguments.length>=8 && z!=null) str += ' z-index:'+z+';'
	if (arguments.length==9 && other!=null) str += ' '+other
	str += '}\n'
	return str
}
function writeCSS(str,showAlert) {
	str = css('START')+str+css('END')
	document.write(str)
	if (showAlert) alert(str)
}
