/*******************************************************
 * This file has the functions required to run an
 * invisible layer useful for transporting code from
 * one file to another without reloading the calling page
 *******************************************************/

__INIT = IE = NS = ver4 = zmax = 0
__registered_Slide = []
__registered_Grow = []
__registered_Loading = []
__dynloaders = [];

function __INIT_Proc() {
	if (!__INIT) {
		IE = document.all ? 1 : 0
		NS = NS4 = document.layers ? 1 : 0
		NS6 = !document.all && document.getElementById != null ? 1 : 0
		IE4 = navigator.appVersion.indexOf("MSIE 5") == -1
		MAC = navigator.appVersion.indexOf("Macintosh") != -1
		zmax = 999
		document.onmouseup = document.onmousedown = document.onmousemove = null
		__INIT=true
	}
}

SimpleLayer = function(path){
	if (path) this.init(path)
}
SimpleLayer.prototype.init = function(path){
	__INIT_Proc()
	this.obj = getObject(path)
	if (IE || NS6) {
		this.x = this.obj.offsetLeft
		this.y = this.obj.offsetTop
		this.w = this.obj.offsetWidth
		this.h = this.obj.offsetHeight
		this.z = this.obj.style.zIndex
	}
	else if (NS4) {
		this.x = this.obj.left
		this.y = this.obj.top
		this.w = this.obj.clip.width
		this.h = this.obj.clip.height
		this.z = this.obj.zIndex
	}
	this.id = this.obj.id
	this.mDownh = this.mUph = this.mMoveh = this.mOverh = this.mOuth = null
	this.path = path
	this.timeout = null
	this.iframe = null
	this.afterSlide = null
	this.clip(0,this.w,0,this.h)
	this.obj.instance = this
}
SimpleLayer.prototype.moveTo = function(x,y){
	this.x = x
	this.y = y
	if (NS) { this.obj.left = x; this.obj.top = y; }
	else if (NS6) { this.obj.style.left = x; this.obj.style.top = y; }
	else { this.obj.style.pixelLeft = x; this.obj.style.pixelTop = y; }
}
SimpleLayer.prototype.moveBy = function(x,y){
	this.moveTo(this.x+x,this.y+y)
}
SimpleLayer.prototype.resize = function(w,h){
	this.w = w
	this.h = h
	this.clip(0,w,0,h)
	if (NS) { this.obj.clip.width = w; this.obj.clip.height = h; }
	else { this.obj.style.pixelWidth = w; this.obj.style.pixelHeight = h; }
}
SimpleLayer.prototype.clip = function(l,r,t,b){ // NS6 Test
	if (NS) {
		this.obj.clip.left = l
		this.obj.clip.right = r
		this.obj.clip.top = t
		this.obj.clip.bottom = b
	}
	else this.obj.style.clip = "rect("+t+"px, "+r+"px, "+b+"px, "+l+"px)"
}
SimpleLayer.prototype.isVisible = function(){
	return NS ? this.obj.visibility == "show": this.obj.style.visibility == "visible" || !this.obj.style.visibility
}
SimpleLayer.prototype.show = function(){
	NS ? this.obj.visibility = "show" : this.obj.style.visibility = "visible"
}
SimpleLayer.prototype.hide = function(){
	NS4 ? this.obj.visibility = "hide" : this.obj.style.visibility = "hidden"
}
SimpleLayer.prototype.bringTo = function(i){
	NS ? this.obj.zIndex=i : this.obj.style.zIndex=i
}
SimpleLayer.prototype.bringToFront = function(){
	this.bringTo(zmax++)
}
SimpleLayer.prototype.bringToBack = function(){
	this.bringTo(1)
}
SimpleLayer.prototype.overflow = function(w){ // NS6 Test
	NS? this.overflow = w : this.obj.style.overflow = w
}
SimpleLayer.prototype.slideTo = function(dx, dy, step, inc){
	if (__registered_Slide[this.path] == null) __registered_Slide[this.path] = this

	var ddx = dx ? Math.max(1, Math.floor(dx/step)) : 0
	var ddy = dy ? Math.max(1, Math.floor(dy/step)) : 0
	inc = inc || 1
	this.moveBy(ddx,-ddy)
	dx -= ddx
	dy -= ddy
	if (step > inc) step -= inc

	if (dx || dy) this.timeout = setTimeout("__registered_Slide['"+this.path+"'].slideTo("+dx+","+dy+","+step+","+inc+")",10)
	else {
		__registered_Slide[this.path] = null
		this.timeout=null
		if (this.afterSlide!=null) this.afterSlide()
	}
}
SimpleLayer.prototype.growTo = function(dw, dh, step, inc){ // NS6 Test
	if (__registered_Grow[this.path] == null) __registered_Grow[this.path] = this

	var ddw = dw ? Math.max(1, Math.floor(dw/step)) : 0
	var ddh = dh ? Math.max(1, Math.floor(dh/step)) : 0
	inc = inc || 1
	this.resize(this.w+ddw, this.h+ddh)
	dw -= ddw
	dh -= ddh
	if (step > inc) step -= inc

	if (dw || dh) this.timeout = setTimeout("__registered_Grow['"+this.path+"'].growTo("+dw+","+dh+","+step+","+inc+")",10)
	else {
		__registered_Grow[this.path] = null
		this.timeout=null
	}
}
SimpleLayer.prototype.load = function(url){
	if (NS) this.obj.load(url,this.w)
	else {
		if (!this.iframe) {
			this.obj.innerHTML = "<div id=SOURCE style=\"visibility:hidden\"><IFRAME id=IF_" + this.id + " style=\"height:0px; width:0px; visibility:hidden\"></IFRAME></div><div id=DATA></div>"
			this.iframe = IE ? this.obj.all("IF_" + this.id) : getObject("IF_" + this.id)
			if (MAC) this.iframe.focus()
		}
		this.iframe.src=url
		__registered_Loading["IF_" + this.id] = this
	}
}
SimpleLayer.prototype.loaded = function(name){
	if (NS) return
	for (var i=document.frames.length; --i>=0; )
	{
		if (document.frames(i).loaded == 1) {
			var fname = document.frames(i).name;
			var t = document.frames(fname).document.body.innerHTML
			var t_obj = __registered_Loading[fname]
			if (t_obj) t_obj.obj.all("DATA").innerHTML = t
			__registered_Loading[fname] = null
		}
	}
}
SimpleLayer.prototype.bgColor = function(c){
	if (NS) this.obj.document.bgColor = c
	else this.obj.style.backgroundColor = c
}
SimpleLayer.prototype.write = function(s){
	if (NS) { this.obj.document.open(); this.obj.document.write(s); this.obj.document.close(); }
	else this.obj.innerHTML = s
}
SimpleLayer.prototype.onMouseDown = function(h){
	if (NS) {
		if (h) this.obj.captureEvents(Event.MOUSEDOWN); else this.obj.releaseEvents(Event.MOUSEDOWN);
	}
	this.obj.onmousedown = h == null ? null : this.mouseDownHandler
	this.mDownh = h
}
SimpleLayer.prototype.onMouseUp = function(h){
	if (NS) {
		if (h) this.obj.captureEvents(Event.MOUSEUP); else this.obj.releaseEvents(Event.MOUSEUP);
	}
	this.obj.onmouseup = h == null ? null : this.mouseUpHandler
	this.mUph = h
}
SimpleLayer.prototype.onMouseMove = function(h){
	if (NS) {
		if (h) this.obj.captureEvents(Event.MOUSEMOVE); else this.obj.releaseEvents(Event.MOUSEMOVE);
	}
	this.obj.onmousemove = h == null ? null : this.mouseMoveHandler
	this.mMoveh = h
}
SimpleLayer.prototype.onMouseOver = function(h){
	this.obj.onmouseover = h == null ? null : this.mouseOverHandler
	this.mOverh = h
}
SimpleLayer.prototype.onMouseOut = function(h){
	this.obj.onmouseout = h == null ? null : this.mouseOutHandler
	this.mOuth = h
}
SimpleLayer.prototype.mouseDownHandler = function(e){
	this.instance.mDownh(e)
}
SimpleLayer.prototype.mouseUpHandler = function(e){
	this.instance.mUph(e)
}
SimpleLayer.prototype.mouseMoveHandler = function(e){
	this.instance.mMoveh(e)
}
SimpleLayer.prototype.mouseOverHandler = function(e){
	if (IE) {
		var fromIsMine = false
		for (var p=event.fromElement; p!=null; p=p.parentElement)
			if (p == this) { fromIsMine = true; break; }
		if (fromIsMine) return
	}
	this.instance.mOverh(e)
}
SimpleLayer.prototype.mouseOutHandler = function(e){
	if (IE) {
		var srcIsMine = false
		for (var p=event.srcElement; p!=null; p=p.parentElement)
			if (p == this) { srcIsMine = true; break; }

		var toIsMine = false
		for (var p=event.toElement; p!=null; p=p.parentElement)
			if (p == this) { toIsMine = true; break; }

		if (srcIsMine && toIsMine) return
	}
	this.instance.mOuth(e)
}
function getObjectPath(name, lvlUp) { // TODO NS6 // Dunno how to handle nested object
	__INIT_Proc();
	var path=null;
	var names = name.split(".");
	var lvl = names.length - (lvlUp = lvlUp || 0)
	if (IE)
		for (var i=0; i<lvl; i++)
			if (!i)
				path = "document.all['" + names[i] + "']"
			else
				path += ".all." + names[i];
	else if (NS6)
	{
		path = "document.getElementById('" + name + "')"
		/*for (var i=0; i<lvl; i++)
			if (!i)
				path = "document.getElementById('" + names[i] + "')"
			else
				path += ".document.getElementById('" + names[i] + "')"*/
	}
	else if (NS)
		for (var i=0; i<lvl; i++)
			if (!i)
				path = "document.layers['" + names[i] + "']"
			else
				path += ".document." + names[i];
	return !path ? null : path
}
function getObject(name, lvlUp) {
	return eval(getObjectPath(name, lvlUp));
}
function writeLayer(name, clazz) { // TODO NS6 // Dunno how to handle nested object
	__INIT_Proc();
	var parent = getObject(name, 1) || document.body
	var i = name.lastIndexOf(".")
	var id = i == -1 ? name : name.substr(i+1)
	var obj = null
	if (NS)
	{
		var path = getObjectPath(name, 1)
		if (path)
			obj = eval(path+".document."+id+" = new Layer(100, "+path+")")
		else
		{
			obj = document.layers[id] = new Layer(100)
			eval("document."+id+" = obj")
		}
		obj.name = id
		obj.top = obj.left = 0
		obj.clip.height = 1
		obj.visibility='inherit'
	}
	else // TODO NS6
	{
		var el = document.createElement("<div id=" + id + " style='position:absolute' class='" + (clazz || "") + "'>");
		if (NS6) el.setAttribute("id", name)
		parent.appendChild( el );
	}
}
function dynLoad(url, lbl)
{
	lbl = lbl || "nil"
	if (!__dynloaders[lbl])
	{
		writeLayer(lbl);
		__dynloaders[lbl] = new SimpleLayer(lbl);
	}
	__dynloaders[lbl].load(url);
}
IMAGE_preload=new Array();
function preloadImage(src) {
	var img = new Image();
	IMAGE_preload[IMAGE_preload.length] = img;
	img.src = src;
}
