/*
 *	@class			domTools
 * 
 *	@author			brsma
 *	@revision		1.0
 *	@description	provides an interface for manipulation of DOM-elements,
 *					supports fully all DOM browser (ie >5.5, moz, safari, ...)
 *					and partly ie4 (using document.all where possible) and ns4
 *
 *	@public			toggleDisplay(element)
 *
 *	@private		(none)
 *
 *	@properties		saveState[]
 */
 
function forceRedraw() {
	// mainly serves for bug compensation in msie
	// looks slightly strange but better than a incorrect update of display
	// and should not be too obvious if viewer's focus is not near window bottom 
	window.resizeBy(0,-1);
	window.resizeBy(0,1);
};
 
domTools = function () {
	this.browserType = {
		ie:document.all ? 1 : 0, 
		ie4:navigator.appName == "Microsoft Internet Explorer" && !document.getElementById, 
		ns4:document.layers ? 1 : 0, 
		dom:document.getElementById ? 1 : 0, 
		mo5:document.getElementById && !document.all ? 1 : 0, 
		mac:navigator.platform == "MacPPC", 
		win:navigator.platform.indexOf("Win")};
	this.saveState = new Array();
	// serves as a temporary container for element states
};

domTools.prototype.toggleDisplay = function(containerElement) {
	var elem = this.MM_findObject(containerElement);
	switch (elem.style.display) {
		case "none" :
			if (this.saveState[containerElement]) {
				// restore previous display style, if possible
				elem.style.display = this.saveState[containerElement];
			} else {
				// or set to element's default if undefined
				elem.style.display = "";
			}
			break;
		case "block" :
		case "inline" :
		default :
			// hide element and remember display type (block, inline, ...)
			// @todo: use a distinct property for saving the state
			this.saveState[containerElement] = elem.style.display;
			elem.style.display = "none";
	}
	if (this.browserType.ie && this.browserType.mac) {
		// force redraw window to compensate for another annoying bug in msie
		forceRedraw();
	}
};

domTools.prototype.switchImg = function(imgName, imgType, active) {
	// provides simple image switching (without preload)
	// the DOM name of the image and the file name must be the same
	//
	// @todo: should better be done with an init object
	this.$imgRoot = "img/";
	this.$imgActiveSuffix = "-a";
	if (this.browserType.ie4) {
		document.all[imgName].src = (this.$imgRoot + imgName + (active ? this.$imgActiveSuffix : "") + "." + imgType);
	} else if (this.browserType.dom) {
		document.images[imgName].src = (this.$imgRoot + imgName + (active ? this.$imgActiveSuffix : "") + "." + imgType);
	}
};

domTools.prototype.MM_findObject = function(n, d) {
	// MM_findObject() incorporated into domTools object
	// code by macromedia.com
	var p, i, x;
	if (!d) {
		d = document;
	}
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all) {
		x = d.all[n];
	}
	for (i = 0; !x && i < d.forms.length; i++) {
		x = d.forms[i][n];
	}
	for (i = 0; !x && d.layers && i < d.layers.length; i++) {
		x = this.MM_findObject(n, d.layers[i].document);
	}
	if (!x && d.getElementById) {
		x = d.getElementById(n);
	}
	return x;
}

domTools.prototype.MM_swapImage = function() {
	// MM_swapImage() incorporated into domTools object
	// code by macromedia.com
	// v3.0
	var i;
	var j = 0;
	var x;
	var a = this.MM_swapImage.arguments;
	this.MM_sr = new Array();
	for (i = 0; i < (a.length - 2); i += 3) {
		if ((x = this.MM_findObj(a[i])) != null) {
			this.MM_sr[j++] = x;
			if (!x.oSrc) {
				x.oSrc = x.src;
			}
			x.src = a[i + 2];
		}
	}
}

domTools.prototype.MM_swapImgRestore = function() {
	//v3.0
	var i, x, a = this.MM_sr;
	for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) {
		x.src = x.oSrc;
	}
}