window.addEvent('domready', function() {
	initExternalLinks();
	initTextAreaCursorPos();
});

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

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

Element.implement({
	$A : function(toadd) {
		this.appendChild(toadd);
		return this;
	}
});

function getBaseHref() {
	try {
		return document.getElementsByTagName("base")[0].getAttribute("href");
	} catch(er) {
		return '';
	}
}

/** New Window **/
function newWindow(url, name) {
	return window.open(url, name, 'width=600,height=600,location=no,directories=no,status=no,menubar=no');
}

/** MultipleList **/
function selectAllListSelect(id, selected) {
	target = $(id);
	for(i = 0; i < target.options.length; i++)
		target.options[i].selected = selected;
}

function selectAllListCheckboxes(prefix, selected) {
	elem = document.getElementsByTagName('input');
	for(i = 0; i < elem.length; i++) {
		if(elem[i].type.toLowerCase() == 'checkbox' && elem[i].name.indexOf(prefix) == 0)
			elem[i].checked = selected;
	} 
}

/** Datum **/
function deltaDatumDag(fielddag, fieldmaand, fieldjaar, delta) {
	date = new Date(
		$(fieldjaar).value,
		$(fieldmaand).value - 1,
		$(fielddag).value
	);
	date.setDate(date.getDate() + delta);
	$(fielddag).value = date.getDate();
	$(fieldmaand).value = date.getMonth() + 1;
	$(fieldjaar).value = date.getFullYear();
}

function waarschuwing(bericht, uri) {
	if(confirm(bericht)) {
		if(url.indexOf("http://") != 0 || url.indexOf("https://") != 0)
			uri = getBaseHref() + uri;
		top.location.href = uri;
	}
}

/** 
 * Maakt de waarde van het target met id id leeg indien die gelijk is aan value
 * indien focus krijg target de focus
 */
function inputDelete(element, value, focus) {
	element = $(element);
	if(element) {
		if(element.value == value || !value) {
			element.value = '';
			if(focus) element.focus();
		}
	}
}

function initTextAreaCursorPos() {
	$$('textarea').each(function(textarea) {
		function textareaStorecursor() {
			this.cursorPos = document.selection.createRange().duplicate();
		}

		if(typeof textarea.createTextRange != 'undefined') {
			textarea.onkeyup = textareaStorecursor;
			textarea.onclick = textareaStorecursor;
			textarea.onselect = textareaStorecursor;
			textarea.onselect();
		}
	});
}


function addValueAtCursor(textarea, value) {
	textarea = $(textarea);
	if(!textarea) return;
	
	if(typeof textarea.cursorPos != 'undefined') {
		textarea.cursorPos.text += value;
	} else if(typeof textarea.selectionStart != 'undefined') {
		var scrollTop = textarea.scrollTop;
		var sStart = textarea.selectionStart;
		var sEnd = textarea.selectionEnd;
		
		textarea.value = textarea.value.substr(0, sStart) + value + textarea.value.substr(sStart);
		var nStart = sStart == sEnd ? sStart + value.length : sStart;
		var nEnd = sStart + value.length;
		textarea.setSelectionRange(nStart, nEnd);

		textarea.scrollTop = scrollTop;
	} else {
		textarea.value += value;
	}
}

/** Menu functions **/
function initMenu(menu) {
	function initSubMenu(menuitem, submenudiv) {
		menuitem.onmouseover = function() {
			showSubMenu(submenudiv);
		}
		menuitem.onmouseout = function() {
			hideSubMenu(submenudiv);
		}
		initMenu(submenudiv.getElement('ul'));
	}

	function showSubMenu(menu) {
		menu.interupted = true;
		menu.style.visibility = 'visible';
	}

	function hideSubMenu(menu) {
		menu.interupted = false;
		setTimeout(function() {
			if(!menu.interupted)
				menu.style.visibility = 'hidden';
		}, 50);
	}

	menu = $(menu);
	if(!menu) return;
	
	menu.getChildren('li').each(function(menuitem) {
		var submenudiv = menuitem.getElement('div');
		if(submenudiv)
			initSubMenu(menuitem, submenudiv);
	});
}

function initExternalLinks() {
	$$('a').each(function(a) {
		if(a.rel != 'external')
			return;
		a.addEvent('click', function() {
			window.open(this.getAttribute('href'));
			return false;
		});
	});
}
