function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function doAjax(options)
{
	var $ = jQuery;
	
	var ajaxOptions = $.extend(
	{
		type: "POST",
		cache: false,
		errorDoDefault: true,
		successDoDefault: true,
		completeDoDefault: true,
		dataType:	"json",
		timeout:	10000
	}, options);
	
	ajaxOptions.error = function(XMLHttpRequest, textStatus, errorThrown)
	{
		if(ajaxOptions.errorDoDefault)
		{
			alert("An error occured while processing your request, please try again. (" + textStatus + ")\n\nReturned text:\n\n" + XMLHttpRequest.responseText);
		}
		if(options.error)
			options.error(XMLHttpRequest, textStatus, errorThrown);
	};
	
	ajaxOptions.success = function(data, textStatus)
	{
		if(ajaxOptions.successDoDefault)
		{
			if(data.errors && data.errors.length > 0)
			{
				var message = "An error occured while processing your request:\n\n";
				$.each(data.errors, function()
				{
					message = message + this.code + ": " + this.message + "\n";
				});
				alert(message);
				return;
			}
		}
		if(options.success)
			options.success(data, textStatus);
	};
	
	ajaxOptions.complete = function()
	{
		if(ajaxOptions.completeDoDefault)
		{
			
		}
		if(options.success)
			options.complete();
	};
						
	
	$.ajax(ajaxOptions);
}

var urlAjaxLoader = "/images/icons/ajax-loader.gif";

jQuery(function($)
{
	// content editable hook
	var contentEntryConstructor = function(element)
	{
		entry = { };
		entry.container = $(element).closest(".editable-content");
		entry.row = entry.container;
		
		// define the fields that are editable
		entry.fields = { };

		entry.fields["content"] = { };
		entry.fields["content"].type = "textarea";
		entry.fields["content"].edit = $("textarea.edit", entry.row);
		entry.fields["content"].display = $("div.display", entry.row);
		
		entry.ajaxLoader = $(".ajax-loader", entry.row);
		
		// collections
		entry.edits = $(".edit", entry.row);
		entry.displays = $(".display", entry.row);
		
		// buttons
		entry.buttons = $(".buttons", entry.row);
		entry.btnSave = $(".btnSave", entry.row);
		entry.btnCancel = $(".btnCancel", entry.row);
		entry.btnEdit = $(".btnEdit", entry.row);
		entry.btnDelete = $(".btnDelete", entry.row);
		
		return entry;
	};
	
	var contentSaveCallback = function(entry)
	{
		// on form submit update the textarea element with the contents of wysiwyg editor
		$.each(WYMeditor.INSTANCES, function() { this.update(); });
		
		var data = $(":input", entry.row).serializeArray();
		entry.buttons.hide();
		entry.ajaxLoader.show();
		$("*", entry.row).attr("disabled", true);
		doAjax(
		{
			url:		"/cont/save/ajax",
			data:		data,
			success: 	function(data, textStatus)
						{
							editable.actions.save(entry);
							$(".wym_box", entry.row).remove();
							for(i in data.saved)
							{
								if(entry.fields[i])
								{
									if(entry.fields[i].setDisplay)
										entry.fields[i].setDisplay(data.saved[i]);
									else
										entry.fields[i].display.html(data.saved[i]);
								}
							}
						},
			complete: 	function()
						{
							entry.ajaxLoader.hide(); 
							entry.buttons.show(); 
							entry.row.removeClass("ajax-loading"); 
							$("*", entry.row).removeAttr("disabled");
						}
		});
		
		return false;
	};
	
	var contentEditCallback = function(entry)
	{
		editable.actions.edit(entry);
		setTimeout(function()
		{
			$("textarea.wysiwyg", entry.row).wymeditor(
			{
				logoHtml: '', classesHtml: '', statusHtml: '',
				boxHtml:	"<div class='wym_box'>"
				              + "<div class='wym_area_top'>"
				              + WYMeditor.TOOLS
				              + WYMeditor.CONTAINERS
				              + WYMeditor.CLASSES
				              + "</div>"
				              + "<div class='wym_area_left'></div>"
				              + "<div class='wym_area_right'>"
				              + "</div>"
				              + "<div class='wym_area_main'>"
				              + WYMeditor.HTML
				              + WYMeditor.IFRAME
				              + WYMeditor.STATUS
				              + "</div>"
				              + "<div class='wym_area_bottom'>"
				              + "</div>"
				              + "</div>"
			});
		}, 0);
		return false;
	}
	
	var conctentCancelCallback = function(entry)
	{
		editable.actions.cancel(entry);
		$(".wym_box", entry.row).remove();
	}

	$.each($(".editable-content"), function()
	{
		var contentEdit = { };
		contentEdit.entryConstructor = contentEntryConstructor;
		contentEdit.container = $(this);
		contentEdit.saveCallback = contentSaveCallback;
		contentEdit.editCallback = contentEditCallback;
		contentEdit.cancelCallback = conctentCancelCallback;
		editable.hook(contentEdit);
	});
});
