/* let xgettext think this is php :) <? */
function _(value) {
	return value;
}

/** functions to create elements */
function $E(tagname) {
	return $(document.createElement(tagname));
}

function $T(text) {
	return $(document.createTextNode(text));
}

Element.addMethods({
	$A : function(element, toadd) {
		element = $(element);
		element.appendChild(toadd);
		return element;
	}
});

function createImage(src, alt) {
	var img = $E('img');
	img.src = src;
	img.alt = alt;
	return img;
}

/** Base href **/
var basehref;
try 		{ basehref = document.getElementsByTagName('base')[0].getAttribute('href'); }
catch(er)	{ basehref = '';	}

function getUrlWithbaseHref(url) {
	if(url.substr(0,7) == "http://")
		return url;
	return basehref+url;
}

window.onload = function() {
	initLinks();
	initOverviews();
	$$('input[rel=autofocus]').each(function(element) { element.focus(); });
}

function inputDelete(id, value, focus) {
	target = document.getElementById(id);

	if(target) {
		if(target.value == value || !value) {
			target.value = '';
			if(focus)
				target.focus();
		}
	}
}

function initOverviews() {
	function initOverviewRow(tr, deleteCellIndex) {
		tr.onmouseover = function() {	this.className = 'active';	}
		tr.onmouseout = function() {	this.className = '';	}
		var onclick;
		if(onclick = tr.onclick) {
			tr.onclick = '';
			for(var k = 0; k < deleteCellIndex; k++)
				tr.cells[k].onclick = onclick;
		}
	}

	if (!document.getElementsByTagName) return;
	var table, tables = document.getElementsByTagName('table');
	for (var i=0; i < tables.length; i++) {
		table = tables[i];
		if(table.className.indexOf('overview') == -1) continue;
		
		var tr = table.rows;
		if(tr.length == 0) continue;
		
		var deleteCellIndex = 0;
		for(; deleteCellIndex < tr[0].cells.length && tr[0].cells[deleteCellIndex].className.indexOf('editdelete') == -1; deleteCellIndex++) {}
		
		for(var j=1; j < tr.length; j++) {
			if(tr[j].className.indexOf('nohover') != -1) continue;
			initOverviewRow(tr[j], deleteCellIndex);
		}
	}
}

/**
 * Let al the links having rel='external' open in a new window and having rel='delete'
 * show a popup
 */
function initLinks() {
	if (!document.getElementsByTagName) return;
	var element, elements = document.getElementsByTagName('a');
	for (var i=0; i<elements.length; i++) {
		element = elements[i];
		if (element.getAttribute('rel') == 'external') {
			element.onclick = function() {
				window.open(this.getAttribute('href'));
				return false;
			}
		}
		if (element.getAttribute('rel') == 'delete') {
			element.onclick = function() {
				if(confirm ('Are you sure you want to delete this item?'))
					document.location = this.getAttribute('href') ;
				return false;
			}
		}
	}
	
	elements = document.getElementsByTagName('div');
	for (var i=0; i<elements.length; i++) {
		element = elements[i];
		if (element.getAttribute('rel') == 'infopopup')
			initInfoPopup(element);
	}
}

var infoPopupCount = 0;
function initInfoPopup(element) {
	var id = infoPopupCount++;
	
	var icon = document.createElement('img');
	icon.className = 'infopopup_icon';
	icon.src = tplroot+'img/icons/infopopup.png';
	
	element.parentNode.appendChild(icon);
	element.id = 'infopopup'+id;
	
	icon.onmouseover = function () {
		showPopup(icon, 'infopopup'+id, window.event);
	}
}

/** Popups **/
var visiblePopups = [];
var popupsNotToHide = [];

function showPopup(item, popup, e, deltax, deltay, nothide) {
	function _popupHide(popup) {
		popup = $(popup);
		
		if(popup && popupsNotToHide.indexOf(popup) == -1) {
			popup.style.display = 'none';
			visiblePopups = visiblePopups.without(popup);
		}
	}

	function _popupVisible(popup, item, deltax, deltay) {
		popup = $(popup);
		visiblePopups.push(popup);
		popup.style.visibility = 'hidden';
		popup.style.display = 'block';
		
		if(item == null) {
			popup.style.top = deltay+'px';
			popup.style.left = Math.min(deltax, $(document.body).getWidth() - popup.getWidth())+'px';
		} else {
			popup.style.top = Position.cumulativeOffset(item)[1] + deltay+'px';;
			popup.style.left = Math.min(Position.cumulativeOffset(item)[0] + deltax, $(document.body).getWidth() - popup.getWidth())+'px';
		}
		
		popup.style.visibility = 'visible';
	}

	if(deltax == undefined) deltax = 0;
	if(deltay == undefined) deltax = 15;
	
	if(!popup) return;
	popup = $(popup);
	
	visiblePopups.each(_popupHide);
	_popupVisible(popup, item, deltax, deltay);
	
	if(popup.onmouseout) return;
	
	if(nothide != true) {
		popup.onmouseout = function() {
			popupsNotToHide = popupsNotToHide.without(popup);
			setTimeout(function() { _popupHide(popup) }, 500);
		}
		
		if(item != null) item.onmouseout = popup.onmouseout;
	}
	
	popup.onmouseover = function() {
		popupsNotToHide.push(popup);
	}
}

function openPopupWindow(url, name) {
	return window.open(url, name, 'width=680,height=600,resizable,scrollbars=yes');
}

function weblogsShowAll(element, url) {
	element = $(element);
	
	//check whether a list exists
	var list;
	try {
		list = element.getElementsByTagName('ul')[0];
	} catch(er) { }
	
	//if it exist, show it
	if(list) {
		element.getElementsByTagName('a')[0].innerHTML = list.style.display == 'none' ? '(-) '+'Hide all weblogs' : '(+) '+'Alle weblogs tonen';
		list.style.display = list.style.display == 'none' ? 'block' : 'none';
		return;
	}
	
	//otherwise create and fill the list
	new Ajax.Request(url, {
		method: 'post',
		onSuccess: function(transport) {
			var weblogs = transport.responseText.evalJSON(true);
			list = $E('ul');
			weblogs.each(function(weblog) {
				var webloglink = $E('a').$A($T(weblog.name));
				webloglink.onclick = function() { window.open(weblog.url); return false; };
				webloglink.href = weblog.url;
				list.$A($E('li').$A(webloglink).$A($T(' ('+weblog.date_start+' - '+weblog.date_end+')')));
			});
			element.getElementsByTagName('a')[0].innerHTML = '(-) '+'Hide all weblogs';
			element.$A(list);
		}
	});	
}

/** website **/
function imagePopup(url, w, h) {
	w += 40;
	h += 40;
	var win = window.open(url,"imagePopup","scrollbars,status,resizable,width="+w+",height="+h);
	win.focus();
}

/* ?> */
404