/* Widget box */
var ActiveWidget = null;
var SelectedWidget = null;
var CustomWidgetBox = null;
var borderwidth = 12;
var IsLocked = false;
var IsAuthorized = false;
var UniqueUserID = "";
var ContentBoundary = null;

function LockWidgets() {
	IsLocked = !IsLocked;
	RenderLock();
	var OUrl = '/section/common/WidgetAjax.ashx?method=setlock&status=' + (IsLocked ? "1" : "0");
	var options = {
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	new Ajax.Request(OUrl, options);
}

function RenderLock() {
	LockImageMouseOut();
	if (ActiveWidget instanceof Widget) {
		ActiveWidget.hideTitle();
		ActiveWidget = null;
	}
	if (SelectedWidget instanceof Widget) {
		SelectedWidget.hideTitle();
		SelectedWidget = null;
	}
	RemoveMarkerBox();
	for (i = 0; i < controls.length; i++) {
		var o = controls[i];
		if (o instanceof Widget && o.showborder == false) {
			if (IsLocked) {
				o.hideBorder();
			}
			else {
				o.showBorder();
			}
		}
	}
}

function LockImageMouseOut() {
	if (Prototype.Browser.IE6 == true) {
		$("imglock").src = "http://artogfx.cloud2.artodata.com/sitegfx/grafik/spacer.gif";
		var OImg = IsLocked ? "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_menu_locked.png" : "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_menu_lock.png";
		$("imglock").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + OImg + ");";
	}
	else {
	    $("imglock").src = IsLocked ? "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_menu_locked.png" : "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_menu_lock.png";
	}
}

function LockImageMouseOver() {
	if (Prototype.Browser.IE6 == true) {
		$("imglock").src = "http://artogfx.cloud2.artodata.com/sitegfx/grafik/spacer.gif";
		var OImg = IsLocked ? "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_menu_locked_mo.png" : "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_menu_lock_mo.png";
		$("imglock").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + OImg + ");";
	}
	else {
	    $("imglock").src = IsLocked ? "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_menu_locked_mo.png" : "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_menu_lock_mo.png";
	}
}

function CreateBorder(id) {
	var result = "<!-- Content box -->\r\n\r\n";
	result += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"" + id + "BorderTable\">\r\n";
	result += " <tr>\r\n";
	result += "   <td class=\"widgetBoxTopLeft\" id=\"" + id + "BorderTable_TopLeft\">\r\n";
	result += "   </td>\r\n";
	result += "   <td class=\"widgetBoxTopMiddle\" id=\"" + id + "BorderTable_TopMiddle\">\r\n";
	result += "   </td>\r\n";
	result += "   <td class=\"widgetBoxTopRight\" id=\"" + id + "BorderTable_TopRight\">\r\n";
	result += "   </td>\r\n";
	result += " </tr>\r\n";
	result += " <tr>\r\n";
	result += "   <td class=\"widgetBoxLeft\" id=\"" + id + "BorderTable_Left\">\r\n";
	result += "   </td>\r\n";
	result += "   <td id=\"" + id + "BorderBody\">\r\n";
	result += "   </td>\r\n";
	result += "   <td class=\"widgetBoxRight\" id=\"" + id + "BorderTable_Right\">\r\n";
	result += "   </td>\r\n";
	result += " </tr>\r\n";
	result += " <tr>\r\n";
	result += "   <td class=\"widgetBoxBottomLeft\" id=\"" + id + "BorderTable_BottomLeft\">\r\n";
	result += "   </td>\r\n";
	result += "   <td class=\"widgetBoxBottomMiddle\" id=\"" + id + "BorderTable_BottomMiddle\">\r\n";
	result += "   </td>\r\n";
	result += "   <td class=\"widgetBoxBottomRight\" id=\"" + id + "BorderTable_BottomRight\">\r\n";
	result += "   </td>\r\n";
	result += " </tr>\r\n";
	result += "</table>\r\n";
	result += "<!-- /Content box -->";
	return result;
}

function HideBorder(id) {
	$(id + "BorderTable_TopLeft").className = "widgetNoBorderWidth widgetNoBorderHeight";
	$(id + "BorderTable_TopMiddle").className = "widgetNoBorderHeight";
	$(id + "BorderTable_TopRight").className = "widgetNoBorderWidth widgetNoBorderHeight";
	$(id + "BorderTable_Left").className = "widgetNoBorderWidth";
	$(id + "BorderTable_Right").className = "widgetNoBorderWidth";
	$(id + "BorderTable_BottomLeft").className = "widgetNoBorderWidth widgetNoBorderHeight";
	$(id + "BorderTable_BottomMiddle").className = "widgetNoBorderHeight";
	$(id + "BorderTable_BottomRight").className = "widgetNoBorderWidth widgetNoBorderHeight";
}

function ShowBorder(id) {
	$(id + "BorderTable_TopLeft").className = "widgetBoxTopLeft";
	$(id + "BorderTable_TopMiddle").className = "widgetBoxTopMiddle";
	$(id + "BorderTable_TopRight").className = "widgetBoxTopRight";
	$(id + "BorderTable_Left").className = "widgetBoxLeft";
	$(id + "BorderTable_Right").className = "widgetBoxRight";
	$(id + "BorderTable_BottomLeft").className = "widgetBoxBottomLeft";
	$(id + "BorderTable_BottomMiddle").className = "widgetBoxBottomMiddle";
	$(id + "BorderTable_BottomRight").className = "widgetBoxBottomRight";
}

function CreateWidget(url, height, showborder) {
	if (url == "custom") {
		CreateCustomWidgetBox();
	}
	else {
		SendCreateWidgetRequest(url, height, showborder);
	}
}

function SendCreateWidgetRequest(url, height, showborder) {
	var OUrl = '/section/common/WidgetAjax.ashx?method=getguid';
	var OOptions = {
		onSuccess: function(t) {
			var OGuid = t.responseText;
			var OWidget = new Widget("Widget" + OGuid, url, 3, 100, 137, height, showborder, UniqueUserID);
			OWidget.save();
			OWidget.setup();
			IsLocked = false;
			RenderLock();
		}
	}
	new Ajax.Request(OUrl, OOptions);
}

function CreateCustomWidget(showborder) {
	var OUrl = $('CustomWidgetBox_TextArea').value;
	if (OUrl.indexOf('http://') < 0) {
		OUrl = "http://" + OUrl;
	}
	var result = OUrl.match(/(http|https):\/\/(\S+)\.(\S+)/)
	if (result && result.length > 0) {
		OUrl = result[0].replace(new RegExp("'", "g"), "");
	}
	else {
		alert('The URL entered is not valid!');
		return;
	}
	SendCreateWidgetRequest(OUrl, 200, true);
	CloseCustomWidgetBox();
}

function CreateCustomWidgetBox() {
	CloseCustomWidgetBox();
	CustomWidgetBox = document.createElement("div");
	with (CustomWidgetBox.style) {
		position = "absolute";
		width = 147 + noPx;
		height = 218 + noPx;
		left = 3 + noPx;
		top = 150 + noPx;
		background = "#888";
		zIndex = 101;
	}
	$(CustomWidgetBox).update("<div style=\"padding: 3px; font-size: 10px\" align=\"center\">With an external widget you can add a widget made yourself or one by others.<br /> Enter widget URL: <br /><textarea id=\"CustomWidgetBox_TextArea\" style=\"width: 124px; height: 120px; margin-bottom: 3px\"></textarea><br /><input type=\"button\" onclick=\"CloseCustomWidgetBox()\" value=\"Cancel\" style=\"width: 57px\" /><input type=\"button\" value=\"OK\" onclick=\"CreateCustomWidget()\" style=\"width: 57px\" /></div>");
	document.body.appendChild(CustomWidgetBox);
}

function CloseCustomWidgetBox() {
	if (CustomWidgetBox && CustomWidgetBox != null) {
		document.body.removeChild(CustomWidgetBox);
		CustomWidgetBox = null;
	}
}

function Widget(id, url, x, y, width, height, showborder, uniqueid, primaryframe) {
	this.id = id;
	this.width = width;
	this.height = height;
	this.x = x;
	this.y = y;
	this.url = url;
	this.showborder = typeof (showborder) != "undefined" ? showborder : true;
	this.uniqueid = uniqueid;
	this.primaryframe = typeof (primaryframe) != "undefined" ? primaryframe : true;

	this.isscaling = false;
	this.ismoving = false;
	controls[controls.length] = this;
	this.initialize();
}

Widget.prototype = {
	initialize: function() {
		/* control */
		this.control = document.createElement("div");
		this.control.id = this.id;
		this.control.className = "widget";
		with (this.control.style) {
			position = "absolute";
			width = this.width + noPx;
			height = this.height + noPx;
			if (this.primaryframe) {
				left = Pos.regionLeft(ContentBoundary) - this.x - this.width + noPx;
			} else {
				left = (Pos.regionLeft(ContentBoundary) + ContentBoundary.getWidth()) + this.x + noPx;
			}
			top = this.y + noPx;
			overflow = "hidden";
			zIndex = 10;
		}
		document.body.appendChild(this.control);

		this.bgimage = document.createElement("img");
		this.bgimage.src = "http://artogfx.cloud2.artodata.com/sitegfx/grafik/spacer.gif";
		with (this.bgimage.style) {
			position = "absolute";
			width = this.control.style.width;
			height = this.control.style.height;
			zIndex = -1;
		}
		this.control.appendChild(this.bgimage);

		/* title */
		this.title = document.createElement("div");
		with (this.title.style) {
			position = "relative";
			height = '20' + noPx;
			try {
				width = ($(this.control).getWidth() - borderwidth) + noPx;
			} catch (ex) {
			}
			zIndex = 1000;
			if (Prototype.Browser.IE6) {
				backgroundColor = '#000000';
				filter = "alpha(opacity=" + 25 + ");";
			}
		}
		if (!Prototype.Browser.IE6) {
			SetImage(this.title, '/grafik/widgets/widg_title.png', true);
		}
		this.title.id = this.id + "_title";
		this.title.className = "widget";
		var sfimage = this.primaryframe ? "moveright" : "moveleft";
		this.title.innerHTML = "<div style=\"position: relative; float: right; width: 100%; padding-top: 5px\" align=\"right\">" + GetImageTag("/grafik/widgets/widg_but_prefs.png", false, "Show settings", "controls.getInstance(\"" + this.id + "\").flipOptions();") + "&nbsp;" + GetImageTag("/grafik/widgets/widg_but_" + sfimage + ".png", false, "Move to opposite page", "controls.getInstance(\"" + this.id + "\").switchFrame();", this.id + "_image") + "&nbsp;" + GetImageTag("http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_but_close.png", false, "Close and remove widget", "controls.getInstance(\"" + this.id + "\").close(true);") + "&nbsp;</div>";

		var borderHTML = CreateBorder(this.id);
		this.borderDiv = document.createElement("div");
		this.borderDiv.id = this.id + "_border";
		this.borderDiv.innerHTML = borderHTML;
		this.control.appendChild(this.borderDiv);

		if (this.showborder == false && IsLocked == true) {
			this.hideBorder();
		}

		$(this.id + "BorderBody").appendChild(this.title);
		this.hideTitle();

		/* iframe */
		if (Prototype.Browser.IE) {
			this.iframe = document.createElement("<iframe frameborder='0' allowtransparency='1'>");
		} else {
			this.iframe = document.createElement("iframe");
		}
		var prefix = this.url.indexOf('?') > 0 ? "&" : "?";
		var OUrl = this.url + prefix + "widgetid=" + this.id + "&uniqueid=" + this.uniqueid + "&height=" + this.height + "&width=" + this.width;
		this.iframe.src = OUrl;
		this.iframe.id = this.id + "_iframe";
		
		with (this.iframe.style) {
			position = "relative";
			try {
				width = ($(this.control).getWidth() - borderwidth) + noPx;
				height = ($(this.control).getHeight() - borderwidth) + noPx;
			}
			catch (ex) {
			}
			border = "0";
		}
		$(this.id + "BorderBody").appendChild(this.iframe);

		this.createSpacerImage();

		var options = {};
		options.handle = this.title;
		options.endeffect = function() { };
		options.scroll = window;
		options.snap = function(x, y, obj) {
			var regionLeft = Pos.regionLeft(ContentBoundary);
			var boundaryWidth = ContentBoundary.getWidth();
			var elmWidth = $(obj.element).getWidth();
			if (x > (regionLeft - elmWidth) && x < (regionLeft + (boundaryWidth / 2))) {
				this.primaryframe = true;
				x = (x > (regionLeft - elmWidth)) ? (Pos.regionLeft(ContentBoundary) - elmWidth) : x;
			}
			else if (x < (regionLeft + boundaryWidth) && x > (regionLeft + (boundaryWidth / 2))) {
				this.primaryframe = false;
				x = (x < (regionLeft + boundaryWidth)) ? (regionLeft + boundaryWidth) : x;
			}
			else {
				if (x < (regionLeft - elmWidth)) {
					this.primaryframe = true;
				}
				if (x > (regionLeft + boundaryWidth)) {
					this.primaryframe = false;
				}
			}
			y = y < 0 ? 0 : y;

			SwitchImage(obj.element.id + "_image", this.primaryframe);

			return [x, y];
		} .bind(this);
		this.draggable = new Draggable(this.control, options);
		this.observer = {};
		this.observer.element = this;
		this.observer.onStart = function() {
			this.element.ismoving = true;
			RemoveWidgetScaleBoxes();
			RemoveMarkerBox();
			this.element.title.style.cursor = "move";
			this.element.showSpacerImage();
			SelectedWidget = this.element;
		};
		this.observer.onEnd = function(eventName, draggableObj) {
			this.element.ismoving = false;
			if (draggableObj.element.id == this.element.id) {
				if (this.element.primaryframe) {
					this.element.x = Pos.regionLeft(ContentBoundary) - (Pos.regionLeft($(this.element.id)) + $(this.element.id).getWidth());
				} else {
					this.element.x = Pos.regionLeft($(this.element.id)) - (Pos.regionLeft(ContentBoundary) + ContentBoundary.getWidth());
				}
				RemoveMarkerBox();
				this.element.select();
				this.element.title.style.cursor = '';
				this.element.hideSpacerImage();
				this.element.save();
			}
		};
		Draggables.addObserver(this.observer);

		Event.observe(this.control, 'mouseover', WidgetMonitorMouseOver.bindAsEventListener(this));
	},

	flipOptions: function() {
		var OUrl = this.iframe.src;
		if (OUrl.indexOf('options=true') > 0) {
			var prefix = this.url.indexOf('?') > 0 ? "&" : "?";
			this.iframe.src = this.url + prefix + "widgetid=" + this.id + "&uniqueid=" + this.uniqueid;
		}
		else {
			var prefix = OUrl.indexOf('?') > 0 ? "&" : "?";
			OUrl += prefix + "options=true";
			this.iframe.src = OUrl;
		}
	},

	setup: function() {
		var OUrl = this.iframe.src;
		var prefix = OUrl.indexOf('?') > 0 ? "&" : "?";
		OUrl += prefix + "setup=true";
		this.iframe.src = OUrl;
	},

	select: function() {
		RemoveWidgetScaleBoxes();
		if (!IsLocked) {
			CreateScaleBox(this, 137, 100);
		}
		if (SelectedWidget instanceof Widget) {
			SelectedWidget.control.style.zIndex = 1;
		}
		this.control.style.zIndex = 10;
	},

	hideTitle: function() {
		if (this.title.style.display == "") {
			this.title.style.display = "none";
			try {
				var titleHeight = $(this.title).getHeight();
				var controlHeight = $(this.control).getHeight();
				this.control.style.height = (controlHeight - titleHeight - 4) + noPx;
				this.control.style.top = (Pos.regionTop(this.control) + titleHeight) + noPx;
			} catch (exception) {
			}
		}
	},

	showTitle: function() {
		if (this.title.style.display == "none") {
			this.title.style.display = "";
			this.control.style.height = ($(this.control).getHeight() + $(this.title).getHeight()) + noPx;
			this.control.style.top = (Pos.regionTop(this.control) - $(this.title).getHeight()) + noPx;
		}
	},

	hideBorder: function() {
		if (this.showborder == false) {
			HideBorder(this.id);
		}
	},

	showBorder: function() {
		if (this.showborder == false) {
			ShowBorder(this.id);
		}
	},

	createSpacerImage: function() {
		/* iframe hack to prevent scroll stops */
		this.scalingimage = document.createElement("img");
		this.scalingimage.src = "http://artogfx.cloud2.artodata.com/sitegfx/grafik/spacer.gif";
		with (this.scalingimage.style) {
			position = "absolute";
			width = this.iframe.style.width;
			height = this.iframe.style.height;
			left = "4px";
			top = ($(this.title).getHeight() + 4) + noPx;
			zIndex = 100;
		}
		this.hideSpacerImage();
		Event.observe(this.scalingimage, 'mousemove', this.scalingimageMouseMove.bindAsEventListener(this));
		this.control.appendChild(this.scalingimage);
	},

	scalingimageMouseMove: function() {
		if (this.isscaling == false && this.ismoving == false) {
			this.hideSpacerImage();
		}
	},

	showSpacerImage: function() {
		this.scalingimage.style.display = "";
	},

	hideSpacerImage: function() {
		this.scalingimage.style.display = "none";
		document.body.style.cursor = "default";
	},

	beginScaling: function() {
		RemoveMarkerBox();
		this.isscaling = true;
		this.showSpacerImage();
	},

	renderScaling: function() {
		this.title.style.width = ($(this.control).getWidth() - borderwidth) + noPx;
		with (this.iframe.style) {
			width = ($(this.control).getWidth() - borderwidth) + noPx;
			height = ($(this.control).getHeight() - borderwidth - $(this.title).getHeight()) + noPx;
		}
		if (this.scalingimage != null) {
			with (this.scalingimage.style) {
				width = this.iframe.style.width;
				height = this.iframe.style.height;
			}
		}
		if (this.bgimage != null) {
			with (this.bgimage.style) {
				width = this.control.style.width;
				height = this.control.style.height;
			}
		}
		RemoveWidgetScaleBoxes();
		this.select();
	},

	endScaling: function() {
		if (this.primaryframe) {
			this.x = Pos.regionLeft(ContentBoundary) - (Pos.regionLeft($(this.id)) + $(this.id).getWidth());
		} else {
			this.x = Pos.regionLeft($(this.id)) - (Pos.regionLeft(ContentBoundary) + ContentBoundary.getWidth());
		}
		this.isscaling = false;
		this.renderScaling();
		this.hideSpacerImage();
		this.save();
	},

	switchFrame: function() {
		if (this.primaryframe) {
			$(this.id + "_image").src = "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_but_moveleft.png";

			var leftResult = (Pos.regionLeft(ContentBoundary) - Pos.regionRight($(this.id))) + Pos.regionRight(ContentBoundary);

			$(this.id).setStyle({
				left: leftResult + "px",
				top: Pos.regionTop($(this.id)) + "px"
			});

			this.primaryframe = false;
		} else {
			$(this.id + "_image").src = "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_but_moveright.png";

			var rightResult = (Pos.regionLeft(ContentBoundary) - $(this.id).getWidth()) - (Pos.regionLeft($(this.id)) - Pos.regionRight(ContentBoundary));

			$(this.id).setStyle({
				left: rightResult + "px",
				top: Pos.regionTop($(this.id)) + "px"
			});

			this.primaryframe = true;
		}
		this.save();
	},

	close: function(deletewidget) {
		try {
			if (deletewidget) {
				var OUrl = '/section/common/WidgetAjax.ashx?method=closewidget&id=' + this.id;
				var options = {
					onFailure: function(t) {
						alert('Error ' + t.status + ' -- ' + t.statusText);
					}
				}
				new Ajax.Request(OUrl, options);

				var prefix = this.url.indexOf('?') > 0 ? "&" : "?";
				var OUrl = this.url + prefix + "widgetid=" + this.id + "&uniqueid=" + this.uniqueid + "&widgetclose=true";
				var options = {
					onFailure: function(t) {
						alert('Error ' + t.status + ' -- ' + t.statusText);
					}
				}
				new Ajax.Request(OUrl, options);
			}
			RemoveWidgetScaleBoxes();
			RemoveMarkerBox();
			Draggables.removeObserver(this);
			if (deletewidget) {
				controls.remove(this);
				Effect.Shrink(this.control.id);
			}
			else {
				controls.remove(this);
				this.control.hide();
				document.body.removeChild(this.control);
			}
		}
		catch (ex) {
			alert(ex);
		}
	},

	save: function() {
		try {
			var OHeight = ActiveWidget == this || SelectedWidget == this ? $(this.control).getHeight() : $(this.control).getHeight() + $(this.title).getHeight();
			var OY = ActiveWidget == this || SelectedWidget == this ? Pos.regionTop($(this.control)) : Pos.regionTop($(this.control)) - $(this.title).getHeight();
			var OX = Pos.regionLeft($(this.control));
			var saveurl = '/section/common/WidgetAjax.ashx?method=savewidget' +
      '&id=' + this.id +
      '&x=' + this.x +
      '&y=' + (OY > 0 ? OY : 0) +
      '&width=' + $(this.control).getWidth() +
      '&height=' + OHeight +
      '&primaryframe=' + (this.primaryframe ? "1" : "0") +
      '&contenturl=' + escape(this.url) +
      '&showborder=' + (this.showborder ? "1" : "0");
			var options = {
				onFailure: function(t) {
					alert('Error ' + t.status + ' -- ' + t.statusText);
				}
			}

			new Ajax.Request(saveurl, options);
		}
		catch (ex) {
			alert(ex);
		}
	}
}

function SwitchImage(obj, primaryframe) {
	if (primaryframe) {
	    if ($(obj).src != "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_but_moveright.png") {
			$(obj).src = "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_but_moveright.png";
		}
	} else {
    if ($(obj).src != "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_but_moveleft.png") {
			$(obj).src = "http://artogfx.cloud2.artodata.com/sitegfx/grafik/widgets/widg_but_moveleft.png";
		}
	}
}

/* Menu box */

function MenuItem(id, content, link) { // constructor
	this.id = id;
	this.link = link;
	this.content = content;
}

function MenuTitle(title) {
	this.value = title;
}

function Menu(id, width, itemheight, maxitems, x, y) { // constructor  
	this.id = id;
	this.width = width;
	this.items = new Array();
	this.itemheight = itemheight;
	this.maxitems = maxitems;
	this.x = x;
	this.y = y;
	this.list = $(id);
	this.initialize();
}

Menu.prototype = {
	initialize: function() {
		controls[controls.length] = this;
		with (this.list.style) {
			position = "absolute";
			left = this.x + noPx;
			top = this.y + noPx;
			width = this.width + noPx;
			overflow = "auto";
			display = "none";
			zIndex = "20";
			filter = "alpha(opacity=80)";
			mozOpacity = ".80";
			opacity = ".80";
		}
	},

	toggle: function() {
		if (this.list.visible() == false) {
			var count = this.items.length > this.maxitems ? this.maxitems : this.items.length;
			this.list.style.height = (this.itemheight * count) + noPx;
			Effect.SlideDown(this.id, { duration: 0.3 });
		} else {
			Effect.SlideUp(this.id, { duration: 0.3 });
		}
	},

	hide: function() {
		if (this.list.visible() == true) {
			Effect.SlideUp(this.id, { duration: 0.3 });
		}
	},

	addItem: function(id, content, link) {
		this.items[this.items.length] = new MenuItem(id, content, link);
	},

	addTitle: function(title) {
		this.items[this.items.length] = new MenuTitle(title);
	},

	render: function() {
		var width = this.items.length > this.maxitems ? (this.list.style.width - 20) : this.list.style.width;
		var content = '<div style="width: ' + width + ';"><table cellspacing="0" cellpadding="0" style="width: ' + width + ';">';
		content += '<tr><td rowspan="' + (this.items.length + 1) + '" style="border-right: 1px solid #555; width: 18px">&nbsp;&nbsp;&nbsp;</td></tr>';
		for (i = 0; i < this.items.length; i++) {
			var item = this.items[i];
			if (item instanceof MenuTitle) {
				content += '<tr><td class="itemtitle" style="height: ' + this.itemheight + 'px; width: ' + width + 'px; padding-left: 5px;"><nobr>' + item.value + '</nobr></tr></td>';
			}
			if (item instanceof MenuItem) {
				content += '<tr><td id="' + this.id + 'item' + item.id + '" class="item" onclick="' + item.link + '" onmouseover="this.className=\'item selected\'" onmouseout="this.className=\'item\'" style="height: ' + this.itemheight + 'px; width: ' + width + 'px; padding-left: 5px"><nobr>' + item.content + '</nobr></tr></td>';
			}
		}
		content += '</table></div>';
		this.list.update(content);
	}
}

/* Remove menues and widget selections when clicked outside their regions */

function WidgetMonitorClick(e) {
	var theElem = Event.element(e);
	var iswidget = false;
	var ismenu = false;
	RemoveMarkerBox();
	while (theElem != null) {
		if (theElem.className == "list" || theElem.className == "menubutton") {
			ismenu = true;
		}
		if (theElem.className == "widget") {
			iswidget = true;
			if (!IsLocked) {
				var obj = controls.getInstance(theElem.id);
				if (obj instanceof Widget) {
					obj.select();
				}
				SelectedWidget = obj;
				ActiveWidget = obj;
			}
		}
		theElem = theElem.offsetParent;
	}

	if (!iswidget) {
		RemoveWidgetScaleBoxes();
		if (ActiveWidget instanceof Widget) {
			ActiveWidget.hideTitle();
			ActiveWidget = null;
		}
		if (SelectedWidget instanceof Widget) {
			SelectedWidget.control.style.zIndex = 1;
			SelectedWidget.hideTitle();
			SelectedWidget = null;
		}
	}

	if (!ismenu) {
		for (i = 0; i < controls.length; i++) {
			var o = controls[i];
			if (o instanceof Menu) {
				o.hide();
			}
		}
	}
	return true;
}

function WidgetMonitorMouseOver(e) {
	var theElem = Event.element(e);
	var iswidget = false;
	var ismenu = false;
	while (theElem && theElem != null) {
		if ($(theElem).hasClassName('widget')) {
			iswidget = true;
			var obj = controls.getInstance(theElem.id);
			if (obj instanceof Widget) {
				if (ActiveWidget != obj && SelectedWidget != obj && !obj.ismoving && !obj.isscaling) {
					if (!IsLocked) {
						ActiveWidget = obj;
						ActiveWidget.showTitle();
						CreateMarkerBox(ActiveWidget);
					}
				}
			}
		}
		theElem = theElem.offsetParent;
	}

	if (!iswidget) {
		if (ActiveWidget instanceof Widget) {
			if (!ActiveWidget.ismoving && !ActiveWidget.isscaling && ActiveWidget != SelectedWidget) {
				ActiveWidget.hideTitle();
				RemoveMarkerBox();
			}
			ActiveWidget = null;
		}
	}
	return true;
}

function WindowResize(e) {
	if (IsAuthorized == true) {
		for (i = 0; i < controls.length; i++) {
			var o = controls[i];
			if (o instanceof Widget && !o.ismoving) {
				if (o.primaryframe) {
					o.control.style.left = ((Pos.regionLeft(ContentBoundary) - $(o.control).getWidth()) - o.x) + "px";
				} else {
					o.control.style.left = ((Pos.regionLeft(ContentBoundary) + ContentBoundary.getWidth()) + o.x) + "px";
				}
				if (Pos.regionLeft(ContentBoundary) < 90) {
					if ($(o.control).visible() != false) {
						$(o.control).hide();
					}
				} else if ($(o.control).visible() == false) {
					$(o.control).show();
				}
			}
		}

		if (SelectedWidget instanceof Widget) {
			RemoveWidgetScaleBoxes();
			SelectedWidget.control.style.zIndex = 1;
			SelectedWidget.hideTitle();
			SelectedWidget = null;
		}
		if (Pos.regionLeft(ContentBoundary) < 90) {
			$('mainmenu').hide();
		} else if ($('mainmenu').visible() == false) {
			$('mainmenu').show();
		}
	}
}

function WindowLoad(e) {
	if (IsAuthorized == true) {
		for (i = 0; i < controls.length; i++) {
			var o = controls[i];
			if (o instanceof Widget) {
				if (Pos.regionLeft(ContentBoundary) < 90) {
					if ($(o.control).visible() != false) {
						$(o.control).hide();
					}
				} else if ($(o.control).visible() == false) {
					$(o.control).show();
				}
			}
		}

		if (Pos.regionLeft(ContentBoundary) < 90) {
			$('mainmenu').hide();
		} else if ($('mainmenu').visible() == false) {
			$('mainmenu').show();
		}
	}
} 

function RemoveWidgetScaleBoxes() {
	var borderRemoved = false;
	for (i = controls.length - 1; i >= 0; i--) {
		var o = controls[i];
		if (o instanceof ScaleBox && o.parent instanceof Widget) {
			controls.splice(i, 1);
			document.body.removeChild(o.corner);
			if (!borderRemoved && o.border && o.border != null) {
				document.body.removeChild(o.border);
				o.border = null;
				borderRemoved = true;
			}
		}
	}
}

//Event.observe(document, 'click', WidgetMonitorClick.bindAsEventListener(this)); (NOTE: MOVED TO Default.Master.cs)
//Event.observe(document, 'mouseover', WidgetMonitorMouseOver.bindAsEventListener(this)); (NOTE: MOVED TO Default.Master.cs)
Event.observe(window, 'resize', WindowResize.bindAsEventListener(this));
Event.observe(window, 'load', WindowLoad.bindAsEventListener(this));
try {
RegTr("15375");
RegTr("15372");
RegTr("15373");
RegTr("15374");
RegTr("99");
RegTr("98");
RegTr("97");
} catch (e) {}
