var ajax_element;
var ajax_inplace;
var ajax_inplace_message;
var ajax_inplace_empty;
var ajax_inplace_defaut;
var ajax_caller;

function v_show(element) { Element.show($(element)); }
function v_hide(element) { Element.hide($(element)); }
function v_toggle(element) { Element.toggle($(element)); }

function v_drag(element) { new Draggable($(element)); }
function v_update(element,codeHtml) { new Element.update($(element),codeHtml); }
function v_load(element,url) { new Ajax.Updater($(element), url); }
function v_refresh(element,url) { new Ajax.Updater($(element), url, {evalScripts:true}); }
function v_replace(element,codeHtml) { return; }

function setAjaxCaller(ajaxCaller) { ajax_caller = ajaxCaller; }

function ajax_success() { v_hide(ajax_element+'_global'); }
function ajax_failure() { 
	ajax_cancel(ajax_element,ajax_caller);
}

function ajax_valide(elemId,ajaxCaller) {
	ajax_caller = ajaxCaller;
	ajax_init_inplace(elemId);
	
	if (ajaxCaller.form.elements[0].value != ajax_inplace_defaut.value) {
		return ajax_caller.onSubmit();
	}
	else {
		ajax_cancel(elemId,ajax_caller);
		return;
	}	
}

function ajax_cancel(elemId,ajaxCaller) {
	ajax_caller = ajaxCaller;
	ajax_define_inplace(elemId);
	
	set_ajax_inplace($(elemId+'_defaut').value);
	ajax_caller.leaveEditMode();
	v_hide(elemId+'_controls');
	v_hide(elemId+'_message');
	//ajax_caller.onclickCancel();
}

function ajax_callback(form) {
	valeurEscaped = form.elements[0].value;
	valeurEscaped = valeurEscaped.replace(/\"|\'|\<|\>*/g,'');
	valeurEscaped = valeurEscaped.replace(/\&/g,'%26');
	valeurEscaped = valeurEscaped.replace(/\+/g,'%20');
	valeurEscaped = escape( valeurEscaped );
	form.elements[0].value = valeurEscaped;
	return Form.serialize(form);
}

function ajax_init_inplace(element) {
	ajax_define_inplace(element);
	
	v_hide(ajax_element+'_message');
}
function ajax_define_inplace(element) {
	ajax_element = element;
	ajax_inplace = $(ajax_element);
	ajax_inplace_message = $(ajax_element+"_message");
	ajax_inplace_empty = $(ajax_element+"_empty");
	ajax_inplace_defaut = $(ajax_element+"_defaut");
	ajax_inplace.firstChild.nodeValue = get_ajax_inplace();
	v_show(ajax_element+'_controls');
}
function rem_ajax_inplace() {
	while (ajax_inplace.hasChildNodes()) { ajax_inplace.removeChild(ajax_inplace.firstChild); }
}
function set_ajax_inplace_defaut() {
	ajax_inplace.appendChild(document.createTextNode(ajax_inplace_defaut.value));
}
function set_ajax_inplace(text) {
	if (trim(text).length == 0) {
		ajax_inplace.firstChild.nodeValue = ajax_inplace_empty.value;
		ajax_inplace.className = "inplaceeditor-empty ";
	}
	else {
		ajax_inplace.firstChild.nodeValue = text;
		ajax_inplace.className = "inplaceeditor";
	}
}
function get_ajax_inplace() {
	if (ajax_inplace.firstChild != null && ajax_inplace.firstChild.nodeValue != null 
	 && ajax_inplace.firstChild.nodeValue.length != 0 
	 && ajax_inplace.firstChild.nodeValue != ajax_inplace_empty.value) {
		return ajax_inplace.firstChild.nodeValue;
	}
	return '';
}

// methode toujours appele en fin d'evenement ajax
function ajax_complete(transport, element) {
	ajax_define_inplace(element.id);
	
	if (ajax_inplace.firstChild == null) {
		set_ajax_inplace_defaut();
		v_hide(ajax_element+'_controls');
	}
	else {
		// success de submit
		var returnsValues = JSON.parse(ajax_inplace.firstChild.nodeValue);

		// il y a eu une erreur d'appel au serveur donc le parse json retourn false
		if ( !returnsValues ) { 
			rem_ajax_inplace();
			set_ajax_inplace_defaut();
			ajax_failure();
		}
		else {
			set_ajax_inplace(returnsValues.inplaceEditor.value);
				if (returnsValues.inplaceEditor.status == "SUCCESS") {
				ajax_inplace_defaut.value = returnsValues.inplaceEditor.value;
				v_hide(ajax_element+'_message');
				v_hide(ajax_element+'_controls');
				// si JSON retourne une balise preview, cela indique l'element preview a maj
				var isUpdate = returnsValues.inplaceEditor.update != undefined && returnsValues.inplaceEditor.update != '';
				var isPreview = returnsValues.inplaceEditor.preview != undefined && returnsValues.inplaceEditor.preview != '';
				if (isUpdate && !isPreview) {
					ajax_update(returnsValues.inplaceEditor.update,returnsValues.inplaceEditor.value);
				}
				else if (!isUpdate && isPreview) {
					proto = eval(returnsValues.inplaceEditor.preview);
					proto.refresh();
				}
				else if (isUpdate && isPreview) {
					proto = eval(returnsValues.inplaceEditor.preview);
					proto.setParam(returnsValues.inplaceEditor.update,returnsValues.inplaceEditor.value);
				}
				ajaxEval = eval("inplace_paramName_"+element.id+"_Complet = null;");
			}
			// echec du submit car donnees invalides
			else if (returnsValues.inplaceEditor.status == "ERROR") {
				ajax_inplace_message.firstChild.nodeValue = returnsValues.inplaceEditor.message;
				//v_show(ajax_element+'_message');
				// RE-EDIT
				this.enterEditMode('click');
			}
			// clique sur cancel, ou tout cas non prevu :)
			else {
				set_ajax_inplace(ajax_inplace_defaut.value);
				v_hide(ajax_element+'_message');
				v_hide(ajax_element+'_controls');
			}
		}
	}
	v_show(ajax_element+'_global');
}

function ajax_update(elementId, value) {
	if ( $(elementId).value != null ) {
		$(elementId).value = value;
	}
	else {
		if ( $(elementId).firstChild == null ) { 
			$(elementId).appendChild(document.createTextNode(value));
		}
		$(elementId).firstChild.nodeValue = value;
	}
}

function ajax_preview(preview,url) {
	v_load(preview,url);
}

/*************************************************************************
* ENSEMBLE DE METHODES PROPRES AU INPLACEEDITOR AVEC AUTOCOMPLETION
*************************************************************************/
var autoCompleteArray = new Array();

function majArray(ary) {
	if (ary==null) return;
	listTab = ary.split(",");
	for (var i=0; i<listTab.length; i++) { listTab[i] = trim(listTab[i]); }
	autoCompleteArray = listTab;
	return autoCompleteArray;
}

function majFromArray(ajaxAuto) {
	if (ajaxAuto==null || ajaxAuto=='') return;
	ajaxEval = eval(ajaxAuto);
	ajaxEval.options.array = autoCompleteArray;
}

function majAutocomplete(ajaxAuto,ary) {
	if (ajaxAuto==null || ajaxAuto=='' || ary==null) return;
	ajaxEval = eval(ajaxAuto);
	listTab = ary.split(",");
	for (var i=0; i<listTab.length; i++) { listTab[i] = trim(listTab[i]); }
	ajaxEval.options.array = listTab;
	autoCompleteArray = listTab;
}

function executeEval(funct) {
	return eval(funct);
}
