/*
    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
*/

// preload images
var preimglist = ['http://777hp.com/i/stock_text_bold.png','http://777hp.com/i/stock_text_bold_o.png','http://777hp.com/i/stock_text_italic.png','http://777hp.com/i/stock_text_italic_o.png','http://777hp.com/i/stock_text_underlined.png','http://777hp.com/i/stock_text_underlined_o.png','http://777hp.com/i/stock_text_left.png','http://777hp.com/i/stock_text_left_o.png','http://777hp.com/i/stock_text_justify.png','http://777hp.com/i/stock_text_justify_o.png','http://777hp.com/i/stock_text_center.png','http://777hp.com/i/stock_text_center_o.png','http://777hp.com/i/stock_text_right.png','http://777hp.com/i/stock_text_right_o.png'];
for (var primg, il = 0; il < preimglist.length; il++){
	primg = new Image();
	primg.src = preimglist[il];
}

function insertEditableArea(editableAreaName, editableAreaWidth, editableAreaHeight, editableAreaLayout) {
	var boldButton = '<img src="http://777hp.com/i/stock_text_bold.png" alt="Bold" name="' + editableAreaName + '" class="editableAreaButton" id="bold" style="width:24px; height:24px;" />';
	var italicButton = '<img src="http://777hp.com/i/stock_text_italic.png" alt="Italic" name="' + editableAreaName + '" class="editableAreaButton" id="italic" style="width:24px; height:24px;" />';
	var underlinedButton = '<img src="http://777hp.com/i/stock_text_underlined.png" alt="Underlined" name="' + editableAreaName + '" class="editableAreaButton" id="underline" style="width:24px; height:24px;" />';
	var alignLeftButton = '<img src="http://777hp.com/i/stock_text_left.png" alt="Align Left" name="' + editableAreaName + '" class="editableAreaButton" id="justifyleft" style="width:24px; height:24px;" />';
	var justifyButton = '<img src="http://777hp.com/i/stock_text_justify.png" alt="Justify" name="' + editableAreaName + '" class="editableAreaButton" id="justifyfull" style="width:24px; height:24px;" />';
	var alignCenterButton = '<img src="http://777hp.com/i/stock_text_center.png" alt="Align Center" name="' + editableAreaName + '" class="editableAreaButton" id="justifycenter" style="width:24px; height:24px;" />';
	var alignRightButton = '<img src="http://777hp.com/i/stock_text_right.png" alt="Align Right" name="' + editableAreaName + '" class="editableAreaButton" id="justifyright" style="width:24px; height:24px;" />';
	var editableArea = '<iframe src="/textDBapi/blank.html" id="' + editableAreaName + '" \n style="width:' + editableAreaWidth + 'px; height:' + editableAreaHeight + 'px; border-style:inset; border-width:thin;" frameborder="0px"></iframe><span class="reqf">*</span>'

	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><span class="reqf">*</span>');
	}
}
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 ititButtons(editableAreaName) {
	var kids = document.getElementsByTagName('img');

	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
	this.src = this.src.replace(/\.png/,'')+'_o.png';
}

function buttonMouseOut() {
	// events for mouseOut on buttons
	// e.g. this.style.xxx = xxx
	this.src = this.src.replace(/_o\.png/,'')+'.png';
}


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); }
	return false;
}