function kTranslation(){
	_this = this;
	var callbacks = {"drag":_this.dragOnEnd,"resize":_this.resizeOnEnd};
	this.kdelement = new delement (callbacks);
	var top = 0;
}
kTranslation.prototype = {
	//
	addTinyMCE: function (id, isDHTML) {
		if ($("text_html_" + id)){
			// variables
			if (typeof(isDHTML) == undefined) isDHTML = false;
			// load tinyMCE
			tinyMCE.execCommand('mceAddControl', true, "text_html_" + id);
			// redimension and repositionning 
			if (isDHTML)
			{
				// elements
				var elm_target = $("text_html_" + id + "_tbl");
				var elm_target_iframe = $("text_html_" + id + "_ifr");
				var elm_source = $("text_dhtml_" + id);
				// style
				var top = elm_source.style.top;
				var topWithoutUnit = top.substr(0,top.length-2);
				var height            = elm_source.style.height;
				var heightWithoutUnit = height.substr(0,height.length-2);
				this.top = top;
				var moveUp = 80;
				if (parseInt(topWithoutUnit) < 90) {
					moveUp = parseInt(topWithoutUnit) - 10 ;
				}
				elm_source.style.top       = (parseInt(topWithoutUnit) - moveUp) + "px";
				elm_source.style.height    = (parseInt(heightWithoutUnit) + 100) + "px";
				elm_target.style.height    = (parseInt(heightWithoutUnit) + 100) + "px";
				elm_target_iframe.style.height = height;
				// hide tinyMCE redimensionment
				if ($("text_html_" + id + "_resize")) $("text_html_" + id + "_resize").style.display = "none";
				// hide header dhtml
				if ($("text_dhtml_" + id + "_header_dhtml")) $("text_dhtml_" + id + "_header_dhtml").style.display = "none";
				// show header html
				if ($("text_dhtml_" + id + "_header_html")) $("text_dhtml_" + id + "_header_html").style.display   = "block";
			}
			else
			{
				// hide blue button
				if ($("img_text_html_" + id)) $("img_text_html_" + id).style.display = "none";
				// add or show toolbox
				if ($("toolbox_text_html_" + id)) {
					$("toolbox_text_html_" + id).style.display = "";
				}
				else {
					var img_add    = '<a onclick="kTranslation.saveTinyMCEcontent(' + id + ')"><img src="/images/icons/add.png" /></a>';
					var img_cancel = '<a onclick="kTranslation.removeTinyMCE(' + id + ')"><img src="/images/icons/cancel.png" /></a>';
					var toolbox = '<span id="toolbox_text_html_' + id + '">' + img_add + img_cancel + '</span>';
					new Insertion.After("img_text_html_" + id, toolbox);
				}
			}
		}
	},
	//
	addDElement: function (id) {
		if ($("text_dhtml_" + id)){
			// dhtml element
			//var elements = new Array("text_dhtml_" + id);
			this.kdelement.setElement("text_dhtml_" + id);
			// toggle header html
			if ($('text_dhtml_' + id + '_header_dhtml_button_html').style.visibility == "hidden") {
				$('text_dhtml_' + id + '_header_dhtml_button_html').style.visibility = "visible";
			}
			else {
				$('text_dhtml_' + id + '_header_dhtml_button_html').style.visibility = "hidden";
			}
		}
	},
	//
	dragOnEnd: function (object) {
		// - variables
		var id   = object.element.id;
		var top  = object.element.style.top;
		var left = object.element.style.left;
		// - ajax call
		// location
		var serverLocation = (translation_serverLocation + "tool_translation.ajax.php");
		// indicators
		var indicators = null;
		// options
		var options = {
			onComplete:function (obj) {
				var response = obj.kJSON;
				kTranslation.removeTinyMCE(id, response);
			},
			onFailure:function() {
				setDivHtml("error_message", message_connection_error, true, "FF0000")
			},
			method:'get',
			parameters:'id=' + id + '&top=' + top + '&left=' + left + '&action=drag_html'
		};
		kAjax.call(serverLocation,indicators,options);
	},
	//
	removeTinyMCE: function (id, isDHTML, response) {
		if ($("text_html_" + id)){
			// - get tinyMCE content into the text div
			if (response) {
				// get tinyMCE
				var ed = tinyMCE.get("text_html_" + id);
				// get content without the first p tag
				var content = ed.getContent();			
				content = content.substr(3, content.length - 7);
				// replace mce new content by our content (without the p tag)
				$("text_html_" + id).innerHTML = content;
			}
			// - remove tinyMCE
			tinyMCE.execCommand('mceRemoveControl', true, "text_html_" + id);
			// - hack - repositionning the text
			$("text_html_" + id).style.marginTop = "-15px";
			// - modifiy the toolbar
			// toolbar
			if (typeof(isDTML) == undefined || !isDHTML) {
				// show blue button
				if ($("img_text_html_" + id)) $("img_text_html_" + id).style.display = "";
				// add
				if ($("toolbox_text_html_" + id)) $("toolbox_text_html_" + id).style.display = "none";
			}
			else
			{
				// - resize
				// elements
				var elm_source = $("text_dhtml_" + id);
				// style
				//var top = elm_source.style.top;
				//var topWithoutUnit = top.substr(0,top.length-2);
				var height            = elm_source.style.height;
				var heightWithoutUnit = height.substr(0,height.length-2);
				elm_source.style.top       = this.top;
				elm_source.style.height    = (parseInt(heightWithoutUnit) - 100) + "px";
				// - toolbar
				// show header dhtml
				if ($("text_dhtml_" + id + "_header_dhtml")) $("text_dhtml_" + id + "_header_dhtml").style.display = "block";
				// hide header html
				if ($("text_dhtml_" + id + "_header_html")) $("text_dhtml_" + id + "_header_html").style.display   = "none"; 
			}
		}
	},
	//
	removeTinyMCE2: function (id, response) {
		if ($("text_html_" + id)){
			if (response == undefined || response)
			{
				// get tinyMCE
				var ed = tinyMCE.get("text_html_" + id);
				// get content without the first p tag
				var content = ed.getContent();			
				content = content.substr(3, content.length - 7);
				// remove mce
				tinyMCE.execCommand('mceRemoveControl', true, "text_html_" + id);
				// replace mce new content by our content (without the p tag)
				$("text_html_" + id).innerHTML = content;
				// show blue button
				if ($("img_text_html_" + id)) $("img_text_html_" + id).style.display = "";
				// add
				if ($("toolbox_text_html_" + id)) $("toolbox_text_html_" + id).style.display = "none";
			}
			else {
				setDivHtml("error_message", message_connection_error, true, "FF0000");
			}
		}
	},
	//
	resizeOnEnd: function (object) {
		// - variables
		var id     = object.element.id;
		var width  = object.element.style.width;
		var height = object.element.style.height;
		// - ajax call
		// location
		var serverLocation = (translation_serverLocation + "tool_translation.ajax.php");
		// indicators
		var indicators = null;
		// options
		var options = {
			onComplete:function (obj) {
				var response = obj.kJSON;
				kTranslation.removeTinyMCE(id, response);
			},
			onFailure:function() {
				setDivHtml("error_message", message_connection_error, true, "FF0000")
			},
			method:'get',
			parameters:'id=' + id + '&width=' + width + '&height=' + height + '&action=resize_html'
		};
		kAjax.call(serverLocation,indicators,options);
	},
	//
	saveTinyMCEcontent: function (id, isDHTML) {
		// - variables
		// get tinyMCE
		var ed = tinyMCE.get("text_html_" + id);
		// get content without the first p tag
		var content = ed.getContent();
		content = Url.encode(content.substr(3, content.length - 7));
		// - ajax call
		// location
		var serverLocation = (translation_serverLocation + "tool_translation.ajax.php");
		// indicators
		var indicators = null;
		// options
		var options = {
			onComplete:function (obj) {
				var response = obj.kJSON;
				kTranslation.removeTinyMCE(id, isDHTML, response);
			},
			onFailure:function() {
				setDivHtml("error_message", message_connection_error, true, "FF0000")
			},
			method:'get',
			parameters:'id=' + id + '&value=' + content + '&action=edit_html'
		};
		kAjax.call(serverLocation,indicators,options);
	},
	//
	openModal: function (id) {
		// size
		var size = {"height":450,"width":100};
		// buttons
		var buttons = {
			modify: {
				label:      translation_modify_button,
				action:     "kTranslation.openModalConfimed",
				parameter:  id
			},
			close: {
				label:  translation_close_button,
				action: "kModal.close"
			},
			test: "ok"
		};
		// text
		var span = document.getElementById("text_text_"+id);
		if (span)
		{
			var html = "";
			html+= translation_new_value;
			html+= '<input type="text" id="text_value" value="'+span.innerHTML+'" size="32" maxlength="64">';
		}
		// display
		kModal.displayMessage(html, size, buttons);
	},
	//
	openModalConfimed : function (id) {
		// - variables
		var value = $("text_value").value;
		// - ajax call
		// location
		var serverLocation = (translation_serverLocation + "tool_translation.ajax.php");
		// indicators
		var indicators = null;
		// options
		var options = {
			onComplete:function (obj) {
				var response = obj.kJSON;
				kTranslation.openModalOnComplete(response, id, value);
			},
			onFailure:function() {
				kModal.close();
				setDivHtml("error_message", message_connection_error, true, "FF0000")
			},
			method:'get',
			parameters:'id=' + id + '&value=' + value + '&action=edit'
		};
		kAjax.call(serverLocation,indicators,options);
	},
	//
	openModalOnComplete: function (response, id, value) {
		// replace text
		if (response){
			var span = $("text_text_"+id);
			if (span) {
				span.innerHTML = value;
			}
		}
		else {
			setDivHtml("error_message", message_connection_error, true, "FF0000");
		}
		// close modal
		kModal.close();
	}
}
kTranslation = new kTranslation();
