/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +--------------------------------------------------------+
// | PHP version 5.0.x                                      |
// +--------------------------------------------------------+
// | Copyright  모두 Sayou World Co.,Ltd All Lights reserved  |
// +--------------------------------------------------------+
//
// $Id: webeditor.tpl.html, v 0.0.1 2005/04/11 11:50:00 crosser Exp $
//
// JS Web Editor

function onceClick()
{
	if(document.all) {
		event.srcElement.click();
		return false;
	}
	return true;
}

String.prototype.tagRegulate = function() {
	return this.replace(/(\r|\n)+/g, "\n").replace(/ {2,}|\t+/g, ' '); //.replace(/(<\/?)strong((?= )[^>]*)?>/ig, '$1b$2>').replace(/(<\/?)em((?= )[^>]*)?>/ig, '$1i$2>')
}

String.prototype.editorToInteger = function() {
	return this.replace(/[^\d]/g, '') * 1;
}

function insertNodeAtSelection(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);
}

function SHJEditor(formName, inputName)
{
	this.divTag = document.getElementById('webeditarea');
	this.toolbarsTag = document.getElementById('webtoolbars');
	this.toggleView = document.getElementsByName('toggleView')[0];
	var formTag = document.getElementById(formName);
	this.inputTag = document.createElement('INPUT');
	this.inputTag.name = inputName;
	this.inputTag.type = 'hidden';
	formTag.appendChild(this.inputTag);
	this.width = this.divTag.style.width;
	this.height = this.divTag.style.height;
	this.iframeText = '<iframe width="100%" height="100%" frameborder="0"></iframe>';
	this.mode = 'html';

	this.designModeOn = function() {
		if(document.all) {
			this.editArea.document.body.contentEditable = 'true';
			this.editArea.document.body.focus();
		} else {
			this.editArea.document.designMode = 'on';
			//this.editArea.document.execCommand('useCSS', false, true);
		}
		return true;
	}

	this.setEditArea = function() {
		this.divTag.innerHTML = this.iframeText;
		this.editArea = this.divTag.getElementsByTagName("IFRAME")[0].contentWindow;
		this.editArea.document.open();
		this.editArea.document.write('<html><head></head><body style="margin:0;"></body></html>');
		this.editArea.document.close();
		return true;
	}

	this.start = function() {
		this.setEditArea();
		this.designModeOn();
		return true;
	}

	this.inputHTML = function(html) {
		this.setEditArea();
		this.editArea.document.body.style.overflow = 'hidden';
		this.editArea.document.body.innerHTML = '<textarea style="width:' + this.width + '; height:' + this.height + '; border-width:0; margin:0; display:inline;">' + html + '</textarea>';
		this.mode = 'source';
		this.toolbarsTag.style.display = 'none';
		this.viewSource();
		return true;
	}

	this.viewSource = function() {
		if(this.toggleView.checked) {
			this.toggleSource();
		} else {
			this.toggleHTML();
		}
		return true;
	}

	this.toggleSource = function() {
		var html;
		html = this.editArea.document.body.innerHTML.tagRegulate().replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
		this.setEditArea();
		this.editArea.document.body.style.overflow = 'hidden';
		this.editArea.document.body.innerHTML = '<textarea style="width:' + this.width + '; height:' + this.height + '; border-width:0; margin:0; display:inline;">' + html + '</textarea>';
		this.mode = 'source';
		this.toolbarsTag.style.display = 'none';
		return true;
	}

	this.toggleHTML = function() {
		var html;
		html = this.editArea.document.body.childNodes[0].value;
		this.setEditArea();
		this.editArea.document.body.innerHTML = html.tagRegulate();
		this.designModeOn();
		this.mode = 'html';
		this.toolbarsTag.style.display = 'block';
		return true;
	}

	this.exec = function(operate) {
		this.editArea.document.execCommand(operate, false, null);
		if(document.all) {
			this.editArea.document.body.focus();
		}
		return true;
	}
	
	this.exec2 = function(operate, value) {
		this.editArea.document.execCommand(operate, false, value);
		if(document.all) {
			this.editArea.document.body.focus();
		}
		return true;
	}
	
	this.attachText = function(txt) {
		if(document.all) {
			if(this.editArea.document.selection != null) {
				var sel;
				sel = this.editArea.document.selection.createRange();
				sel.pasteHTML(txt);
			} else {
				this.editArea.document.body.innerHTML += txt;
			}
			this.editArea.document.body.focus();
		} else {
			var elem;
			elem = document.createTextNode(txt);
			insertNodeAtSelection(this.editArea, elem);
      	}
		return true;
	}

	this.attachElement = function(elem) {
		if(document.all) {
			var appVersion = window.navigator.appVersion;
			var msiePos    = appVersion.indexOf("MSIE")+5;
			var winsPos    = appVersion.lastIndexOf("Windows");
			var version    = parseFloat(appVersion.substring(msiePos, winsPos));
			if(version >= 8) {
				this.editArea.document.body.appendChild(elem);
				return true;
			}
			if(this.editArea.document.selection != null) {
				var sel, divTag;
				divTag = document.createElement('DIV');
				divTag.appendChild(elem);
				sel = this.editArea.document.selection.createRange();
				sel.pasteHTML(divTag.innerHTML);
			} else {
				this.editArea.document.body.appendChild(elem);
			}
			this.editArea.document.body.focus();
		} else {
			insertNodeAtSelection(this.editArea, elem);
      	}
		return true;
	}
	
	this.attachHTML = function(html) {
		var divTag;
		divTag = document.createElement("DIV");
		divTag.innerHTML = html;
		this.attachElement(divTag.childNodes[0]);
		return true;
	}

	this.attachMedia = function(link) {
		var emTag;
		emTag = document.createElement("EMBED");
		emTag.src = link;
		this.attachElement(emTag);
		return true;
	}

	this.attachImage = function(link, w, h) {
		var imgTag;
		imgTag = document.createElement("IMG");
		imgTag.src = link;
		imgTag.style.width = w;
		imgTag.style.height = h;
		this.attachElement(imgTag);
		return true;
	}
	
	this.attachTable = function(rows, cols) {
		var tableHTML, row, col;
		tableHTML = '<table width="100%" border="1">';
		rows = rows.editorToInteger();
		cols = cols.editorToInteger();
		if(rows == 0) {
			rows = 3;
		}
		if(cols == 0) {
			cols = 5;
		}
		for(row = 0; row < rows; row ++) {
			tableHTML += '<tr>';
			for(col = 0; col < cols; col ++) {
				tableHTML += '<td>&nbsp;</td>';
			}
			tableHTML += '</tr>';
		}
		tableHTML += '</table>';
		this.attachHTML(tableHTML);
		return true;
	}
	
	this.linkWindow = function() {
		window.open('/board/linkurl.html', '_blank', 'width=300px,height=30px,resizable=no');
		return true;
	}

	this.mediaWindow = function() {
		window.open('/board/mediaadd.html', '_blank', 'width=500px,height=400px,resizable=no');
		return true;
	}

	this.imageWindow = function() {
		window.open('/board/imageadd.html', '_blank', 'width=500px,height=400px,resizable=no');
		return true;
	}

	this.symbolWindow = function() {
		window.open('/board/symboladd.html', '_blank', 'width=400px,height=100px,resizable=no');
		return true;
	}

	this.forecolorWindow = function() {
		window.open('/board/forecoloradd.html', '_blank', 'width=306px,height=256px,resizable=no');
		return true;
	}

	this.backcolorWindow = function() {
		window.open('/board/backcoloradd.html', '_blank', 'width=306px,height=256px,resizable=no');
		return true;
	}

	this.formatblockWindow = function() {
		window.open('/board/formatblockadd.html', '_blank', 'width=200px,height=400px,resizable=no');
		return true;
	}

	this.fontnameWindow = function() {
		window.open('/board/fontnameadd.html', '_blank', 'width=200px,height=400px,resizable=no');
		return true;
	}

	this.fontsizeWindow = function() {
		window.open('/board/fontsizeadd.html', '_blank', 'width=200px,height=400px,resizable=no');
		return true;
	}

	this.tableWindow = function() {
		window.open('/board/tableadd.html', '_blank', 'width=400px,height=300px,resizable=no');
		return true;
	}

	this.preview = function() {
		var newwin;
		if(this.mode == 'source') {
			this.toggleHTML();
			this.toggleView.checked = false;
		}
		newwin = window.open('about:blank', '_blank', 'width=610px,height=410px,resizable=yes,scrollbars=yes');
		newwin.document.write('<html><head><title>미리보기</title></head><body style="margin:5px;"></body></html>');
		newwin.document.close();
		newwin.document.body.innerHTML = this.editArea.document.body.innerHTML;
		return true;
	}
	
	this.beforeSubmit = function() {
		if(this.mode == 'source') {
			this.toggleHTML();
		}
		this.inputTag.value = this.editArea.document.body.innerHTML.tagRegulate();
		return true;
	}
}
