

function initMiceEditor(textarea_id) {

}

function initFCKEditor(textarea_id) {
	
	var ta = document.getElementById(textarea_id);
	// get height
	height = parseInt(ta.style.height);
	
	
	var oFCKeditor = new FCKeditor(textarea_id);
	sBasePath = "/javascripts/FCKeditor/" ;
	oFCKeditor.BasePath = sBasePath;
	
	// set height
	if(height && typeof height == 'number') oFCKeditor.Height = height+50;
	else oFCKeditor.Height = "400";
	
	
/*FCKConfig.ToolbarSets['PluginTest'] = [

	['Source'],

	['My_Find','My_Replace','-','Placeholder'],

	['Table','-','TableInsertRow','TableDeleteRows','TableInsertColumn','TableDeleteColumns','TableInsertCell','TableDeleteCells','TableMergeCells','TableSplitCell'],

	['Bold','Italic','-','OrderedList','UnorderedList','-','Link','Unlink','-','About']

] ;*/

	//oFCKeditor.Config['CustomConfigurationsPath'] = sBasePath + 'smart_fckconfig.js' ;
	oFCKeditor.Config['CustomConfigurationsPath'] = '/local/javascripts/smart_fckconfig.js' ;
	//oFCKeditor.Config[ "AutoDetectLanguage" ] = false ;
	//oFCKeditor.Config[ "DefaultLanguage" ] = "pt-BR" ;
	// Let's use a custom toolbar for this sample.

	//oFCKeditor.ToolbarSet	= 'PluginTest' ;
	oFCKeditor.ReplaceTextarea();
	//oFCKeditor.Value = '';
	


/*

var sBasePath = document.location.pathname.substring(0,document.location.pathname.lastIndexOf('_samples')) ;

var oFCKeditor = new FCKeditor( 'FCKeditor1' ) ;
oFCKeditor.BasePath	= sBasePath ;
oFCKeditor.Height	= 300 ;
oFCKeditor.Value	= 'This is some <strong>sample text<\/strong>. You are using <a href="http://www.fckeditor.net/">FCKeditor<\/a>.' ;
oFCKeditor.Create() ;

*/	

	if(document.getElementById(textarea_id+'_elink')) document.getElementById(textarea_id+'_elink').style.display = 'none';
}

function FCKEditor_getInstance(instance_id) {
	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance(instance_id) ;
	return oEditor;
}

function FCKEditor_SwitchEditMode(instance_id) {
    var oEditor = FCKeditorAPI.GetInstance(instance_id);
	oEditor.SwitchEditMode();
}

function FCKEditor_InsertHTML(instance_id,insert) {
	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance(instance_id) ;

	// Check the active editing mode.
	if ( oEditor.EditMode == FCK_EDITMODE_WYSIWYG )
	{
		// Insert the desired HTML.
		oEditor.InsertHtml(insert) ;
	}
	else
		alert( 'You must be on WYSIWYG mode!' ) ;
}

function FCKEditor_SetContents(instance_id,content) {
	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance(instance_id) ;

	// Set the editor contents (replace the actual one).
	oEditor.SetHTML(content) ;
}

function FCKEditor_GetContents(instance_id) {
	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance(instance_id) ;

	// Get the editor contents in XHTML.
	return oEditor.GetXHTML( true );
	alert( oEditor.GetXHTML( true ) ) ;		// "true" means you want it formatted.
}

function FCKEditor_ExecuteCommand(instance_id,commandName) {
	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance(instance_id) ;

	// Execute the command.
	oEditor.Commands.GetCommand( commandName ).Execute() ;
}

function FCKEditor_GetLength(instance_id) {
	// This functions shows that you can interact directly with the editor area
	// DOM. In this way you have the freedom to do anything you want with it.

	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance(instance_id) ;

	// Get the Editor Area DOM (Document object).
	var oDOM = oEditor.EditorDocument ;

	var iLength ;

	// The are two diffent ways to get the text (without HTML markups).
	// It is browser specific.

	if ( document.all )		// If Internet Explorer.
	{
		iLength = oDOM.body.innerText.length ;
	}
	else					// If Gecko.
	{
		var r = oDOM.createRange() ;
		r.selectNodeContents( oDOM.body ) ;
		iLength = r.toString().length ;
	}
	return iLength;
	alert( 'Actual text length (without HTML markups): ' + iLength + ' characters' ) ;
}

function FCKEditor_GetInnerHTML(instance_id) {
	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance(instance_id) ;
	return oEditor.EditorDocument.body.innerHTML;
	alert( oEditor.EditorDocument.body.innerHTML ) ;
}



function loadImageEditor(el_id) {
	var manager = '/javascripts/FCKeditor/editor/plugins/SmartImageManager/manager.php';
	var outparam = null;
	
	var preview = document.getElementById('view'+el_id);
	var image = FindElementsByTagName(preview, 'IMG')
	
	if (!image || typeof image == "undefined") {
			image = null;
			outparam = {
				f_id    :  el_id,
				f_url    :  '',
				f_alt    : '',
				f_border : '',
				f_align  : '',
				f_vert   : '',
				f_horiz  : ''				
			};					
	}

	if (image) outparam = {
		f_id    :  el_id,
		f_url    :  SmartAgent.indexOf('MSIE') == 0 ? stripBaseURL(image.src) : image.getAttribute("src"),
		f_alt    : image.alt,
		f_border : image.border,
		f_align  : image.align,
		f_vert   : image.vspace,
		f_horiz  : image.hspace
	};	

	//var imanager = window.open(manager,'ImageManager','height=460,width=600,status=no,toolbar=no,location=no,menubar=no,resizable=yes');
	SmartDialog(manager, 'setImage()', outparam,'no',600,500); //(manager, null, outparam)
}

function deleteImageFromEditor(el_id) {
	document.getElementById(el_id).value = '';
	document.getElementById('view'+el_id).innerHTML = '';
}


stripBaseURL = function(string) {
	baseURL = document.baseURI || document.URL;
	if (baseURL && baseURL.match(/(.*)\/([^\/]+)/)) baseURL = RegExp.$1 + "/";
	var baseurl = baseURL;

	// strip to last directory in case baseurl points to a file
	baseurl = baseurl.replace(/[^\/]+$/, '');
	var basere = new RegExp(baseurl);
	string = string.replace(basere, "");

	// strip host-part of URL which is added by MSIE to links relative to server root
	baseurl = baseurl.replace(/^(https?:\/\/[^\/]+)(.*)$/, '$1');
	basere = new RegExp(baseurl);
	return '/'+string.replace(basere, "");
};

function setImage() {
	val = SmartDialog._arguments;
	//DebugDump(val,'val');
	if(document.getElementById(val['f_id'])) {
		ofw = 'auto';
		f_width = parseInt(val['f_width']);
		f_height = parseInt(val['f_height']);
		document.getElementById(val['f_id']).value = val['f_url'];
		if(f_height && f_height > 0) {
			if(f_height > 400) {
				ofw = 'scroll';
				f_height = 400;
			} 
		} else f_height = 200;
		if(f_width && f_width > 0) {
			if(f_width > 400) {
				ofw = 'scroll';
				f_width = 400;
			}  
		} else f_width = 200;
		//DebugDump(f_height,'height '+f_height+', width '+f_width+', overflow '+ofw);
		document.getElementById('view'+val['f_id']).style.height = (f_height+10)+'px';
		document.getElementById('view'+val['f_id']).style.width = (f_width+10)+'px';
		document.getElementById('view'+val['f_id']).style.overflow = ofw;
		document.getElementById('view'+val['f_id']).innerHTML = '<img src="'+val['f_url']+'">';
	
	}
}


function loadFileEditor(el_id) {
	var manager = '/javascripts/FCKeditor/editor/plugins/SmartInsertFile/filedialog.php';
	var outparam = null;
	
	var preview = document.getElementById('view'+el_id);
	var link = FindElementsByTagName(preview, 'A')
	//DebugDump(link,'link');
	if (!link || typeof link == "undefined") {
			link = null;
			outparam = {
				f_id    :  el_id,
				f_url    :  ''/*,
				f_caption    : '',
				f_addicon : '',
				f_addsize  : '',
				f_adddate   : '',
				f_date   : '',
				f_size   : '',
				f_icon  : ''*/	
			};					
	}
	
	
	//HTMLArea.is_ie ? editor.stripBaseURL(image.src) : image.getAttribute("src"),
	if (link) { 
		outparam = {
			f_id    :  el_id,
			f_url    :  SmartDialog.is_ie ? link.href : link.getAttribute("href"),
			f_caption    : link.alt
		};	
	}
	
	//var imanager = window.open(manager,'ImageManager','height=460,width=600,status=no,toolbar=no,location=no,menubar=no,resizable=yes');
	SmartDialog(manager, 'setFile()', outparam,'no',650,500);//Dialog(manager, null, outparam);	
}

function deleteFileFromEditor(el_id) {
	document.getElementById(el_id).value = '';
	document.getElementById('view'+el_id).innerHTML = '';
}

function setFile() {
	val = SmartDialog._arguments;
	//DebugDump(val,'val');
	if(document.getElementById(val['f_id'])) {
		document.getElementById(val['f_id']).value = val['f_url'];
		//alert(document.getElementById(val['f_id']).value);
		document.getElementById('view'+val['f_id']).innerHTML = '<a href="'+val['f_url']+'">'+val['f_url']+'</a>';
	
	}
}

function SmartDialog(url, action, init,scroll,width,height) {
	if (typeof init == "undefined") {
		init = window;	// pass this window object by default
	}
	url = checkUrl(url);
	url = addColorset(url);
	SmartDialog._geckoOpenModal(url, action, init,scroll,width,height);
};

SmartDialog._parentEvent = function(ev) {
	setTimeout( function() { if (SmartDialog._modal && !SmartDialog._modal.closed) { SmartDialog._modal.focus() } }, 50);
	if (SmartDialog._modal && !SmartDialog._modal.closed) {
		SmartDialog._stopEvent(ev);
	}
};


// should be a function, the return handler of the currently opened dialog.
SmartDialog._return = null;

// constant, the currently opened dialog
SmartDialog._modal = null;

// the dialog will read it's args from this variable
SmartDialog._arguments = null;

SmartDialog._geckoOpenModal = function(url, action, init,scroll,width,height) {
	//var urlLink = "hadialog"+url.toString();

	var myURL = "hadialog"+url;
	var regObj = /\W/g;
	myURL = myURL.replace(regObj,'_');
	
	if(!scroll) scroll = 'no';
	
	var dlg = window.open(url, myURL,
			      "toolbar=no,menubar=no,personalbar=no,width="+width+",height="+height+"," +
			      "scrollbars="+scroll+",resizable=yes,modal=yes,dependable=yes");
			     
	SmartDialog._modal = dlg;
	SmartDialog._arguments = init;

	// capture some window's events
	function capwin(w) {
		SmartDialog._addEvent(w, "click", SmartDialog._parentEvent);
		SmartDialog._addEvent(w, "mousedown", SmartDialog._parentEvent);
		SmartDialog._addEvent(w, "focus", SmartDialog._parentEvent);
		//if(closeFunction) SmartDialog._addEvent(SmartDialog._modal, "close",closeFunction);
	};
	// release the captured events
	function relwin(w) {
		SmartDialog._removeEvent(w, "click", SmartDialog._parentEvent);
		SmartDialog._removeEvent(w, "mousedown", SmartDialog._parentEvent);
		SmartDialog._removeEvent(w, "focus", SmartDialog._parentEvent);
	};
	capwin(window);
	// capture other frames
	for (var i = 0; i < window.frames.length; capwin(window.frames[i++]));
	// make up a function to be called when the SmartDialog ends.
	if(action) {
		SmartDialog._return = function (val) {
			//alert(action);
			SmartDialog._arguments = val;
			if (val && action) {
				eval(action);
			}
			relwin(window);
			// capture other frames
			for (var i = 0; i < window.frames.length; relwin(window.frames[i++]));
			SmartDialog._modal = null;
		};
	} else SmartDialog._return = null;
};


// event handling

SmartDialog._addEvent = function(el, evname, func) {
	if (SmartDialog.is_ie) {
		el.attachEvent("on" + evname, func);
	} else {
		el.addEventListener(evname, func, true);
	}
};


SmartDialog._removeEvent = function(el, evname, func) {
	if (SmartDialog.is_ie) {
		el.detachEvent("on" + evname, func);
	} else {
		el.removeEventListener(evname, func, true);
	}
};


SmartDialog._stopEvent = function(ev) {
	if (SmartDialog.is_ie) {
		ev.cancelBubble = true;
		ev.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
};

SmartDialog.agt = navigator.userAgent.toLowerCase();
SmartDialog.is_ie	   = ((SmartDialog.agt.indexOf("msie") != -1) && (SmartDialog.agt.indexOf("opera") == -1));


function HTMLEncode( text ) {
	text = text.replace(/&/g, "&amp;") ;
	text = text.replace(/"/g, "&quot;") ;
	text = text.replace(/</g, "&lt;") ;
	text = text.replace(/>/g, "&gt;") ;
	text = text.replace(/'/g, "&#39;") ;

	return text ;
}
