function setAjaxBehaviour() {
    // Deze is nodig voor confirm-vensters en lightboxes
    //includeScript("/scripts/jquery/ui/ui.dialog.js");
    //includeScript("/scripts/jquery/ui/ui.draggable.js");
    
	// Doe een Ajax-load voor links met class ajax
    $('a.ajax').click(function () {
        var confirmText = $(this).attr('confirm');
        var noHistory   = $(this).attr('nohistory');
        var redirectUrl = $(this).attr('redirecturl');
        var url = $(this).attr('href');
        
        if (confirmText) {
        	$('#content').append('<div id="dialog">' + confirmText + '</div>');
			$('#dialog').dialog({
				width: 350,
        		draggable: false,
        		resizable: false,
				buttons: {
					"Nee": function() { 
						$(this).dialog("close"); 
					},
					"Ja": function() {
						ajax_load(url, redirectUrl, noHistory);
						$(this).dialog("close"); 
					} 
				}
			});
        } else {
            ajax_load(url, redirectUrl, noHistory);
        }
        return false;
    });

    
    // Doe een Ajax-load voor forms met class ajax
    $('form.ajax').submit(function () {
        var formError = false;
        var redirectUrl = $(this).attr('redirecturl');
        var noHistory   = $(this).attr('nohistory');

        $(this).find(":input").each(function(i, input){
            if($(input).attr('required')) {
                if(!$(input).val()) {
                    var title = $(input).attr('title');
                    notify("Verplicht veld: '" + title + "'");
                    formError = true;
                }
                if ($(input).attr('type') == "checkbox" && !$(input).attr('checked')) {
                    var title = $(input).attr('required');
                    notify("Verplicht veld: '" + title + "'");
                    formError = true;
                }
            }
        });

        // Als er verplicht velden ontbreken breek dan hier af...
        if (formError) return false;

        
        // Check of er bestanden worden meegestuurd 
        // Als er bestanden meegestuurd worden gebruik dan ajaxSubmit
        // Er wordt dan een iframe aangemaakt
        if ($(this).find('input[type=file]').length > 0) {
            $(this).ajaxSubmit({
                data: {fileform: true},
                success: function(data) {
                    // stukje code om de response te kunnen parsen
                    data = data.replace('<textarea>','');
                    data = data.replace('</textarea>','');
                    data = data.replace(/&lt;/g,'<');
                    data = data.replace(/&gt;/g,'>');
                    data = eval('('+data+')');
                    $.each(data, function(i,div){
                        if (div.id == 'notify') notify(div.message, div.type);
                        else $("#"+div.id).html(div.html);
                    });
                    if (redirectUrl) {
                        ajax_load(redirectUrl);
                    } else {
                        setBehaviour();
                    }
                }
            });
        } else {
            $(this).ajaxSubmit({
                data: {ajaxload: true},
                success: function(data) {

                        data = eval('('+data+')');
                        $.each(data, function(i,div){
                            if (div.id == 'notify') notify(div.message,div.type);
                            else $("#"+div.id).html(div.html);
                        });
                        if (redirectUrl) {
                            ajax_load(redirectUrl);
                        } else {
                            setBehaviour();
                        }
                    }
            });
        }
        return false;
    });

    
    $('a.lightbox').click(function (){
    	var url = $(this).attr("href");
    	$('#lightbox').remove();
    	$('body').append('<div id="lightbox"></div>');
    	$('#lightbox').dialog({
    		draggable: false,
    		resizable: false,
    	    width: 800,
    		height: 623,
    		position: top,
    		modal: true
    	});
        if (url.search(/\?/) > -1) {
        	url += '&lightbox=true';
            
        } else {
        	url += '?lightbox=true';
        }
        ajax_load(url, null, true);
        return false;
    });
    
    $('a.ui-dialog-titlebar-close').click(function () {
        $('#lightbox').dialog('close');
    });
    
    $('body').keypress(function(e) {
        if(e.which== 27) {
            $('#player embed').css('visibility','visible');
            $('#lightbox').dialog('close');
        }
    });

}


/**
* Laadt een url via ajax
*/
function ajax_load(alink, redirectUrl, noHistory) {
    // Werk de history bij.
    //if (!noHistory) $.history.add(alink, alink);
    
    
    
    // Voeg een extra parameter toe om aan 
    // te geven dat het een ajax_load is
    if (alink.search(/\?/) > -1) {
        alink += '&ajaxload=true';
        
    } else {
        alink += '?ajaxload=true';
    }
    $.get(alink, function(data) {
        // Als er geen JSON object terug is gekomen
        // ga dan niet een div proberen te vullen
        // maar gooi de HTML in de gehele pagina
        if (data[0] == '<') {
            $('body').html(data);
        } else {
            data = eval('('+data+')');
            $.each(data, function(i,div){
                if (div.id == 'notify') notify(div.message, div.type);
                if (div.id == 'debug') Debug.write(div.message, div.name);
                else $("#"+div.id).html(div.html);
            });
        }
        if (redirectUrl) {
            // Al er een redirectUrl is gegeven laadt die dan nu
            ajax_load(redirectUrl);
        } else {
            // Zet de behaviour anders opnieuw
            setBehaviour();
        }
    });
    
}


//History - AJAX calls
function initHistory() {
    // Start ajax history
    $.history.addListener(historyChange);
}

var historyChange = function(newLocation, historyData) {
   if (newLocation == "")  newLocation = "/";
    else if (oldLocation != newLocation) {
        ajax_load(newLocation);
    }
    oldLocation = newLocation;
}
var oldLocation;


