$(document).ready(function() {
    (function($){

    $.fn.extend({

        //pass the options variable to the function
        nodesorter: function(options) {

            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                attrname_nodeid: 'nodeid',
		msg_success: 'Volgorde aangepast',
		msg_fail: 'Vanwege een technische fout is de volgorde niet aangepast'
            };
	    
            var options =  $.extend(defaults, options);

            return this.each(function() {
		
                var o = options;

		
		var childids = new Array();
		
		$(this).sortable();
		$(this).bind('sortupdate', function (event, ui) {
		    $(event.target).children().each(function(i) {
			childids[i] = $(this).attr(o.attrname_nodeid);
		    });

		    var str_childids = childids.join(",");
		    $.ajax({
			type: "post",
			url: o.url,
			data: {childids: str_childids},
			success: function (msg) { alert(o.msg_success); },
			error: function (msg) { alert(o.msg_fail); }
		    });
		});
            });

        }
    });

    })(jQuery);
});
