function initTiny(){
tinyMCE.init({
	mode : "textareas",
	theme : "advanced",
	language : wcm_iso_language,
	plugins : "safari,inlinepopups,table,contextmenu,destlink,advhr",
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,advhr,separator,numlist,bullist,destlink,unlink,code,removeformat",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_path_location : "bottom",
	content_css : rel_css_path,
	extended_valid_elements : "hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style],a[name|href|target|title|onclick|ondblklick],img[src|alt|title|width|height|obj|class|param|embed]",
	theme_advanced_resize_horizontal : true,
	theme_advanced_resizing : true,
	theme_advanced_path : false,
	convert_urls : false,
	editor_deselector : "mceNoEditor",
	remove_script_host : false,
	relative_urls : false,
	entity_encoding:"numeric",
	inline_styles:true,
	document_base_url : rel_root_path,
	paste_auto_cleanup_on_paste : true,
	theme_advanced_statusbar_location : "bottom",
	inline_styles:true,
	doctype : '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
	theme_advanced_resizing_use_cookie : false
});
}
initTiny();

var imgWin;
var imgField;
var imgUrl;
var imgType;
var imgField_name='';

function myCustomFileBrowser(field_name, url, type, win) {
	imgWin=win;
	imgField_name=field_name;
	imgUrl=url;
	imgType=type;
	//alert();
	if(type=='image' || type=='flash'){
		var wn = window.open("/admin/objects.php?imgID=HTML&field_name="+field_name,"Bildbanken", "height=600,width=800,navigation=no,resizable=yes,scrollbars=yes");
	}else{
		var wn = window.open("/admin/linkSelector.php?oldLink="+url+"&fromform=other&field_name="+field_name,"Linkhandler", "height=200,width=300,navigation=no,resizable=yes,scrollbars=yes");
	}
}
function SetUrl(path,height,width,Filename, alt, editor_id){
	//alert('Kalle');

	if(imgField_name=='src'){
		imgWin.document.forms[0].elements[imgField_name].value = path;
		imgWin.document.forms[0].elements['alt'].value = alt;
		img_Field_name='';
	}
	if(imgField_name=='href'){
		imgWin.document.forms[0].elements[imgField_name].value = path;
		imgWin.document.forms[0].elements['title'].value = alt;
		img_Field_name='';
	}
}
function insertLinkNew(path){
	//alert('Kalle');
	imgWin.document.forms[0].elements[imgField_name].value = path;
}
function convertWord(type, html )
{
	if(type=='before'){
		// Remove all SPAN tags
		html = html.replace(/<\/?SPAN[^>]*>/gi, "" );
		html = html.replace(/<\/?DIV[^>]*>/gi, "" );
		html = html.replace(/<\/?FONT[^>]*>/gi, "" );
		// Remove Class attributes
		html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
		// Remove Style attributes
		html = html.replace(/<(\w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3") ;
	// Remove Lang attributes
	html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
	// Remove XML elements and declarations
	html = html.replace(/<\\?\?xml[^>]*>/gi, "") ;
	// Remove Tags with XML namespace declarations: <o:p></o:p>
	html = html.replace(/<\/?\w+:[^>]*>/gi, "") ;
	// Replace the &nbsp;
	html = html.replace(/&nbsp;/, " " );
	//Transform <P> to <DIV>
	//var re = new RegExp("(<P)([^>]*>.*?)(<\/P>)","gi") ;	// Different because of a IE 5.0 error
		//html = html.replace( re, "<div$2</div>" ) ;
	}
	return html;
}
function removeTiny(){
	var areas=$$('.mceEditorIframe');
	for(n in tinyMCE.editors) {
		inst = tinyMCE.editors[n];
		try{
			inst.remove();
		}catch(ex){}
		/*
		if (!tinyMCE.getInstanceById(inst))
		continue;

		$(inst.editorId+'_toolbar').remove();
		inst.select();
		inst.formElement.innerHTML = tinyMCE.getContent();
		tinyMCE.removeInstance(inst);
		* */
	}
}

function ToggleTinyMCEEditors()
{
		removeTiny();
		initTiny();
		/*
		$A($('destinator_edit_form').getElementsByTagName("textarea")).each(
		function(elm, idx){
			var selector = tinyMCE.getParam("editor_selector");
			if (selector != '' && !new RegExp('\\b' + selector + '\\b').test(tinyMCE.getAttrib(elm, "class"))){
				return;
			}
			tinyMCE.addMCEControl(elm, elm.name);
		}
		);*/
}
//Event.observe(window,'load',function(){	Element.setStyle('destinator_loaderdiv',{height:'1px', width:'1px'});$('destinator_loaderdiv').hide();});

function reloadTiny(){
	ToggleTinyMCEEditors();
}
function loadingTiny(){
	Element.setStyle('destinator_loaderdiv',{height:'100%', width:'100%'});
	Element.show('destinator_loaderdiv');
}
function finishedLoadingTiny(){
	/*Placera tiny i destinator diven.*/
	var tinyElements=$$('div.mceToolbarExternal');
	$A(tinyElements).each(
		function(elm){
			var container = $('destinator_tiny_container');
			if (container) {
				container.appendChild($(elm));
			}
	});
	Element.setStyle('destinator_loaderdiv',{height:'1px', width:'1px'});
	$('destinator_loaderdiv').hide();
}
// move toolbars to it's containers, set event handlers
function initExternalToolbar(editor_id, body, doc) {
	var inst = tinyMCE.get(editor_id);
	if ($(inst.editorContainer)) {
		var container = $('destinator_tiny_container');
		if (container) {
			container.appendChild($(inst.editorContainer));
			//container.style.width = inst.iframeElement.offsetWidth+'px';
			container.style.display = 'none';
			try{
				$(inst).hide();
			}catch(Exception){

			}
			if (inst.contentDocument) {
				var target = doc;    // Mozilla
			} else {
				var target = body;    // IE
			}
			inst.onActivate.add(function (ed) {ed.show();/*toggleStyleDisplay(container,'block')*/; return true;});
			inst.onDeactivate.add(function(ed){ed.hide();return true;});
		}
	}
}
function nodeChangeHandler(editor_id, node, undo_index, undo_levels, visual_aid, any_selection){
	var visible=false;
	for (var n in tinyMCE.editors) {
		var instance = tinyMCE.editors[n];
		if($(instance.editorContainer) !=null){
			if(instance == tinyMCE.activeEditor){
				$(instance.editorContainer).show();
				visible=true;
			}else{
				$(instance.editorContainer).hide();
				//instance.toolbarElement.style.display='none';
			}
		}
	}
	if(visible){
		showTiny();
	}
}