(function($) {
    // Plugin implementation
    $.callDotNet = function(url, data, callback, errorHandler) {
		
        // shift arguments if data argument was ommited
        if (jQuery.isFunction(data)) {
            callback = data;
            data = "{}";
        }
        return jQuery.ajax({
            type: "POST",
            url: url,
            data: data,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                var result = msg.d;
				if(jQuery.isFunction(callback))
                	return callback(result);
            },
            error: $.callDotNet.onError
        });
    }

    // Default implementation of the error function
    $.callDotNet.onError = function(error) {
        alert("There was an error processing your request.\n"
                    + "[" + error.status + "]"
                    + " [" + error.statusText + "]");
    }    

    // private function for debugging
    function debug(log) {
        if (window.console && window.console.log)
            window.console.log(log);
    };    
})(jQuery);