/* Last Update: 18-1-2005 14:30:00 */
/* Updated By: Asa Alvebrand (Framfab) */ 
/* $Id: fo.js,v 1.4 2004/09/10 17:25:18 rolipe Exp $ */

var fo = {
	magic: {
		fold: "fold",
		collect: "collect",
		deflect: "deflect",
		cover: "cover",
		split: "sub"
	},
	shift: {
		x: 0,
		y: 0,
		custom: []
	}
};

/* show([id])
Shows all layers with a value for id starting with fo.magic.fold + id, and
their ancestors.  Ancestors are recognized by the "foldfoosubbarsubbaz" naming
convention.  When id is undefined, hides all affected layers. The div_id is the
identifyer of the containing div.
*/

function show(lid, relative_to, div_id, ox, oy) {

	// IE4 bugfix: IE4 cannot handle an argument named "id" in a function
	var id = lid;

	if (dom.finished) {

		// Position the layer to show relative to some object (usually caller):
		if (typeof id != 'undefined' && typeof relative_to != 'undefined') {

			offset = get_offset(relative_to);
			var id = fo.magic.fold + id;
			var div_id = fo.magic.fold + div_id;
			if (typeof dom.handles[div_id] != 'undefined' && typeof document.layers != 'undefined') {
				offset['x'] += dom.handles[div_id].style.left;
				offset['y'] += dom.handles[div_id].style.top;
			}

			// Shift the layer:
			offset['x'] += (typeof ox != 'undefined') ? ox : fo.shift.x;
			offset['y'] += (typeof oy != 'undefined') ? oy : fo.shift.y;
			//alert(navigator.userAgent);
			for (var i = 0; i < fo.shift.custom.length; i++) {
				if (fo.shift.custom[i].ua.test(navigator.userAgent) ) {
					offset['x'] += eval(fo.shift.custom[i].x);
					offset['y'] += eval(fo.shift.custom[i].y);
					break;
				}
			}

			/*
				Get the viewport offset for browsers which set top and left
				relative to a positioned container (hardcoded as being a div
				with classname "view":
			*/
			if (/Gecko/.test(navigator.userAgent) ) {
				var vp = null;
				var divs = document.getElementsByTagName('DIV');
				for (var i = 0; i < divs.length; i++) {
					if (divs[i].className == "view") {
						vp = divs[i];
						break;
					}
				}
				var vpoffset = get_offset(vp);
				offset['x'] -= vpoffset['x'];
				offset['y'] -= vpoffset['y'];
			}
			// Position the layer:
			if (!/MSIE\ 5\.23.*Mac_PowerPC\)$/.test(navigator.userAgent) ) {
				dom.handles[id].style.left = offset['x'];
				dom.handles[id].style.top = offset['y'];
			}
		}

		// Determine for every layer if it should show or not:
		// fos:
		for (p in dom.handles) {
			if (typeof id != 'undefined') {
				if (id.indexOf(fo.magic.fold) != 0) {
					id = fo.magic.fold + id;
				}
			}
			if (typeof dom.handles[p].id != 'undefined') {
				if (dom.handles[p].id.indexOf(fo.magic.fold) == 0) {
					if (_is_ancestor(id, dom.handles[p].id, fo.magic.split) ) {
						dom.handles[p].style.visibility = "visible";
						dom.handles[p].style.zIndex = 1000;
						if (typeof dom.handles[p].filters != 'undefined') {
							for (var i = 0; i < dom.handles[p].filters.length; i++) {
								if (typeof dom.handles[p].filters.item(i).apply != 'undefined') {
									dom.handles[p].filters.item(i).apply();
								}
								if (typeof dom.handles[p].filters.item(i).play != 'undefined') {
									dom.handles[p].filters.item(i).play();
								}
							}
						}
					}
					else {
						dom.handles[p].style.visibility = "hidden";
						dom.handles[p].style.zIndex = 1;
					}
				}
			}
		}

		// Collectors:
		if (typeof id != 'undefined') {
			for (p in dom.handles) {
				if (typeof dom.handles[p].id != 'undefined') {
					if (dom.handles[p].id.indexOf(fo.magic.collect) == 0) {
						dom.handles[p].style.visibility = "visible";
						dom.handles[p].style.zIndex = 600;
					}
				}
			}
		}
		else {
			for (p in dom.handles) {
				if (typeof dom.handles[p].id != 'undefined') {
					if (dom.handles[p].id.indexOf(fo.magic.collect) == 0) {
						dom.handles[p].style.visibility = "hidden";
						dom.handles[p].style.zIndex = 1;
					}
				}
			}
		}
		if (typeof id != 'undefined' && typeof relative_to != 'undefined') {
			var line = id.split(fo.magic.split);
			var cover_id = line[0] + fo.magic.cover;
			if (typeof dom.handles[cover_id] != 'undefined') {
				var offset = get_offset(relative_to);
				dom.handles[cover_id].style.left = offset['x'];
				dom.handles[cover_id].style.top = offset['y'];
				dom.handles[cover_id].style.visibility = "visible";
				dom.handles[cover_id].style.zIndex = 100;
			}
		}
	}
}

/* _is_ancestor([child id], [potential ancestor id])
To find out if we need to show a div we have to compare the id of this div with
the id of the main div to show.  Besides the main div there can be helper-divs
to create a multiple div fo, which must be shown together with the main
div, and a line of ancestor main-divs and their helpers which must also be
shown.
*/

function _is_ancestor(showid, id) {
	
	if (typeof showid == 'undefined') return false;
	var showid_line = showid.split(fo.magic.split);
	var id_line = id.split(fo.magic.split);
	for (var i = 0; i < id_line.length; i++) {
		if (id_line[i].indexOf(showid_line[i]) != 0) {
			return false;
		}
	}
	return true;
}