// Toolbar image down function
function Editor_image_down(sender)
{
	try
	{
		obj = sender;
		sender.style["marginTop"] = "3px";
		setTimeout("Editor_image_up(obj)", 100);
	}
	catch(err){}
}

// Toolbar image up function
function Editor_image_up(sender)
{
	try
	{
		sender.style["marginTop"] = "0px";
	}
	catch(err){}
}


// Show image properties
function ImageProperties(obj)
{
	try
	{
		imageObj = obj
		window.showModalDialog("Pages/image_dialog_align.php", imageObj, "dialogHeight:250px; dialogWidth:400px;")
	}
	catch(err){}
}



// Prepare the editor
function prepareEditor()
{
	try
	{
		root = document.getElementById("editor")
		rootImages = root.getElementsByTagName("img")
		for(x in rootImages)
		{
			try
			{
				rootImages[x].onclick = function(){}
				rootImages[x].ondblclick = function(){ImageProperties(rootImages[x])}
			}
			catch(err){}
		}
	}
	catch(err){}
}


// Main event handler
function Editor_Event(sender, mode)
{
	try
	{
		obj = sender;
		Editor_image_down(obj);
	
		root = document.getElementById("editor")
		heap = document.getElementById("editor_text")
		var EventPoint = new Editor_CentralEngine(root , heap);
		
		switch(mode)
		{
			case 'undo': 			EventPoint.Undo(); 			break;
			case 'redo': 			EventPoint.Redo(); 			break;
			case 'cut': 			EventPoint.Cut(); 			break;
			case 'copy': 			EventPoint.Copy(); 			break;
			case 'paste': 			EventPoint.Paste(); 		break;
			case 'bold': 			EventPoint.Bold(); 			break;
			case 'italic':  		EventPoint.Italic(); 		break;
			case 'underl':  		EventPoint.Underline(); 	break;
			case 'alignleft':   	EventPoint.AlignLeft(); 	break;
			case 'aligncenter': 	EventPoint.AlignCenter(); 	break;
			case 'alignright': 		EventPoint.AlignRight(); 	break;
			case 'alignjustify': 	EventPoint.AlignJustify(); 	break;
			case 'image': 			EventPoint.AddImage(); 		break;
			case 'link': 			EventPoint.HyperLink(); 	break;
			case 'color': 			EventPoint.Color(); 		break;
		}
	}
	catch(err){}
}


// Parse and manipulate hexadecimal codes
function hexParser(sender)
{
	if(sender.length < 6)
	{
		bypass = "";
		rest = 6 - sender.length;
		for(a = 1; a <= rest; a ++)
		{
			bypass += "0";
		}
		sender = bypass + sender;
	}
	return sender;
}


// Collector of engine based functions
function Editor_CentralEngine(root, heap)
{
	try
	{
		this.rootContainer = root
		this.editorTextBox = heap
		this.colorDialog = document.getElementById("dlgColor")
		
		getHost = window.location.href.split("/")
		this.host = getHost[2]
	}
	catch(err){}
}


// Undo event function
Editor_CentralEngine.prototype.Undo = function()
{
	try
	{
		document.execCommand("Undo");
		this.rootContainer.focus();
	}
	catch(err){}
}


// Redo event function
Editor_CentralEngine.prototype.Redo = function()
{
	try
	{
		document.execCommand("Redo");
		this.rootContainer.focus();
	}
	catch(err){}
}


// Cut event function
Editor_CentralEngine.prototype.Cut = function()
{
	try
	{
		document.execCommand("Cut");
		this.rootContainer.focus();
	}
	catch(err){}
}


// Copy event function
Editor_CentralEngine.prototype.Copy = function()
{
	try
	{
		document.execCommand("Copy");
		this.rootContainer.focus();
	}
	catch(err){}
}


// Copy event function
Editor_CentralEngine.prototype.Paste = function()
{
	try
	{
		this.rootContainer.focus();
		document.execCommand("Paste");
	}
	catch(err){}
}


// Bold event function
Editor_CentralEngine.prototype.Bold = function()
{
	try
	{
		this.rootContainer.focus();
		document.execCommand("bold");
	}
	catch(err){}
}


// Italic event function
Editor_CentralEngine.prototype.Italic = function()
{
	try
	{
		this.rootContainer.focus();
		document.execCommand("italic");
	}
	catch(err){}
}


// Underline event function
Editor_CentralEngine.prototype.Underline = function()
{
	try
	{
		this.rootContainer.focus();
		document.execCommand("underline");
	}
	catch(err){}
}


// Left align event function
Editor_CentralEngine.prototype.AlignLeft = function()
{
	try
	{
		this.rootContainer.focus();
		document.execCommand("JustifyLeft");
	}
	catch(err){}
}


// Center align event function
Editor_CentralEngine.prototype.AlignCenter = function()
{
	try
	{
		this.rootContainer.focus();
		document.execCommand("JustifyCenter");
	}
	catch(err){}
}


// Right align event function
Editor_CentralEngine.prototype.AlignRight = function()
{
	try
	{
		this.rootContainer.focus();
		document.execCommand("JustifyRight");
	}
	catch(err){}
}


// Justify align event function
Editor_CentralEngine.prototype.AlignJustify = function()
{
	try
	{
		this.rootContainer.focus();
		document.execCommand("JustifyFull");
	}
	catch(err){}
}


// Add image event function
Editor_CentralEngine.prototype.AddImage = function()
{
	try
	{
		this.rootContainer.focus();
		var Parameters = new Object();
		Parameters.imagelink = document.getElementById("imagelink")
		Parameters.imagelink.value = "";
		
		window.showModalDialog("Pages/image_dialog.htm", Parameters, "dialogHeight:300px; dialogWidth:500px;")
		if(Parameters.imagelink.value.length > 0)
		{
			pageImages = document.images;
			document.execCommand("InsertImage", false, Parameters.imagelink.value);
			for(x in pageImages)
			{
				if(pageImages[x].src == Parameters.imagelink.value)
				{
					pageImages[x].height = "150";
					pageImages[x].align = "left"
					pageImages[x].onclick = function() {}
					pageImages[x].ondblclick = function(){ImageProperties(this)}
				}
			}
		}
	}
	catch(err)
	{ }
}


// Add hyperlink event function
Editor_CentralEngine.prototype.HyperLink = function()
{
	try
	{
		this.rootContainer.focus();
		document.execCommand("Createlink");
	}
	catch(err){}
}



// Color event function
Editor_CentralEngine.prototype.Color = function()
{
	try
	{
		this.rootContainer.focus();
		color = this.colorDialog.ChooseColorDlg();
		document.execCommand("Forecolor", false, hexParser(color.toString(16)));
	}
	catch(err){}
}


// Parses the HTML text into the hidden textarea
function parseHTMLtext()
{
	try
	{
		pSource = document.getElementById("editor").innerHTML;
		pTarget = document.getElementById("editor_text");
		
		tagSplit = new Object();
		tagSplit.begin = pSource.split(/<IMG/)
		
		for(x in tagSplit.begin)
		{
			try
			{
				tagSplit.Isource = tagSplit.begin[x].match(/src="(.+)"/)
				
				if(tagSplit.begin[x].indexOf("onclick") == -1)
				if(tagSplit.Isource[1].length > 0)
				pSource = pSource.replace(/src/g, "onclick=\"popup_image(this, 'Afbeelding')\" style='cursor:pointer' src");
			}
			catch(err){}
		}
		pTarget.value = pSource;
	}
	catch(err){}
}


// Change the font type
function changeFont(obj)
{
	try
	{
		root = document.getElementById("editor")
		root.focus();
		document.execCommand("FontName", 1, obj.options[obj.selectedIndex].text);
	}
	catch(err){}
}
