/*
 * Simpele redirect functie
 */
function redirect(locatie) {
	window.location = locatie;
}

/*
 * Zet een notificatie neer, welke bij de volgende page load getoond zal worden
 */
function setNotification(notification) {
	$.cookies.set('notification', notification);
}

/*
 * Controleer of er notificaties getoond moeten worden
 */
function checkNotifications() {
	var cookie = $.cookies.get('notification');
	
	if (typeof(cookie) != 'undefined' && cookie) {
		notification(cookie);
	}
	
	$.cookies.del('notification');
}

/*
 * Toon een notificatie
 */
function notification(notification) {
	$('#dialog').dialog('destroy');
	if (notification) {
		$('#notificationcontent').html(notification);
		$('#notification').animate({height: '82px'}, 1000);
		setTimeout('notificationClose()', 2*1000);
	}
}

/*
 * Sluit de notificatie weer
 */
function notificationClose() {
	if ($('#notification').is(':visible')) {
		$('#notification').animate({height: '0px'}, 1000);
	}
}

/*
 * Submenu tonen/hiden
 */
function toggleSubMenu(obj) {
	var parent = $(obj).parent();
	if (parent.hasClass('pathactive')) {
		return false;
	}
	
	var el = parent.next('dd');
	el.toggle();
	
	if (el.css('display') == 'block') {
		parent.css('background-position', '0 -72px');
	} else {
		parent.css('background-position', '0 -37px');
	}
}

/*
 * Toon een jQuery dialog venser
 */
function dialog(title, content, options) {
	$('#dialog').dialog('destroy');
	
	options = $.extend({
		bgiframe: true,
		modal: true,
		autoOpen: false
	}, options);
	
	$('#dialog').attr('title', title).html(content);
	$('#dialog').dialog(options);
	$('#dialog').dialog('open');
}

/*
 * Toon een jQuery dialog venster, met 2 knoppen
 *
 * Usage:
 * 
 * confirmation('<p>Weet je het zeker?</p>', function() {
 *     alert('Okay dan!');
 * });
 *
 * confirmation('<p>Weet je het zeker?</p>', function() {alert('Okay dan!');},
 * {
 *     title: 'Dit is een andere titel',
 *     okButtonTxt: 'Okay',
 *     cnButtonTxt: 'Cancel',
 *     cnButtonFunc: function() {
 *         alert('Nou, dan niet');
 *     }
 * });
 */
function confirmation(question, okButtonFunc, options, dialogoptions) {
	$('#dialog').dialog('destroy');
	
	var defaultOptions = {
		okButtonTxt: 'Ok',
		cnButtonTxt: 'Annuleren',
		title: 'Bevestiging',
		cnButtonFunc: function() { 
			$(this).dialog('close'); 
		}
	};
	if (options) $.extend(defaultOptions, options);
	
	var buttons = {};
	buttons[defaultOptions['okButtonTxt']] = okButtonFunc;
	if (defaultOptions['cnButtonTxt'] != '') {
		buttons[defaultOptions['cnButtonTxt']] = defaultOptions['cnButtonFunc'];
	}
	
	var defaultDialogOptions = {
		bgiframe: true,
		modal: true,
		buttons: buttons,
		autoOpen: false
	};
	if (dialogoptions) $.extend(defaultDialogOptions, dialogoptions);
	

	$('#dialog').attr('title', defaultOptions['title']).html(question);
	$('#dialog').dialog(defaultDialogOptions);
	
	$('#dialog').dialog('open');
}

/*
 * Voer een AJAX actie uit op een bepaalde url.
 * De response wordt getoond als notificatie, eventueel pas na het verversen van de pagina 
 * (handig bij deleten van een item uit een lijst)
 * 
 * Bij een error wordt het bericht als dialog getoond (het is een error als je een 403 teruggeeft)
 */
function ajaxAction(url, options) {
	var defaultOptions = {
		refresh: false,
		dataType: 'text'
	};
	
	if (typeof(options) != undefined && options) {
		for (key in options) {
			defaultOptions[key] = options[key];
		}
	}
	
	$.ajax({
		url: url,
		dataType: defaultOptions['dataType'],
		success: function(msg) {
			if (defaultOptions['refresh']) {
				setNotification(msg);
				redirect(window.location);
			} else if (defaultOptions['dataType'] == 'script') {
				// het resultaat wordt ge-eval'd als javascript, we tonen hier dus niets
			} else if (msg) {
				notification(msg);
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			dialog('Foutmelding', XMLHttpRequest.responseText);
		}
	});	
}

/*
 * Er mogen alleen nummers ingevoerd worden (ook op keypad!), en natuurlijk backspace, enter, tab, pijltjes (links en rechts) en delete
 * Usage: onkeydown="numericOnly(event)"
 */
function numericOnly(evt) {
	var charCode = (evt.which) ? evt.which : evt.keyCode;
	if ((charCode>=48 && charCode<=57) || (charCode>=96 && charCode<=105) || charCode==8 || charCode==9 || charCode==13 || charCode==37 || charCode==39 || charCode==46 ) {
	} else {
		Event.stop(evt);
	}
}

jQuery.fn.debug = function() {
	return this.each(function() {
		alert(this);
	});
};

jQuery.log = function(message) {
	if(window.console) {
		console.debug(message);
	} else {
		alert(message);
	}
};
