﻿
//jsLibs/richTextEditor.js

//a מקבל שדה טקסטאראה והופך אותו לעורך טקסט משוכלל
function convertTextAreaToRichText(dtextarea)
{
	var self = this;

	var diframe;
	var codeUpdater;
	var mode;
	
	var theObject = document.createElement("div");

	//a כאילו קונסטרקטור
	//constructor(dtextarea)
	{
		mode = "rich";

		s = "<fieldset class=\"richTextEditor\">";

		s+="	<div class=\"controls\">";
		s+="		<button id=\"richUndo\" title=\"בצע שוב\"><img src=\"../style/editor/redo.png\" alt=\"בצע שוב\"></button>";
		s+="		<button id=\"richRedo\" title=\"בטל\"><img src=\"../style/editor/undo.png\" alt=\"בטל\"></button>";
		s+="		<button id=\"richLink\" title=\"הוסף קישור\"><img src=\"../style/editor/link.png\" alt=\"הוסף קישור\"></button>";
		s+="		<button id=\"richInsertImage\" title=\"הוסף תמונה\"><img src=\"../style/editor/insertImage.png\" alt=\"הוסף תמונה\"></button>";
		s+="		<button id=\"richTable\" title=\"הוסף טבלה\"><img src=\"../style/editor/table.png\" alt=\"הוסף טבלה\"></button>";
		s+="		<button id=\"richHr\" title=\"הכנס קו\"><img src=\"../style/editor/hr.png\" alt=\"הכנס קו\"></button>";
		s+="		<button id=\"richBold\" title=\"הדגש\"><img src=\"../style/editor/bold.png\" alt=\"הדגש\"></button>";
		s+="		<button id=\"richItalic\" title=\"נטוי\"><img src=\"../style/editor/italic.png\" alt=\"נטוי\"></button>";
		s+="		<button id=\"richUnderLine\" title=\"קו תחתון\"><img src=\"../style/editor/underLine.png\" alt=\"קו תחתון\"></button>";
		s+="		<button id=\"richRight\" title=\"ישור לימין\"><img src=\"../style/editor/right.png\" alt=\"ישור לימין\"></button>";
		s+="		<button id=\"richCenter\" title=\"ישור למרכז\"><img src=\"../style/editor/center.png\" alt=\"ישור למרכז\"></button>";
		s+="		<button id=\"richLeft\" title=\"ישור למרכז\"><img src=\"../style/editor/left.png\" alt=\"ישור למרכז\"></button>";
		s+="		<button id=\"richNumList\" title=\"מספור\"><img src=\"../style/editor/numList.png\" alt=\"מספור\"></button>";
		s+="		<button id=\"richDotList\" title=\"תבליטים\"><img src=\"../style/editor/dotList.png\" alt=\"תבליטים\"></button>";
		s+="		<button id=\"richOutDent\" title=\"הקטן כניסה\"><img src=\"../style/editor/outDent.png\" alt=\"הקטן כניסה\"></button>";
		s+="		<button id=\"richInDent\" title=\"הגדל כניסה\"><img src=\"../style/editor/inDent.png\" alt=\"הגדל כניסה\"></button>";
		s+="		<button id=\"richRemoveFormat\" title=\"נקה עיצובים\"><img src=\"../style/editor/removeFormat.png\" alt=\"נקה עיצובים\"></button>";
		s+="		<button id=\"richInsertCode\" title=\"הוספת קוד של סרטון\"><img src=\"../style/editor/insertCode.png\" alt=\"הוספת קוד של סרטון\"></button>";
		s+="		<button id=\"richViewSource\" title=\"תצוגת קוד\"><img src=\"../style/editor/viewSource.png\" alt=\"תצוגת קוד\"></button>";

		s+="		<div>";
		s+="			<label>";
		s+="				<img src=\"../style/editor/fontSize.png\" alt=\"גודל‎\"> גודל:";
		s+="				<select id=\"richFontSize\">";
		s+="					<option value=\"auto\">- בחר -</option>";
		s+="					<option style=\"font-size:4pt\" value=\"1\">1</option>";
		s+="					<option style=\"font-size:8pt\" value=\"2\">2</option>";
		s+="					<option style=\"font-size:12pt\" value=\"3\">3</option>";
		s+="					<option style=\"font-size:16pt\" value=\"4\">4</option>";
		s+="					<option style=\"font-size:20pt\" value=\"5\">5</option>";
		s+="					<option style=\"font-size:24pt\" value=\"6\">6</option>";
		s+="					<option style=\"font-size:28pt\" value=\"7\">7</option>";
		s+="				</select>";
		s+="			</label>";

		s+="			<label>";
		s+="				<img src=\"../style/editor/fill.png\" alt=\"רקע‎\"> רקע:";
		s+="				<select id=\"richFill\">";
		s+="					<option value=\"auto\">- בחר -</option>";
		s+="					<option style=\"background:#ffffff\" value=\"#ffffff\">לבן</option>";
		s+="					<option style=\"background:#000000\" value=\"#000000\">שחור</option>";
		s+="					<option style=\"background:#ff0000\" value=\"#ff0000\">אדום</option>";
		s+="					<option style=\"background:#00ff00\" value=\"#00ff00\">ירוק</option>";
		s+="					<option style=\"background:#008000\" value=\"#008000\">ירוק כהה</option>";
		s+="					<option style=\"background:#0000ff\" value=\"#0000ff\">כחול</option>";
		s+="					<option style=\"background:#000080\" value=\"#000080\">כחול כהה</option>";
		s+="					<option style=\"background:#0080ff\" value=\"#0080ff\">תכלת</option>";
		s+="					<option style=\"background:#ff8000\" value=\"#ff8000\">כתום</option>";
		s+="					<option style=\"background:#ffff00\" value=\"#ffff00\">צהוב</option>";
		s+="					<option style=\"background:#ff80ff\" value=\"#ff80ff\">ורוד</option>";
		s+="					<option style=\"background:#c0c0c0\" value=\"#c0c0c0\">אפור</option>";
		s+="					<option style=\"background:#808080\" value=\"#808080\">אפור כהה</option>";
		s+="					<option style=\"background:#804000\" value=\"#804000\">חום</option>";
		s+="					<option style=\"background:#800080\" value=\"#800080\">סגול</option>";
		s+="					<option style=\"background:#addee3\" value=\"#addee3\">תכלת אתר</option>";
		s+="					<option style=\"background:#090830\" value=\"#090830\">כחול כהה אתר</option>";
		s+="				</select>";
		s+="			</label>";

		s+="			<label>";
		s+="				<img src=\"../style/editor/color.png\" alt=\"צבע‎\"> צבע:";
		s+="				<select id=\"richColor\">";
		s+="					<option value=\"auto\">- בחר -</option>";
		s+="					<option style=\"color:#ffffff\" value=\"#ffffff\">לבן</option>";
		s+="					<option style=\"color:#000000\" value=\"#000000\">שחור</option>";
		s+="					<option style=\"color:#ff0000\" value=\"#ff0000\">אדום</option>";
		s+="					<option style=\"color:#00ff00\" value=\"#00ff00\">ירוק</option>";
		s+="					<option style=\"color:#008000\" value=\"#008000\">ירוק כהה</option>";
		s+="					<option style=\"color:#0000ff\" value=\"#0000ff\">כחול</option>";
		s+="					<option style=\"color:#000080\" value=\"#000080\">כחול כהה</option>";
		s+="					<option style=\"color:#0080ff\" value=\"#0080ff\">תכלת</option>";
		s+="					<option style=\"color:#ff8000\" value=\"#ff8000\">כתום</option>";
		s+="					<option style=\"color:#ffff00\" value=\"#ffff00\">צהוב</option>";
		s+="					<option style=\"color:#ff80ff\" value=\"#ff80ff\">ורוד</option>";
		s+="					<option style=\"color:#c0c0c0\" value=\"#c0c0c0\">אפור</option>";
		s+="					<option style=\"color:#808080\" value=\"#808080\">אפור כהה</option>";
		s+="					<option style=\"color:#804000\" value=\"#804000\">חום</option>";
		s+="					<option style=\"color:#800080\" value=\"#800080\">סגול</option>";
		s+="				</select>";
		s+="			</label>";

		s+="			<label>";
		s+="				<img src=\"../style/editor/font.png\" alt=\"גופן‎\"> גופן:";
		s+="				<select id=\"richFont\">";
		s+="					<option value=\"auto\">- בחר -</option>";
		s+="					<option style=\"font-family:Arial\">Arial</option>";
		s+="					<option style=\"font-family:Comic Sans MS\">Comic Sans MS</option>";
		s+="					<option style=\"font-family:Courier\">Courier</option>";
		s+="					<option style=\"font-family:'Ktav Yad CLM','Guttman Yad'\" value=\"'Ktav Yad CLM','Guttman Yad'\">כתב יד</option>";
		s+="					<option style=\"font-family:'Anka CLM','Guttman Yad-Brush'\" selected=\"selected\" value=\"Anka CLM\">כתב יד 2</option>";
		s+="					<option style=\"font-family:Times New Roman\">Times New Roman</option>";
		s+="				</select>";
		s+="			</label>";

		s+="			<label>";
		s+="				<img src=\"../style/editor/format.png\" alt=\"סגנון‎\"> סגנון:";
		s+="				<select id=\"richFormat\">";
		s+="					<option value=\"auto\">- בחר -</option>";
		s+="					<option value=\"&lt;h1&gt\">כותרת ראשית</option>";
		s+="					<option value=\"&lt;h2&gt\">כותרת משנית</option>";
		s+="					<option value=\"&lt;h3&gt\">כותרת שלישית</option>";
		s+="					<option value=\"&lt;h4&gt\">כותרת רביעית</option>";
		s+="					<option value=\"&lt;h5&gt\">כותרת חמישית</option>";
		s+="					<option value=\"&lt;p&gt\">פסקה</option>";
		s+="				</select>";
		s+="			</label>";
		s+="		</div>";
		s+="	</div>";

		s+="	<div class=\"editArea\">";

		s+="	</div>";

		s+="</fieldset>";

		theObject.innerHTML = s;
		dtextarea.parentNode.appendChild(theObject);

		var editArea = getElementsByClassName(theObject,"editArea")[0];

		diframe = document.createElement("iframe");
		diframe.src = "../common/empty.html";
		if (window.attachEvent){diframe.attachEvent("onload",function(){self.makeIframeEditable();});} else //IE8
		diframe.onload = function(){self.makeIframeEditable();}
		editArea.appendChild(diframe);

		dtextarea.style.display = "none";
		editArea.appendChild(dtextarea);
	}

	//a עושה את האייפריים ניתן לעריכה ועושה שכל הזמן זה יעדכן את הטקסט-ארה עם הקוד של האייפריים
	//aבאקספלורר (בדקתי בגרסאות 6 עד 9) זה קורא לפונקציה הזאת עוד פעם אחת (לפחות), ולכן הפונקציה מופרדת ל2 חלקים כדי לא לקבל שגיאות (אם כי זה רץ גם עם השגיאות) י
	var iefix = false;
	this.makeIframeEditable = function()
	{
		if (iefix == false) //חלק ראשון: אם זה הפעם הראשונה
		{
			this.createControls();
			diframe.contentWindow.document.designMode = "on";
		}
		if (navigator.appName != "Microsoft Internet Explorer" || iefix == true) //חלק שני: אם זה פעם שניה או שזה לא אקספלורר בכלל
		{
			diframe.contentWindow.document.body.innerHTML = dtextarea.value;
			codeUpdater = setInterval(self.updateTextArea,100);
		}
		iefix = true; //מגדיר שהפעם הראשונה עברה במשתנה סטטי
	}

	//a מעדכן את הטקסטאראה בקוד של האיפריים
	this.updateTextArea = function()
	{
		if (!diframe.contentWindow)
			clearInterval(codeUpdater);
		else 
			dtextarea.value = diframe.contentWindow.document.body.innerHTML;
	}

	//a נותן לכל הכפתורים את הפונקציות שלהם
	this.createControls = function()
	{
		getElementById(theObject,"richUndo").onclick = function(){self.edit('Undo','');return false;}
		getElementById(theObject,"richRedo").onclick = function(){self.edit('Redo','');return false;}
		getElementById(theObject,"richLink").onclick = function(){url=prompt('הכנס כתובת לקישור:','http://');  if(url!=null){self.edit('createlink', url);}return false;}
		getElementById(theObject,"richInsertImage").onclick = function(){img=prompt('הכנס כתובת של תמונה:','http://');if(img!=null){self.edit('Insertimage',img);}return false;}
		getElementById(theObject,"richTable").onclick = function(){self.createTable(parseInt(prompt('שורות:','3')),parseInt(prompt('עמודות:','3')));return false;}
		getElementById(theObject,"richHr").onclick = function(){self.edit('InsertHorizontalRule','');return false;}
		getElementById(theObject,"richBold").onclick = function(){self.edit('bold','');return false;}
		getElementById(theObject,"richItalic").onclick = function(){self.edit('Italic','');return false;}
		getElementById(theObject,"richUnderLine").onclick = function(){self.edit('Underline','');return false;}
		getElementById(theObject,"richRight").onclick = function(){self.edit('Justifyright','');return false;}
		getElementById(theObject,"richCenter").onclick = function(){self.edit('JustifyCenter','');return false;}
		getElementById(theObject,"richLeft").onclick = function(){self.edit('Justifyleft','');return false;}
		getElementById(theObject,"richNumList").onclick = function(){self.edit('InsertorderedList','');return false;}
		getElementById(theObject,"richDotList").onclick = function(){self.edit('InsertUnorderedList','');return false;}
		getElementById(theObject,"richOutDent").onclick = function(){self.edit('outdent','');return false;}
		getElementById(theObject,"richInDent").onclick = function(){self.edit('indent','');return false;}
		getElementById(theObject,"richRemoveFormat").onclick = function(){self.edit('removeFormat','');return false;}
		getElementById(theObject,"richInsertCode").onclick = function(){code=prompt('הכנס קוד\nhtml (embed)\nשל סרטון:','');if(code!=null){self.insertHTML(code);}return false;}
		getElementById(theObject,"richViewSource").onclick = function(){self.changeMode();return false;}
		getElementById(theObject,"richFontSize").onchange = function(){self.edit('FontSize',this.value);return false;}
		getElementById(theObject,"richFill").onchange = function(){if(!document.all){self.edit('HiliteColor',this.value);}else{self.edit('backcolor',this.value);}return false;}
		getElementById(theObject,"richColor").onchange = function(){self.edit('ForeColor',this.value);return false;}
		getElementById(theObject,"richFont").onchange = function(){self.edit('FontName',this.value);return false;}
		getElementById(theObject,"richFormat").onchange = function(){self.edit('FormatBlock',this.value);return false;}
	}

	//a פונקצייה שמפעילה את הפקודות עריכה המובנות של הדפדפן
	this.edit = function(a,b)
	{
		diframe.contentWindow.focus();
		diframe.contentWindow.document.execCommand(a,false,b);
		diframe.contentWindow.focus();
	}

	//a מחליף בין מצב עריכת קוד למצב עורך טקסט
	this.changeMode = function()
	{
		if (mode == "rich")
		{
			mode = "code";
			clearInterval(codeUpdater);
			dtextarea.style.display = "";
			diframe.style.display = "none";
		}
		else
		{
			mode = "rich";
			diframe.contentWindow.document.body.innerHTML = dtextarea.value;
			codeUpdater = setInterval("self.updateTextArea()",100);
			dtextarea.style.display = "none";
			diframe.style.display = "";
		}
	}

	//a פונקציית הוספת טבלה
	this.createTable = function(trs,tds)
	{
		table = document.createElement("table");
		tableBody = document.createElement("tbody");
		for (i=0; i<trs; i++)
		{
			tr = document.createElement('tr');
			for(j = 0; j<tds; j++)
			{
				td = document.createElement('td');
				td.innerHTML = "&nbsp;";
				tr.appendChild(td);
			}			
			table.appendChild(tr);
		}
		table.border = '1';
		diframe.contentWindow.document.body.appendChild(table);
	}

	//a הכנסת קוד, יש פונקציה פשוטה לזה אבל אקספלורר כמובן לא תומך בה
	this.insertHTML = function(html)
	{
		if (document.all) //if IE
		{
			diframe.contentWindow.focus();
			var oRng = diframe.contentWindow.document.selection.createRange();
			oRng.pasteHTML(html);
			oRng.collapse(false);
			oRng.select();
		}
		else
		{
			this.edit('insertHTML',html);
		}
	}
}

//getElementById that works on nodes (the builtin one works on 'document' only)
//very usefull if you want to get node from the ajah function before body.appendChild-ing it
function getElementById(parent,id)
{
	var elems = parent.getElementsByTagName("*");
	
	for (var i=0; i<elems.length; i++)
	{
		if (elems[i].id && elems[i].id == id)
		{
			return elems[i];
		}
	}
	
	return false;
}

//getElementsByClassName
function getElementsByClassName(parent,className)
{
	var i;
	var y;

	var elems = parent.getElementsByTagName("*");
	
	var elemsArray = new Array();
	
	for (i=0,y=0; i<elems.length; i++)
	{
		if (!elems[i].className || elems[i].className != className)
		{
			elemsArray[y++] = elems[i];
		}
	}
	
	return elemsArray;
}

