/**
 * General utility functions
 * 
 * @author Brian Stegman <brianstegman@tnbsolutions.com>
 * @copyright Copyright Stegman Company LLC
 */

/**
 * Shortcut function for getting a dom object
 * @param {String} id
 */
function _dom(id) {
	
	return document.getElementById(id);
}

/**
 * Generates a uniq numeric id
 */
function _genUniqId() {
	
	var time = new Date();
	return time.getTime();
}

/**
 * Sets the opacity for the given object/id.  This can be an object or
 * object id.  The original author of this function is Scott Upton (www.couloir.org)
 * However I altered it to take a string or an object.
 * 
 * @param {String/Object} obj
 * @param {String} opacity
 */
function _setOpacity(obj, opacity) {

	// Check to see if we were passed an object
	if (typeof obj == 'string') {
		obj = _dom(obj);
	}

	opacity = (opacity == 100) ? 99.999 : opacity;

	// IE
	obj.style.filter = "alpha(opacity:"+opacity+")";

	// Safari < 1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;

	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;

	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

/**
 * Takes a string and escapes the single and double quotes 
 * @param {String} str
 */
function _escape(str) {
	
	str = str.replace(/'/ig, "\\'");
	str = str.replace(/"/ig, '\"');
	str = str.replace(/;/ig, '\;');
	return str;
}

/**
 * Empties all the options in a select object
 * @param {Object} obj
 */
function _emptySelObj(selObj) {
	
	for(var i=selObj.options.length; i>=0; i--) {
		selObj.options[i]=null;
	}
}

/**
 * Destroys the div
 * @param {Object} divName
 */
function _destroyDiv(divName) {
	
	var win = document.getElementsByTagName('div')[divName];
	win.parentNode.removeChild(win);
}

/**
 * Toggles a dom object on/off by style display
 * @param {String/Object} name
 */
function _toggleDisplay(name) {
	
	if (document.getElementById(name)) {
		
		var div = _dom(name);
		if (div.style.display == 'none') {
			
			div.style.display = 'block';
		} else {
			
			div.style.display = 'none';
		}
	}
}

/**
 * Returns the pos of an element
 * @param {Object:String} e
 */
function _getPos(e) {

		if (typeof e == 'string') {
			e = _dom(e);
		}

		var x = 0;
		var y = 0;
		while(e) {
			
			x += e.offsetLeft;
			y += e.offsetTop;
			e = e.offsetParent;
		}
		return {x:x, y:y};
}

function _toggleOverlay(zindex)
{
	var divName = 'overlay' + zindex;
	if (_dom(divName)) {
		
		_destroyDiv(divName);
	} else {
		
		if (typeof zindex != 'number') {
			zindex = 10000;
		}
		
		var d = _getDocumentDimensions();
		
		var layer = document.createElement('div');
		layer.id = divName;
		var cssText = 'position:absolute;left:0px;top:0px;z-index:'+zindex+';display:block;' +
					  'background-color:#000;width:'+d.width+'px;height:'+d.height+'px;' +
					  '-moz-opacity: 0.40;opacity:.40;filter: alpha(opacity=40);';
		layer.style.cssText = cssText;
		document.body.appendChild(layer);
	}
}

function _getDocumentDimensions()
{
	var width	= 0;
	var height  = 0;
	var cHeight = 0;
	
	if (document.documentElement &&
		document.documentElement.scrollWidth) {
			width   = document.documentElement.scrollWidth;
			height  = document.documentElement.scrollHeight;
			cHeight = document.documentElement.clientHeight;
			if (cHeight > height) {
				height = cHeight;
			}
			
	} else if (document.body.scrollWidth) {
		width   = document.body.scrollWidth;
		height  = document.body.scrollHeight;
		cHeight = document.body.clientHeight;
		if (cHeight > height) {
			height = cHeight;
		}
	}
	
	return {width:width,height:height};
}
