/* $Id: FWTpanel.js 469 2010-07-12 13:28:42Z felix $ */

FWTpanel = {
	active: null,
	restore: "",
	offset: { x: 50, y: 50 },
	thumbPath: "",
	interval: 2700,
	_t: null,
	toggle: function(t, p) {
		if (!t || !t.nodeType || (window.opera && parseFloat(window.opera.version()) < 9)) {
			return;
		}
		if (!p) {
			FWTpanel.stop();
		}
		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;
		}
		document.body.className = document.body.className.replace(/\bFWTpanel\b/, "").replace(/\bveiled\b/, "");
		if (d.className.match(/\bloose\b/)) {
			// close panel
			d.className = d.className.replace(/\bloose\b/, "");
			d.onmousedown = function(){};
			if (typeof jQuery == 'function') {
				$(document).unbind('keydown', FWTpanel.keys);
			}
			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';
			document.body.className += ' FWTpanel';
			FWTpanel.active = d;
			
			if (typeof jQuery == 'function') {
				$(document).keydown(FWTpanel.keys);
			}
			
// 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/)) {
				document.body.className += ' veiled';
				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 w = Math.min(parseInt(document.documentElement.scrollWidth), parseInt(document.documentElement.clientWidth)) + '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');
				var img = imgs[imgs.length - 1];
				// read image dimensions first, then load full image (otherwise safari does not know the values yet)
				if (FWTpanel.adaptOrientation) {
					d.className = d.className.replace(/\blandscape\b/, "");
					if (img.width > img.height) {
						d.className += ' landscape';
					}
				}
				if (FWTpanel.thumbPath) {
					img.src = img.src.replace(new RegExp(FWTpanel.thumbPath + "/?"), '');
				}
/*				
				for (i = 0; i < imgs.length; i++) {
					if (FWTpanel.thumbPath) {
						imgs[i].src = imgs[i].src.replace(new RegExp(FWTpanel.thumbPath + "/?"), '');
					}
				}
				if (FWTpanel.adaptOrientation) {
					d.className = d.className.replace(/\blandscape\b/, "");
					if (imgs[i - 1].width > imgs[i - 1].height) {
						d.className += ' landscape';
						alert('w: ' + imgs[i - 1].width + '; h: ' + imgs[i - 1].height);
					}
				}
*/			}
			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(){};
	},
	// internal funtion, that returns the neighbours of the current panel
	_n: function() {
		var spans = document.getElementsByTagName('span'), i, r = [null];
		for (i = 0; i < spans.length; i++) {
			if (spans[i].className.match(/\bFWTpanel\b/)) {
				if (r.length == 2 || (!FWTpanel.active && r[0] == null)) {
					r[2] = spans[i];
				}
				if (spans[i] == FWTpanel.active) {
					r[1] = spans[i];
				}
				if (r.length < 2 || !FWTpanel.active) {
					r[0] = spans[i];
				}
			}
		}
		return r;
	},
	
	next: function() {
		var r = FWTpanel._n();
		FWTpanel.toggle(r[1], true);
		FWTpanel.toggle(r[2], true);
		if (!r[2]) {
			FWTpanel.stop();
		}
	},
	prev: function() {
		var r = FWTpanel._n();
		FWTpanel.toggle(r[1], true);
		FWTpanel.toggle(r[0], true);
	},
	play: function() {
		FWTpanel.stop();
		FWTpanel._t = setInterval('FWTpanel.next()', FWTpanel.interval);
		document.body.className += ' playing';
	},
	stop: function() {
		if (FWTpanel._t) {
			clearInterval(FWTpanel._t);
			FWTpanel._t = null;
			document.body.className = document.body.className.replace(/\bplaying\b/, "");
		}
	},
	keys: function(ev) {
		switch (ev.keyCode) {
			case 39 : case 38 : FWTpanel.next(); ev.stopPropagation(); break;
			case 37 : case 40 : FWTpanel.prev(); ev.stopPropagation(); break;
			case 27 : FWTpanel.toggle(FWTpanel.active); break;
			case 32 : FWTpanel._t !== null ? FWTpanel.stop() : FWTpanel.play(); ev.stopPropagation(); break;
		}
	}
};

