/*	see: www.cs.sun.ac.za/~tim/dhtml for help on how to implement
	this code in your own webpages.

	----------------------------------------------------
	This JavaScript code uses techniques found at
	The Dynamic Duo - http://www.dansteinman.com/dynduo/
	----------------------------------------------------

*/

var layerCount = 0;				//number of layers in this container
var Layers = new Array();		//the layers
var layerIDs = new Array();		//the IDs of the layers used to access the
								//layers in the "Layer" array

var menuItemNum = 0;			//used by tpMenus to indicate the index of the option in the current menu
var layersLoadInfo =new Array();//Array with info of layers to be loaded with urls (only for ie4+ & ns6+)
var layersLoadCount = 0;		//Number of above
var layerLoadID = "";			//LayerID currently loading a url


// temp vars!!
var tpLcount;
var tpLs = new Array(); //divs
var tpLp = new Array(); //div parents
var tpLn = new Array(); //div style for Netscape
var tpLo;


function showLayer() {
	this.visibility = "visible";
}

function hideLayer() {
	this.visibility = "hidden";
}

function writeToLayer(layerHTML) {
	if (is.ns4) {
		this.HTM.open();
		this.HTM.write(layerHTML);
		this.HTM.close();
		this.HTML = layerHTML;
	}
	else if (is.ie || is.ns6)
		this.HTM.innerHTML = layerHTML;
}

function doLoad() {
	if (layersLoadCount > 0) {
		url = layersLoadInfo[0];
		layerLoadID = layersLoadInfo[1];
		var tmp = new Array();
		for (var i = 1; i < layersLoadCount; i++) {
			tmp[(i-1)*2] = layersLoadInfo[i*2];
			tmp[(i-1)*2+1] = layersLoadInfo[i*2+1];
		}
		layersLoadInfo = tmp;
		if (is.ns4) {
			Layers[layerLoadID].load(url, Layers[layerLoadID].w);
		}
		else {
			if (is.ns6) {	
			    alert("System Admin: Working on loading <DIV> with HTML documents - please use Internet Explorer to view this page correctly meanwhile.");
			}
			else if (is.ie & is.ie6) {
				document.getElementById(layerLoadID+'Div').src = url								
			}	
			else if (is.ie4) {
				document.all["iframeBuffer"].src = url	
			}
		}
		layersLoadCount--;
	}
}

function loadLayerUrl(url) {
	layersLoadInfo[layersLoadCount*2] = url;
	layersLoadInfo[layersLoadCount*2+1] = this.HTM.ID;
	layersLoadCount++;
	if (layersLoadCount == 1)
		doLoad();
}

function iframeLoaded() {
	if (Layers[layerLoadID]) {
		if (is.ie || is.ns6) {
			Layers[layerLoadID].HTM.innerHTML = iframeBuffer.document.body.innerHTML
			Layers[layerLoadID].scrollH = iframeBuffer.document.body.scrollHeight
			Layers[layerLoadID].scrollW = iframeBuffer.document.body.scrollWidth
		}
		else {
			Layers[layerLoadID].scrollH = Layers[layerLoadID].HTM.height
			Layers[layerLoadID].scrollW = Layers[layerLoadID].HTM.width
		}
		Layers[layerLoadID].alertLoad()
		doLoad()
	}
}

function moveLayerBy(byX, byY) {
	if (~this.cancelMove) {
		this.x += byX;
		this.y += byY;
		this.top = this.y;
		this.left = this.x;
	}
	this.alertMove(byX, byY);
}

function moveLayerTo(toX, toY) {
	if (!this.cancelMove) {
		this.x = toX;
		this.y = toY;
		this.top = this.y;
		this.left = this.x;
	}
	this.alertMove(toX, toY);
}

function scrollLayer(byX, byY) {
	this.moveBy(-byX, -byY)
	with (this)
		setClip(ClipInfo.x1+byX, ClipInfo.y1+byY, ClipInfo.x2+byX, ClipInfo.y2+byY)
}

function packClip(obj) {
	with (obj) {
		if (is.ns) {
			clip.left = ClipInfo.x1;
			clip.top = ClipInfo.y1;
			clip.right = ClipInfo.x2;
			clip.bottom = ClipInfo.y2;
		}
		else if (is.ie) {
			clip = "rect("+ClipInfo.y1+"px "+ClipInfo.x2+"px "+ClipInfo.y2+"px "+ClipInfo.x1+"px)";
		}
	}
}

function setLayerClip(x1, y1, x2, y2) {
	this.ClipInfo.x1 = x1;
	this.ClipInfo.y1 = y1;
	this.ClipInfo.x2 = x2;
	this.ClipInfo.y2 = y2;
	packClip(this);	
}

function setLayerClipx1(x1) {
	this.ClipInfo.x1 = x1;
	packClip(this);
}
function setLayerClipy1(y1) {
	this.ClipInfo.y1 = y1;
	packClip(this);
}

function setLayerClipx2(x2) {
	this.ClipInfo.x2 = x2;
	packClip(this);
}

function setLayerClipy2(y2) {
	this.ClipInfo.y2 = y2;
	packClip(this);
}

function setLayerBGColor(col) {
	this.setBGImage("")
	if (is.ns4)
		this.bgColor = col;
	else
		this.backgroundColor = col;
}
function setLayerBGImage(url) {
	if (is.ns4) 
		this.background.src = url;
	else
		this.backgroundImage = 'url("'+url+'")';
}
function setLayerColor(col) {
	if (is.ns) {
		var str = '<font color='+col+'>'+this.HTML+'</font>';
		this.write(str);
		}
	else
		this.color = col;
}

function tpClip(layerObj) {
	if (is.ns) {
		this.x1 = layerObj.clip.left;
		this.y1 = layerObj.clip.top;
		this.x2 = layerObj.clip.right;
		this.y2 = layerObj.clip.bottom;
	}	
	else {
		if (layerObj.clip=="") {
			this.x1 = layerObj.pixelLeft ;
			this.x2 = this.x1 + layerObj.pixelWidth ;
			this.y1 = layerObj.pixelTop ;
			this.y2 = this.y1 + layerObj.pixelHeight ;
		}
		else {
			var clipv = layerObj.clip.split("rect(")[1].split(")")[0].split("px")
			this.x1 = parseInt(clipv[0]);
			this.x2	= parseInt(clipv[1]);
			this.y2 = parseInt(clipv[2]);
			this.y1 = parseInt(clipv[3]);
		}
	}
}

function layerObj(id, parent) {
	ID = id;
	id = id + "Div";
	var obj;
	var styText;
	if (parent && is.ns4) 
		styText = Layers[parent].htmText + '["' + id + '"]' + sty; 
	else
		if (is.ns6) {
			styText = "document.getElementById('" + id + "').style"
		}
		else
			styText = doc + '["' + id + '"]' + sty;
	obj = eval(styText);
	
	//properties
	obj.id = id;
	if (is.ns4 || is.ns6) {
		if (is.ns6) {
			obj.htmText = "document.getElementById('" + id + "')"
		}
		else {
			if (parent) 
				obj.htmText = Layers[parent].htmText + '["' + id + '"].document';
			else 
				obj.htmText = 'document["' + id + '"].document';
		}
		obj.HTM = eval(obj.htmText);
		
	} 
	else {
		obj.htmText = 'document.all["' + id + '"].document';
		if (is.ie6) {
			obj.HTM = document.all[id]
		}
		else {
			obj.HTM = eval(doc + '["' + id + '"]'+htm);
		}
	}
	obj.htm = eval(obj.htmText)
	obj.HTM.ID = ID;
	if (is.ns)
		obj.HTML = "";
	obj.x = parseInt(obj.left);
	obj.y = parseInt(obj.top);
	obj.scrollW = 0;
	obj.scrollH = 0;
	obj.ClipInfo = new tpClip(obj);
	if (!is.ie) {
		obj.Clip = new tpClip(obj);
	}
	else {
		tpClip(obj) ; 
	}
	packClip(obj) ;
	obj.w = obj.ClipInfo.y2 - obj.ClipInfo.y1;
	obj.cancelMove = false
	obj.dragable = false
	obj.vMax = 0;
	obj.hMax = 0;
	obj.vPos = 0;
	obj.hPos = 0;
		
	//methods
	obj.show = showLayer;
	obj.hide = hideLayer;
	obj.write = writeToLayer;
	obj.loadUrl = loadLayerUrl;
	obj.moveBy = moveLayerBy;
	obj.moveTo = moveLayerTo;
	obj.scroll = scrollLayer;
	obj.setClipx1 = setLayerClipx1;
	obj.setClipy1 = setLayerClipy1;
	obj.setClipx2 = setLayerClipx2;
	obj.setClipy2 = setLayerClipy2;
	obj.setClip = setLayerClip;
	obj.setBGColor = setLayerBGColor;
	obj.setBGImage = setLayerBGImage;
	obj.setColor = setLayerColor;
	obj.alertMove = dummyFunction;				
	obj.alertLoad = dummyFunction;
	return obj;
}
function addLayer(id, parent, left, top, width, height, bgcolor, zIndex, visibility, cTop, cRight, cBottum, cLeft) {
	id += 'Div'
	bgcolor = bgcolor || "white"
	cTop = cTop || 0
	cRight = cRight || width
	cBottum = cBottum || height
	cLeft = cLeft || 0
	visibility = visibility || "visible"
	zIndex = zIndex || 1
	
	var str = 'position: absolute; top: '+top+'; left: '+left+'; \n'+
			  'width: '+width+'; height: '+height+'; clip: rect('+cTop+','+cRight+','+cBottum+','+cLeft+');\n'+
			  'background-color: '+bgcolor+'; layer-background-color: \n'+
			  bgcolor+'; z-index: '+zIndex+'; visibility: '+visibility+'; ';

	if (is.ns) {
		tpLn[layerCount] = '#'+id+' { '+str+' }';
		tpLs[layerCount] = '<div id="'+id+'">';
	}
	else 
		tpLs[layerCount] = '<div id="'+id+'" style="'+str+'">';			  
	if (parent == "")
		parent = null		
	tpLp[layerCount] = parent
	layerIDs[layerCount] = id.substring(0,id.indexOf("Div"))
	layerCount++
}

function initLayerStyles() {
	if (is.ns) {
		document.write('<style type="text/css">\n');
		for (var i = 0; i < layerCount; i++)
			document.write(tpLn[i]);
		document.write('\n</style>');
	}
}

function writeChildLayers(parent) {
	if (!parent)
		return ''
	var str = ''
	for (var i = 0; i < layerCount; i++) {
		if (tpLp[i] == parent) {
			tpLo[++tpLCount] = i
			str += '\n'+tpLs[i]
			str += writeChildLayers(layerIDs[i])
			str += '</div>'
		}
	}
	return str
}

function initLayers() {
	var str
	for (var i = 0; i < layerCount; i++) {
		if (!tpLp[i]) {
			tpLCount = 0
			tpLo = new Array()
			tpLo[tpLCount] = i
			str = tpLs[i]
			str += writeChildLayers(layerIDs[i])
			document.write(str+'\n</div>')
			for (var j = 0; j <= tpLCount; j++)
				Layers[layerIDs[tpLo[j]]] = layerObj(layerIDs[tpLo[j]],tpLp[tpLo[j]])
		}
	}
	tpLo = null
	tpLp = null
	tpLs = null
}

