/**
 * This delegate handle the translation inline editor and hopefully
 * make it easier to translate texts.
 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
 * @since 1.0.0
 */
var MooTranslationDelegate = 
{
	/**
	 * @var array The windows.
	 */
	windows : null,

	/**
	 * Initialize the delegate.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	initialize : function(el) {
		this.windows = [];
	},

	/**
	 * This event happens when the mouse enters
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	onTranslationTextMouseEnter : function(el) {
		$$('.translation-inline-editor').dispose();

		var translationId = el.getId().replace('translation-', '');
		var translationText = el.getHtml();
		translationText = translationText.replace(/<br \/>/g, "");
		translationText = translationText.replace(/<br\/>/g, "");
		translationText = translationText.replace(/<br>/g, "");
		var translationPosition = el.getCoordinates();
		// its quite possible the translation window is already opened so in this case
		// we do not need and should not open the window
		var editorWindow = document.getElement('.translation-inline-editor-for-' + translationId);
		if (editorWindow == null) {
			this.createEditorWindow(translationId, translationText, translationPosition.left, translationPosition.top + translationPosition.height);
		}
	},

	/**
	 * This event happens when the mouse leaves the translation editor.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	onTranslationTextMouseLeave : function(el) {
	},
	
	/**
	 * This event happens when the mouse enter the editor window.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	onEditorWindowMouseEnter : function() {
	},
	
	/**
	 * This event happens when the mouse leave the editor window.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	onEditorWindowMouseLeave : function() {
		//$$('.translation-inline-editor').dispose();
	},
	
	/**
	 * This event happens when the mouse hit the editor window.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	onEditorWindowMouseDown : function() {
	},
	
	/**
	 * This event happens when the mouse enters
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	createEditorWindow : function(translationId, translationText, left, top) {

		// create the window by making a clone of the current form
		var editorWindow = $('translation-inline-editor').clone(true);
		editorWindow.addClass('translation-inline-editor')
		editorWindow.addClass('translation-inline-editor-for-' + translationId);
		editorWindow.setStyle('top', top + 'px');
		editorWindow.setStyle('left', left + 'px');
		editorWindow.setStyle('display', 'block');
		editorWindow.setStyle('position', 'absolute');
		editorWindow.store('translation_id', translationId);
		editorWindow.addEvent('mouseenter', this.onEditorWindowMouseEnter.bind(this));
		editorWindow.addEvent('mouseleave', this.onEditorWindowMouseLeave.bind(this));
		editorWindow.addEvent('mousedown', this.onEditorWindowMouseDown.bind(this));
		editorWindow.inject(document.body);
		
		// add the event that will expand the window to edit the text
		// on the edit text
		var editButton = editorWindow.getElement('.edit');
		if (editButton) {
			editButton.addEvent('click', function(el) {
				new Fx.Tween(editorWindow, {property: 'width', duration: 'long'}).start([80,500]);
				new Fx.Tween(editorWindow, {property: 'height', duration: 'long'}).start([25,350]);
			});
		}

		// add the text into the textarea
		var textarea = editorWindow.getElement('textarea');
		if (textarea) {
			textarea.value = translationText;
		}
		// add an event on the main window that will make sure the window is put on
		// the top of the other when it gets clicked
		editorWindow.addEvent('mousedown', function(e) { 
			this.putEditorWindowOnTop(e.target); 
		}.bind(this));
		// add an event on all the elements inside that window that will also make sure
		// it comes on the top
		editorWindow.getElements('*').each(function(el) {
			el.addEvent('mousedown', function(e) { 
				this.putEditorWindowOnTop(e.target.getParent('.translation-inline-editor')); 
			}.bind(this));
		}.bind(this));
		// update the form action so the translation id parameter is updated
		// to the current translation id
		var form = editorWindow.getElement('form');
		if (form) {
			form.action = form.action.replace('1111', translationId);
		}
		// add the event that will hide the window when the user clicks
		// on the close button
		var closeButton = editorWindow.getElement('.close-button');
		if (closeButton) {
			closeButton.addEvent('click', function(el) {
				editorWindow.dispose();
			});
		}
		// make the window draggeable
		var windowDrag = new Drag.Move(editorWindow, { handle : editorWindow.getElement('.title') });
		// finally we set a z index on the window to make the last apppear
		this.putEditorWindowOnTop(editorWindow);
		// save the window and save it's position in the stack, t
		this.windows.push(editorWindow);
	},
	
	/**
	 * Put the given window on the top of the others.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	putEditorWindowOnTop : function(editorWindow) {
		var topIndex = 1000;
		this.windows.each(function(editorWindowItem, index) {
			var editorWindowZIndex = parseInt(editorWindowItem.getStyle('z-index'));
			if (editorWindowZIndex > topIndex) {
				topIndex = editorWindowZIndex;
			}
		}.bind(this));
		topIndex = topIndex + 1;
		// update the editor window z index
		editorWindow.setStyle('z-index', topIndex);
	}	
};

new MooSelector().start
({
	/**
	 * Initialize the delegate.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	'body #translation-inline-editor' : function(el) {
		MooTranslationDelegate.initialize(el);
	},

	/**
	 * Show the inline editor when the user move his mouse over an editable text.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	'body .translation-text::mouseenter' : function(el) {
		MooTranslationDelegate.onTranslationTextMouseEnter(el);
	}
});
