/*
	Vizuális szerkesztő
	===================
*/

//
// Konstruktor
// ===========
//
function VisualEditor(_instance, _upload_root)
{
	this.instance = _instance;
	this.id = "editor_" + this.instance;
	this.state = true;
	
	// funkció változók
	this.table_borders = true;
	this.toolbar = "";
	this.toolbar_auto_close = true;
	this.upload_root = _upload_root;
	
	// Mentés automatizálása
	eval("jumper.registerOnSubmit( function () { return " + this.instance + ".save(); } );");
}

//
// Parancskezelő
// =============
//
VisualEditor.prototype.load = function ()
{
	this.getEditorBody().innerHTML = this.getValueStore().value;
	
	return true;
}

VisualEditor.prototype.save = function ()
{
	this.getValueStore().value = this.getEditorBody().innerHTML;
	
	return true;
}

VisualEditor.prototype.execute = function (_name, _arg)
{
	if(_name == undefined || _name == null)
		return;
		
	_name = _name.toLowerCase();
	
	if(!this.state && _name != "show_source")
	{
		alert(ve_translations["message"]["function_banned"]);
		return;
	}
	
	switch(_name)
	{
		// Szövegformázás
		case "bold":
			this.executeCommand("bold");
		break;
		case "italic":
			this.executeCommand("italic");
		break;
		case "underline":
			this.executeCommand("underline");
		break;
		case "strike":
			this.executeCommand("strikethrough");
		break;
		case "superscript":
			this.executeCommand("superscript");
		break;
		case "subscript":
			this.executeCommand("subscript");
		break;
		case "small":
			this.executeCommand("decreasefontsize");
		break;
		case "big":
			this.executeCommand("increasefontsize");
		break;
		case "capital":
			this.executeCommand("inserthtml", "<span style='font-variant:small-caps;'>" + this.getSelection() + "</span>");
		break;
		case "header_1":
			this.executeCommand("formatblock", "h1");
		break;
		case "header_2":
			this.executeCommand("formatblock", "h2");
		break;
		case "header_3":
			this.executeCommand("formatblock", "h3");
		break;
		case "header_4":
			this.executeCommand("formatblock", "h4");
		break;
		case "header_5":
			this.executeCommand("formatblock", "h5");
		break;
		case "header_6":
			this.executeCommand("formatblock", "h6");
		break;
		case "paragraph":
			this.executeCommand("formatblock", "p");
		break;
		case "remove_format":
			this.executeCommand("removeformat");
		break;
		case "preformat":
			this.executeCommand("formatblock", "pre");
		break;
		case "set_font":
			this.executeCommand("fontname", _arg);
		break;
		case "set_font_size":
			this.executeCommand("fontsize", _arg);
		break;
		case "unordered_list":
			this.executeCommand("insertunorderedlist");
		break;
		case "ordered_list":
			this.executeCommand("insertorderedlist");
		break;
		
		// Szövegigazítás
		case "align_left":
			this.executeCommand("justifyleft");
		break;
		case "align_right":
			this.executeCommand("justifyright");
		break;
		case "align_center":
			this.executeCommand("justifycenter");
		break;
		case "align_justify":
			this.executeCommand("justifyfull");
		break;
		case "indent":
			this.executeCommand("indent");
		break;
		case "outdent":
			this.executeCommand("outdent");
		break;
		
		// Blokkosítás, elválasztás
		case "blockquote":
			this.executeCommand("formatblock", "blockquote");
		break;
		case "horizontal_rule":
			this.executeCommand("inserthorizontalrule");
		break;
		
		// Elrendezések
		case "toggle_table_borders":
			this.toggleTableBorders();
		break;
		case "show_layouts":
			this.toggleToolbar("show_layouts", layouts_tpl);
		break;
		case "layout_1":
			this.executeCommand("inserthtml", "<table border=\""+(Number(this.table_borders))+"\"><tr><td>\na\n</td><td>\nb\n</td></tr></table>");
		break;
		case "layout_2":
			this.executeCommand("inserthtml", "<table border=\""+(Number(this.table_borders))+"\"><tr><td>\na\n</td><td>\nb\n</td><td>\nc\n</td></tr></table>");
		break;
		case "layout_3":
			this.executeCommand("inserthtml", "<table border=\""+(Number(this.table_borders))+"\"><tr><td colspan=\"2\" rowspan=\"2\">\na\n</td><td colspan=\"2\">\nb\n</td></tr><tr><td colspan=\"2\">\nc\n</td></tr></table>");
		break;
		case "layout_4":
			this.executeCommand("inserthtml", "<table border=\""+(Number(this.table_borders))+"\"><tr><td colspan=\"2\">\na\n</td><td colspan=\"2\" rowspan=\"2\">\nb\n</td></tr><tr><td colspan=\"2\">\nc\n</td></tr></table>");
		break;
		case "layout_5":
			this.executeCommand("inserthtml", "<table border=\""+(Number(this.table_borders))+"\"><tr><td>\na\n</td><td colspan=\"3\" rowspan=\"3\">\nb\n</td></tr><tr><td>\nc\n</td></tr><tr><td>\nd\n</td></tr></table>");
		break;
		case "layout_6":
			this.executeCommand("inserthtml", "<table border=\""+(Number(this.table_borders))+"\"><tr><td colspan=\"3\" rowspan=\"3\">\na\n</td><td>\nb\n</td></tr><tr><td>\nc\n</td></tr><tr><td>\nd\n</td></tr></table>");
		break;
		case "layout_7":
			this.executeCommand("inserthtml", "<table border=\""+(Number(this.table_borders))+"\"><tr><td colspan=\"2\" rowspan=\"2\">\na\n</td><td>\nb\n</td></tr><tr><td>\nc\n</td></tr><tr><td colspan=\"3\">\nd\n</td></tr></table>");
		break;
		case "layout_8":
			this.executeCommand("inserthtml", "<table border=\""+(Number(this.table_borders))+"\"><tr><td>\na\n</td><td rowspan=\"2\">\nb\n</td></tr><tr><td>\nc\n</td></tr><tr><td colspan=\"2\">\nd\n</td></tr></table>");
		break;
		
		// Speckók
		case "show_source":
			this.toggleSourceEditor();
		break;
		
		case "mailto_toolbar":
			this.toggleToolbar("mailto_toolbar", mailto_tpl);
		break;
		case "insert_mailto":
			this.insertMailto();
		break;
		
		case "link_toolbar":
			this.toggleToolbar("link_toolbar", link_tpl);
		break;
		case "insert_link":
			this.insertLink();
		break;
		case "unlink":
			this.executeCommand("unlink");
		break;
		
		case "toggle_toolbar_auto_close":
			this.toggleToolbarAutoClose();
		break;
		
		case "style_toolbar":
			this.toggleToolbar("style_toolbar", style_tpl);
		break;
		case "set_style":
			this.setStyle();
		break;
		
		case "files_toolbar":
			this.toggleToolbar("files_toolbar", files_tpl);
			if(this.toolbar == "files_toolbar")
				setIframe("upload", "page=upload&instance=" + this.instance + "_upload&onclick=" + this.instance + ".insertFile&root=" + this.upload_root);
		break;
		
		case "smiley_toolbar":
			eval("this.toggleToolbar('smiley_toolbar', smileys_" + smiley_toolbars[0] + "_tpl);");
		break;
		case "open_smiley_toolbar":
			eval("this.setToolbar('smiley_toolbar', smileys_" + smiley_toolbars[_arg] + "_tpl);");
		break;
		case "insert_smiley":
			this.executeCommand("insertimage", "./themes/global/images/smileys/" + _arg);
			
			if(this.toolbar_auto_close)
				this.toggleToolbar();
		break;
		
		case "insert_image":
			img = prompt(ve_translations["message"]["image_url"], "http://");
			if(img != null)
				this.executeCommand("insertimage", img);
		break;
	}
}



//
// Összetett funkciók
// ==================
//
// Toolbar kezelés
VisualEditor.prototype.toggleToolbar = function (_name, _tpl)
{
	toolbar = this.getToolbar();
	
	if(this.toolbar == "")
		this.setToolbar(_name, _tpl);
	else
		this.closeToolbar();
}

VisualEditor.prototype.setToolbar = function (_name, _tpl)
{
	toolbar = this.getToolbar();
	
	// Toolbar generálása
	tpl = new Themeplaty3();
	tpl.setVar("instance", this.instance);
	tpl.setVar("theme", theme_name);
	tpl.setLabels(ve_translations);
	toolbar.innerHTML = tpl.parse(_tpl);
	
	this.toolbar = _name;
}

VisualEditor.prototype.closeToolbar = function ()
{
	// Toolbar ürítése
	toolbar.innerHTML = "";
	
	this.toolbar = "";
}

// Toolbar automatikus bezárása
VisualEditor.prototype.toggleToolbarAutoClose = function ()
{
	this.toolbar_auto_close = !this.toolbar_auto_close;
}

// Táblázatszélek állítása
VisualEditor.prototype.toggleTableBorders = function ()
{
	var body = this.getEditorBody();

	if(this.table_borders)
	{
		this.table_borders = false;
		html = body.innerHTML;
		html = html.replace(/table border=\"1\"/g, "table border=\"0\"");
		body.innerHTML = html;
	}
	else
	{
		this.table_borders = true;
		html = body.innerHTML;
		html = html.replace(/table border=\"0\"/g, "table border=\"1\"");
		body.innerHTML = html;
	}
}

// Forráskódszerkesztő funkciók
VisualEditor.prototype.toggleSourceEditor = function ()
{
	toolbar = this.getToolbar();

	if(this.toolbar == "")
	{
		// Toolbar generálása
		tpl = new Themeplaty3();
		tpl.setVar("width", 80);
		tpl.setVar("height", 7);
		tpl.setVar("instance", this.instance);
		toolbar.innerHTML = tpl.parse(source_editor_tpl);
		
		// Tartalom átmásolása
		se = this.getSourceEditor();
		eb = this.getEditorBody();
		se.value = eb.innerHTML;
		
		// Szerkesztő letiltás
		this.setEditorState(false);
		
		this.toolbar = "source_editing";
	}
	else
	{
		if(this.toolbar == "source_editing")
		{
			// Szerkesztő engedélyezés
			this.setEditorState(true);
		
			// Tartalom átmásolása
			se = this.getSourceEditor();
			eb = this.getEditorBody();
			eb.innerHTML = se.value;
		}
	
		// Toolbar ürítése
		toolbar.innerHTML = "";
		
		this.toolbar = "";
	}
}

VisualEditor.prototype.updateEditorFromSource = function ()
{
		se = this.getSourceEditor();
		eb = this.getEditorBody();
		eb.innerHTML = se.value;
}

// Levélcím beszúrása funkciók
VisualEditor.prototype.insertMailto = function ()
{
	var name = document.getElementById("name").value;
	var email = document.getElementById("email").value;
	var secured = document.getElementById("secure");
	var mailstr = "";
	
	if(this.getSelection() == "" && name == "")
	{
		alert(ve_translations["message"]["no_selection"]);
		return false;
	}
	else if(!email.match(/((\w|.|_)+)@((\w|.|_)+)/))
	{
		alert(ve_translations["message"]["bad_email"]);
		return false;
	}

	if(!secured.checked)
	{
		mailstr = "mailto:" + email;
	}
	else
	{
		user = email.replace(/((\w|.|_)+)@((\w|.|_)+)/, "$1");
		domain = email.replace(/((\w|.|_)+)@((\w|.|_)+)/, "$3");
		mailstr = "javascript:level(\"" + user + "\",\"" + domain + "\")";
	}

	if(name != "")
	{
		this.executeCommand("inserthtml", "<a href='" + mailstr + "'>" + name + "</a>");
	}
	else
		this.executeCommand("createlink", mailstr);

	if(this.toolbar_auto_close)
		this.closeToolbar();
	
	return true;
}

// Link beszúrása funkció
VisualEditor.prototype.insertLink = function ()
{
	var url = document.getElementById("url").value;
	var new_window = document.getElementById("new_window").checked;
	var mailstr = "";
	
	if(!url.match(/http\:\/\/(.)+/))
		url = "http://" + url;
	
	if(!new_window)
	{
		this.executeCommand("createlink", url);
	}
	else
	{
		selection = url;
		if(this.getSelection() != "")
			selection = this.getSelection();			
	
		this.executeCommand("inserthtml", "<a href='" + url + "' target='_blank'>" + selection + "</a>");
	}

	if(this.toolbar_auto_close)
		this.closeToolbar();
	
	return true;
}

// Stílus beállító panel
VisualEditor.prototype.setStyle = function ()
{
	var url = document.getElementById("url").value;
	var new_window = document.getElementById("new_window").checked;
	var mailstr = "";
	
	if(!url.match(/http\:\/\/(.)+/))
		url = "http://" + url;
	
	if(!new_window)
	{
		this.executeCommand("createlink", url);
	}
	else
	{
		selection = url;
		if(this.getSelection() != "")
			selection = this.getSelection();			
	
		this.executeCommand("inserthtml", "<a href='" + url + "' target='_blank'>" + selection + "</a>");
	}

	if(this.toolbar_auto_close)
		this.closeToolbar();
	
	return true;
}


//
// Parancsfuttatási alapok
// =======================
//
VisualEditor.prototype.executeCommand = function (_command, _arg)
{
	if(_command == undefined || _command == null)
		return;

	if(_arg == undefined)
		_arg = null;

	if(isIE)
	{
		if(_command == "formatblock")
		{
			_arg = "<" + _arg + ">";
		}
	}
	
	var editor = this.getEditor();
	editor.document.execCommand(_command, false, _arg);
	editor.focus();
}

VisualEditor.prototype.setEditorState = function (_state)
{
	ed = this.getEditor();
	this.state = _state;
	if(!_state)
	{
		if(!isIE)
			ed.document.designMode = "off";
		else
			eb.setAttribute("contentEditable", false);
	}
	else
	{
		if(!isIE)
			ed.document.designMode = "on";
		else
			eb.setAttribute("contentEditable", true);
	}
}

VisualEditor.prototype.insertFile = function (_path, _img)
{
	if(_img)
		this.executeCommand("insertimage", _path);
	else
		this.executeCommand("createlink", _path);
}




//
// Tulajdonság lekérdezők
// ======================
//
VisualEditor.prototype.getEditor = function ()
{
	return eval(this.id + ";");
}

VisualEditor.prototype.getSelection = function ()
{
	return this.getEditor().getSelection();
}

VisualEditor.prototype.getToolbar = function ()
{
	return document.getElementById("editor_toolbar_" + this.instance);
}

VisualEditor.prototype.getEditorBody = function ()
{
	return this.getEditor().document.body;
}

VisualEditor.prototype.getSourceEditor = function ()
{
	return document.getElementById("editor_source_" + this.instance);
}

VisualEditor.prototype.getValueStore = function ()
{
	name = this.instance.substr(3);
	return document.getElementById("textarea_" + name);
}
