
function TLayer(id,nestref,frame) {
	if(!is.ns5 && !TLayer.set && !frame) TLayerInit()
	this.frame = frame || self

	if(is.ns) {
		if(is.ns4) {
			if(!frame) {
				if(!nestref) var nestref = TLayer.nestRefArray[id]
				if(!TLayerTest(id,nestref)) return
				this.css = (nestref)? eval("document."+nestref+".document."+id) : document.layers[id]
			}
			else this.css = (nestref)? eval("frame.document."+nestref+".document."+id) : frame.document.layers[id]
			this.elm = this.event = this.css
			this.doc = this.css.document
		} else if(is.ns5) {
			this.elm = document.getElementById(id)
			this.css = this.elm.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(is.ie) {
		this.elm = this.event = this.frame.document.all[id]
		this.css = this.frame.document.all[id].style
		this.doc = document
		this.x = this.elm.offsetLeft
		this.y = this.elm.offsetTop
		this.w = (is.ie4)? this.css.pixelWidth : this.elm.offsetWidth
		this.h = (is.ie4)? this.css.pixelHeight : this.elm.offsetHeight
	}
	this.id = id
	this.nestref = nestref
	this.obj = id + "TLayer"
	eval(this.obj + "=this")
}

///////////////////////////////////////////////////////

var moveSlidePosArray = new Array(1,2,4,8,16,32,64,0);
var locCounter = 0;
var startX = 0;
var startY = 0;
var destX = 0;
var destY = 0;
var moveX = 1;
var moveY = 1;
var timerID;
var slideLayerRef = null;

function TLayerSlideTo( theDestX, theDestY ) {
	slideLayerRef = this;
	destX = theDestX;
	destY = theDestY;
	startX = slideLayerRef.x;
	startY = slideLayerRef.y;

	if(timerID){ clearInterval(timerID); }
	if(destX != 0) { destX = eval((-1)*destX); }
	if(destY != 0) { destY = eval((-1)*destY); }

	if(destX != startX || destY != startY){
		if( destX != startX ) {
			dirX = eval(destX - startX);
			if(dirX < 0){ moveX = -1; } else { moveX = 1; }
		} else {
			moveX = 0;
		}

		if( destY != startY ){
			dirY = eval(destY - startY);
			if(dirY < 0){ moveY = -1; } else { moveY = 1; }
		} else {
			moveY = 0;
		}

		timerID = setInterval("SlideStart()",40);
	}
}

function SlideStart(){
	nextLoc = moveSlidePosArray[locCounter];

	if(nextLoc == "0"){
		clearInterval(timerID);
		locCounter = 0;
		timerID = setInterval("SlideHandler()",40);
	} else {
		slideLayerRef.moveBy( moveX*nextLoc, moveY*nextLoc );
		locCounter++;
	}
}

function SlideHandler(){
	nowX = slideLayerRef.x;
	nowY = slideLayerRef.y;

	nowDirX = eval(destX - nowX);
	nowDirY = eval(destY - nowY);

	distanceX = Math.abs(nowDirX);
	distanceY = Math.abs(nowDirY);

	if(nowDirX < 0){ moveX = -1; } else { moveX = 1; }
	if(nowDirY < 0){ moveY = -1; } else { moveY = 1; }

	if(distanceX > 300) {
		ax = 300*moveX;
	} else if(distanceX > 200) {
		ax = 200*moveX;
	} else if(distanceX > 150) {
		ax = 150*moveX;
	} else if(distanceX > 100) {
		ax = 100*moveX;
	} else if(distanceX > 50) {
		ax = 50*moveX;
	} else if(distanceX > 20) {
		ax = 20*moveX;
	} else {
		ax = 0;
	}

	if(distanceY > 300) {
		ay = 300*moveY;
	} else if(distanceY > 200) {
		ay = 200*moveY;
	} else if(distanceY > 150) {
		ay = 150*moveY;
	} else if(distanceY > 100) {
		ay = 100*moveY;
	} else if(distanceY > 50) {
		ay = 50*moveY;
	} else if(distanceY > 20) {
		ay = 20*moveY;
	} else {
		ay = 0;
	}

	if( ax == 0 && ay == 0 ) {
		slideLayerRef.moveTo( destX, destY );
		clearInterval(timerID);
	} else {
		slideLayerRef.moveBy( ax, ay );
	}
}


/////////////////////////////////////////////////////

TLayer.prototype.slideTo = TLayerSlideTo;

///////////////////////////////////////////////////////

function TLayerMoveTo(x,y) {
	if(x!=null) {
		this.x = x
		if(is.ns) this.css.left = this.x
		else this.css.pixelLeft = this.x
	}

	if(y!=null) {
		this.y = y
		if(is.ns) this.css.top = this.y
		else this.css.pixelTop = this.y
	}
}

function TLayerMoveBy(x,y) {
	this.moveTo(this.x+x,this.y+y)
}

function TLayerShow() {
	this.css.visibility = (is.ns4)? "show" : "visible"
}

function TLayerHide() {
	this.css.visibility = (is.ns4)? "hide" : "hidden"
}

TLayer.prototype.moveTo = TLayerMoveTo
TLayer.prototype.moveBy = TLayerMoveBy
TLayer.prototype.show = TLayerShow
TLayer.prototype.hide = TLayerHide
TLayerTest = new Function('return true')

function TLayerInit(nestref) {
	if(!TLayer.set) TLayer.set = true
	if(is.ns) {
		if(nestref) ref = eval('document.'+nestref+'.document')
		else {nestref = ''; ref = document;}
		for (var i=0; i<ref.layers.length; i++) {
			var divname = ref.layers[i].name
			TLayer.nestRefArray[divname] = nestref
			var index = divname.indexOf("Div")
			if(index > 0) {
				eval(divname.substr(0,index)+' = new TLayer("'+divname+'","'+nestref+'")')
			}
			if(ref.layers[i].document.layers.length > 0) {
				TLayer.refArray[TLayer.refArray.length] = (nestref=='')? ref.layers[i].name : nestref+'.document.'+ref.layers[i].name
			}
		}
		if(TLayer.refArray.i < TLayer.refArray.length) {
			TLayerInit(TLayer.refArray[TLayer.refArray.i++])
		}
	} else if(is.ie) {
		for (var i=0; i<document.all.tags("DIV").length; i++) {
			var divname = document.all.tags("DIV")[i].id
			var index = divname.indexOf("Div")
			if(index > 0) {
				eval(divname.substr(0,index)+' = new TLayer("'+divname+'")')
			}
		}
	}
	return true
}

TLayer.nestRefArray = new Array()
TLayer.refArray = new Array()
TLayer.refArray.i = 0
TLayer.set = false

function TLayerClipInit(clipTop,clipRight,clipBottom,clipLeft) {
	if(is.ie) {
		if(arguments.length==4) this.clipTo(clipTop,clipRight,clipBottom,clipLeft)
		else if(is.ie4) this.clipTo(0,this.css.pixelWidth,this.css.pixelHeight,0)
	}
}

function TLayerClipTo(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(is.ns) {
		this.css.clip.top = t
		this.css.clip.right = r
		this.css.clip.bottom = b
		this.css.clip.left = l
	}
	else if(is.ie) this.css.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)"
}

function TLayerClipBy(t,r,b,l) {
	this.clipTo(this.clipValues('t')+t,this.clipValues('r')+r,this.clipValues('b')+b,this.clipValues('l')+l)
}

function TLayerClipValues(which) {
	if(is.ie) var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px")
	if(which=="t") return (is.ns)? this.css.clip.top : Number(clipv[0])
	if(which=="r") return (is.ns)? this.css.clip.right : Number(clipv[1])
	if(which=="b") return (is.ns)? this.css.clip.bottom : Number(clipv[2])
	if(which=="l") return (is.ns)? this.css.clip.left : Number(clipv[3])
}

TLayer.prototype.clipInit = TLayerClipInit
TLayer.prototype.clipTo = TLayerClipTo
TLayer.prototype.clipBy = TLayerClipBy
TLayer.prototype.clipValues = TLayerClipValues

function TLayerWrite(html) {
	if(is.ns) {
		this.doc.open()
		this.doc.write(html)
		this.doc.close()
	}
	else if(is.ie) {
		this.event.innerHTML = html
	}
}

TLayer.prototype.write = TLayerWrite

function BrowserCheck() {
	var b = navigator.appName
	if(b=="Netscape") this.b = "ns"
	else if(b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.version = navigator.appVersion
	this.v = parseInt(this.version)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie5 = (this.version.indexOf('MSIE 5')>0)
	this.min = (this.ns||this.ie)
}
is = new BrowserCheck()

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 += (is.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)
}
