// JavaScript Document




function hexAjax() {
	var objModuleManager 	= new moduleManager
	var objEvents			= new clsEventManager
	var objFuncOnload 		= null

	this.root   = ""
	this.folder = "library/hex/" 
	
	this.events = objEvents
	this.modules = objModuleManager
	this.module = objModuleManager.module
	
	this.Dictionary = hexDictionary
	this.eventManager = clsEventManager

	this.init =  function init() {
		objModuleManager.loadModules()
	}
	
	this.refresh = function refresh() {
		objModuleManager.init()
	}
	
	this.onLoaded = function (objFunc) {objFuncOnload = objFunc}
	
	
	this.getByTagName = function (strTagName,objParent) {
		if (objParent == null) {objParent = document;}
		var objElements= objParent.getElementsByTagName(strTagName)
		if (objElements.length == 0) {
			objElements= objParent.getElementsByTagName(strTagName.split(":")[1])
		}
		return (objElements)
	}
	
	
	function moduleManager() {
		var objModules = new hexModule
		this.name = null
		this.fileSrc = null
		this.className = null
		
		this.module = objModules
		
		this.setModuleState = setModuleState
		
		this.init = function () {
			var objModule
			for (objModule in objModules) {
				if (objModules[objModule] != undefined) {
					
					if (objModules[objModule].status == 2) {
						if (objModules[objModule].init != undefined) {						
							objModules[objModule].init()
						}
					}else{
						var intCount = 0
						if (objModules[objModule].status == 1) {
							if (objModules[objModule].init != null && objModules[objModule].init != undefined) {		
								var objTempFunc = objModules[objModule].init()
								if (objTempFunc != null && objTempFunc != undefined ) {
									
									objModules[objModule] = new objTempFunc
									objModules[objModule].status = 2
									if (objModules[objModule].init != null && objModules[objModule].init != undefined) {
									objModules[objModule].init()
									}
								}
							}
						}
					}
					
				}
			}
		}
		
		this.add = function (strRef,strFileName,strClassName) {
			objModules.add(strRef,strFileName,strClassName)
		}
		
		function hexModule() {
			this.add = function (strRef,strFileName,strClassName) {
				this[strRef] = function() {}
				this[strRef].name 		= strRef
				this[strRef].fileSrc = strFileName
				this[strRef].className = strClassName
				this[strRef].status = -1
			}
		}
		function setModuleState(strRef) {
			objModules[strRef].status = 1
			var objModule
			var blnAllLoaded = true
			var strTest = ""
			for (objModule in objModules) {
				if (objModules[objModule] != undefined) {
					if (objModules[objModule].status == -1) {
						blnAllLoaded = false
						break;
					}
				}
			}			
			if (blnAllLoaded) {
				modelesLoaded()
			}
			
		}
		
		function modelesLoaded () {
			objModuleManager.init()
			if (objFuncOnload != null) {
				objFuncOnload()
				objFuncOnload = null
			//	objModuleManager.init()
				//onloaded()
			}
		}
		
		this.loadModules = function () {
			var objModule = ""
			for (objModule in objModules) {
				if (objModules[objModule].name  != null) {
				if (objModules[objModule].fileSrc != null) {
					
					var objScript = document.createElement("script")
					objScript.language = "javascript"
					objScript.type = "text/javascript"
					objScript.src = hex.root + hex.folder + objModules[objModule].fileSrc + "?rID=" + Math.floor(Math.random()*10000)
					objScript.onreadystatechange = (function (vars) {return function () {
							if (this.readyState.toString() == "loaded") {
								setModuleState(vars);
							}
							}})(objModule)
					
					objScript.onload  = (function (vars) {return function () {
								setModuleState(vars);
							}})(objModule)
					
					document.getElementsByTagName("head")[0].appendChild(objScript);
					

				}
				if (objModules[objModule].className != null) {
					objModules[objModule].init = (function (vars) {return function () {return eval(vars);}})(objModules[objModule].className)
					objModules[objModule].name = objModule
				}
			}
			}		
		}
	}
	
	
	
	
	function clsEventManager() {
		this.onLoad 	= new Events
		this.onResize 	= new Events
		this.onkeyup	= new Events
		
		this.clsEvent = Events
		
		function Events() {
			var dctEvents = new hexDictionary
			this.add = function (strRef,objEvent,strVars) {
				
				if (strVars != undefined) {
					
					dctEvents.add(strRef,new Array(objEvent,strVars))
				}else{
				dctEvents.add(strRef,objEvent)
				}
			}
			this.remove = function (strRef) {
				dctEvents.remove(strRef)
			}
			this.removeAll = function () {
				dctEvents = new hexDictionary
			}
			this.events = function () {
				var objEvents = ""
				for (objEvents in dctEvents.keys()) {
					if (dctEvents.lookup(objEvents) != null) {
						var strVars = ""
						var objFunc = ""
						if (dctEvents.lookup(objEvents)[0]!= null) {
							objFunc = dctEvents.lookup(objEvents)[0]
							strVars = dctEvents.lookup(objEvents)[1]
							objFunc(strVars)
						}else{
							dctEvents.lookup(objEvents)()
						}
						
					}
				}
			}
		}
	}
	
	
	function hexDictionary() {
		this.count = function () {
			var intCount = 0
			for (objItem in this){
				if (objItem != "keys" && objItem != "count" && objItem != "add" && objItem != "remove" && objItem != "lookup" && objItem != "exists"){
					if (this[objItem] != null){
					intCount += 1
					}
				}
			}
			return intCount
		}
		this.add = function () {
			for (c=0; c<arguments.length; c+=2) {
				this[arguments[c]] = arguments[c+1];
			}
		}
		this.remove = function (strKeyName) {
			for (c=0; c<arguments.length; c++) {
				this[arguments[c]] = null;
			}
		}
		this.lookup = function (strKeyName) {
			return(this[strKeyName]);
		}
		this.exists = function (strKeyName) {
			return (this[strKeyName] != null);
		}
		this.keys = function() {
			var arrKeys = function() {}
			for (objItem in this){
				if (objItem != "keys" && objItem != "count" && objItem != "add" && objItem != "remove" && objItem != "lookup" && objItem != "exists"){
					if (this[objItem] != null){
						arrKeys[objItem] = objItem
					}
				}
			}
			return arrKeys
		}
	}
	
	this.getAttr = getAttr
	
	function getAttr(objElement,strAttribute) {
		var strReturn = ""
		if (objElement != null) {
			if (objElement.attributes.getNamedItem(strAttribute) != null) {
				strReturn = objElement.attributes.getNamedItem(strAttribute).nodeValue
			}
		}
		return strReturn 
	}
}
var hex = new hexAjax

hex.events.onLoad.add("init_hex",hex.init)


hex.modules.add ("events","hexevents.js","cls_hexEvents")

hex.modules.add ("data","hexdataset.js","cls_hexDataSet")
//hex.modules.add ("ribbon","hexribbon.js","cls_hexRibbon")
//hex.modules.add ("shadow","hexshadow.js","cls_hexShadow")
//hex.modules.add ("ribbongroup","hexribbongroup.js","cls_hexRibbonGroup")
//hex.modules.add ("ribbonselect","hexribbonselect.js","cls_hexRibbonSelect")



window.onload = hex.events.onLoad.events
window.onresize = hex.events.onResize.events
window.onkeyup = hex.events.onkeyup.events
//hex.init()