function showBox (box) {
	var offset = 1;
	var steps = (2 * Math.floor (Math.random () * 6));
	box.style.zIndex = ++zIndex;
	for (var i = 0; i < steps; i++) {
		if (i % 2 == 0) {
			if (box.style.top) { window.setTimeout ( function () { try { box.style.top = (box.style.top.replace ('px', '') * 1 - offset) + 'px'; } catch (ex) {} }, i * 30); }
			else { window.setTimeout ( function () { try { box.style.bottom = (box.style.bottom.replace ('px', '') * 1 - offset) + 'px'; } catch (ex) {} }, i * 30); }
			window.setTimeout ( function () { try { box.style.left = (box.style.left.replace ('px', '') * 1 - offset) + 'px'; } catch (ex) {} }, i * 30);
		}
		else {
			if (box.style.top) { window.setTimeout ( function () { try { box.style.top = (box.style.top.replace ('px', '') * 1 + offset) + 'px'; } catch (ex) {} }, i * 30); }
			else { window.setTimeout ( function () { try { box.style.bottom = (box.style.bottom.replace ('px', '') * 1 + offset) + 'px'; } catch (ex) {} }, i * 30); }
			window.setTimeout ( function () { try { box.style.left = (box.style.left.replace ('px', '') * 1 + offset) + 'px'; } catch (ex) {} }, i * 30);
		}
	}
}

function openNews (id) {
	AJS.getElement ('news' + id + 'teaser').style.display = 'none';
	AJS.getElement ('news' + id + 'full').style.display = 'block';
	// show image if there is one
	var imgWrapper;
	if (imgWrapper = AJS.getElement ('news' + id + 'img')) {
		var img = AJS.getElementsByTagAndClassName ('img', null, imgWrapper);
		img[0].src = img[0].src.replace ('preview0&', 'preview&');
		imgWrapper.lastChild.style.display = 'none';
	}
}

function closeNews (id) {
	AJS.getElement ('news' + id + 'full').style.display = 'none';
	AJS.getElement ('news' + id + 'teaser').style.display = 'block';
	var imgWrapper;
	if (imgWrapper = AJS.getElement ('news' + id + 'img')) {
		var img = AJS.getElementsByTagAndClassName ('img', null, imgWrapper);
		img[0].src = img[0].src.replace ('preview&', 'preview0&');
		imgWrapper.lastChild.style.display = 'block';
	}
}

function showImage (evt, url, width, height) {
	// already exists?
	var mPos = AJS.getMousePos (evt);
	var ws = AJS.getWindowSize ();
	if (AJS.getElement (url.replace ('/', '').replace ('.', ''))) {
		var d = AJS.getElement (url.replace ('/', '').replace ('.', ''));
		d.style.display = 'block';
		showBox (d);
	}
	else {
		// create div
		var d = AJS.DIV ( { className: 'imgpopup' } );
		d.id = url.replace ('/', '').replace ('.', '');
		d.style.position = 'absolute';
		d.style.width = (width + 10) + 'px';
		d.style.height = (height + 10) + 'px';
		d.style.cursor = 'move';
		d.onclick = function () { showBox (d); };
		AJS.appendChildNodes (d, AJS.IMG ());
		d.firstChild.src = url;
		d.firstChild.style.width = width + 'px';
		d.firstChild.style.height = height + 'px';
		AJS.appendChildNodes (d, AJS.DIV ( {className: 'imgtopright'} ), AJS.DIV ( {className: 'imgbottomright'} ), AJS.DIV ( {className: 'imgbottomleft'} ), AJS.DIV ( {className: 'closebutton'} ), AJS.DIV ( {className: 'imgbottom'} ), AJS.DIV ( {className: 'imgright'} ));
		d.lastChild.previousSibling.previousSibling.innerHTML = 'X';
		d.lastChild.previousSibling.previousSibling.onclick = function () { this.parentNode.style.display = 'none'; };
		d.lastChild.previousSibling.style.width = (width - 10) + 'px';
		d.lastChild.style.height = (height - 10) + 'px';
		AJS.appendChildNodes (document.body, d);
		// position the div
		d.style.top = Math.max (10, Math.floor ((ws.h - height - 10) / 2) + AJS.getScrollTop ()) + 'px';
		d.style.left = Math.max (10, Math.floor ((ws.w - width - 10) / 2)) + 'px';
		showBox (d);
		AJS.dnd.dragAble (d.id, { handler: d.id });
	}
	evt.cancelBubble = true;
	if (evt.stopPropagation) {
		evt.stopPropagation ();
	}
}

function unscrambleLink (id, sUrl, sText, isEmail) {
	var obj = document.getElementById (id);
	if (obj == null) {
		alert ('unscrambleLink: Object not found (' + id + ')');
		return;
	}
	var parent = obj.parentNode;
	if (parent == null) {
		alert ('unscrambleLink: Parent object not found');
		return;
	}
	if (parent.nodeName.toLowerCase () == 'div') parent = parent.firstChild;
	if (parent.nodeName.toLowerCase () != 'a') {
		alert ('unscrambleLink: Parent object is not an anchor tag');
		return;
	}
	if (sUrl != null && sUrl.length > 0) {
		eval ('var txt = String.fromCharCode(' + sUrl + ');');
		parent.href = txt;
	}
	if (sText != null && sText.length > 0) {
		var firstChild = parent.firstChild;
		if (firstChild && firstChild.nodeType == 3 && firstChild.nodeValue != null && firstChild.nodeValue.replace (' ', '').length > 0) return;
		eval ('var txt = String.fromCharCode (' + sText + ');');
		txt = txt.replace (/&amp;/g, '&');
		parent.innerHTML = txt;
		if (parent.title != null && parent.title.length == 0) {
			parent.title = txt;
		}
	}
}

function openGallery (id, urlPrefix) {
	if (AJS.isMac ()) {
		window.open (urlPrefix + 'popup.php?id=' + id, '', 'width=642,height=580,resizable=yes,scrollbars=no,status=yes,toolbar=no,menubar=no');
	}
	else {
		// create the frame for the gallery
		var p1;
		// close all other galleries first
/*		var rows = AJS.getElementsByTagAndClassName ('div', 'row');
		for (var i = 0; i < rows.length; i++) {
			if (rows[i].id.indexOf ('g_') == 0) {
				closeGallery (rows[i].id.substr (2));
			}
		}
*/
		// now open the requested one
		if (AJS.getElement ('gallery-' + id)) {
			p1 = AJS.getElement ('gallery-' + id);
			p1.style.display = 'block';
		}
		else {
			var p = AJS.getElement ('gallery-prototype');
			p1 = AJS.DIV ();
			p1.id = 'gallery-' + id;
			p1.className = p.className;
			p1.innerHTML = p.innerHTML;
			p1.style.top = '100px';
			p1.style.left = '100px';
			p1.onclick = function () { showBox (this); };
			AJS.insertAfter (p1, p);
			AJS.dnd.dragAble ('gallery-' + id, { handler: 'gallery-' + id })
			// add simpleviewer
			var c = AJS.getElementsByTagAndClassName ('div', null, p1)[0];
			AJS.appendChildNodes (c, AJS.DIV ());
			c.firstChild.id = 'gallery-' + id + '-viewer';
			var flashvars = {};
			flashvars['xmlDataPath'] = urlPrefix + 'galleryxml.php?id=' + id + '&mid=';
			var params = {};
			params.loop = 'false';
			params.menu = 'false';
			params.quality = 'high';
			params.scale = 'showall';
			params.wmode = 'transparent';
			params.bgcolor = '000000';
			var attributes = {};
			swfobject.embedSWF (urlPrefix + 'swf/viewer.swf', 'gallery-' + id + '-viewer', "100%", "100%", "9.0.0", false, flashvars, params, attributes)
			zIndex++;
		}
		showBox (p1);
	}
}

function closeGallery (id) {
	var g = AJS.getElement ('gallery-' + id);
	if (g) {
		g.style.display = 'none';
	}
}

function openYouTube (galleryId, width, height, urlPrefix) {
	var w = window.open (urlPrefix + 'popup.php?id=' + galleryId, '', 'width=' + width + ',height=' + height + ',resizable=yes,scrollbars=no,status=yes,toolbar=no,menubar=no');
	w.focus ();
}

