//===============================[Editor Class]==============================\\


/**
 * Initializes the object.
 * @param areaID The ID of the textarea to be replaced.
 */
function HTMLEditor(areaID, width, height)
{
	this.name = areaID+"HTMLEditor";
	this.areaID = areaID;
	this.width = width;
	this.height= height;
	this.toolbar = new ToolBar(this.name+'ToolBar');
	HTMLEditor.createToolBar(this);
}


HTMLEditor.createToolBar = function(editor)
{
	var toolbar = editor.toolbar;
	var tbbasic=toolbar.addToolBar('basic', 'Formázás');
	tbbasic.addButton('bold', baseurl+'/modules/icons/bold.png','Félkövér', 					"javascript:HTMLEditor.execCommand('"+editor.name+"','bold',null);");
	tbbasic.addButton('italic', baseurl+'/modules/icons/italic.png','Dőlt', 					"javascript:HTMLEditor.execCommand('"+editor.name+"','italic',null);");
	tbbasic.addButton('underline', baseurl+'/modules/icons/underline.png','Aláhúzott', 			"javascript:HTMLEditor.execCommand('"+editor.name+"','underline',null);");
	tbbasic.addSeparator();
	tbbasic.addButton('left', baseurl+'/modules/icons/left.png','Balra zárt', 					"javascript:HTMLEditor.execCommand('"+editor.name+"','justifyleft',null);");
	tbbasic.addButton('right', baseurl+'/modules/icons/right.png','Jobbra zárt', 				"javascript:HTMLEditor.execCommand('"+editor.name+"','justifyright',null);");
	tbbasic.addButton('center', baseurl+'/modules/icons/center.png','Középre zárt',			 	"javascript:HTMLEditor.execCommand('"+editor.name+"','justifycenter',null);");
	tbbasic.addButton('justify', baseurl+'/modules/icons/justify.png','Sorkizárt', 				"javascript:HTMLEditor.execCommand('"+editor.name+"','justifyfull',null);");
	tbbasic.addSeparator();
	tbbasic.addButton('enum', baseurl+'/modules/icons/enum.png','Számozott felsorolás', 		"javascript:HTMLEditor.execCommand('"+editor.name+"','insertorderedlist',null);");
	tbbasic.addButton('item', baseurl+'/modules/icons/item.png','Felsorolás', 					"javascript:HTMLEditor.execCommand('"+editor.name+"','insertunorderedlist',null);");
	tbbasic.addSeparator();
	tbbasic.addButton('indentinc', baseurl+'/modules/icons/identinc.png','Behúzás növelése',	"javascript:HTMLEditor.execCommand('"+editor.name+"','indent',null);");
	tbbasic.addButton('indentdec', baseurl+'/modules/icons/identdec.png','Behúzás csökkentése',	"javascript:HTMLEditor.execCommand('"+editor.name+"','outdent',null);");
	tbbasic.addSeparator();
	tbbasic.addButton('pict', baseurl+'/modules/icons/pict.png','Kép beszúrása', 				"javascript:HTMLEditor.insertImage('"+editor.name+"');");
	tbbasic.addButton('link', baseurl+'/modules/icons/link.png','Link beszúrása', 				"javascript:HTMLEditor.insertLink('"+editor.name+"');");
	tbbasic.addButton('table', baseurl+'/modules/icons/table.png','Táblázat beszúrása', 				"javascript:HTMLEditor.insertTable('"+editor.name+"');");
	var ddheading = tbbasic.addDropDown('heading', 'Stílus', function(name, value, data){HTMLEditor.setHeading(name, value, data);} , editor.name );
		ddheading.addOption('Címsor 1', '<h1>');
		ddheading.addOption('Címsor 2', '<h2>');
		ddheading.addOption('Címsor 3', '<h3>');
		ddheading.addOption('Címsor 4', '<h4>');
		ddheading.addOption('Normál',   '<p>');
	var tbother=toolbar.addToolBar('other', 'Egyéb');
	tbother.addButton('other', baseurl+'/modules/icons/txt2html.png','Egyéb', 					"javascript:HTMLEditor.toHTML('"+editor.name+"');");
}


HTMLEditor.editor = new Array();
HTMLEditor.count = 0;


HTMLEditor.prototype.onSubmit = function()
{
	var area = document.getElementById(this.areaID);
	var htmleditor = document.getElementById(this.name);
	area.value = htmleditor.contentWindow.document.body.innerHTML.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
	//window.alert(area.value);
}

HTMLEditor.onSubmit = function()
{
	var result = true;
	for (i=0;i<HTMLEditor.count;i++) result &= HTMLEditor.editor[i].onSubmit();
	return result;
}

HTMLEditor.attachEvents = function(name)
{
	if (document.all)
	{
		document.attachEvent('submit', HTMLEditor.onSubmit  );
	}
	else
	{
		document.addEventListener('submit', HTMLEditor.onSubmit , false );
	}
}


HTMLEditor.insertNodeAtSelection = function(win, insertNode)
  {
      // get current selection
      var sel = win.getSelection();

      // get the first range of the selection
      // (there's almost always only one range)
      var range = sel.getRangeAt(0);

      // deselect everything
      sel.removeAllRanges();

      // remove content of current selection from document
      range.deleteContents();

      // get location of current selection
      var container = range.startContainer;
      var pos = range.startOffset;

      // make a new range for the new selection
      range=document.createRange();

      if (container.nodeType==3 && insertNode.nodeType==3) {

        // if we insert text in a textnode, do optimized insertion
        container.insertData(pos, insertNode.nodeValue);

        // put cursor after inserted text
        range.setEnd(container, pos+insertNode.length);
        range.setStart(container, pos+insertNode.length);

      } else {


        var afterNode;
        if (container.nodeType==3) {

          // when inserting into a textnode
          // we create 2 new textnodes
          // and put the insertNode in between

          var textNode = container;
          container = textNode.parentNode;
          var text = textNode.nodeValue;

          // text before the split
          var textBefore = text.substr(0,pos);
          // text after the split
          var textAfter = text.substr(pos);

          var beforeNode = document.createTextNode(textBefore);
          afterNode = document.createTextNode(textAfter);

          // insert the 3 new nodes before the old one
          container.insertBefore(afterNode, textNode);
          container.insertBefore(insertNode, afterNode);
          container.insertBefore(beforeNode, insertNode);

          // remove the old node
          container.removeChild(textNode);

        } else {

          // else simply insert the node
          afterNode = container.childNodes[pos];
          container.insertBefore(insertNode, afterNode);
        }

        range.setEnd(afterNode, 0);
        range.setStart(afterNode, 0);
      }

      sel.addRange(range);
  };


HTMLEditor.insertImage = function(name)
{
	var r = window.showModalDialog(baseurl+"/images/select",null, "dialogwidth: 600; dialogheight: 300; resizable: yes; location: no");
	if (r!=null) HTMLEditor.execCommand(name,'insertimage',r);
}

HTMLEditor.insertTable = function(name)
{
	var e = document.getElementById(name);
	table = e.contentWindow.document.createElement("table");
	table.setAttribute("border", "0");
	table.setAttribute("width", "300");
	table.setAttribute("cellpadding", "2");
	table.setAttribute("cellspacing", "0");
	tbody = e.contentWindow.document.createElement("tbody");
	for (var i=0; i < 1; i++) 
	{
		tr =e.contentWindow.document.createElement("tr");
		for (var j=0; j < 2; j++) 
		{
			td =e.contentWindow.document.createElement("td");
			br =e.contentWindow.document.createElement("span");
			br.innerHTML="Oszlop"+j+1;
			td.appendChild(br);
			tr.appendChild(td);
		}
		tbody.appendChild(tr);
	}
	table.appendChild(tbody);
	HTMLEditor.insertNodeAtSelection(e.contentWindow, table);
}

HTMLEditor.insertLink = function(name)
{
	var r = prompt("Kérem adja meg az URL címet:","");
	if (r!=null) HTMLEditor.execCommand(name,'createlink',r);
}

HTMLEditor.setHeading = function(name, value, data)
{
	HTMLEditor.execCommand(data,'formatblock',value);
}

HTMLEditor.create = function(areaID, width, height)
{
	var editor = new HTMLEditor(areaID, width, height);
	HTMLEditor.editor[HTMLEditor.count++]=editor;
	editor.realize();	
}


HTMLEditor.getEditor = function(name)
{
	for (i=0;i<HTMLEditor.count;i++) if (HTMLEditor.editor[i].name==name) return HTMLEditor.editor[i];
}


HTMLEditor.StartIFrame2 = function(name)
{
	var editor = HTMLEditor.getEditor(name);
	if (editor==undefined) alert('Could not find the specified editor!');
	var area = document.getElementById(editor.areaID);
	var doc = document.getElementById(name).contentWindow.document;
	doc.designMode="on";
	area.style.display='none';
}

HTMLEditor.StartIFrame = function(name)
{
	var editor = HTMLEditor.getEditor(name);
	if (editor==undefined) alert('Could not find the specified editor!');
	var area = document.getElementById(editor.areaID);
	var doc = document.getElementById(name).contentWindow.document;
	doc.open();
	doc.writeln('<html><body>');
	doc.writeln(area.value.replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">"));
	doc.writeln('</body></html>');
	doc.close();

HTMLEditor.StartIFrame2(name);

}

HTMLEditor.switchToToolbar = function(edtname, tbname)
{
	var editor = HTMLEditor.getEditor(edtname);
	ToolBar.switchTo(tbname, editor.toolbar);
}

HTMLEditor.toHTML = function(name)
{
	//var editor = HTMLEditor.getEditor(edtname);
	var doc = document.getElementById(name).contentWindow.document;
	doc.body.innerHTML = doc.body.innerHTML.replace(/\n/g,'\n\r<br/>');
	
}

HTMLEditor.setWidthByClass = function(width_array, class_array)
{
	var allHTMLTags=document.getElementsByTagName("*");

	for (i=0; i<allHTMLTags.length; i++) 
	{
		for (j=0; j<class_array.length; j++) 
		{
			if (allHTMLTags[i].className == class_array[j])
			{
				allHTMLTags[i].style.width=width_array[j]+'px';
			}		
		}
	}
}

HTMLEditor.execCommand = function(name, cmd, value)
{
	//var editor = HTMLEditor.getEditor(name);
	document.getElementById(name).contentWindow.document.execCommand(cmd, false, value);	
}

HTMLEditor.prototype.realize = function()
{
	document.writeln('<span class="HTMLEditorInput">');
	ToolBar.init(this.toolbar, "HTMLEditor.switchToToolbar('"+this.name+"','"); //Doesn't need to close the function!!!
	document.writeln('<iframe id="'+this.name+'" width="'+this.width+'" height="'+this.height+'" onload="HTMLEditor.StartIFrame(\''+this.name+'\')" class="HTMLEditor"></iframe>');
	HTMLEditor.setWidthByClass(new Array(this.width,this.width-3,this.width,this.width,this.width-10, this.width),new Array('tbCaptionRow0','tbCaptionRow1','tbCaptionPart','tbContentPart','tbContentPartSelected1','tbContent') );
	document.writeln('</span>');

}

