
var uploadUrl = '/index.php?option=com_comment&controller=comment&task=upload';
var tempUploadUrl = '/index.php?option=com_comment&controller=comment&task=getTempUpload';

function customEditorConfig(config) {
	config.toolbarCanCollapse = false;
	config.resize_minHeight = 150;
	config.disableObjectResizing = true;
	config.startupFocus = false;	
	config.plugins += ',bbcode';
	config.bodyClass = 'tor_comm_text';	
	config.resize_maxWidth = '100px';

}

var dialogEvent = null;
var imagesEnabled = 0;

function imageUploaded() {
	dialogEvent.data.definition.dialog.hide();
	//dialogEvent.editor.insertHtml('<img src="'+tempUploadUrl+'" />');
	dialogEvent.editor.insertHtml('[img]'+tempUploadUrl+'&t='+(new Date()).getTime()+'[/img]');
}


CommentSaver = new Class({

	submitting : false,
	ready : false,
	storage : {setItem:function(){},getItem:function(){},removeItem:function(){}},

	initialize : function(editor) {
		this.editor = editor;

		var form = $(editor.element.$.form);
		this.key = 'comments.edit.'+form.content_id.value+'.'+form.comment_id.value;

		if (sessionStorage) {
			this.storage = sessionStorage;
		}

		//intercept form submit in js and normal
		this.submitForm = form.submit.bind(form);
		form.submit = this.submit.bind(this);
		form.addEvent('submit', form.submit);

		//CKEDITOR.on('currentInstance', this.updateStorage.bind(this));
		window.addEvent('unload', this.updateStorage.bind(this));

		this.editor.on('dataReady', function() {
			 this.ready = true;
		}.bind(this));

		this.updateEditor();
	},
	
	submit : function() {
		this.submitting = true;
		this.updateStorage();
		this.submitForm();
	},

	updateEditor : function() {
		var text = this.storage.getItem(this.key);
		if (text) {
			this.editor.setData(text);
		}
		this.updateStorage();
	},

	updateStorage : function() {
		if (!this.ready) {
			this.updateStorage.delay(50, this);
			return;
		}
		
		if (this.submitting) {
			this.storage.removeItem(this.key);
		} else {
			var text = this.editor.getData();
			if (text) {
				this.storage.setItem(this.key, text);
			} else {
				this.storage.removeItem(this.key);	
			}
		}
	}
});

CKEDITOR.on('instanceCreated', function(evt) {
	var editor = evt.editor;
	editor.on( 'customConfigLoaded', function() {
		if (!imagesEnabled) {
			editor.config.toolbar_basic[2] = ['Link'];	
		}
		editor.config.removePlugins += ',jhtmlencode,elementspath';		
}, null, null, 100);
});	

CKEDITOR.on('instanceReady', function(ev) {
	new CommentSaver(ev.editor);

	if (ev.editor._.menuItems) {
		ev.editor._.menuItems.jlink = null;
	}
});

CKEDITOR.on('dialogDefinition', function(ev) {

	dialogEvent = ev;

	var dialogName = ev.data.name, dialogDefinition = ev.data.definition,
		onShow = dialogDefinition.onShow, onOk = dialogDefinition.onOk;

	dialogDefinition.onShow = function() {
		var element;
		if (element = ev.editor.getSelection().getSelectedElement()) {
			if (element.getAttribute('src').indexOf(tempUploadUrl) != -1) {
				element.setAttribute('_cke_saved_src', '{UPLOADED}');					
			}
		}
		onShow.apply(this);
	};
	dialogDefinition.onOk = function() {
		onOk.apply(this);
		if (this.imageElement && this.imageElement.getAttribute('src') == '{UPLOADED}') {
			this.imageElement.setAttribute('src', tempUploadUrl+'&t='+(new Date()).getTime());
		}
	};

	switch (dialogName) {
		case 'link':
			dialogDefinition.removeContents('target');
			dialogDefinition.removeContents('advanced');
			var infoTab = dialogDefinition.getContents('info');
			infoTab.remove('linkType');
			infoTab.remove('browse');
			dialogDefinition.minHeight = 100;
			dialogDefinition.onFocus = function() {
				var urlField = this.getContentElement('info', 'url');
				urlField.select();
			};
			break;
		case 'image':
			dialogDefinition.removeContents('Link');
			dialogDefinition.removeContents('advanced');
			var infoTab = dialogDefinition.getContents('info');	
			infoTab.elements[2].style = 'display:none';				
			infoTab.remove('browse');
			infoTab.remove('txtAlt');
			
			var uploadTab = dialogDefinition.getContents('Upload');
			uploadTab.hidden = false;
			uploadTab.elements[0].action = uploadUrl;

			dialogDefinition.minHeight = 100;
			break;				
	}
});


