/*
	* javascript forms.js
	* Actions javascript pour les formulaires, autocompleter, inPlaceEditor, etc.
	* auteur: Valentin Bersier
*/

/* In-Place editors */

$(document).ready(function(){
	$.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };
});

function setClickable(id, url, textarea, compact, notooltip) {
	var tag = $('#'+id)[0].tagName;
	if($('#'+id).next().attr('class') != 'addToolTip' && !notooltip)
		$('#'+id).after('&nbsp;<a href="#" class="addToolTip" title="Vous pouvez modifier cette valeur en cliquant sur le texte."><img src="http://univoc.net/static/style/images/help.png" alt="Aide" /></a>');
	if($('#'+id).css('display') == 'block') {
		var balise = 'div';
	}
	else {
		var balise = 'span';
	}
	
	$('#'+id).click(function(){
		if($('#'+id).next().attr('class') == 'erreur')
			$('#'+id).next().remove();
		
		$('#'+id+' + .addToolTip').css('display', 'none');
		if(textarea) {
			var field = '<'+balise+' class="inplace"><textarea cols="20" rows="3" class="ipe'+id+'">' + $(this).html() + '</textarea>';
		}
		else {
			if(compact) {
				var field = '<'+balise+' class="inplace"><input type="text" value="' + $(this).html() + '" class="text compact ipe'+id+'" />';
			}
			else {
				var field = '<'+balise+' class="inplace"><input type="text" value="' + $(this).html() + '" class="text ipe'+id+'" />';
			}
		}
		if(compact) {
			var button = ' <input type="submit" class="validate" style="height: 20px; font-size: 70%;" value="" /><input type="submit" class="cancel" style="height: 20px; font-size: 70%;" value="" /></'+balise+'>';
		}
		else {
			var button = ' <input type="submit" class="validate" style="height: 23px; font-size: 70%;" value="Enregistrer" /><input type="submit" class="cancel" style="height: 23px; font-size: 70%;" value="Annuler" /></'+balise+'>';
		}
		var revert = $(this).html();
		$(this).after(field+button).remove();
		$('.validate').click(function(){saveChanges(this, false, revert, id, url, tag, textarea, compact, notooltip);});
		$('.cancel').click(function(){saveChanges(this, true, revert, id, url, tag, textarea, compact, notooltip);});
		$('.ipe'+id).select();
		});
		
	$('#'+id).mouseover(function() {
		$(this).addClass("editable");
	})
	$('#'+id).mouseout(function() {
		$(this).removeClass("editable");
	});
}

function saveChanges(obj, cancel, revert, id, url, tag, textarea, compact, notooltip) {
	if (!cancel) {
		var t = $(obj).siblings(0).val();
		$.ajax({
			type: "POST",
			url: url,
			data: "value="+t,
			beforeSend: function(){
				$(obj).parent().after('<'+tag+' id="'+id+'">Envoi...</'+tag+'>').remove();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				$('#'+id).html(revert);
				$('#'+id).after('&nbsp;<span class="erreur">Erreur</span>');
			},
			success: function(data, textStatus){
				reg = new RegExp("ok$")
				if(reg.test(data))
					$('#'+id).html(t);
				else {
					$('#'+id).html(revert);
					$('#'+id).after('&nbsp;<span class="erreur">'+data+'</span>');
				}
			},
			complete: function(){
				$('#'+id).fadeOut('fast').fadeIn('normal');
			}

		});
	}
	else {
		var t = revert;
		$(obj).parent().after('<'+tag+' id="'+id+'">'+t+'</'+tag+'>').remove();
	}
	setClickable(id, url, textarea, compact, notooltip);
}



