// JavaScript Document

function cls_hexFrame() {
	var dctFrames 		= new hexDictionary
	var objElements 	= new hexElement()
	
	Frame.prototype.Create			=  _createFrame;	
	Frame.prototype.calcSize		= _calcSize
	Frame.prototype.CalcAutoSize	= _CalcAutoSize;
	
	Frame.prototype.resize			= _resize;
	Frame.prototype.changeWidth		= _changeWidth;
	Frame.prototype.changeHeight	= _changeHeight;
	Frame.prototype.changeSize		= _changeSize;
	Frame.prototype.getType			= _getType
	
	Frame.prototype.show			= _show;
	Frame.prototype.hide			= _hide;
	
	this.frames = function _frame(strFrameID) {
		return dctFrames.lookup(strFrameID)
	}
	
	this.init = function () {
		this.clear()
		var objElements= getByTagName("hex:frame")
		var intLength = objElements.length
		for (var i=0; i < intLength; i++) {
			applyHexFrame(objElements[0])
		}
		for (var objItem in dctFrames.keys()) {
			dctFrames[objItem].resize()
		}
		objEvents.onResize.add("refreshFrames",this.refresh)
		objEvents.onUnload.add("framesclear",this.clear)
	}
	
	function applyHexFrame(objElement) {
		if (objElement != undefined) {	
			var objTempFrame = new Frame
			dctFrames.add(getAttr(objElement,"id"),objTempFrame)
			
			objElement.parentNode.insertBefore(objTempFrame.Create(objElement),objElement)
			objElement.parentNode.removeChild(objElement)
			objTempFrame = null
		}
	}
	
	this.refresh = function () {
		for (var objFrame in dctFrames.keys()) {
			dctFrames.lookup(objFrame).resize()
		}
	}
	this.clear = function () {
		for (var objItem in dctFrames.keys()) {
			if (dctFrames.exists(objItem) && (!dctFrames.lookup(objItem).global)) {
				dctFrames.remove(objItem)
			}
		}
	}
	
	
	function Frame () {
		this.ID 		= null
		
		this.winWidth	= getElementWidth("window")
		this.winHeight	= getElementHeight("window")
		
		this.RelWidth = null
		this.RelHeight= null
		
		this.widthExp	= null
		this.heightExp	= null
		
		this.widthType	= null
		this.heightType	= null
		
		this.width		= null
		this.height 	= null
		
		this.global 	= false
		this.showing	= true
		
		this.marginwidth = 0
		this.marginheight = 0
		
		this.frame 		= function () {
			return document.getElementById(this.ID)
		}
		
		

	}
	
	function _createFrame(objElement) {
		
		/* Size Expression :: 	
				interger 	: fixed size in pixels 
				-integer 	: size calculated from the "Relation" Size subtracting the integer
				"auto"		: size based on other elements within the parentNode|group
				"%"			: size as a persent of the "Ralation" Size
				null		: size is defined by browser
		*/
		
		this.ID 		= objElement.id
		var objDivContainer = objElements.create(this.ID,"div")
		
		this.widthExp	= getAttr(objElement,"width")	
			
		this.heightExp 	= getAttr(objElement,"height")
		this.showing	= (getAttr(objElement,"show") == "false") ? false : true
		
		this.Relation	= getAttr(objElement,"rel")			// null/0 : parentNode | 1/'win':window | 2 : group | string : Element By ID
		this.Group		= getAttr(objElement,"group")
		
		this.marginwidth = cNum(getAttr(objElement,"marginwidth"))
		this.marginheight = cNum(getAttr(objElement,"marginheight"))
		
		this.widthExp	= cNum(this.widthExp,false,true)
		this.heightExp	= cNum(this.heightExp,false,true)
		this.Relation	= cNum(this.Relation,false,true)
		
		
		
		
		this.group		= getAttr(objElement,"group")

		
		this.frame			= objElements[this.ID]
		
		this.getType(1)
		this.getType(2)
		
		objDivContainer.style.display = (this.showing) ? "block" : "none"
		if (objElement.style.cssText != null) { 
			objDivContainer.style.cssText = objElement.style.cssText
		}
		objDivContainer.className = objElement.className 
		objDivContainer.innerHTML = objElement.innerHTML
		
		return objDivContainer;
		objDivContainer = null;
	}

	function _getType(intType) {
		var strDim			= (intType == 1) ? "width" : "height"
		var strType			= (intType == 1) ? "widthType": "heightType"
		var strHexGet		= (intType == 1) ? "getWidth" : "getHeight"
		var strRelDim		= (intType == 1) ? "RelWidth" : "RelHeight"
		var strExp			= (intType == 1) ? "widthExp" : "heightExp"
		var strMarginExp	= (intType == 1) ? "marginwidth" : "marginheight"
		
		if (isString(this[strExp])) {
			if (this[strExp] == "auto") {
				this[strType] = 3
			}else if (this[strExp].indexOf("%") > -1){
				this[strType] = 4
			}
		}else{
			this[strType] = (this[strExp] > 0) ? 1 : 2
		}
	}
	function _calcSize(intType) {
		var strDim			= (intType == 1) ? "width" : "height"
		var strType			= (intType == 1) ? "widthType" : "heightType"
		var strHexGet		= (intType == 1) ? "getElementWidth" : "getElementHeight"
		var strRelDim		= (intType == 1) ? "RelWidth" : "RelHeight"
		var strExp			= (intType == 1) ? "widthExp" : "heightExp"
		var strMarginExp	= (intType == 1) ? "marginwidth" : "marginheight"

		if (this[strExp] != null) {
			switch(this.Relation) {
				case null:
				case 0:
					this[strRelDim] =  eval(strHexGet)(this.frame().parentNode);						
				break;
				case 1:
				case "win":
				case "window":
					this[strRelDim] =   eval(strHexGet)("window");		
				break;
				default:
					this[strRelDim] = null;
				break;
			}
			if (isString(this.Relation) && (this[strRelDim] == null)) {
				this[strRelDim] = eval(strHexGet)(getByID(this.Relation))
			}
			
			
			switch(this[strType]) {
				case 1:
					this[strDim] = this[strExp]
				break;
				case 2:
					this[strDim] = this[strRelDim] + this[strExp]
				break;
				case 3:
					this[strDim] = this.CalcAutoSize(intType)
				break;
				case 4:
					this[strDim] = (this[strRelDim]/100) * parseInt(this[strExp])
				break;
				default:
					this[strDim] = null
				break;
			}
		}else{
			this[strDim] = null
		}
		if (this[strMarginExp] > 0 ) {
			this[strDim] -= this[strMarginExp]
		}
		if (this[strDim] < 0) { this[strDim] = 0}
		return this[strDim]
	}
	
	function _CalcAutoSize(intType) {
		var objParent 		= this.frame().parentNode
		var intUsedSize 	= 0;
		var intAutoCount 	= 0;		
		var intToFill		= (intType == 1) ? this.RelWidth : this.RelHeight	
		
		var strDimension 	= (intType == 1) ? "width" : "height"
		var strType			= (intType == 1) ? "widthType" : "heightType"
		var strHexGet		= (intType == 1) ? "getElementWidth" : "getElementHeight"
		
		for (var i=0; i < objParent.childNodes.length;i++) {
			var objChild = objParent.childNodes[i]
			if (objChild.id != this.ID) {
				if (dctFrames.exists(objChild.id)) {
					if (dctFrames[objChild.id].showing) {
						if (dctFrames[objChild.id][strType] == 3)  {
							intAutoCount += 1
						}else{
							intUsedSize += (dctFrames[objChild.id][strDimension] != null) ? dctFrames[objChild.id][strDimension] : eval(strHexGet)(dctFrames[objChild.id].frames())
						}
					}
				}else{
					intUsedSize += eval(strHexGet)(objChild)
				}
			}
			objChild = null
		}
		intToFill -= (intUsedSize != null && (!isNaN(intUsedSize) && (intUsedSize > 0)))  ? intUsedSize : 0
		if (intAutoCount > 0 ) {
			intToFill = intToFill/(intAutoCount+1)
		}		
		return intToFill
	}
	
	function _resize() {
		this.getType(1)
		this.getType(2)
		this.calcSize(1);
		this.calcSize(2);
		this.frame().style.width 	= (this.width != null ) 	? this.width 	: ""
		this.frame().style.height 	= (this.height != null ) 	? this.height 	: ""

		if (this.showing) {
			this.show()
		}else{
			this.hide()
		}
		return true
	}
	function _changeWidth(intNewWidth) {
		 this.widthExp = intNewWidth
		 this.resize ()
	}
	function _changeHeight(intNewHeight) {
		 this.heightExp = intNewHeight
		 this.resize ()
	}
	function _changeSize(intNewWidth,intNewHeight) {
		 this.widthExp = (intNewWidth != null) ? intNewWidth :  this.widthExp
		 this.heightExp = (intNewHeight != null) ? intNewHeight : this.heightExp
		 this.resize ()
	}
	function _show() {
		this.showing = true
		this.frame().style.display = "block";
	}
	function _hide() {
		this.showing = false
		this.frame().style.display = "none";
	}
	
}