/*
    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="http://bloggboken.se/system/global/eArea/icons/stock_text_bold.png" value="Bold" alt="Bold" name="' + editableAreaName + '" class="editableAreaButton" id="bold" style="width:24; height:24;">';
	var italicButton = '<input type="image" src="http://bloggboken.se/system/global/eArea/icons/stock_text_italic.png" value="Italic" alt="Italic" name="' + editableAreaName + '" class="editableAreaButton" id="italic" style="width:24; height:24;">';
	var underlinedButton = '<input type="image" src="http://bloggboken.se/system/global/eArea/icons/stock_text_underlined.png" value="Underlined" alt="Underlined" name="' + editableAreaName + '" class="editableAreaButton" id="underline" style="width:24; height:24;">';
	var alignLeftButton = '<input type="image" src="http://bloggboken.se/system/global/eArea/icons/stock_text_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="http://bloggboken.se/system/global/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="http://bloggboken.se/system/global/eArea/icons/stock_text_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="http://bloggboken.se/system/global/eArea/icons/stock_text_right.png" value="Align Right" alt="Align Right" name="' + editableAreaName + '" class="editableAreaButton" id="justifyright" style="width:24; height:24;">';
		var host		= window.location.hostname;
		alert(host);

	var editableArea = '<iframe src="http://bloggboken.se/system/global/eArea/blank.html?style=' + editableAreaStyle + '" id="' + editableAreaName + '" \n style="background-color: #ffffff; 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 old_show_blog_editor(editableAreaName, editableAreaWidth, editableAreaHeight, editableAreaLayout, editableAreaStyle, username, blog_id, headline, preamble, keywords) {
	var boldButton = '<input type="image" style="border: 0px;" src="http://bloggboken.se/system/global/eArea/icons/stock_text_bold.png" value="Bold" alt="Bold" name="' + editableAreaName + '" class="editableAreaButton" id="bold" style="width:24; height:24;">';
	var italicButton = '<input type="image" style="border: 0px;" src="http://bloggboken.se/system/global/eArea/icons/stock_text_italic.png" value="Italic" alt="Italic" name="' + editableAreaName + '" class="editableAreaButton" id="italic" style="width:24; height:24;">';
	var underlinedButton = '<input type="image" style="border: 0px;" src="http://bloggboken.se/system/global/eArea/icons/stock_text_underlined.png" value="Underlined" alt="Underlined" name="' + editableAreaName + '" class="editableAreaButton" id="underline" style="width:24; height:24;">';
	var alignLeftButton = '<input type="image" style="border: 0px;" src="http://bloggboken.se/system/global/eArea/icons/stock_text_left.png" value="Align Left" alt="Align Left" name="' + editableAreaName + '" class="editableAreaButton" id="justifyleft" style="width:24; height:24;">';
	var justifyButton = '<input type="image" style="border: 0px;" src="http://bloggboken.se/system/global/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" style="border: 0px;" src="http://bloggboken.se/system/global/eArea/icons/stock_text_center.png" value="Align Center" alt="Align Center" name="' + editableAreaName + '" class="editableAreaButton" id="justifycenter" style="width:24; height:24;">';
	var alignRightButton = '<input type="image" style="border: 0px;" src="http://bloggboken.se/system/global/eArea/icons/stock_text_right.png" value="Align Right" alt="Align Right" name="' + editableAreaName + '" class="editableAreaButton" id="justifyright" style="width:24; height:24;">';
	var host		= window.location.hostname;
	var editableArea = '<iframe src="http://' + host + '/system/global/eArea/blank.php?blog_id='+blog_id+'&style=' + editableAreaStyle + '" id="' + editableAreaName + '" \n style="background-color: #ffffff; width: 100%; height:' + editableAreaHeight + 'px; border-style:inset; border-width:thin;" frameborder="0px"></iframe>'
		var host		= window.location.hostname;
	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) {
		if(headline!=0) {
			var forms = "<p style='margin: 0px;'><strong>Rubrik</strong></p><input type='text' id='headline' value='"+headline+"' style='width: 60%; padding: 5px;' /><p style='margin: 10px 0px 0px 0px;'><strong>Ingress</strong></p><textarea style='width: 60%; padding: 5px; height: 30px;' id='preamble'>"+preamble+"</textarea><p style='margin: 10px 0px 3px 0px;'><strong>Brödtext</strong></p>";
		} else {
			var forms = "<p style='margin: 0px;'><strong>Rubrik</strong></p><input type='hidden' id='blog_id' value='"+blog_id+"'><input type='text' id='headline' style='width: 60%; padding: 5px;' /><p style='margin: 10px 0px 0px 0px;'><strong>Ingress</strong></p><textarea style='width: 60%; padding: 5px; height: 30px;' id='preamble'></textarea><p style='margin: 10px 0px 3px 0px;'><strong>Brödtext</strong></p>";
		}
		if(headline!=0) {
			var submit	= "<p style='text-align: center;'><a href='javascript: save_edited_blog(\"" + username + "\","+blog_id+");' style='text-decoration: none; font-weight: bold; padding: 6px; display: block; width: 80px; border: 1px solid #555555;'>Spara blogg</a></p>";
		} else {
			var submit	= "<div style='text-align: center; '><a href='javascript: save_blog(\"" + username + "\");' style='text-decoration: none; font-weight: bold; padding: 6px; display: block; width: 80px; border: 1px solid #555555; background-color: #ffffff; color: #222222;'>Spara blogg</a></div>";
		}
		if(keywords) {		
			var keywords_field	= "<p style='margin: 0px;'><strong>Taggar</strong><input type='text' id='keywords' value='"+keywords+"'></p>"
		} else {
			var keywords_field	= "<p style='margin: 0px;'><strong>Taggar&nbsp;</strong><input type='text' id='keywords' value=''></p>"
		}
		var publish_checkbox	= "<p style='margin: 0px;'><strong>Publicera inl&auml;gget&nbsp;</strong><input type='checkbox' id='publish' checked='checked' /></p>"
		var table_top 		= "<table cellpadding='0px' cellspacing='0px'><tr><td class='innershadow_left'></td><td class='innershadow_content' >"
		var table_right 	= "</td><td class='innershadow_right'></td></tr>"
		var table_bottom 	= "<tr><td><img style='vertical-align: top;' src='/system/global/images/innershadow_corner_leftbottom.gif' alt='' /></td><td class='innershadow_bottom'></td><td><img style='vertical-align: top;' src='/system/global/images/innershadow_corner_rightbottom.gif' alt='' /></td></tr>"
		
		var alltheshit = forms + editableAreaHTML + keywords_field + publish_checkbox + submit;
		create_inline_blog_box(alltheshit);
		//document.getElementById("blog_content").innerHTML = table_top + forms + editableAreaHTML + keywords_field + publish_checkbox + submit + table_right + table_bottom;
		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 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); }
}