//	Javascript class developped by Michaël Villar
//	http://www.nemstudio.com/
//	Require script.aculo.us library
//	Cette création est mise à disposition selon le Contrat Paternité-Partage des Conditions Initiales 
//	à l'Identique 2.0 Belgique disponible en ligne http://creativecommons.org/licenses/by-sa/2.0/be/ 
//	ou par courrier postal à Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
TextModifier = Class.create();
TextModifier.prototype = {
	input : Object,
	options : "",
	original : "",
	timeout : "",
	
	initialize: function(input) {
		this.options = Object.extend({
			type: "text",
			onChanged: "",
			size: "50",
			cols: "58",
			rows: "6",
			style: "",
			empty: true}, arguments[1] || {});
		this.input = $(input);
		this.input.ref = this;
		this.makeClickable();
		window["TextModifier"+input] = this;
	},
	
	makeClickable: function() {
		this.input.onclick = function() {
			this.ref.click();
		}	
	},
	
	click: function() {
		tmp = this.input.innerHTML;
		tmp = tmp.replace(/"/g,"&quot;");
		this.input.onclick = "";
		this.original = tmp;
		
		if (this.options.type == "text") {
			this.input.innerHTML = '<input style="'+this.options.style+'" id="'+this.input.id+'input'+'" type="text" value="'+tmp+'" size="'+this.options.size+'" />';
		}
		else if (this.options.type == "textarea") {
			//tmp = tmp.split('<\/br />').join('\n');
			//tmp = tmp.replace(/<\/?(BR|br \/).+?>/gi, '');
			tmp = tmp.replace(/<\/?(EM|script|SPAN).+?>/gi, '');
			tmp = tmp.replace("Cliquez sur le texte de votre message pour l'éditer.", '');
			tmp = tmp.replace("Cliquez sur le texte du message pour le modérer.", '');
			//tmp = tmp.replace(/\n/g,"");
			tmp = tmp.replace(/<br>/gi,'<br />');
			//alert(tmp);
			
			browser = navigator.appName
			if (browser == "Microsoft Internet Explorer")
				tmp = tmp.replace(/<br \/>/gi,"\n");
			else{
				tmp = tmp.replace(/\n<br \/>/gi,"\n");
				tmp = tmp.replace(/<br \/>/gi,"\n");
			}
			//tmp = tmp.replace(/<br \/>/gi, "\n");
			tmp = trim(tmp);
			
			//this.original = tmp;
			this.input.innerHTML = '<textarea style="'+this.options.style+'" id="'+this.input.id+'input'+'" cols="'+this.options.cols+'" rows="'+this.options.rows+'">'+tmp+'</textarea>';
		}
		
		nInput = $(this.input.id+"input");
		nInput.ref = this;
		if (this.options.type != "textarea") {
			nInput.onkeyup = function(event) {
				code = event.keyCode;
				if (code == 13) {
					nInput.onblur = "";
					this.ref.valid();
				}
			}
		}
		nInput.onblur = function() {
			this.ref.blured();
		}
		nInput.focus();
		nInput.select();
	},
	
	blured: function() {
		// To prevent safari bug
		clearTimeout(this.timeout);
		this.timeout = setTimeout('TextModifier'+this.input.id+'.valid();',10);
	},
	
	valid: function() {
		nInput = $(this.input.id+"input");
		name = nInput.value;
		if ((name == '' && !this.options.empty) || name == this.original) {
			this.original = this.original.replace(/\n/gi,"<br />");
			this.input.innerHTML = this.original;
			this.makeClickable();
		}
		else {
			//name = name.escapeHTML();
			if (this.options.type == "textarea") {
				name = trim(name);
				name = name.replace(/\n/gi,"<br />");
			}
			if (this.options.onChanged != "" && confirm('Vous êtes sur de vouloir modifier votre commentaire ?')){
				majTextModifier(name,this.input.id); // Fonction définie dans Gameosphere.js
				this.input.innerHTML = name;
			}
			else
				this.input.innerHTML = this.original;
			this.makeClickable();
		}
	}
}
