//----------------------------------------------------------------------------------------------------------------------
/**
 * This file contains the {@link ApolloTemplate_Helper_NicEdit} class
 *
 * @author      Blue Moon IT <info@bluemoonit.net>
 * @copyright   2008, Blue Moon IT
 * @package     BlueMoonIt--ApolloTemplate
 * @subpackage  JS
 * @version     SVN: $Id: page_view_nicedit.js 100 2009-03-06 09:16:02Z johan $
 */
//----------------------------------------------------------------------------------------------------------------------
/**
 * Class to help with nicEdit creation for content parts on the view of a page
 *
 * @package     BlueMoonIt--ApolloTemplate
 * @subpackage  JS
 * @author      Johan B.W. de Vries <johan@bluemoonit.net>
 */
ApolloTemplate_Helper_NicEdit = function()
{
	//------------------------------------------------
	/**
	 * Message to be displayed when a page was successfully saved
	 *
	 * @var  string
	 */
	this.msgPageSuccessfullySaved = 'Page successfully saved';
	//------------------------------------------------
	/**
	 * Message to be displayed in the popup box asking the user to describe their change
	 *
	 * @var  string
	 */
	this.msgDescribeYourChange    = 'Describe your change';
	//------------------------------------------------
	/**
	 * Message to be set as title to notify users that they have to doubleclick an HTML element before they can edit it
	 *
	 * @var  string
	 */
	this.msgDoubleClickToEdit     = 'Double click to edit';
	//------------------------------------------------
	/**
	 * Contains the list of (ids of) the HTML elements that hold the content parts
	 *
	 * Note that each HTML element must have the "data-content-part-name" attribute set
	 *
	 * @var  array
	 */
	this.boxList = [];
	//------------------------------------------------
	/**
	 * Contains the path where the icons for the panel are
	 *
	 * var  string
	 */
	this.iconsPath = '/nicEdit/nicEditorIcons.gif';
	//------------------------------------------------
	/**
	 * Contains the id of the HTML element that should hold the panel
	 *
	 * @var  string
	 */
	this.panelId = 'myNicPanel';
	//------------------------------------------------
	/**
	 * Contains the id of the page
	 *
	 * @var  integer
	 */
	this.pageId = 0;
	//------------------------------------------------
	/**
	 * Contains the language of the page translation
	 *
	 * @var  string
	 */
	this.pageTranslationLanguage = 'xx_XX';
	//------------------------------------------------
	/**
	 * Contains the current revision of the page translation
	 *
	 * @var  integer
	 */
	this.pageTranslationRevision = 0;
	//------------------------------------------------
	/**
	 * Contains the list of buttons to display on the nicedit panel
	 *
	 * @var  integer
	 */
	this.buttonList = [
		'save',
		'bold',
		'italic',
		'underline',
		'left',
		'center',
		'right',
		'justify',
		'ol',
		'ul',
		'subscript',
		'superscript',
		'strikethrough',
		'removeformat',
		'indent',
		'outdent',
		'hr',
		'image',
		'link',
		'unlink',
		'fontFormat'
	];
	//------------------------------------------------
	/**
	 * Gets called when the page is updated. Alerts the user.
	 *
	 * @param  dtmlXMLLoaderObject  loader  The loader
	 *
	 * @return  void
	 */
	this.onUpdatedPage = function(loader)
	{
		result = BlueMoonIt_App.parseResult(loader);

		if( 'INVALID_RESULT' == result.errorCode ) {
			alert(loader.xmlDoc.responseText);
			return;
		}

		if( 'none' != result.errorCode ) {
			alert(errorNode.textContent);
			return;
		}

		contentPartName = loader.doXPath('/result/contentmodule_page_translation_content_part__name', null, null, 'single').textContent;
		element         = null;
		for(var i = 0; (i < ApolloTemplate_Helper_NicEdit__instanceList.length) && (null == element); i++) {
			instance = ApolloTemplate_Helper_NicEdit__instanceList[i];

			for(var j = 0; (j < instance.boxList.length) && (null == element); j++) {
				box = instance.boxList[j];

				if( 'object' != typeof(box) ) {
					continue;
				}

				if( box.getAttribute('data-content-part-name') == contentPartName ) {
					element = box;
				}
			}
		}

		if( null === element ) {
			alert('No such content part: "' + contentPartName + '"');
			return;
		}

		var pageViewNicEdit = element.pageViewNicEdit;

		alert(pageViewNicEdit.msgPageSuccessfullySaved);
		pageViewNicEdit.pageTranslationRevision = loader.doXPath('/result/contentmodule_page_translation__revision', null, null, 'single').textContent;
	}
	//------------------------------------------------
	/**
	 * Creates a new instance of the nicedit panel
	 *
	 * @return  ApolloTemplate_Helper_NicEdit  Provides a fluent interface
	 */
	this.create = function()
	{
		if( 0 == this.boxList.Length ) {
			return;
		}

		for(var i = 0; i < this.boxList.length; i++) {
			if( 'string' == typeof(this.boxList[i]) ) {
				this.boxList[i] = document.getElementById(this.boxList[i]);
			}
		}

		this.nicEditor = new nicEditor({
			iconsPath: this.iconsPath,
			onSave: function(content, id, instance) {
				var element         = document.getElementById(id);
				var pageViewNicEdit = element.pageViewNicEdit;
				var contentPartName = element.getAttribute('data-content-part-name');

				var editSummary = prompt(pageViewNicEdit.msgDescribeYourChange, '');
				if( null == editSummary ) {
					return;
				}

				dhtmlxAjax.post(
					BlueMoonIt_App.basePath + 'content/page/translate/',
					'id=' + pageViewNicEdit.pageId
					  + '&language=' + pageViewNicEdit.pageTranslationLanguage
					  + '&part=' + contentPartName
					  + '&previousRevisionWas=' + pageViewNicEdit.pageTranslationRevision
					  + '&content=' + encodeURIComponent(content)
					  + '&edit_summary=' + encodeURIComponent(editSummary),
					pageViewNicEdit.onUpdatedPage
				);
			},
			buttonList: this.buttonList
		});

		this.nicEditor.setPanel(this.panelId);

		for(var i = 0; i < this.boxList.length; i++) {
			this.boxList[i].oldTitle = this.boxList[i].title;
			this.boxList[i].title    = this.msgDoubleClickToEdit;

			this.boxList[i].nicEditor       = this.nicEditor;
			this.boxList[i].pageViewNicEdit = this;

			this.boxList[i].ondblclick = function(event) {
				this.nicEditor.addInstance(this.id);

				this.title      = this.oldTitle;
				this.oldTitle   = null;
				this.ondblclick = null;

				if (event.preventDefault)
					event.preventDefault();
				else
					event.returnValue= false;
				return false;
			}
		}

		return this;
	}
	//------------------------------------------------
	/**
	 * Constructor code
	 */
	ApolloTemplate_Helper_NicEdit__instanceList.push(this);
	//------------------------------------------------
	return this;
}
//----------------------------------------------------------------------------------------------------------------------
/**
 * Contains a list of all the ApolloTemplate_Helper_NicEdit instances
 *
 * Needed 'cause ApolloTemplate_Helper_NicEdit.onUpdatedPage doesn't have direct access to the right instance
 *
 * @var  Array
 */
ApolloTemplate_Helper_NicEdit__instanceList = new Array();
//----------------------------------------------------------------------------------------------------------------------

