/*  Rich Text Editor Adapter in JavaScript. *
 *		Author: Rostislav Brizgunov			*/

var RTE_DEFAULT_WIDTH = 920;
var RTE_DEFAULT_HEIGHT = 700;
var RTE_DEFAULT_SAVE_MODE = 0;
var RTE_DEFAULT_FORM_MANAGE_MODE = 0;
var RTE_DEFAULT_LANGUAGE = "en";

function RTE(editor) {

	if (editor) this.editor = editor
	else this.editor = "TinyMCE";
	this.width = RTE_DEFAULT_WIDTH?RTE_DEFAULT_WIDTH:800;
	this.height = RTE_DEFAULT_HEIGHT?RTE_DEFAULT_HEIGHT:600;
	this.mode = "exact";
	this.elements = "RTE_Field";
	this.save_mode = RTE_DEFAULT_SAVE_MODE?RTE_DEFAULT_SAVE_MODE:0;
	this.form_manage_mode = RTE_DEFAULT_FORM_MANAGE_MODE?RTE_DEFAULT_FORM_MANAGE_MODE:0;
	this.css_classes_list = "";
	this.language = RTE_DEFAULT_LANGUAGE?RTE_DEFAULT_LANGUAGE:"en";
	
	this.init = function() {
		if (this.editor=="TinyMCE") {
			var save_mode_str = this.save_mode + "";
			var save_text = (save_mode_str == '0')?"":((save_mode_str == '1')?",negeso_local_save,negeso_local_save_and_exit":((save_mode_str == '2')?",negeso_local_save,negeso_local_save_and_exit,negeso_global_save,negeso_global_save_and_exit":((save_mode_str == '3')?",negeso_global_save,negeso_global_save_and_exit":"")));
			var form_manager_text_part_1 = ",|,form_list";
			var form_manager_text_part_2 = "form_text,form_password,form_submit,form_reset,form_checkbox,form_radio,form_radio_group,form_file,form_textarea,form_select,form_cleanup,|,";
			
			switch (this.form_manage_mode) {
				case 0: form_manager_text_part_2 = ""; break;
				case 1: form_manager_text_part_1 = ""; break;
			}
			
			save_text += ",negeso_exit";
			tinyMCE.init({
				mode : this.mode?this.mode:"textareas",
				elements : this.elements?this.elements:null,
				plugins : "style,layer,table,negeso_save,advhr,advimage,negeso_template,negeso_advimage,negeso_flash,negeso_advlink,advlink,"+/*"negeso_emotions,"+*/"negeso_form_manager,negeso_import_style,"+/*"iespell,"+*/"insertdatetime,preview,zoom,flash,searchreplace,print,contextmenu,paste,directionality,fullscreen",
				theme : "advanced",
				theme_advanced_buttons1 : "|,cut,copy,pasteword,pastetext,selectall,negeso_clear_document,cleanup,|,search,replace,|,undo,redo,",
				theme_advanced_buttons2 : "|"+/*",styleprops"*/",formatselect,fontselect,fontsizeselect,styleselect,removeformat,|",
				theme_advanced_buttons3 : "|,tablecontrols,|,",
				theme_advanced_buttons1_add_before : "|,newdocument"+save_text+",|,print,preview,|,fullscreen,code",
				theme_advanced_buttons1_add : "|,link,negeso_advlink_to_page,negeso_advlink_to_file,negeso_advlink_to_image,anchor,unlink,|,image,negeso_advimage_add,negeso_advimage_media,negeso_flash,|,negeso_template"+form_manager_text_part_1+",|,visualaid,zoom,help",
				theme_advanced_buttons2_add : ",bold,italic,underline,strikethrough,|,sub,sup,|,justifyleft,justifycenter,justifyright,justifyfull,|,numlist,bullist,outdent,indent,|,backcolor,forecolor",
				theme_advanced_buttons3_add : form_manager_text_part_2+"ltr,rtl,|,advhr,charmap,"+/*"emotions,"+*/"insertdate,inserttime,|,insertlayer,moveforward,movebackward,absolute",
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				theme_advanced_path_location : "bottom",
				plugin_insertdate_dateFormat : "%Y-%m-%d",
				plugin_insertdate_timeFormat : "%H:%M:%S",
				valid_elements : "+a[id|style|rel|rev|charset|hreflang|dir|lang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup],-strong/-b[class|style],-em/-i[class|style],-strike[class|style],-u[class|style],#p[id|style|dir|class|align],-ol[class|style],-ul[class|style],-li[class|style],br,img[id|dir|lang|longdesc|usemap|style|class|src|onmouseover|onmouseout|onclick|border=0|alt=|title|hspace|vspace|width|height|align],-sub[style|class],-sup[style|class],-blockquote[dir|style],-table[border=0|cellspacing|cellpadding|width|height|class|align|summary|style|dir|id|lang|bgcolor|background|bordercolor],-tr[id|lang|dir|class|rowspan|width|height|align|valign|style|bgcolor|background|bordercolor],tbody[id|class],thead[id|class],tfoot[id|class],-td[id|lang|dir|class|colspan|rowspan|width|height|align|valign|style|bgcolor|background|bordercolor|scope],-th[id|lang|dir|class|colspan|rowspan|width|height|align|valign|style|scope],caption[id|lang|dir|class|style],-div[id|dir|class|align|style],-span[style|class|align],-pre[class|align|style],address[class|align|style],-h1[id|style|dir|class|align],-h2[id|style|dir|class|align],-h3[id|style|dir|class|align],-h4[id|style|dir|class|align],-h5[id|style|dir|class|align],-h6[id|style|dir|class|align],hr[class|style],-font[face|size|style|id|class|dir|color],dd[id|class|title|style|dir|lang],dl[id|class|title|style|dir|lang],dt[id|class|title|style|dir|lang]",
				extended_valid_elements : "hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
				//extended_valid_elements : "img[id|dir|lang|longdesc|usemap|style|class|src|onmousedown|onmouseover|onmouseout|onclick|border|alt=|title|hspace|vspace|width|height|align]",
				extended_valid_elements : "input[name|id|title|style|class|type=text|value|checked|readonly|size|maxlength|tabindex|required|numeric_field_params|is_email|timedate_field_format|onclick]," +
										  //"form[name|id|title|style|enctype|action|method]," +
										  "textarea[name|id|title|style|class|rows|cols|readonly|tabindex|value|required]," +
										  "select[name|id|title|style|class|size|multiple|tabindex]," +
										  "option[id|title|style|class|selected|value|inner_text]," +
										  "-div[align<center?justify?left?right|class|dir<ltr?rtl|id|lang|onclick|ondblclick|onkeydown|onkeypress|onkeyup|onmousedown|onmousemove|onmouseout|onmouseover|onmouseup|style|title|negeso_form_id]",
				cleanup_on_startup : true,
				theme_advanced_resizing_use_cookie : false,
				theme_advanced_resize_horizontal : false, // <-- true?
				theme_advanced_resizing : false, // <-- true?
				language : this.language,
				content_css : "/site/css/content_styles.css",
				editor_css : "/rte/negeso_custom_css/Negeso_RTE_Main.css",
				//popups_css : "/rte/negeso_custom_css/Negeso_RTE_Popup.css",
				//content_css : "/rte/negeso_custom_css/Negeso_RTE_Content.css",
				width: this.width?this.width+"":"975",
				height: this.height?this.height+"":"490",
				paste_auto_cleanup_on_paste: true,
				paste_use_dialog: false,
				paste_strip_class_attributes: "all",
				custom_shortcuts: true,
				oninit: function(){setTimeout(function(){tinyMCE.negeso_backup_content = tinyMCE.getContent();},500);},
				convert_fonts_to_spans : true,
				font_size_style_values : "11px, 12px, 14px, 18px, 24px, 30px, 46px"
			});

			switch (this.form_manage_mode) {
				case 0: tinyMCE.negeso_save_form_mode = "as_form_insert"; break;
				case 1: tinyMCE.negeso_save_form_mode = "as_form_manager"; break;
			}
			
			tinyMCE.negeso_css_classes_list = this.css_classes_list;
			
			this.registerPreRequestFunction(RTE_Form_Request);
			return true
		}
		return false
	};
	
	this.setDestination = function(destination_IDs) {
		if (typeof(destination_IDs)=="undefined") destination_IDs = "RTE_Field";
		if (this.editor=="TinyMCE") {
			tinyMCE.dest_ids = destination_IDs;
			return true
		}
		return false
	};

	this.setSource = function(source_ID) {
		if (typeof(source_ID)=="undefined") source_ID = "RTE_Field";
		if (this.editor=="TinyMCE") {
			tinyMCE.source_id = source_ID;
			return true
		}
		return false
	}

	this.setArticle = function(article_ID) {
		if (typeof(article_ID)=="undefined") article_ID = -1;
		if (this.editor=="TinyMCE") {
			tinyMCE.article_id = article_ID;
			return true
		}
		return false		
	}
	
	// Privat part of class. It is almost useless for directly calls
	this.registerPreRequestFunction = function(func_name) {
		if (this.editor=="TinyMCE") {
			tinyMCE.negeso_pre_request = function(content,id) { return func_name(content,id) };
			return true
		}
		return false
	}
	
}

// CONSTANT OBJECT: Need for making some ID-transport between site and pop-up RTE window
var RTE_Transporter = {
	source_id : "",
	dest_ids : "",
	article_id : "",
	save_mode : 0,
	form_manage_mode : 0,
	language : "en"
};

function RTE_Init(source_id, dest_ids, article_id, save_mode, form_manage_mode, css_classes_list, language) {

	if (typeof(source_id) == "undefined" || source_id == null) source_id = "RTE_Field";
	if (typeof(dest_ids) == "undefined" || dest_ids == null) dest_ids = source_id;
	if (typeof(article_id) == "undefined" || article_id == null) article_id = source_id.substr(12,source_id.length-1);
	if (typeof(save_mode) == "undefined" || save_mode == null) save_mode = RTE_DEFAULT_SAVE_MODE?RTE_DEFAULT_SAVE_MODE:1;
	if (typeof(form_manage_mode) == "undefined" || form_manage_mode == null) form_manage_mode = RTE_DEFAULT_FORM_MANAGE_MODE?RTE_DEFAULT_FORM_MANAGE_MODE:0;	
	if (typeof(css_classes_list) == "undefined" || css_classes_list == null) css_classes_list = '';
	if (typeof(language) == "undefined" || language == null) language = "en";

	RTE_Transporter.source_id = source_id;
	RTE_Transporter.dest_ids = dest_ids;
	RTE_Transporter.article_id = article_id;
	RTE_Transporter.save_mode = save_mode;
	RTE_Transporter.form_manage_mode = form_manage_mode;
	RTE_Transporter.css_classes_list = css_classes_list;
	RTE_Transporter.language = language;
	
	var win_width = RTE_DEFAULT_WIDTH?RTE_DEFAULT_WIDTH:800;
	var win_height = RTE_DEFAULT_HEIGHT?RTE_DEFAULT_HEIGHT:600;
	var win_left = (screen.availWidth - win_width)/2;
	var win_top = (screen.availHeight - win_height)/2;
	var new_window = window.open("/rte/RTE_Template.html","Negeso_RTE_Popup_Window","directories=0,width="+win_width+",height="+win_height+",left="+win_left+",top="+win_top+",location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,modal=no");
	new_window.focus();
}

function RTE_Form_Request(content,id) {
	if (content == null || typeof(content) == "undefined") content = "";
	var temp = ''+
		'<SOAP-ENV:Envelope'+'\n'+
		'		xmlns=""'+'\n'+
		'		xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"'+'\n'+
		'		SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"'+'\n'+
		'		xmlns:xsd="http://www.w3.org/2001/XMLSchema"'+'\n'+
		'		xmlns:tns="http://negeso.com/2003/Framework-v.1.1/wsdl/serviceAdmin/serviceAdmin"'+'\n'+
		'		xmlns:ns2="http://negeso.com/2003/Framework-v.1.1/wsdl/serviceAdmin/types/serviceAdmin"'+'\n'+
		'		xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"'+'\n'+
		'		xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"'+'\n'+
		'		xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"'+'\n'+
		'		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+'\n'+
		'	<SOAP-ENV:Header/>'+'\n'+
		'	<SOAP-ENV:Body>'+'\n'+
		'		<mswsb:auxOperation xmlns:mswsb="http://negeso.com/2003/Framework-v.1.1/wsdl/serviceAdmin/serviceAdmin">'+'\n'+
		'			<operationKind xsi:type="xsd:string">update-article-text-command</operationKind>'+'\n'+
		'			<parameterNames xsi:type="soapenc:Array" soapenc:arrayType="xsd:string[2]">'+'\n'+
		'				<string xsi:type="xsd:string">id</string>'+'\n'+
		'				<string xsi:type="xsd:string">text</string>'+'\n'+
		'			</parameterNames>'+'\n'+
		'			<parameterValues xsi:type="soapenc:Array" soapenc:arrayType="xsd:string[2]">'+'\n'+
		'				<string xsi:type="xsd:string">'+id+'</string>'+'\n'+
		'				<string xsi:type="xsd:string"><![CDATA['+content+']]></string>'+'\n'+
		'			</parameterValues>'+'\n'+
		'		</mswsb:auxOperation>'+'\n'+
		'	</SOAP-ENV:Body>'+'\n'+
		'</SOAP-ENV:Envelope>';
	return temp;
}