$(document).ajaxError( function(e, xhr, settings, exception) {
	setAjaxLoadProgress(false);
	testConsole('Error:', e);
	testConsole('Exception:', exception);

});

/*
 * NOTE: THESE FUNCTIONS ARE CALLED FROM THE RENDERED Ajax-PLACEHOLDERS (AND
 * NOWHERE ELSE). SEE: CmsAjaxAction CLASS
 */

function AjaxLoadSync(element, targetSelector, url, replace, parentSelector) {
	testConsole('AjaxLoadSync:' + parentSelector + ', ' + targetSelector);

	var target = $(targetSelector);
	if (target.length) {
		testConsole('AjaxLoadSync-processing:' + targetSelector);

		setAjaxLoadProgress(true);
		setAjaxPostValues(null); // reset before posting async
		$.ajax( {
			async :false,
			url :url,
			success : function(data) {
				// data ='<pre> ' + url + ', target: ' + targetSelector +
			// '</pre>'+data;
			if (replace) {
				testConsole('replacing: ' + targetSelector);
				// testConsole(data);
			if (targetSelector != target.selector) {
				alert(targetSelector + ' != ' + target.selector);
			}
			$(targetSelector).replaceWith(data);
			SetContextMenu(targetSelector);
		} else {
			testConsole('htmling: ' + targetSelector);
			// testConsole(data);
			if (targetSelector != target.selector) {
				alert(targetSelector + ' != ' + target.selector);
			}
			$(targetSelector).html(data);
		}

		setAjaxLoadProgress(false);
	}
		});
	} else {
		testConsole('TARGET EMPTY: ' + targetSelector);
	}
	if (targetSelector != target.selector) {
		alert(targetSelector + ' != ' + target.selector);
	}

}

function AjaxLoad(element, targetSelector, url, replace, parentSelector, fade) {
	//testConsole('AjaxLoad-URL:' + url + ' -- BEGIN: ' + targetSelector);

	var target = $(targetSelector);
	if (target.length) {

		if(!fade){
			setAjaxLoadProgress(true);
		}
		setAjaxPostValues(null); // reset before posting async

		//testConsole('AjaxLoad-URL:' + url + ' -- AjaxLoad-processing:' + targetSelector);
		
				$.ajax( {
					async :true,
					url :url,
					error : function(XMLHttpRequest, textStatus, errorThrown) {
						testConsole('AjaxLoad-URL:' + url + ' -- ERROR: '+ textStatus);
					},
					success : function(data) {
					if (replace) {
						testConsole(targetSelector + ': replacing by AjaxLoad-URL:' + url);
						// testConsole(data);
						if (targetSelector != target.selector) {
							alert(targetSelector + ' != ' + target.selector);
						}
						//testConsole('AjaxLoad-URL:' + url + ' -- ok1');
						// testConsole(data);
						
						 
						if(fade){
							$(targetSelector).fadeOut('2000', function(){
								$(targetSelector).replaceWith(data).fadeIn('2000', function(){
									SetContextMenu(targetSelector);
									SetSortable();
								});
							});
						}else{
							//testConsole('AjaxLoad-URL:' + url + ' -- before replace:' + data);
							$(targetSelector).replaceWith(data);
							//testConsole('AjaxLoad-URL:' + url + ' -- after replace');
							SetContextMenu(targetSelector);
							SetSortable();
						}
						
						
						//testConsole('AjaxLoad-URL:' + url + ' -- ok2');
						
					} else {
						testConsole(targetSelector + ': htmling by AjaxLoad-URL:' + url);
						// testConsole(data);

						if (targetSelector != target.selector) {
							alert(targetSelector + ' != ' + target.selector);
						}
						//testConsole('AjaxLoad-URL:' + url + ' -- ok1');
						// testConsole(data);
						
						if(fade){
							$(targetSelector).fadeOut('slow', function(){
								$(targetSelector).html(data);
								
								$(targetSelector).fadeIn('slow', function(){
									//do nothing, but be a callback?!
								});
								
							});
						}else{
							$(targetSelector).html(data);
						}
						
						//testConsole('AjaxLoad-URL:' + url + ' -- ok2');
					}
					
					
					if(!fade){
						setAjaxLoadProgress(false);
					}
				}
				});
	} else {
		testConsole(targetSelector + ': NOT FOUND FOR AjaxLoad-URL:' + url);
	}

}

var ajaxPostValues;
var testPostValues;

function setAjaxPostValues(value) {
	testConsole('SETTING POST VALUES:' + value);
	testConsole(value);
	ajaxPostValues = value;
}
function AjaxPost(element, targetSelector, url, replace, parentSelector) {
	testConsole('AjaxPost-URL:' + url + ' -- BEGIN: ' + targetSelector);
	// note: always send these even, if the targetSelector does not exist
	// the server may depend on the postbackValues...

	var target = $(targetSelector);
	if (target.length) {
		setAjaxLoadProgress(true);

		var tmpPost = ajaxPostValues;
		testConsole('AjaxPost: getting PostValues');

		if (tmpPost == null || tmpPost == "") {
			testConsole('AjaxPost: tmpPost == null');
			testConsole($(element).parents('form').length);
			tmpPost = $(element).parents('form').serialize();
			
			if (tmpPost == null || tmpPost == "") {
				tmpPost=$(element).serialize();
				testConsole('SETTING POST VALUES FROM ELEMENT:' + tmpPost);
			}else{
				testConsole('SETTING POST VALUES FROM FORM:' + tmpPost);
			}
			
		} else {
			testConsole('AjaxPost: tmpPost == "' + tmpPost + '"');
		}

		testConsole('AjaxPost:' + parentSelector + ', ' + targetSelector);
		testConsole('AjaxPost-URL:' + url);
		testConsole('AJAXPOSTVALUES:');
		testConsole(tmpPost);
		setAjaxPostValues(null); // reset before posting async
		testConsole('AjaxPost-URL:' + url + ' -- AjaxPost-processing:'
				+ targetSelector);
		
		testPostValues=tmpPost;
		
		$.ajax( {
			async :false,
			type :'POST',
			data :tmpPost,
			url :url,
			success : function(data) {
				// data ='<pre> ' + url + ', target: ' + targetSelector +
			// '</pre>'+data;
			if (replace) {
				testConsole('AjaxPost-URL:' + url + ' -- replacing: '
						+ targetSelector);
				if (targetSelector != target.selector) {
					alert(targetSelector + ' != ' + target.selector);
				}
				testConsole('AjaxPost-URL:' + url + ' -- ok1');
				// testConsole(data);
				$(targetSelector).replaceWith(data);
				SetContextMenu(targetSelector);
				SetSortable();
				testConsole('AjaxPost-URL:' + url + ' -- ok2');
			} else {
				testConsole('AjaxPost-URL:' + url + ' -- htmling: '
						+ targetSelector);

				if (targetSelector != target.selector) {
					alert(targetSelector + ' != ' + target.selector);
				}
				testConsole('AjaxPost-URL:' + url + ' -- ok1');
				// testConsole(data);
				$(targetSelector).html(data);
				testConsole('AjaxPost-URL:' + url + ' -- ok2');
			}

			setAjaxLoadProgress(false);
		}
		});
	} else {
		testConsole('TARGET EMPTY: ' + targetSelector);
	}
	if (targetSelector != target.selector) {
		alert(targetSelector + ' != ' + target.selector);
	}

}

// note: keep in sync with CmsEditor->Prepare
var CmsContextMenuClass = "CmsContextMenu";
var CmsSortableClass = "CmsSortable";

function SetContextMenu(el) {
	//testConsole("SetContextMenu");
	var selector;
	if (false && el) {
		//testConsole('SetContextMenu: el set');
		if ($(el).hasClass(CmsContextMenuClass)) {
			selector = el;
		}
	} else {
		//testConsole('SetContextMenu: el NOT set');
		selector = "." + CmsContextMenuClass;
	}

	if (selector) {
		$(selector).hover( function() {
			$(this).addClass('ui-state-highlight');
		}, function() {
			$(this).removeClass('ui-state-highlight');
		});

		// note: callback not neccessary; clicking is handled with own onclick
		//testConsole(selector);
		$(selector).destroyContextMenu();
		$(selector).contextMenu( {
			menu :'ajaxContextMenu'
		});
		
		
	}
}

function SetSortable() {
	
	$("." + CmsSortableClass).sortable( {
		/*items : 'li',  --- issue: sometimes it's not the direct children to sort; elements can have any tag - how to determine globally (assign css-class to sortable children? ...by asking parent during rendering?) */
		update : function(event, ui) {

			onSort(ui.item.attr("id"), ui.item.next().attr("id"));

		}
	});

}
function setAjaxLoadProgress(show) {


	if (show) {
		$("#ajaxLoadProgress").show();
	} else {
		$("#ajaxLoadProgress").hide();
	}
}






