/******************************************************************

	DarkShell JavaScript File
	
	File: layer_control.js
	
	Description: Operations on layers of unknown contents. Invaluable for DHTML pages.
	
	Created: Sh 'Riff, October 28, 2002
	Copyright 1999-2003 Blessed Darkness, Inc.
	
*******************************************************************
	
	Version: 0.4
	
	Version History:
		0.1 / 28.10.02 / Created
		0.2 / 04.11.02 / Updated / Icon event hadler added
		0.3 / 25.11.02 / Updated / Safe switcher added
		0.4 / 21.12.02 / Released
			PC browsers tested:
			IE 5.0
			
*******************************************************************/
<!--
var arrayOfLayers = new Array();

/********************************
*	Browsers Compatibility	*
********************************/

_IE = (document.all) ? true : false;
_NS4 = (document.layers) ? true : false;
_NS6 = (!document.all && document.getElementById) ? true : false;

/********************************
*	Layer Group Utilities	*
********************************/

function groupOfLayers(name, separator, expireInDays, eventHandler) {
/****************************************************************
	Creates a new group of similar layers.
	------------------
	Object Properties:
	------------------
	name		- The group's name.
	separator	- The separator used in the group's cookies.
	expireInDays	- Specifies how long the group's cookies
			  should be kept in days.
	eventHandler	- An event handler object (optional).
	---------------
	Object Methods:
	---------------
	exhumation	- Invokes the group's cookies and stores
			  the group's layers status.
	inhumation	- Checks whether the status of the group's
			  layers has been changed and refreshes the
			  cookies if that's the case.
****************************************************************/
	this.name = name;
	this.separator = separator;
	this.expireInDays = expireInDays;
	if (eventHandler != null) {
		this.eventHandler = eventHandler;
	}
	this.statusArray = new Array();
	this.exhumation = exhumation;
	this.inhumation = inhumation;
}

function exhumation() {
	var separator = this.separator;
	var name = this.name;
	var oldStatus = getCookie(name);
	if (oldStatus != null) {
		this.oldStatus = oldStatus;
		this.statusArray = oldStatus.split(separator);
	} else {
		this.oldStatus = "";
	}
}

function inhumation() {
	var separator = this.separator;
	var name = this.name;
	var newStatus = this.statusArray.join(separator);
	if (newStatus != this.oldStatus) {
		var today = new Date();
		var expire = new Date();
		expire.setTime(today.getTime() + 1000*60*60*24*this.expireInDays);
		setCookie(name, newStatus, expire);
	}
}

/********************************
*	Layer Utilities		*
********************************/

function smartLayer(group, name, defaultStatus) {
/****************************************************************
	Creates a new layer within a group.
	------------------
	Object Properties:
	------------------
	group		- The name of layer's group.
	name		- The layer's name.
	defaultStatus	- The layer's default visibility status if there're no cookies.
	---------------
	Object Methods:
	---------------
	setLayer	- Activates the layer according to its visibility status.
	switchLayer	- Changes the layer's visibility status.
****************************************************************/
	this.group = group;
	this.name = name;
	this.defaultStatus = defaultStatus;
	var flag = 0;
	if (group.statusArray.length > 0) {
		for (var i = 0; i < group.statusArray.length; i += 2) {
			if (group.statusArray[i] == name) {
				this.status = group.statusArray[i + 1];
				flag = 1;
				break;
			}
		}
	}
	if (flag == 0) {
		this.status = defaultStatus;
		var arrayLength = group.statusArray.length;
		group.statusArray[arrayLength] = name;
		group.statusArray[arrayLength + 1] = defaultStatus;
	}
	this.setLayer = setLayer;
	this.switchLayer = switchLayer;
}

function setLayer() {
	var group = this.group;
	var name = this.name;
	var status = this.status;
	if (_IE) {
		var layer = 'document.all.' + name;
	}
	if (_NS4) {
		var layer = findLayer(name, document);
	}
	if (_NS6) {
		var layer = document.getElementById(name);
	}
	eval(status + "Layer(" + layer + ")");
	if (group.eventHandler != null && group.eventHandler.type == 'image') {
		eval("document.images['handler" + name + "'].src = group.eventHandler." + status + "Obj.src");
	}
	var length = arrayOfLayers.length;
	arrayOfLayers[length] = name;
}

function switchLayer() {
	var group = this.group;
	var name = this.name;
	var status = this.status;
	if (_IE) {
		var layer = 'document.all.' + name;
	}
	if (_NS4) {
		var layer = findLayer(name, document);
	}
	if (_NS6) {
		var layer = document.getElementById(name);
	}
	(status == 'show') ? status = 'hide' : status = 'show';
	this.status = status;
	eval(status + "Layer(" + layer + ")");
	if (group.eventHandler != null && group.eventHandler.type == 'image') {
		eval("document.images['handler" + name + "'].src = group.eventHandler." + status + "Obj.src");
	}
	for (var i = 0; i < this.group.statusArray.length; i += 2) {
		if (this.group.statusArray[i] == name) {
			this.group.statusArray[i + 1] = status;
		}
	}
}

function findLayer(name, doc) {
	var i, layer;
	for (i = 0; i < doc.layers.length; i++) {
		layer = doc.layers[i];
		if (layer.name == name) {
			return layer;
		}
		if (layer.document.layers.length > 0) {
			layer = findLayer(name, layer.document);
			if (layer != null) {
				return layer;
			}
		}
	}
	return null;
}

function switchIt(layer) {
/****************************************************************
* 	If the page wasn't completely loaded
*	the switcher prevents operations on non-activated layers.
****************************************************************/
	if (arrayOfLayers) {
		for (i = 0; i < arrayOfLayers.length; i++) {
			if (arrayOfLayers[i] == layer) {
				eval("_" + layer + ".switchLayer();");
				break;
			}
		}
	}
}

/********************************
*	Layer Visibility	*
********************************/

function showLayer(layer) {
	if (_NS4) {
		layer.visibility = "show";
	}
	if (_IE || _NS6) {
		layer.style.visibility = "visible";
	}
	layer.style.position = "";
}

function hideLayer(layer) {
	if (_NS4) {
		layer.visibility = "hide";
	}
	if (_IE || _NS6) {
		layer.style.visibility = "hidden";
	}
	layer.style.position = "absolute";
}

/********************************
*	Event Handler (IMG)	*
********************************/

function imgObj(width, height, showObj, hideObj) {
	this.type = 'image';
	this.showObj = new Image(width, height);
	this.showObj.src = showObj;
	this.hideObj = new Image(width, height);
	this.hideObj.src = hideObj;
}

/********************************
*	Cookies Control		*
********************************/

function setCookie(name, value, expire) {
	document.cookie = name + "=" + escape(value) +
	((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
}

function getCookie(name) {
	var dc = document.cookie;
	var search = name + "=";
	if (dc.length > 0) {
		offset = dc.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			var end = dc.indexOf(";", offset);
			if (end == -1) {
				end = dc.length;
			}
			return unescape(dc.substring(offset, end));
		}
	}
}
//-->