/*
name: workspace
alias: workspace.messageHub
extends: workspace.extensions
*/
if (!window.workspace) window.workspace = {};

window.cleanUpItems = new Array()

workspace.isIE = navigator.userAgent.indexOf('MSIE') > 0
workspace.css1 = (document.compatMode == 'CSS1Compat')
workspace.isIE6 = navigator.userAgent.indexOf('MSIE 6.0') > 0
workspace.isRefresh = false; //document.scripts.namedItem('workspace').refresh == "-1"
workspace.root = '/'
workspace.skinName = "standard"; 
workspace.userID = null

workspace.putRoot= function(root) { this.root = root; }
workspace.putSkin = function(skinName) { this.skinName = skinName; }
workspace.putUserID = function(value) { this.userID = value; }

workspace.dealiasUrl = function(url)
{	
	if(url.charAt(0)=="~")
	{
		if (this.root=='/')
			return url.slice(1,url.length)
		else
			return this.root + url.slice(1,url.length);
	}
	else
	{
		return url;
	}    
}

if (!workspace.isIE)
{
	Element.prototype.attachEvent = function(name, fn)
	{
		if (name.substr(0,2) == "on") name = name.substr(2)
		this.addEventListener(name, fn, false)
	}		
	window.attachEvent = Element.prototype.attachEvent

	Element.prototype.detachEvent = function(name, fn)
	{
		if (name.substr(0,2) == "on") name = name.substr(2)
		this.removeEventListener(name, fn, false)
	}		
	window.detachEvent = Element.prototype.detachEvent	
}

function doWorkspaceCleanup()
{
	for (var i = 0; i < cleanUpItems.length; i++)
	{
		try
		{	
			cleanUpItems[i].cleanUp()
		}
		catch(e)
		{}
	}
	window.detachEvent("onunload", doWorkspaceCleanup);
}
window.attachEvent("onunload", doWorkspaceCleanup)

workspace.createFunction = function(functionBody, paramList)
{
	if (typeof(functionBody) == 'function') return functionBody
	if (!paramList) paramList = ''
	eval("var fn = function(" + paramList + ") { " + functionBody + " }")
	paramList = null
	functionBody = null
	return fn
}

workspace.getTop = function()
{
	//TODO: Incomplete
	try
	{
		if (window.parent && window.parent.window.workspace)
			return window.parent.window.workspace.getTop()
		else
			return this
	}
	catch(e)
	{
		return this
	}
}

workspace.getQueryStringValue = function(name, def)
{
		var rgx = new RegExp(name + "=(.*?)(?:&|$)","i")
		var asMatches = window.location.search.match(rgx)
		return asMatches ? asMatches[1] : def
}


workspace.Base = function() {}
workspace.Base.prototype.inherit = function(src)
{
	if (typeof(src) == 'function') src = src.prototype;
	for (var key in src) this[key] = src[key];
}
workspace.Base.prototype.getFieldValue = function(field, def, bindProperties)
{
	if (!bindProperties)
	{
		return Nz(this['get' + field](), def)
	}
	else
	{
		var oRet = new Object()
		var src = field == 'this' ? this : this['get' + field]()
		
		for (var key in bindProperties)
			oRet[key] = Nz(src['get' + bindProperties[key]](), def ? def[key] : undefined)
	
		return oRet
	}

}
workspace.Base.prototype.putFieldValue = function(field, value, bindProperties)
{				
	
	if (!bindProperties)
	{
		this['put' + field](value)			
	}
	else
	{
		var src = field == 'this' ? this : this['get' + field]()
		
		for (var key in bindProperties)
			src['put' + bindProperties[key]](value)
	}	
}

workspace.stateCodes = {initialising: 1, documentcomplete: 2, loaded: 3, ready: 4}
workspace.state = workspace.stateCodes.initialising
	
workspace._messageHub_construct = function()
{
		
	function Channel(owner, name)
	{
		this.name = name
		this.owner = owner
		this.afnListeners = []
	}
	Channel.prototype.addListener = function(fn)
	{
		this.afnListeners.push(fn)
		//return this.afnListners.length - 1
	}
	Channel.prototype.removeListener = function(fn)
	{
			
		for (var i = this.afnListeners.length - 1; i >= 0 ; i--)
		{
			if (this.afnListeners[i] == fn)
			{
				this.afnListeners.splice(i,1);
				return true;
			}	
		}	
	}
	Channel.prototype.notify = function()
	{
		for (var i = 0; i < this.afnListeners.length; i++)
			if (this.afnListeners[i]) this.afnListeners[i].apply(this.owner, arguments)
	}
	Channel.prototype.discard = function()
	{
		this.afnListeners = []		
	}
	Channel.prototype.listenerCount = function()
	{
		return this.afnListners.length
	}
	
	function MessageHub(owner)
	{
		var mcolChannels = {}
	
		function getChannel(name)
		{
			var oChan = name ? mcolChannels[name] : new Channel(owner)
			if (!oChan) oChan = mcolChannels[name] = new Channel(owner, name)
			return oChan
		}

		function discardChannel(name)
		{
			var oChan = mcolChannels[name]
		
			if (oChan)
			{
				oChan.discard()
				mcolChannels[name] = null
			}
		}
		
		this.getChannel = getChannel
		this.discardChannel = discardChannel
		this.Channel = Channel
		this.listenerCount = function(name)
		{
			var chan = mcolChannels[name]
			return chan ? chan.listenerCount() : 0
		}
	}

	
	BaseEventer = function() {}
	BaseEventer.prototype = new workspace.Base()
	BaseEventer.prototype.attachEvent = function(name, fn)
	{
		if (!this.messageHub) this.messageHub = new MessageHub(this)
		this.messageHub.getChannel(name).addListener(fn)
	}
	BaseEventer.prototype.detachEvent = function(name, fn)
	{
		if (!this.messageHub) this.messageHub = new MessageHub(this)
		this.messageHub.getChannel(name).removeListener(fn)
	}
	BaseEventer.prototype.fireEvent = function(name)
	{
		var args
		var retVal
			
		if (this[name] || this.messageHub)
		{
			args = []
			for (var i = 1; i < arguments.length; i++) args.push(arguments[i])

			if (this[name]) retVal = this[name].apply(this, args)		
		
			if (this.messageHub)
			{
				var chan = this.messageHub.getChannel(name)
				chan.notify.apply(chan, args)
			}
		}
		return retVal
	}
	BaseEventer.prototype.getEventFunctions = function(name)
	{
		var afn = []
		if (this[name]) afn.push(this[name])
		if (this.messageHub) this.messageHub.getChannel(name).afnListners.forEach(function(fn) {afn.push(fn);} )
		return afn
	}
	BaseEventer.prototype.deferEvent = function(name)
	{
		if (this.messageHub)
		{
			var args = []
			for (var i = 1; i < arguments.length; i++) args.push(arguments[i])
			var self = this
			window.setTimeout(fnNotify, 100)
		
			function fnNotify()
			{
				var chan = self.messageHub.getChannel(name)
				chan.notify.apply(chan, args)
			}
		}
	}
	BaseEventer.prototype.listenerCount = function(name)
	{
		return this.messageHub ? this.messageHub.listenerCount(name) : 0
	}
	BaseEventer.prototype.parseEventFromElem = function(name, elem)
	{
		if (!elem) elem = this.elem ? this.elem : document.getElementById(this.ID);
		if (elem)
		{
			var sEventCode = elem.getAttribute(name)
			if (sEventCode) this[name] = workspace.createFunction(sEventCode);
		}
	}
	BaseEventer.prototype.dropEvent = function(name)
	{
		if (this.messageHub) this.messageHub.discardChannel(name)
	}
	
	this.MessageHub = MessageHub
	this.messageHub = new MessageHub(this)
	this.BaseEventer = BaseEventer
	this.attachEvent = BaseEventer.prototype.attachEvent
	this.detachEvent = BaseEventer.prototype.detachEvent
	this.fireEvent = BaseEventer.prototype.fireEvent
	this.deferEvent = BaseEventer.prototype.deferEvent
	this.listenerCount = BaseEventer.prototype.listenerCount
	
	function fnOnReadyStateChange()
	{
		if (!workspace.isIE || document.readyState == 'complete')
		{
			if (workspace.isIE) 
				document.detachEvent("onreadyStatechange", fnOnReadyStateChange)	
			else
				document.removeEventListener("DOMContentLoaded", fnOnReadyStateChange, false)

			workspace.fireEvent('ondocumentcomplete')
			workspace.messageHub.discardChannel('ondocumentcomplete')
			workspace.state = workspace.stateCodes.documentcomplete
			workspace.fireEvent('afterdocumentcomplete')
			workspace.messageHub.discardChannel('afterdocumentcomplete')
			
		}
	}
	
	if (workspace.isIE)
		document.attachEvent("onreadystatechange", fnOnReadyStateChange)
	else
		document.addEventListener("DOMContentLoaded", fnOnReadyStateChange, false)
		
	function fnOnload()
	{
		workspace.fireEvent('onload')
		workspace.messageHub.discardChannel('onload')
		workspace.state = workspace.stateCodes.loaded
		workspace.fireEvent('afteronload')
		workspace.messageHub.discardChannel('afteronload')
		
    //document.body.attachEvent("oncontextmenu",blockContextMenu);
        
		window.setTimeout(fnNotify, 100)
		
		function fnNotify()
		{
		    if(workspace.UI)
		    {
		        workspace.UI.resizeParentIFrame();
		    }
			workspace.fireEvent('ondeferedload')
			workspace.messageHub.discardChannel('ondeferedload')	
			workspace.state = workspace.stateCodes.ready				
		}
		
	}					
	window.attachEvent("onload", fnOnload)
	
	function blockContextMenu()
	{
		if (!event.ctrlKey)
		{
			event.cancelBubble = true;
			event.returnValue = false;
		}
	}

}
workspace._messageHub_construct()

workspace.fnNull = function() {}

workspace.Page = function()
{
	var self = this
	workspace.attachEvent('ondocumentcomplete', fnOndocumentcomplete)
	
	function fnOndocumentcomplete()
	{
		if (self.ondocumentcomplete) self.ondocumentcomplete();
		workspace.attachEvent('onload', fnOnload)
	}
	
	function fnOnload()
	{
		if (workspace.UI && workspace.UI.controls) self.bodyCtls = workspace.UI.controls.bodyCtls
		if (self.onload) self.onload();
		workspace.attachEvent('ondeferedload', fnOnDeferedload)
	}
	
	function fnOnDeferedload()
	{
		if (self.ondeferedload) self.ondeferedload();
	}

	this.specialise = function(myPage)
	{
		myPage.prototype = thisPage
		thisPage = new myPage()
		self = thisPage
	}
	
	this.getPageModifiedString = function()
	{
		var metaModified = document.getElementById('meta.pagemodifiedtime')
		return (metaModified) ? metaModified.getAttribute("content") : null;
	}
}
workspace.Page.prototype = new workspace.BaseEventer()
workspace.Page.prototype.reload = function() { return true; }
window.thisPage = new workspace.Page()
