var js = document.getElementsByTagName("script");
var base = js[js.length-1].src.substring(0,js[js.length-1].src.lastIndexOf("/")+1) + "fckeditor/";
document.write("<script src=\"" + base + "fckeditor.js\"></script>");

function InitFCKEditor()
{
	var tas = document.getElementsByTagName("TEXTAREA");
	for (var i = 0; i < tas.length; i++)
	{
		var lang = tas[i].getAttribute("EditorLang");
		var toolbar = tas[i].getAttribute("EditorToolBar");
		var instanceID = tas[i].getAttribute("EditorInstanceID");
		var browserURL = tas[i].getAttribute("EditorBrowserURL");
		var folder = tas[i].getAttribute("EditorFolder");
		if (lang == null)
			continue;
		
		var editor = new FCKeditor(tas[i].id, tas[i].style.width, tas[i].style.height, (toolbar == null || toolbar == "") ? "Default" : toolbar);
		editor.Config["EnterMode"] = "br";
		editor.Config["ShiftEnterMode"] = "p";
		editor.Config["InstanceID"] = instanceID;
		editor.Config["DefaultLanguage"] = lang;
		editor.BasePath = base;
		if (lang == "UYG" || lang == "KZA" || lang == "KGA")
		{
			editor.Config["ContentLangDirection"] = "rtl";
			editor.Config["EditorAreaCSS"] = editor.BasePath + "editor/css/fck_editorarea" + lang + ".css";
		}
		
		editor.Config["LinkUpload"] = false;
		editor.Config["ImageUpload"] = false;
		editor.Config["FlashUpload"] = false;

		if (browserURL == null)
		{
			editor.Config["LinkBrowser"] = false;
			editor.Config["ImageBrowser"] = false;
			editor.Config["FlashBrowser"] = false;
		}
		else
		{
			var path = browserURL + "?Lang=" + lang + "&Type=Editor";
			if (instanceID != null)
				path += "&Root=" + instanceID;
			if (folder != null)
				path += "&Folder=" + folder;
			
			path += "&ListByPicture=";

			editor.Config["LinkBrowserURL"] = path + "false";
			editor.Config["LinkBrowserWindowWidth"] = 900;
			editor.Config["LinkBrowserWindowHeight"] = 600;
			
			editor.Config["ImageBrowserURL"] = path + "true";
			editor.Config["ImageBrowserWindowWidth"] = 900;
			editor.Config["ImageBrowserWindowHeight"] = 600;

			editor.Config["FlashBrowserURL"] = path + "false";
			editor.Config["FlashBrowserWindowWidth"] = 900;
			editor.Config["FlashBrowserWindowHeight"] = 600;
		}
		
		editor.ReplaceTextarea();
	}
}

function FCKeditor_OnComplete(editor)
{
	if (editor.Config["DefaultLanguage"] != "UYG" && editor.Config["DefaultLanguage"] != "KZA" && editor.Config["DefaultLanguage"] != "KGA")
		return;
	
	editor.EditorDocument.body.lang = editor.Config["DefaultLanguage"];
	if (navigator.userAgent.indexOf("MSIE") != -1)
	{
		editor.EditorDocument.attachEvent("onkeydown", OnEdit);
		editor.EditorDocument.attachEvent("onkeypress", OnEdit);
	}
	else
	{
		editor.EditorDocument.addEventListener("keydown", OnEdit, false);
		editor.EditorDocument.addEventListener("keypress", OnEdit, false);
	}
} 


if(window.addEventListener)
	window.addEventListener("load", InitFCKEditor, false); 
else
	window.attachEvent("onload", InitFCKEditor);
