var __buttons = Array();
var __panels = Array();
var __actions = Array();
var ACTION_SelectButton = 1;
var ACTION_HideToolbar = 2;
var ACTION_CreateEditor = 3;
var ACTION_ShowToolbar = 4;

function addButton(sLabel, sURL) {
	if (__buttons.length <= 5)
		__buttons.push(new buttonHolder("btn16x16_" + (__buttons.length + 1), sLabel, sURL));
}

function addButtonEx(sParentID, sLabel, sURL) {
	__buttons.push(new buttonHolder(sParentID, sLabel, sURL));
}

function addExpandingPanel(sGenericPanelID, bExpanded) {
	__panels.push(new expandingPanelHolder(sGenericPanelID, bExpanded));
}

function addEditor(sEditorID) {
	__actions.push(new actionHolder(ACTION_CreateEditor, sEditorID, ""));
}

function selectButton(sLabel) {
	__actions.push(new actionHolder(ACTION_SelectButton, sLabel, ""));
}

function hideSmallToolbar() {
	__actions.push(new actionHolder(ACTION_HideToolbar, "", ""));
}

function showSmallToolbar() {
	__actions.push(new actionHolder(ACTION_ShowToolbar, "", ""));
}

window.onload = function(evnt) {
	for(var i = 0; i < __buttons.length; i++) {
		var btn = __buttons[i];
		addXPButton(btn.sParentID, btn.iSize, btn.bPressed, btn.sLabel, btn.sImgSrc, btn.sURL);
	}
	
	for(var i = 0; i < __panels.length; i++) {
		var pnl = __panels[i];
		__ExpandingPanelAddNew(pnl.sGenericPanelID, pnl.sImgExpandedSrc, pnl.sImgCollapsedSrc, pnl.bExpanded, pnl.sExpandedView);
	}
	
	__initExpandingPanel();
	
	//Now handle any pending actions...
	for(var i = 0; i < __actions.length; i++) {
		var action = __actions[i];
		
		if (action != null) {
			switch(action.actionID) {
				case ACTION_SelectButton:
					for(var j = 0; j < buttons.length; j++) {
						if (buttons[j].label == action.param1) {
							buttons[j].setPressed(true);
						}
					}
					break;
				case ACTION_HideToolbar:
					var obj = document.getElementById("toolbarSmall");
					if (obj != null)
						obj.style.display = "none";
					break;
				case ACTION_ShowToolbar:
					var obj = document.getElementById("toolbarSmall");
					if (obj != null)
						obj.style.display = "block";
					break;
				case ACTION_CreateEditor:
					var tas = document.getElementById(action.param1);
					var config = new HTMLArea.Config();

					var editor = new HTMLArea(tas, config);
					editor.config.pageStyle = "body { font-family: Arial, Tahoma, Verdana, Helvetica; font-size: 10pt; margin: 0px; background-color: #ffffff; color:  black; } " +
						"A { text-decoration: none; color: Gray; } " + 
						"A:hover { text-decoration: underline; color: Gray; } ";
					editor.config.sizeIncludesToolbar = true;
					createStandardToolbars(editor);
					editor.generate();
					editor._iframe.style.border = "none"; //Necessary to make it look right in firefox...
					break;
			}
		}
	}
}

function buttonHolder(sParentID, sLabel, sURL) {
	//Holds these values until we're ready to deploy them on page load...
	this.sParentID = sParentID;
	this.sLabel = sLabel;
	this.sURL = sURL;
	
	//Defaults
	this.iSize = 16;
	this.bPressed = false;
	this.sImgSrc = "images/go.gif";
}

function expandingPanelHolder(sGenericPanelID, bExpanded) {
	//Holds these values until we're ready to deploy them on page load...
	this.sGenericPanelID = sGenericPanelID;
	this.bExpanded = bExpanded;
	
	//Defaults
	this.sImgExpandedSrc = "images/panel_collapsed.gif";
	this.sImgCollapsedSrc = "images/panel_expanded.gif";
	this.sExpandedView = "auto";
}

function actionHolder(actionID, param1, param2) {
	this.actionID = actionID;
	this.param1 = param1;
	this.param2 = param2;
}

function createStandardToolbars(editor) {
	editor.config.toolbar = 
	[
		[	"custom-handle", 
			"bold", "italic", "underline", 
			"custom-separator", 
			"justifyleft", "justifycenter", "justifyright", "justifyfull", 
			"custom-separator",
			"insertorderedlist", "insertunorderedlist", "outdent", "indent", 
			"custom-separator",
			"forecolor", "hilitecolor", 
			"custom-end" 
		],
		[	"custom-handle",  
			"copy", "cut", "paste", 
			"custom-separator", 
			"createlink", "inserthorizontalrule",  
			"custom-separator",
			"strikethrough", "subscript", "superscript", 
			"custom-end" 
		]
	];
}