var editor = null;

// Images
var bold = new Image();
var boldHover = new Image();
var italic = new Image();
var italicHover = new Image();
var underline = new Image();
var underlineHover = new Image();

// Images hover or not
var isBold = true;
var isItalic = true;
var isUnderline = true;

// init the editor
function initEditor()
{
	editor = document.getElementById('editor');

	// set the editingMode for Internet Explorer
	try
	{
		editor.contentWindow.document.designMode = "on";
	}
	catch(ex)
	{
		//alert("IE not supported");
	}

	// set the editigMode for other Browser
	try
	{
		editor.contentWindow.document.execCommand("undo", false, null);
	}
	catch(ex)
	{
		//alert("Mozilla not supported");
	}

	initImages();

	editor.contentWindow.focus();
}

// init the images
function initImages()
{
	// Bold
	bold.src = "images/icons/bold.gif";
	boldHover.src = "images/icons/boldHover.gif";

	// Italic
	italic.src = "images/icons/italic.gif";
	italicHover.src = "images/icons/italicHover.gif";

	// Underline
	underline.src = "images/icons/underline.gif";
	underlineHover.src = "images/icons/underlineHover.gif";
}

// formatted the text to the given command
function textFormatter(command)
{
	editor.contentWindow.document.execCommand(command, false, null);
	editor.contentWindow.focus();
}

// changes the images
function changeImage(command)
{
	if(command == "bold")
	{
		if(isBold == true)
		{
			document.getElementById(command+"Image").src = boldHover.src;
			isBold = false;
		}
		else
		{
			document.getElementById(command+"Image").src = bold.src;
			isBold = true;
		}
	}
	else if(command == "italic")
	{
		if(isItalic == true)
		{
			document.getElementById(command+"Image").src = italicHover.src;
			isItalic = false;
		}
		else
		{
			document.getElementById(command+"Image").src = italic.src;
			isItalic = true;
		}
	}
	else if(command == "underline")
	{
		if(isUnderline == true)
		{
			document.getElementById(command+"Image").src = underlineHover.src;
			isUnderline = false;
		}
		else
		{
			document.getElementById(command+"Image").src = underline.src;
			isUnderline = true;
		}
	}
}

// submit the text
function submit()
{
	var text = null;
	text = editor.contentWindow.document.body.innerHTML;

	document.getElementById("output").innerHTML = text;
	document.getElementById("foo").value = text;
}