/*
    eArea - a simple web-based text editor
    Copyright (C) 2006  Oliver Moran

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

function insertEditableArea(editableAreaName, editableAreaWidth, editableAreaHeight, editableAreaLayout, editableAreaStyle) {
	var boldButton = '<input type="image" src="zabots/eArea/icons/rte-bold.png" value="Bold" alt="Bold" name="' + editableAreaName + '" class="editableAreaButton" id="bold" style="width:24; height:24;">';
	var italicButton = '<input type="image" src="zabots/eArea/icons/rte-italic.png" value="Italic" alt="Italic" name="' + editableAreaName + '" class="editableAreaButton" id="italic" style="width:24; height:24;">';
	var underlinedButton = '<input type="image" src="zabots/eArea/icons/rte-underlined.png" value="Underlined" alt="Underlined" name="' + editableAreaName + '" class="editableAreaButton" id="underline" style="width:24; height:24;">';
	var alignLeftButton = '<input type="image" src="zabots/eArea/icons/rte-align-left.png" value="Align Left" alt="Align Left" name="' + editableAreaName + '" class="editableAreaButton" id="justifyleft" style="width:24; height:24;">';
	var justifyButton = '<input type="image" src="zabots/eArea/icons/stock_text_justify.png" value="Justify" alt="Justify" name="' + editableAreaName + '" class="editableAreaButton" id="justifyfull" style="width:24; height:24;">';
	var alignCenterButton = '<input type="image" src="zabots/eArea/icons/rte-align-center.png" value="Align Center" alt="Align Center" name="' + editableAreaName + '" class="editableAreaButton" id="justifycenter" style="width:24; height:24;">';
	var alignRightButton = '<input type="image" src="zabots/eArea/icons/rte-align-right.png" value="Align Right" alt="Align Right" name="' + editableAreaName + '" class="editableAreaButton" id="justifyright" style="width:24; height:24;">';
	var editableArea = '<iframe src="zabots/eArea/blank.html?style=' + editableAreaStyle + '" id="' + editableAreaName + '" \n style="width:' + editableAreaWidth + 'px; height:' + editableAreaHeight + 'px; border-style:inset; border-width:thin;" frameborder="0px"></iframe>'

	var editableAreaHTML = editableAreaLayout;
	editableAreaHTML = editableAreaHTML.replace("[bold]", boldButton);
	editableAreaHTML = editableAreaHTML.replace("[italic]", italicButton);
	editableAreaHTML = editableAreaHTML.replace("[underlined]", underlinedButton);
	editableAreaHTML = editableAreaHTML.replace("[align-left]", alignLeftButton);
	editableAreaHTML = editableAreaHTML.replace("[justify]", justifyButton);
	editableAreaHTML = editableAreaHTML.replace("[align-center]", alignCenterButton);
	editableAreaHTML = editableAreaHTML.replace("[align-right]", alignRightButton);
	editableAreaHTML = editableAreaHTML.replace("[edit-area]", editableArea);
	
	if (document.designMode) {
		document.write(editableAreaHTML);
		ititButtons(editableAreaName);
	} else {
		// create a normal <textarea> if document.designMode does not exist
		document.write('<textarea id="' + editableAreaName + '" style="width:' + editableAreaWidth + 'px; height:' + editableAreaHeight + 'px; ' + editableAreaStyle + '"></textarea>');
	}
}

function editableAreaContents(editableAreaName) {
	if (document.designMode) {
		// Explorer reformats HTML during document.write() removing quotes on element ID names
		// so we need to address Explorer elements as window[elementID]
		if (window[editableAreaName]) return window[editableAreaName].document.body.innerHTML;
		return document.getElementById(editableAreaName).contentWindow.document.body.innerHTML;
	} else {
		// return the value from the <textarea> if document.designMode does not exist
		return document.getElementById(editableAreaName).value;
	}
}

function clearAreaContents(editableAreaName) {
	//added by: camilord
	//to clear the contents of editableAreaName
	window[editableAreaName].document.body.innerHTML = '';
	editableAreaContents(editableAreaName);
}

function setAreaContents(editableAreaName,contentForEditableArea) {
	//added by: camilord
	//to clear the contents of editableAreaName
	window[editableAreaName].document.body.innerHTML = contentForEditableArea;
	editableAreaContents(editableAreaName);
}

function ititButtons(editableAreaName) {
	var kids = document.getElementsByTagName('input');

	for (var i=0; i < kids.length; i++) {
		if (kids[i].className == "editableAreaButton" && kids[i].name == editableAreaName) {
			kids[i].onmouseover = buttonMouseOver;
			kids[i].onmouseout = buttonMouseOut;
			kids[i].onmouseup = buttonMouseUp;
			kids[i].onmousedown = buttonMouseDown;
			kids[i].onclick = buttonOnClick;
		}
	}
}

function buttonMouseOver() {
	// events for mouseOver on buttons
	// e.g. this.style.xxx = xxx
}

function buttonMouseOut() {
	// events for mouseOut on buttons
	// e.g. this.style.xxx = xxx
}


function buttonMouseUp() {
	// events for mouseUp on buttons
	// e.g. this.style.xxx = xxx
}

function buttonMouseDown(e) {
	// events for mouseDown on buttons
	// e.g. this.style.xxx = xxx

	// prevent default event (i.e. don't remove focus from text area)
	var evt = e ? e : window.event; 

	if (evt.returnValue) {
		evt.returnValue = false;
	} else if (evt.preventDefault) {
		evt.preventDefault( );
	} else {
		return false;
	}
}

function buttonOnClick() {
	// Explorer reformats HTML during document.write() removing quotes on element ID names
	// so we need to address Explorer elements as window[elementID]
	if (window[this.name]) { window[this.name].document.execCommand(this.id, false, null); }
	else { document.getElementById(this.name).contentWindow.document.execCommand(this.id, false, null); }
}