
function eventHandler()
{ };

eventHandler.prototype = {
		
	addEvent : function (id, eventType, funct)
	{
		//if ($(id) != null)
		//{
		//	id = $(id);
		//}
	
		if (id && id.id)
		{
			//log ("Trying to Attach Event to " + id.id);
			if (window.addEventListener)
			{
				id.addEventListener (eventType, funct, false);
			}
			else if (window.attachEvent)
			{
				id.attachEvent ('on' + eventType, funct);
			}
			else
			{
				alert ("Event Error (Attach).\nID: " + id.id + "\nFunction: " + funct);
			}
			//log ("Event Attachment Complete (" + id.id + ")");
		}
		else
		{
			log ("Event Attachment Failed - ID not found (" + id + ")");
		}
	},
	
	removeEvent : function (id, eventType, funct)
	{
		if ($(id) != null)
		{
			id = $(id);
		}
		//log ("Event Detachment Begun");
		if (window.removeEventListener)
		{
			id.removeEventListener (eventType, funct, false);
		}
		else if (window.detachEvent)
		{
			id.detachEvent ('on' + eventType, funct);
		}
		else
		{
			log ("Event Error (Detach).\nID: " + id + "\nFunction: " + funct);
		}
	},
	
	stopEventPropagation : function (ev)
	{
		log ("Trying to stop event propagation...");
		if (ev.stopPropagation)
		{
			ev.stopPropagation();
		}
		else
		{
			ev.cancelBubble = true;
		}
	},
	
	preventDefaultEvent : function (ev)
	{
		if (ev.preventDefault)
		{
			ev.preventDefault();
		}
		else
		{
			ev.returnValue = false;
		}
	},
	
	stopEvent : function (ev)
	{
		this.stopEventPropagation(ev);
		this.preventDefaultEvent(ev);
	},
	
	getEventTarget : function (e) 
	{ 
		var e = e || window.event; 
		var targ = e.target || e.srcElement; 
		if (targ.nodeType == 3) 
		{ // defeat Safari bug 
			targ = targ.parentNode; 
		} 
		return targ; 
	}
};
		
function handlerManager()
{ 
	this.titleHandler = [];
};

// Adding event handlers
// All events should be called from here!
handlerManager.prototype = {
		
		titleHandler : [],
		blurbHandler : [],
		aTitleHandler : [],
		aBlurbHandler : [],
		num : 0,
		
		addTitleHandler : function (iid)
		{
			this.titleHandler[iid] = function (ev) { ajaxCall(e, ev, 111200, 'itemtitle' + iid, ['iid=' + iid], []); }; 
			e.addEvent ($('itemtitle' + iid), 'dblclick', this.titleHandler[iid]);
		},
		
		addTitleHandlers : function (ids)
		{
			var iids = Array();
			iids = ids.split(",");
			for (iid in iids)
			{
				this.aTitleHandler[iids[iid]] = function (ev) { ajaxCall(e, ev, 111200, 'itemtitle' + iids[iid], ['iid=' + iids[iid]], []); }; 
				e.addEvent ($('itemtitle' + iids[iid]), 'dblclick', this.aTitleHandler[iids[iid]]);
			}
		},
		
		addTextBoxEditorHandlers_g : function (idplus, data)
		{
			for (innerid in data.insertin)
			{
				var id = innerid;
				var grabValues = getGrabValues('["text' + id + '"]');
				//log (innerid);
				//log (document.getElementById(innerid));
			}		
			var params = [idplus.param];
			var newCode = getPlusOne(data.code);
			var newCode2 = getPlusOne(newCode);
			log (newCode + " code");
			e.addEvent (data['fillform']['savebutton'], 'click', function(ev) { ajaxCall(e, ev, newCode, id, params, grabValues); } );
			e.addEvent (data['fillform']['cancelbutton'], 'click', function(ev) { ajaxCall(e, ev, newCode2, id, params, grabValues); } );
			e.addEvent (data['fillform']['input'], 'keydown', function(ev) { return extendTextBox(e, ev, newCode, 'text' + id, params, grabValues); } );
			if (data.autoRemoveEvent)
			{
				d = data.autoRemoveEvent.funct.split('.');
				e.removeEvent (innerid, 'dblclick', hm[d[0]][d[1]]);
			}
		},

		addBlurbHandler : function (iid)
		{
			this.blurbHandler[iid] = function (ev) { ajaxCall(e, ev, 111210, 'itemblurb' + iid, ['iid=' + iid], []); }; 
			e.addEvent ($('itemblurb' + iid), 'dblclick', this.blurbHandler[iid]);
		},
		
		addBlurbHandlers : function (ids)
		{
			var iids = Array();
			iids = ids.split(",");
			for (iid in iids)
			{
				this.aBlurbHandler[iids[iid]] = function (ev) { ajaxCall(e, ev, 111210, 'itemblurb' + iids[iid], ['iid=' + iids[iid]], []); }; 
				e.addEvent ($('itemblurb' + iids[iid]), 'dblclick', this.aBlurbHandler[iids[iid]]);
			}
		},
		
		addItemHandler : function (iid)
		{
			this.addBlurbHandler(iid);
			this.addTitleHandler(iid);
		},
		
		addTextAreaEditorHandlers_g : function (idplus, data)
		{
			for (innerid in data.insertin)
			{
				var id = innerid;
				var grabValues = getGrabValues('["text' + id + '"]');
				log ("GV: " + grabValues);
			}		
			var newCode = getPlusOne(data.code);
			var newCode2 = getPlusOne(newCode);
			var params = [idplus.param];
			e.addEvent (data['fillform']['savebutton'], 'click', function(ev) { ajaxCall(e, ev, newCode, id, params, grabValues); } );
			e.addEvent (data['fillform']['cancelbutton'], 'click', function(ev) { ajaxCall(e, ev, newCode2, id, params, grabValues); } );
			e.addEvent (data['fillform']['textarea'], 'keydown', function(ev) { return extendTextArea(e, ev, newCode, 'text' + id, params, grabValues); } );
			d = data.autoRemoveEvent.funct.split('.');
			e.removeEvent (innerid, 'dblclick', hm[d[0]][d[1]]);
		},
		
		addPageTitleHandler : function ()
		{
			this.ptitleHandler = function (ev) { ajaxCall(e, ev, 115000, 'pagetitle', [], ['pagetitle']); };
			e.addEvent ($('pagetitle'), 'dblclick', this.ptitleHandler);
		},
		
		addMsgBoxXHandler : function (data)
		{
			this.msgBoxXHandler = function (ev) { a = new eventHandler(); removeEl('box' + data.code + data.key, data.hide); a.preventDefaultEvent(ev); };
			e.addEvent ($('x' + data.code + data.key), 'click', this.msgBoxXHandler);
		},
		
		addMsgBoxCloseHandler : function (data)
		{
			this.msgBoxCloseHandler = function (ev) { removeEl('box' + data.code + data.key, data.hide); };
			e.addEvent ($('cancelbutton' + data.code + data.key), 'click', this.msgBoxCloseHandler);
		},
		
		addMsgBoxSubmitHandlers : function (data)
		{
			var newCode = getPlusOne(data.code);
			if (!data.callData)
			{
				data.callData = Array();
				data.callData.param = "";
			}
			this.msgBoxSubmitHandler = function (ev) { ajaxCall(e, ev, newCode, data.boxId, data.callData.param, data.grabValues); removeEl('box' + data.code + data.key, data.hide); };
			e.addEvent ($('closebutton' + data.code + data.key), 'click', this.msgBoxSubmitHandler);
		},
		
		addMsgBoxExtraSubmitHandlers : function (data)
		{
			this.msgBoxExtraSubmitHandler = function (ev) { window[data.customcall](data); };
			e.addEvent ($('closebutton' + data.code + data.key), 'click', this.msgBoxExtraSubmitHandler);
		},
		
		addAddItemHandler : function (col)
		{
			this.addNewItemHandler = function (ev) { ajaxCall(e, ev, 111100, '', ['col=' + col], []); };
			e.addEvent ($('addItemButton'), 'click', this.addNewItemHandler);
		},
		
		addPagePermissionsHandler : function ()
		{
			this.pagePermissionsHandler = function (ev) { ajaxCall(e, ev, 120010, '', [], []); };
			e.addEvent ($('pagePermissionsButton'), 'click', this.pagePermissionsHandler);
		},
		
		openItemToolbox : function (iid, pos, piid)
		{
			$('itemtoolbox' + iid).style.display = "block";
		},
		
		closeItemToolbox : function (iid, pos, piid)
		{
			$('itemtoolbox' + iid).style.display = "none";
		},
		
		addItemToolboxHandler : function (iid, pos, piid)
		{
			this.itemToolboxHandler = function (ev) { hm.openItemToolbox(iid, pos, piid); };
			this.itemToolboxCloseHandler = function (ev) { hm.closeItemToolbox(iid, pos, piid); };
			e.addEvent ($('item' + iid), 'mouseover', this.itemToolboxHandler);
			e.addEvent ($('item' + iid), 'mouseout', this.itemToolboxCloseHandler);
			
			itemDelHandler = function (ev) { ajaxCall(e, ev, 111300, '', ["iid=" + iid], []); };
			e.addEvent ($('itemdel' + iid), 'click', itemDelHandler);
			if ($('itemmoveup' + iid) && $('itemmoveup' + iid).style.display != 'none')
			{
				itemMoveUpHandler = function (ev) { ajaxCall(e, ev, 111500, '', ["iid=" + iid, "pos=" + (pos - 3), "piid=" + piid], []); };
				e.addEvent ($('itemmoveup' + iid), 'click', itemMoveUpHandler);
			}
			if ($('itemmoveup' + iid) && $('itemmovedown' + iid).style.display != 'none')
			{
				itemMoveDownHandler = function (ev) { ajaxCall(e, ev, 111501, '', ["iid=" + iid, "pos=" + (pos + 3), "piid=" + piid], []); };
				e.addEvent ($('itemmovedown' + iid), 'click', itemMoveDownHandler);
			}
			itemPermHandler = function (ev) { ajaxCall(e, ev, 120020, '', ["iid=" + iid], []); };
			e.addEvent ($('itemperm' + iid), 'click', itemPermHandler);
		},
		
		removeItemToolboxHandler : function (iid)
		{
			e.removeEvent ($('item' + iid), 'mouseover', this.itemToolboxHandler);
			e.removeEvent ($('item' + iid), 'mouseout', this.itemToolboxCloseHandler);
		},
		
		addPermissionBoxHandler : function (data)
		{
			this.permissionBoxHandler = function (ev) { ajaxCall(e, ev, 120100, '', ['id=' + getClickedElement(ev).id], []); }
			e.addEvent($('boxbody'), 'click', this.permissionBoxHandler);
		},
		
		addSitePermButtonHandler : function ()
		{
			this.sitePermButtonHandler = function (ev) { ajaxCall(e, ev, 120000, '', [], []); };
			e.addEvent ($('sitePermButton'), 'click', this.sitePermButtonHandler);
		},
		
		addSiteUserButtonHandler : function ()
		{
			this.siteUserButtonHandler = function (ev) { ajaxCall(e, ev, 130000, '', [], []); };
			e.addEvent ($('siteUserButton'), 'click', this.siteUserButtonHandler);
		},
		
		addSitePermissionsHandler : function ()
		{
			this.sitePermissionsHandler = function (ev) { ajaxCall(e, ev, 120100, '', ['id=' + getClickedElement(ev).id], []); return false; }
			e.addEvent($('sitePermBody'), 'click', this.sitePermissionsHandler);
		},
		
		addLogInHandler : function ()
		{
			this.logInHandler = function(ev) { ajaxCall(e, ev, 110000, "loginbar", [], ["loginu","loginp"]); } 
			e.addEvent ($("loginForm"), "submit", this.logInHandler);
		},
		
		addLogOutHandler : function ()
		{
			this.logOutHandler = function(ev) { ajaxCall(e, ev, 110100, "loginbar", [], []); }
			e.addEvent ($("logout"), "click", this.logOutHandler);
		},
		
		addReturnToPageHandler : function (hide, others)
		{
			log ("return handler");
			this.returnToPageHandler = function(ev) { 
					while($(store.activeCol).childNodes.length > 0)
					{
						if ($(store.activeCol).childNodes[0])
						{
							$(store.activeCol).removeChild($(store.activeCol).childNodes[0]);
						}
					}
					while($('hider').childNodes.length > 0)
					{
						if ($('hider').childNodes[0])
						{
							$(store.activeCol).appendChild($('hider').childNodes[0]);
						}
					}
					$('hider').parentNode.removeChild($('hider'));
					document.getElementById(hide).style.visibility = 'visible';
				}
			e.addEvent ($("returnToPageLink"), "click", this.returnToPageHandler);
			if (others != null)
			{
				for (o in others)
				{
					e.addEvent ($(others[o]), "click", this.returnToPageHandler);
				}
			}
		},
		
		addAddUserHandler : function (iid)
		{
			this.addUserHandler = function(ev) { ajaxCall(e, ev, 130100, "", ['iid=' + iid], []); } 
			e.addEvent ($("AddUser"), "click", this.addUserHandler);
		},
		
		addEditUserElementHandler : function ()
		{
			this.editUserElementHandler = function(ev) { ajaxCall(e, ev, 130200, "", ['id=' + getClickedElement(ev).id], []); } 
			e.addEvent ($("userTable"), "dblclick", this.editUserElementHandler);
		},
		
		addChangeUserRoleHandler : function (uids)
		{
			for (uid in uids)
			{
				this.changeUserRoleHandler = function(ev) { ajaxCall(e, ev, 130400, "", ['uid=' + uid], []); } 
				e.addEvent ($("roleSelect" + uids[uid]), "change", this.changeUserRoleHandler);
			}
		},
		
		addUserActionHandler : function (uids)
		{
			for (uid in uids)
			{
				this.userActionHandler = function(ev) { ajaxCall(e, ev, 130900, "", ['uid=' + uid], []); } 
				e.addEvent ($("userPartAction" + uids[uid]), "change", this.userActionHandler);
			}
			//130900 = 130300 and 131100
		}
	
}
