// Property of Omega Technology Group, Inc.
// http://www.OmegaTechnologyGroup.com
// Global Fluid Layout Modules & Common Routines
// Version: 2.0
// Dated: 2007 March 01

var activateDebug = true; //set true or false
var incompatibleURL = 'incompatible.php';
var is = new DomSniffer();

function DomSniffer() {
	this.afterLoad = function(minWidth,minHeight) {
		this.domTest1 = (typeof document.implementation!="undefined" && document.implementation.hasFeature('HTML','1.0'));
		this.domTest2 = (typeof document.documentElement!="undefined" && document.documentElement.clientWidth)?true:false;
		this.domTest3 = (typeof(document.defaultView)!="undefined" && document.defaultView.getComputedStyle)?true:false;
		this.domTest4 = (document.body && document.body.currentStyle)?true:false;
		this.domOld1 = (!this.domTest2); // NS6.2 & NS7.0
		this.domOld2 = (!this.domTest2 && !this.domTest3 && this.domTest4); // IE5.2:mac
		this.domOld3 = (!this.domTest3 && !this.domTest4); // Safari:mac < 1.3
		this.domOld = (this.domOld1 || this.domOld2 || this.domOld3); // Browser supports DOM 1 but not enough to work - broke
		this.dom1 = (this.domTest1 && !this.domOld); // Browser will work as DOM Level 1 standards-compliant
		if(this.dom1) {
			this.domStyleStrict = this.domTest3;
			this.domStyleIE = (!this.domTest3 && this.domTest4);
			this.minWidth = minWidth;
			this.minHeight = minHeight;
			this.clientOrigWidth = document.documentElement.clientWidth;
			this.clientOrigHeight = document.documentElement.clientHeight;
			if(this.clientOrigHeight==0) this.clientOrigHeight = window.innerHeight; // Safari 1.x glitch
			this.clientWidth = (this.clientOrigWidth < minWidth)?minWidth:this.clientOrigWidth;
			this.clientHeight = (this.clientOrigHeight < minHeight)?minHeight:this.clientOrigHeight;
		}else{
			window.location.href = incompatibleURL;
		}
		if(activateDebug && !debugRunning) runDebug();
	}
}

function LayerObj(id) {
	this.obj = document.getElementById(id);	
	this.id = id;
	this.obj.layerObj = this;
	this.left = this.obj.offsetLeft;
	this.top = this.obj.offsetTop;
	this.width = this.obj.offsetWidth;
	this.height = this.obj.offsetHeight;
	this.rightPos = this.left + this.width;
	this.bottomPos = this.top + this.height;
	this.resizeSet = false;		
	this.setLeft = function(posLeft) {
		this.left = posLeft;
		this.rightPos = this.left + this.width;
		this.obj.style.left = this.left+'px';
	}
	this.setRight = function(posRight) {
		this.rightPos = this.obj.parentNode.offsetWidth - posRight;
		this.left = this.rightPos - this.width;
		this.obj.style.left = this.left+'px'
	}
	this.setRightPos = function(posRight) {
		this.rightPos = posRight;
		this.left = posRight-this.width;
		this.obj.style.left = this.left+'px';
	}
	this.setTop = function(posTop) {
		this.top = posTop;
		this.bottomPos = this.top+this.height;
		this.obj.style.top = this.top+'px';
	}
	this.setBottom = function(posBottom) {
		this.bottomPos = this.obj.parentNode.offsetHeight - posBottom;
		this.top = this.bottomPos - this.height;
		this.obj.style.top = this.top+'px'
	}
	this.setBottomPos = function(posBottom) {
		this.bottomPos = posBottom;
		this.top = posBottom-this.height;
		this.obj.style.top = this.top+'px';
	}
	this.setPos = function(posLeft, posTop) {
		this.setLeft(posLeft);
		this.setTop(posTop);
	}
	this.setWidth = function(newWidth) {
		this.width = newWidth;
		this.rightPos = this.left+newWidth;
		this.obj.style.width = this.width+'px';
	}
	this.setHeight = function(newHeight) {
		this.height = newHeight;
		this.bottomPos = this.top+newHeight;
		this.obj.style.height = this.height+'px';
	}
	this.setSize = function(newWidth, newHeight) {
		this.setWidth(newWidth);
		this.setHeight(newHeight);
	}
	this.setCenter = function() {
		var newL = Math.round((this.obj.parentNode.offsetWidth-this.width)/2);
		var newT = Math.round((this.obj.parentNode.offsetHeight-this.height)/2);
		this.setPos(newL, newT);
	}
	this.setCenterH = function() {
		var newL = Math.round((this.obj.parentNode.offsetWidth-this.width)/2);
		this.setLeft(newL);
	}
	this.setCenterV = function() {
		var newT = Math.round((this.obj.parentNode.offsetHeight-this.height)/2);
		this.setTop(newT);
	}
	this.setVisible = function(state) {
		this.obj.style.visibility = (state)?'visible':'hidden';
	}
	this.getStyle = function(styleStr) { // css style layout ex: background-color
		if (is.domStyleStrict) { // DOM Style Strict
			return document.defaultView.getComputedStyle(this.obj,null).getPropertyValue(styleStr);
		}else{ // DOM Style IE
			return this.obj.currentStyle[camelize(styleStr)];
		}
	}
	this.setStyle = function(styleStr, value) { // css style layout ex: background-color
		this.obj.style[camelize(styleStr)] = value;
	}
	this.initResizeContainer = function(origContentWidth, origContentHeight) {
		contentObj = this;
		this.origWidth = origContentWidth;
		this.origHeight = origContentHeight;
		this.origRatio = (origContentWidth / origContentHeight);
	}
	this.initResize = function(origLeft, origTop, origWidth, origHeight, constrain) {
		this.origLeft = origLeft;
		this.origTop = origTop;
		this.origWidth = origWidth;
		this.origHeight = origHeight;
		this.constrain = constrain;
		this.resizeSet = true;		
	}
	this.resize = function(center) {
		var nRatio = (contentObj.width / contentObj.height);
		var wRatio = (contentObj.width / contentObj.origWidth);
		var hRatio = (contentObj.height / contentObj.origHeight);
		if (this.constrain && (nRatio < contentObj.origRatio)) hRatio = wRatio;
		if (this.constrain && (nRatio > contentObj.origRatio)) wRatio = hRatio;
		this.setSize(Math.round(this.origWidth*wRatio), Math.round(this.origHeight*hRatio));
		if (center) {
			this.setCenter(contentObj);
		} else {
			this.setPos(Math.round(this.origLeft*wRatio), Math.round(this.origTop*hRatio));
		}
	}
	this.textResize = function(fs, lh) {
		defRat = this.origWidth/this.origHeight;
		var maxHeight = (this.width/this.height < defRat)?(this.width/defRat):this.height;
		var maxWidth = (this.width/this.height > defRat )?(this.height*defRat):this.width;
		lhRat = this.origHeight/lh;
		fsRat = this.origWidth/fs;
		lhNew = Math.round(maxHeight/lhRat);
		fsNew = Math.round(maxWidth/fsRat);
		this.obj.style.lineHeight = lhNew+'px';
		this.obj.style.fontSize = fsNew+'px';
	}
	this.textResizeFill = function(fs, lh) {
		var fRat = fs/lh;
		var fArea = (this.width * this.height * fs * lh)/(this.origWidth * this.origHeight);
		fsNew = Math.floor(Math.sqrt(fArea * fRat));
		lhNew = Math.floor(Math.sqrt(fArea/fRat));
		this.obj.style.lineHeight = lhNew+'px';
		this.obj.style.fontSize = fsNew+'px';
	}
}

function getObj(id) {
	return document.getElementById(id);
}

function getEventObj(e) {
	if (!e) e = window.event;
	var trg = null;
	if (e.target) {trg = e.target;}else if(e.srcElement){trg = e.srcElement;}
	return trg;
}

function numToStr(num,length) { // left padded with zeros;
	var numStr = new Array(length).join('0') + num;
	return numStr.substr(numStr.length-length, length);
}

function fixDec(num,places) {
	if (isNaN(num)) num = 0;
	var mult = Math.pow(10,places);
	var tmp = (Math.round(num*mult)/mult);
	var tmpArr = (tmp+'').split('.');
	var tmpInt = tmpArr[0];
	var tmpDec = (isNaN(tmpArr[1]))?'0':tmpArr[1];
	var tmpDecPad = (tmpDec + new Array(places).join('0')).substr(0,places);
	tmpStr = (places==0)?tmpInt:tmpInt+'.'+(tmpDecPad);
	return tmpStr;
}

function camelize(str) {
	return str.replace(/-(.)/g, function(m, l){return l.toUpperCase()});
}

function create2dArray(rows, cols) {
	this.dim = new Array(rows);
	for (var i=0; i<rows; i++)
	this[i] = new Array(cols);
	return this;
}

function activateObj(s) {
	document.writeln(s);
}
