MLP.VisualEditor = {

	mode : 'advanced',
	prepared : false,
	main_language : 'en',
	language : '',

	init : function(mode, visual_editor_off, main_language, language) {

		MLP.VisualEditor.mode = mode;
		MLP.VisualEditor.main_language = main_language;
		MLP.VisualEditor.language = language;

		if ( !visual_editor_off ) {
			MLP.VisualEditor.prepare_editor(MLP.VisualEditor.init_editor);
		}
		element_addEvent($('editor_simple'), 'click', MLP.VisualEditor.hide_visual_editor);
		element_addEvent($('editor_visual'), 'click', MLP.VisualEditor.show_visual_editor);
		element_addEvent($('preview_entry'), 'click', MLP.VisualEditor.hide_preview);
	},
	prepare_editor : function(afterPrepare) {
			if ( !MLP.VisualEditor.prepared ) {
			var languages = (MLP.VisualEditor.language != MLP.VisualEditor.main_language) ? MLP.VisualEditor.language + ", " + MLP.VisualEditor.main_language : MLP.VisualEditor.language;
			MLP.ControlPanel.wait(MLP.Lang.get('editor_is_loading'));		
			if ( 'simple' == MLP.VisualEditor.mode ) {
				afterPrepare = afterPrepare || MLP.VisualEditor.init_simple_editor;
				tinyMCE_GZ.init(
					{
						themes : "advanced",
						plugins : "safari,advimage,emotions,advlink,tabfocus,mlp", 
						languages : languages,
						page_name : 'tiny_mce_gzip_3',
						disk_cache : true
						//suffix : '_src' // if uncommented, source codes will be loaded
					}, 
					afterPrepare
				);
				MLP.ControlPanel.stopWait();
			} else {
				afterPrepare = afterPrepare || MLP.VisualEditor.init_advanced_editor;
				tinyMCE_GZ.init(
					{
						themes : "advanced",
						plugins : "safari,table,advhr,advimage,emotions,advlink,print,fullscreen,tabfocus,mlp", 
						languages : languages,
						page_name : 'tiny_mce_gzip_3',
						disk_cache : true
						//suffix : '_src' // if uncommented, source codes will be loaded
					}, 
					afterPrepare
				);
				MLP.ControlPanel.stopWait();
			}
		}
	},

	init_simple_editor : function() {
		tinyMCE.init({
			mode : "textareas",
			theme : "advanced",
			language : MLP.VisualEditor.language,
			plugins : "safari,advimage,advlink,tabfocus,mlp,paste,emotions",
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,link,unlink,|,bullist,numlist,|,outdent,indent,blockquote,emotions",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : false,
			theme_advanced_resizing : false,
			theme_advanced_resize_horizontal : false,
			paste_auto_cleanup_on_paste : true,
			paste_convert_headers_to_strong : false,
			paste_strip_class_attributes : "all",
			paste_remove_spans : false,
			paste_remove_styles : false,
			relative_urls : false, // do not use relative URLs
			remove_script_host : false, // do not strip domain part of URL
			tab_focus : ':prev,:next',
			editor_selector : "mceEditor", // className of textarea that could be visual
			force_br_newlines : true // On [Enter] insert <br/> instead of <p></p>	(this option must be last)
		});
		$('message_text_decor').className = '';
		$('message_use_wiki').style.display = 'none';
		MLP.VisualEditor.prepared = true;
		MLP.ControlPanel.stopWait();
	},
	
	init_advanced_editor : function() {
		tinyMCE.init({
			mode : "textareas",
			theme : "advanced",
			language : MLP.VisualEditor.language,
			plugins : "safari,advimage,advlink,tabfocus,mlp,paste,emotions",
			theme_advanced_buttons1 : "pasteword,removeformat,|,bold,italic,underline,strikethrough,|,undo,redo,|,bullist,numlist,|,outdent,indent,blockquote,|,justifyleft,justifycenter,justifyright,justifyfull,|",
			theme_advanced_buttons2 : "link,unlink,|,emotions,mlpcut,mlpimage,mlpvideo,mlpaudio,mlpobject",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
			theme_advanced_resize_horizontal : false,
			theme_advanced_styles : "Code=codeStyle;Quote=quoteStyle",
			remove_linebreaks : false,
			paste_auto_cleanup_on_paste : true,
			paste_convert_headers_to_strong : false,
			paste_strip_class_attributes : "all",
			paste_remove_spans : false,
			paste_remove_styles : false,
			relative_urls : false, // do not use relative URLs
			remove_script_host : false, // do not strip domain part of URL
			tab_focus : ':prev,:next',
			editor_selector : "mceEditor", // className of textarea that could be visual
			force_br_newlines : true // On [Enter] insert <br/> instead of <p></p> (this option must be last)
		});
		$('message_text_decor').className = '';
		$('message_use_wiki').style.display = 'none';
		MLP.VisualEditor.prepared = true;
		MLP.ControlPanel.stopWait();
	},

	show_visual_editor : function() {

		var myAjax = new Ajax.Request('/ajax/visual_editor_on', { method: "post", postBody: "visual_editor_off=0" } );
	
		MLP.VisualEditor.prepare_editor();
		if ( 'undefined' != typeof(tinyMCE) && !tinyMCE.get('message_text') ) {
			tinyMCE.execCommand('mceAddControl', false, 'message_text');
		}

		$('editor_simple').className = '';
		$('editor_visual').className = 'selected_editor';
		$('message_text_decor').className = '';
		$('message_use_wiki').style.display = 'none';
	},

	hide_visual_editor : function() {

		var myAjax = new Ajax.Request('/ajax/visual_editor_off/', { method: "post", postBody: "visual_editor_off=1" } );

		if ( 'undefined' != typeof(tinyMCE) && tinyMCE.get('message_text') ) {
			tinyMCE.execCommand('mceRemoveControl', false, 'message_text');
		}

		$('editor_simple').className = 'selected_editor';
		$('editor_visual').className = '';
		$('message_text_decor').className = 'decorated';
		$('message_use_wiki').style.display = '';
	},

	show_preview : function(text_id, subj_id, service) {

		MLP.ControlPanel.wait(MLP.Lang.get('preview_is_loading'));

		var text = '', subject = '';

		if ( $(subj_id) ) {
			subject = $(subj_id).value;
		}
		service = service || "";

		if ( 'undefined' != typeof(tinyMCE) && tinyMCE.get('message_text') ) {
			text = tinyMCE.get('message_text').getContent(); 
		} else {
			text = $(text_id).value;
		}

		var data = "subject="+encodeURIComponent(subject) + "&text=" + encodeURIComponent(text) + "&service=" + encodeURIComponent(service);
		var myAjax = new Ajax.Request("/ajax/preview/", { method: "post", postBody: data, onComplete: MLP.VisualEditor.preview_complete } );
	},

	preview_complete : function(response) {

		MLP.ControlPanel.stopWait();

		$('editor_simple').style.display = 'none';
		$('editor_visual').style.display = 'none';
		$('preview_entry').style.display = '';

		$('textareadecor').style.display = 'none';
		$('preview_text').innerHTML = response.responseText;
		$('preview_text').style.display = '';
		$('message_text_decor').className = 'decorated';

		$('preview_title').style.display = 'block';
	},

	hide_preview : function() {

		$('editor_simple').style.display = '';
		$('editor_visual').style.display = '';
		$('preview_entry').style.display = 'none';
		
		$('textareadecor').style.display = '';
		$('preview_text').style.display = 'none';

		if ( 'undefined' != typeof(tinyMCE) && !tinyMCE.get('message_text') ) {
			$('message_text_decor').className = 'decorated';
		} else {
			$('message_text_decor').className = '';
		}
		
		$('preview_title').style.display = 'none';
	}
}

function send_preview(text_id, subj_id, service) {
	MLP.VisualEditor.show_preview(text_id, subj_id, service);
}

function insert_file_tinyMCE(t,n) {
	tinyMCE.execCommand("mceFocus", null, MLP.FileSelector.editor_id);
	switch(t) { 
		case 31: 
			tinyMCE.execCommand("mceInsertContent", false, "[[Image:" + n + "]]")
		break;
		case 32:
			tinyMCE.execCommand("mceInsertContent", false, "[[Video:" + n + "]]")
		break;
		case 33:
			tinyMCE.execCommand("mceInsertContent", false, "[[MP3Button:" + n + "]] " + n)
		break;
	}
	fs_hide();
}
