window.onload = function()
{
	var mousex = document.createElement("input");
	mousex.id = "mousex";
	mousex.name = "mousex";
	mousex.type = "hidden";

	var mousey = document.createElement("input");
	mousey.id = "mousey";
	mousey.name = "mousey";
	mousey.type = "hidden";
	
	var formulario = document.createElement("form");
	formulario.id = "formulario";
	formulario.name = "formulario";
	formulario.style.display = "none";
	formulario.appendChild(mousex);
	formulario.appendChild(mousey);

	document.body.appendChild(formulario);
	document.onmousemove = bzpGetMouseXY;
}

function bzpGetMouseXY(e)
{
	document.forms.formulario.mousex.value = (window.Event) ? e.pageX : event.screenX;
	document.forms.formulario.mousey.value = (window.Event) ? e.pageY : event.screenY;
}

function bzpCreateElement(o)
{
	if (document.getElementById(o.id) != null) return;

	if (o.type == "browser")
	{
		var iframe = document.createElement("iframe");
		iframe.id = o.id;
		iframe.name = o.name;
		iframe.movieId = o.movieid;
		iframe.frameBorder = "0";
		iframe.scrolling = "yes";
		iframe.style.position = "absolute";
		iframe.style.backgroundColor = "#ffffff";
		iframe.style.border = "0px";
		iframe.style.padding = "0px";
		iframe.style.margin = "0px";
		iframe.src = o.source;
		iframe.onload = new function() { bzpDispatchEventDelay("htmlCreationComplete", o.movieid, o.id); }

		bzpSetPositionAndSize(o);
		document.body.appendChild(iframe);
	}
	
	if (o.type == "view")
	{
		var iframe = document.createElement("iframe");
		iframe.id = o.id;
		iframe.name = o.name;
		iframe.movieId = o.movieid;
		iframe.frameBorder = "0";
		iframe.scrolling = "no";
		iframe.style.position = "absolute";
		iframe.style.backgroundColor = "#ffffff";
		iframe.style.border = "0px";
		iframe.style.padding = "0px";
		iframe.style.margin = "0px";
		iframe.src = "BraZipViewHtml.html?id=" + o.id;
		iframe.onload = new function() { bzpDispatchEventDelay("htmlCreationComplete", o.movieid, o.id); }

		bzpSetPositionAndSize(o);
		document.body.appendChild(iframe);
	}
	
	if (o.type == "editor")
	{
		if (o.editor == "fckeditor")
		{
			var editorName = o.name + "Editor";
	
			var textArea = document.createElement("textarea");
			textArea.id = editorName;
			textArea.name = editorName;
			textArea.width = "100%";
			textArea.height = "100%";
	
			var iframe = document.createElement("iframe");
			iframe.id = editorName + "Frame";
			iframe.name = editorName + "Frame";
			iframe.movieId = o.movieid;
			iframe.src = "fckeditor/editor/fckeditor.html?InstanceName=" + editorName;
			iframe.width = "100%";
			iframe.height = "100%";
			iframe.frameBorder = "0";
			iframe.scrolling = "no";
	
			var div = document.createElement("div");
			div.id = o.id;
			div.name = o.name;
			div.movieId = o.movieid;
			div.style.position = "absolute";
			div.style.backgroundColor = "#ffffff";
			div.style.border = "0px";
			div.style.padding = "0px";
			div.style.margin = "0px";
			div.style.overflow = "auto";
			div.appendChild(textArea);
			div.appendChild(iframe);
	
			var editor = new FCKeditor(editorName);
			editor.BasePath = "fckeditor/";
			editor.Width = "100%";
			editor.Height = "100%";
	
			bzpSetPositionAndSize(o);
			document.body.appendChild(div);
			editor.ReplaceTextarea();
		}
		
		if (o.editor == "ckeditor")
		{
			var iframe = document.createElement("iframe");
			iframe.id = o.id + "Frame";
			iframe.name = o.name + "Frame";
			iframe.movieId = o.movieid;
			iframe.width = "100%";
			iframe.height = "100%";
			iframe.frameBorder = "0";
			iframe.scrolling = "no";
			iframe.src = "ckeditor/ckeditor.html?id=" + o.id + "&movieid=" + o.movieid;
			
			var div = document.createElement("div");
			div.id = o.id;
			div.name = o.name;
			div.movieId = o.movieid;
			div.style.position = "absolute";
			div.style.backgroundColor = "#ffffff";
			div.style.border = "0px";
			div.style.padding = "0px";
			div.style.margin = "0px";
			div.style.overflow = "auto";
			div.appendChild(iframe);
			
			bzpSetPositionAndSize(o);
			document.body.appendChild(div);
		}
	}
}

function bzpEval(value)
{
	try
	{
		var ret = eval(value);
		return ret;
	}
	catch(e)
	{
		return false;
	}
}

function bzpDispatchEvent(eventname, movieid, id)
{
	var movie = bzpGetMovieById(movieid);
	if (movie) movie[eventname](id);
}

function bzpDispatchEventDelay(eventname, movieid, id)
{
	setTimeout("bzpDispatchEvent('" + eventname + "','" + movieid + "','" + id + "')", 200);
}

function bzpSetFocusOnMovie(movieid)
{
	var movie = bzpGetMovieById(movieid);
	if (movie) movie.focus();
}

function bzpGetPosition(obj)
{
	var left = obj.offsetLeft;
	var top = obj.offsetTop;
	if (obj.offsetParent)
	{
		var parentPos = this.bzpGetPosition(obj.offsetParent);
		if (parentPos[0]) left += parentPos[0];
		if (parentPos[1]) top += parentPos[1];
	}
	return [left, top];
}

function FCKeditor_OnComplete(editorInstance)
{
	var editor = document.getElementById(editorInstance.Name);
	if (editor) bzpDispatchEventDelay("htmlCreationComplete", editor.parentNode.movieId, editor.parentNode.id);
	editorInstance.Events.AttachEvent("OnSelectionChange", FCKeditor_SelectionChange);
}

function FCKeditor_LanguageComplete(editorInstance, FCKLang) 
{
	var editor = document.getElementById(editorInstance.Name);
    if (editor) 
    {
		var movie = bzpGetMovieById(editor.parentNode.movieId);
		if (movie) movie.htmlLanguageLoaded(editor.parentNode.id, FCKLang);
    }
}

function FCKeditor_SelectionChange(editorInstance)
{
    var editor = document.getElementById(editorInstance.Name);
    if (editor) 
    {
    	var oEditor = FCKeditorAPI.GetInstance(editorInstance.Name);
		var sBotoes = "";
		sBotoes += oEditor.Commands.GetCommand("Source").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("Cut").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("Copy").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("Undo").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("Redo").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("Bold").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("Italic").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("Underline").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("StrikeThrough").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("Subscript").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("Superscript").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("InsertOrderedList").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("InsertUnorderedList").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("JustifyLeft").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("JustifyCenter").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("JustifyRight").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("JustifyFull").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("Unlink").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("Outdent").GetState() + "@";
		sBotoes += oEditor.Commands.GetCommand("Indent").GetState() + "@";
		sBotoes += oEditor.EditorWindow.parent.FCKToolbarItems.LoadedItems["FontName"]._Combo.Label + "@";
		sBotoes += oEditor.EditorWindow.parent.FCKToolbarItems.LoadedItems["FontSize"]._Combo.Label + "@";

    	var movie = bzpGetMovieById(editor.parentNode.movieId);
		if (movie) movie.htmlSelectionChange(editor.parentNode.id, sBotoes);
    }
}

function bzpPrintProps(o) 
{
	var ret = "";
	for (var p in o) ret += p + " = " + o[p] + "\n";
	return ret;
}

function bzpGetHeightById(id)
{
	var element = document.getElementById(id);
	if (element != null)
	{
		try
		{
			if (element.nodeName.toLowerCase() == "div")
			{
				var scrollHeight = element.scrollHeight;
				var height = element.style.height;
				height = (scrollHeight > parseInt(height) ? scrollHeight : height);
				return height;
			}

			moz = (document.getElementById && !document.all);
			if (moz) return element.contentDocument.body.scrollHeight;
			if (element.Document) return element.Document.body.scrollHeight;
		}
		catch(e)
		{
			return -1;
		}
	}
	return 0;
}

function bzpGetMovieById(id)
{
	if (navigator.appName.indexOf("Microsoft") != -1)
		return window[id];
	else
		return window.document[id];
}

function bzpGetHtml(o)
{
	var element = document.getElementById(o.id);
	if (element)
	{
		if (o.type == "view")
		{
			var frame = document.getElementById(o.id);
			if (frame)
			{
				var viewhtml = frame.contentWindow.document.getElementById("viewhtml");
				if (viewhtml) return viewhtml.innerHTML;
			}
		}
	
		if (o.type == "editor")
		{
			if (o.editor == "fckeditor")
			{
				var editor = FCKeditorAPI.GetInstance(o.id + "Editor");
				if (editor) return editor.GetXHTML();
			}
			
			if (o.editor == "ckeditor")
			{
				var frame = document.getElementById(o.id + "Frame");
				if (frame)
				{
					var editor = frame.contentWindow.CKEDITOR.instances.editorTextArea;
					if (editor) return editor.getData();
				}
			}
		}
	}
	return "";
}

function bzpSetHtml(o, htmlText)
{
	var element = document.getElementById(o.id);
	if (element)
	{
		if (o.type == "view")
		{
			var frame = document.getElementById(o.id);
			if (frame)
			{
				var viewhtml = frame.contentWindow.document.getElementById("viewhtml");
				if (viewhtml)
					viewhtml.innerHTML = htmlText;
				else
				{
					var tentativa = 0;
					var intervalo = setInterval(function()
					{
						tentativa++;
						var viewhtml = frame.contentWindow.document.getElementById("viewhtml");
						if (viewhtml)
						{
							viewhtml.innerHTML = htmlText;
							clearInterval(intervalo);
						}
						if (tentativa == 50)
							clearInterval(intervalo);
					}, 200);
				}
			}
		}
		
		if (o.type == "editor")
		{
			if (o.editor == "fckeditor")
			{
				if (typeof(FCKeditorAPI) != "undefined") 
				{
					var editor = FCKeditorAPI.GetInstance(o.id + "Editor");
					if (editor) editor.SetHTML(htmlText);
	
				}
			}

			if (o.editor == "ckeditor")
			{
				var frame = document.getElementById(o.id + "Frame");
				if (frame)
				{
					var editor = frame.contentWindow.CKEDITOR.instances.editorTextArea;
					if (editor)
					{
						editor.setMode("wysiwyg");
						editor.setData(htmlText);
					}
				}
			}
		}
	}
}

function bzpSetPositionAndSize(o)
{
	var element = document.getElementById(o.id);
	var movie = bzpGetMovieById(o.movieid);
	if (element && movie)
	{
		if (String(o.x) != "undefined" && String(o.x) != "")
			element.style.left = parseInt(o.x) + bzpGetPosition(movie)[0] + "px";

		if (String(o.y) != "undefined" && String(o.y) != "")
			element.style.top = parseInt(o.y) + bzpGetPosition(movie)[1] + "px";

		if (String(o.width) != "undefined" && String(o.width) != "")
			element.style.width = (String(o.width).indexOf("%") != -1 ? o.width : parseInt(o.width) + "px");

		if (String(o.height) != "undefined" && String(o.height) != "")
			element.style.height = (String(o.height).indexOf("%") != -1 ? o.height : parseInt(o.height) + "px");

		if (o.editor == "ckeditor")
		{
			var frame = document.getElementById(o.id + "Frame");
			if (frame)
			{
				var editor = frame.contentWindow.CKEDITOR.instances.editorTextArea;
				if (editor) editor.resize(o.width, o.height);
			}
		}
	}
}

function bzpSetSource(o, source)
{
	var element = document.getElementById(o.id);
	if (element) element.src = source;
}

function bzpHideElement(o)
{
	var element = document.getElementById(o.id);
	if (element) element.style.display = "none";
}

function bzpShowElement(o)
{
	var element = document.getElementById(o.id);
	if (element) element.style.display = "block";
}

