/* $Id: FWTpanel.js 409 2009-07-19 20:42:23Z felix $ */

FWTpanel = {
	active: null,
	restore: "",
	offset: { x: 50, y: 50 },
	thumbPath: "",
	toggle: function(t) {
		if (!t || !t.nodeType) {
			return;
		}
		var i, d = t, v = document.getElementById('FWTlightboxVeil');
//		while (typeof d.nodeName == 'undefined' || d.nodeName.toLowerCase() != 'span' || d.className.indexOf('FWTpanel') < 0) {
		while (typeof d.nodeName == 'undefined' || !d.className.match(/\bFWTpanel\b/)) {
			d = d.parentNode;
		}
		if (d.className.match(/\bloose\b/)) {
			// close panel
			d.className = d.className.replace(/\bloose\b/, "");
			d.onmousedown = function(){};
			if (v) {
//				v.setAttribute("style", "display: none");
				v.style.display = 'none';
			}
//			try {
//				d.parentNode.removeChild(document.getElementById('FWTlightboxVeil'));
//			} catch (e) {}
			FWTpanel.active = null;
			if (window.ActiveXObject) {
				var ch = d.getElementsByTagName('IMG');
				for (i = 0; i < ch.length; i++) {
					ch[i].className = ch[i].className;
				}
			}
		} else {
			// open panel
			d.className = d.className + ' loose';
			FWTpanel.active = d;
// why on earth did I ever introduce this line?
//			d.style.top = (typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop) + 'px';
			
			if (d.className.match(/\lightbox\b/)) {
				if (v) {
//					v.setAttribute("style", "display: block");
					v.style.display = 'block';
				} else {
					// insert the light box veil
					v = document.createElement('div');
					v.id = "FWTlightboxVeil";
//					v.innerHTML = "&#160;";
					if (navigator.userAgent.indexOf('rv:1.8') > -1) {
						d.parentNode.appendChild(v);
					} else {
						document.body.appendChild(v);
					}
				}
				if (navigator.userAgent.indexOf('MSIE 6') > -1 && navigator.userAgent.indexOf('Opera') < 0) {
					var w = parseInt(document.documentElement.scrollWidth) + 'px';
//					var h = parseInt(document.documentElement.scrollHeight) + 'px';
					var h = Math.max(parseInt(document.documentElement.scrollHeight), parseInt(document.documentElement.clientHeight)) + 'px';
//					v.setAttribute("style", v.style + ';width:' + w);
					v.style.width = w;
					v.style.height = h;
/*					v.style.backgroundColor = '#111';
					v.style.position = 'absolute';
					v.style.top = '0px';
					v.style.left = '0px';
					v.style.filter = "alpha(opacity=76)";
*/
//					d.setAttribute("style", 'width:' + w);
					d.style.width = w;
					d.style.left = parseInt(document.documentElement.scrollLeft) + 'px';
					d.style.top = parseInt(document.documentElement.scrollTop) + 'px';
				}
			}
			if (FWTpanel.thumbPath || FWTpanel.adaptOrientation) {
				var imgs = d.getElementsByTagName('img');
				for (i = 0; i < imgs.length; i++) {
					if (FWTpanel.thumbPath) {
						imgs[i].src = imgs[i].src.replace(new RegExp(FWTpanel.thumbPath + "/?"), '');
					}
				}
				if (FWTpanel.adaptOrientation && imgs[i - 1].width < imgs[i - 1].height) {
					d.className = d.className.replace(/\blandscape\b/, "");
				}
			}
			if (!d.className.match(/\bfixed\b/)) {
				// make it draggable
				var c = d.getElementsByTagName('*');
				for (i = 0; i < c.length; i++) {
					if (c[i].className.match(/\bFWThandle\b/)) {
						c[i].onmousedown = FWTpanel.drag;
						break;
					}
				}
				if (i == c.length) {
					d.onmousedown = FWTpanel.drag;
				}
			}
		}
		if (typeof t.blur == 'function') {
			t.blur();
		}
	},
	drag: function(e) {
		var ev = e ? e : window.event;
		FWTpanel.offset = {
			x: (ev.clientX ? ev.clientX : ev.pageX) - (FWTpanel.active.style.left ? parseInt(FWTpanel.active.style.left) : FWTpanel.offset.x),
			y: (ev.clientY ? ev.clientY : ev.pageY) - (FWTpanel.active.style.top ? parseInt(FWTpanel.active.style.top) : FWTpanel.offset.y)
		};
		document.onmouseup = FWTpanel.drop;
		document.onmousemove = FWTpanel.move;
	},
	move: function(e) {
		var ev = e ? e : window.event;
		FWTpanel.active.style.left = (ev.clientX ? ev.clientX : ev.pageX) - FWTpanel.offset.x + 'px';
		FWTpanel.active.style.top = (ev.clientY ? ev.clientY : ev.pageY) - FWTpanel.offset.y + 'px';
		if (window.getSelection && window.getSelection().removeAllRanges) {
			window.getSelection().removeAllRanges();
		} else if (document.selection && document.selection.empty) {
			document.selection.empty();
		}
	},
	drop: function(e) {
		document.onmousemove = function(){};
	},
	next: function() {
		var spans = document.getElementsByTagName('span'), i, next = null;
		for (i = 0; i < spans.length && !next; i++) {
			if (spans[i].className.match(/\bFWTpanel\b/)) {
				if (FWTpanel.active) {
					if (spans[i] == FWTpanel.active) {
						FWTpanel.toggle(FWTpanel.active) //.firstChild);
					}
				} else {
					next = spans[i];
				}
			}
		}
		FWTpanel.toggle(next);
	}
};
