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 upload = tas[i].getAttribute("EditorUpload");
		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";
		}
		
		if (upload == null)
		{
			editor.Config["LinkBrowser"] = false;
			editor.Config["ImageBrowser"] = false;
			editor.Config["FlashBrowser"] = false;
			editor.Config["LinkUpload"] = false;
			editor.Config["ImageUpload"] = false;
			editor.Config["FlashUpload"] = false;
		}
		else
		{
			var path = upload + "IlgharCMS.SystemModule/file_manager/select.aspx?Lang=" + lang + "&Type=Editor&Root=" + instanceID + "&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(window.addEventListener)
	{
		editor.EditorDocument.addEventListener("keydown", OnEdit, false);
		editor.EditorDocument.addEventListener("keypress", OnEdit, false);
	}
	else
	{
		editor.EditorDocument.attachEvent("onkeydown", OnEdit);
		editor.EditorDocument.attachEvent("onkeypress", OnEdit);
	}
} 


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