var tinyMCE_GZ = {
	settings : {
		themes : '',
		plugins : '',
		languages : '',
		disk_cache : true,
		page_name : 'tiny_mce_gzip.php',
		debug : false,
		suffix : ''
	},

	init : function(s, cb, sc) {
		var t = this, n, i, nl = document.getElementsByTagName('script');

		for (n in s)
			t.settings[n] = s[n];

		s = t.settings;

		if (window.tinyMCEPreInit) {
			t.baseURL = tinyMCEPreInit.base;
		} else {
			for (i=0; i<nl.length; i++) {
				n = nl[i];

				if (n.src && n.src.indexOf('tiny_mce') != -1)
					t.baseURL = n.src.substring(0, n.src.lastIndexOf('/'));
			}
		}

		if (!t.coreLoaded)
			t.loadScripts(1, s.themes, s.plugins, s.languages, cb, sc);
	},

	loadScripts : function(co, th, pl, la, cb, sc) {
		var t = this, x, w = window, q, c = 0, ti, s = t.settings;

		function get(s) {
			x = 0;

			try {
				x = new ActiveXObject(s);
			} catch (s) {
			}

			return x;
		};

		// Build query string
		q = 'js=true&diskcache=' + (s.disk_cache ? 'true' : 'false') + '&core=' + (co ? 'true' : 'false') + '&suffix=' + escape(s.suffix) + '&themes=' + escape(th) + '&plugins=' + escape(pl) + '&languages=' + escape(la) + '&dummy=.js';

		if (co)
			t.coreLoaded = 1;

		// Send request
		x = w.XMLHttpRequest ? new XMLHttpRequest() : get('Msxml2.XMLHTTP') || get('Microsoft.XMLHTTP');
		x.overrideMimeType && x.overrideMimeType('text/javascript');
		x.open('GET', t.baseURL + '/' + s.page_name + '?' + q, !!cb);
//		x.setRequestHeader('Content-Type', 'text/javascript');
		x.send('');

		// Handle asyncronous loading
		if (cb) {
			// Wait for response
			ti = w.setInterval(function() {
				if (x.readyState == 4 || c++ > 10000) {
					w.clearInterval(ti);

					if (c < 10000 && x.status == 200) {
						t.loaded = 1;
						t.eval(x.responseText);
						var doTheStuff = function() {
							if (typeof(tinymce) == 'undefined') {
								w.setTimeout(doTheStuff, 10);
							} else {
								tinymce.dom.Event.domLoaded = true;
								cb.call(sc || t, x);
							}
						};
						w.setTimeout(doTheStuff, 10);
					}

					ti = x = null;
				}
			}, 10);
		} else
			t.eval(x.responseText);
	},

	start : function() {
		var t = this, each = tinymce.each, s = t.settings, ln = s.languages.split(',');

		tinymce.suffix = s.suffix;

		function load(u) {
			tinymce.ScriptLoader.markDone(tinyMCE.baseURI.toAbsolute(u));
		};

		// Add core languages
		each(ln, function(c) {
			if (c)
				load('langs/' + c + '.js');
		});

		// Add themes with languages
		each(s.themes.split(','), function(n) {
			if (n) {
				load('themes/' + n + '/editor_template' + s.suffix + '.js');

				each (ln, function(c) {
					if (c)
						load('themes/' + n + '/langs/' + c + '.js');
				});
			}
		});

		// Add plugins with languages
		each(s.plugins.split(','), function(n) {
			if (n) {
				load('plugins/' + n + '/editor_plugin' + s.suffix + '.js');

				each(ln, function(c) {
					if (c)
						load('plugins/' + n + '/langs/' + c + '.js');
				});
			}
		});
	},

	end : function() {
	},

	eval : function(co) {
		var se = document.createElement('script');

		// Create script
		se.type = 'text/javascript';
		se.text = co;

		// Add it to evaluate it and remove it
		(document.getElementsByTagName('head')[0] || document.documentElement).appendChild(se);
		se.parentNode.removeChild(se);
	}
};



function fl_tinymce_fiteditor(ed) {
		editorID = ed.id;
		var tble, frame, doc, docHeight, frameHeight;

		frame = document.getElementById(editorID+"_ifr");
		if ( frame != null )
		{
				//get the document object
				if (frame.contentDocument) doc = frame.contentDocument;
				else if (frame.contentWindow) doc = frame.contentWindow.document;
				else if (frame.document) doc = frame.document;

				if ( doc == null )
				return;

				//prevent the scrollbar from showing
				// doc.body.style.overflow = "hidden";

				//Fixes the issue of the table leaving empty space below iframe
				tble = frame.parentNode.parentNode.parentNode.parentNode;
				tble.style.height = 'auto';

				frameHeight = parseInt(frame.style.height);

				//Firefox
				if ( doc.height ) docHeight = doc.height;
				//MSIE
				else docHeight = parseInt(doc.body.scrollHeight);

				if(docHeight < 277) docHeight = 277;

				//MAKE BIGGER
				if ( docHeight > frameHeight ) frame.style.height = (docHeight + 20) + "px";
				//MAKE SMALLER
				else if ( docHeight < frameHeight ) frame.style.height = Math.max((docHeight + 20), 100) + "px";
		}
}

function fl_tinymce_init() {

	if(tinyMCE) {
		var tinymce_gz_lang = navigator.userAgent.indexOf("MSIE 9.0")>0 ? 'en' : 'sv';
		tinyMCE.init({
			editor_selector : "mceAdvancedSilver",
			theme : "advanced",
			mode : "textareas",
			language : tinymce_gz_lang,
			plugins : "table,emotions,inlinepopups,media,advlink,noneditable,paste",
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,outdent,indent",
			theme_advanced_buttons2 : "fontselect,fontsizeselect,forecolor,backcolor",
			theme_advanced_buttons3 : "tablecontrols,|,image,charmap,hr,emotions,link,unlink,anchor",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "",
			theme_advanced_resizing : true,
			theme_advanced_resize_horizontal : false,
			forced_root_block : false,
			force_br_newlines : true,
			force_p_newlines : false,
			//extended_valid_elements : "font[color|size|face],span[style]",
			content_css : "/css/fmli/tinymce.20100607.css",
			oninit : fl_tinymce_after,
			relative_urls : false,
			gecko_spellcheck : true,
            extended_valid_elements : "-div[id|class|style|title|dir<ltr?rtl|lang|xml::lang|onclick|ondblclick|"
                                    + "onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|"
                                    + "onkeydown|onkeyup|data-id]"
		});

		tinyMCE.init({
			editor_selector : "mceSimple",
			theme : "simple",
			mode : "textareas",
			language : tinymce_gz_lang,
			//extended_valid_elements : "font[color|size|face],span[style]",
			content_css : "/css/fmli/tinymce.20100607.css",
			forced_root_block : false,
			force_br_newlines : true,
			force_p_newlines : false,
			oninit : fl_tinymce_after,
			relative_urls : false,
			gecko_spellcheck : true,
            extended_valid_elements : "-div[id|class|style|title|dir<ltr?rtl|lang|xml::lang|onclick|ondblclick|"
                                    + "onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|"
                                    + "onkeydown|onkeyup|data-id]"
		});

		tinyMCE.init({
			editor_selector : "mceSimpleSilver",
			theme : "advanced",
			mode : "textareas",
			language : tinymce_gz_lang,
			plugins : "emotions,inlinepopups,media,advlink,preview,paste",
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,outdent,indent,charmap,emotions,link,unlink,preview",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "",
			theme_advanced_resizing : true,
			theme_advanced_resize_horizontal : false,
			forced_root_block : false,
			force_br_newlines : true,
			force_p_newlines : false,
			//extended_valid_elements : "font[color|size|face],span[style]",
			content_css : "/css/fmli/tinymce.20100607.css",
			oninit : fl_tinymce_after,
			relative_urls : false,
			gecko_spellcheck : true,
            extended_valid_elements : "-div[id|class|style|title|dir<ltr?rtl|lang|xml::lang|onclick|ondblclick|"
                                    + "onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|"
                                    + "onkeydown|onkeyup|data-id]"
		});

	}

}

function fl_tinymce_after() {
	if(document.getElementById('fl_tinymce_editor')) {
		document.getElementById('fl_tinymce_editor').style.display = 'block';
	}
	if(document.getElementById('fl_tinymce_loading')) {
		document.getElementById('fl_tinymce_loading').style.display = 'none';
	}
}
