/**
 * 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) {
	
	selObj.options.length = 0;
}

/**
 * Selects an item in a select object
 * @param {Object} obj
 */
function _selectItemInSelObj(selObj, item, type) {

	for(var i=0; i<selObj.options.length; i++) {
		
		if ((type == 'text') &&
		    (item == selObj.options[i].text)) {
		
				selObj.options[i].selected = true;
		} else if ((type == 'value') &&
				   (item == selObj.options[i].value)) {

				selObj.options[i].selected = true;
		} else  if ((type == 'index') &&
				(item == i)) {
				selObj.options[i].selected = true;
		}
	}
}

/**
 * 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';
			return 'block';
		} else {
			
			div.style.display = 'none';
			return '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';
	if (_dom(divName)) {
		
		_destroyDiv(divName);
	} else {
		
		if (typeof zindex != 'number') {
			zindex = 100;
		}
		
		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.60;opacity:.60;filter: alpha(opacity=60);';
		layer.style.cssText = cssText;
		document.body.appendChild(layer);
	}
}

function _getDocumentDimensions()
{
	var width		= 0;
	var height  	= 0;
	var clientHeight= 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};
}

function bookmark(title, url)
{
	title = 'SilverPlanet.com - ' + title;
	if (window.sidebar) { // firefox
		window.sidebar.addPanel(title, url, "");
		
	} else if (window.opera && window.print) { // opera
			var elem = document.createElement('a');
			elem.setAttribute('href', url);
			elem.setAttribute('title', title);
			elem.setAttribute('rel', 'sidebar');
			elem.click();
	} else if (document.all) {// ie
				window.external.AddFavorite(url, title);
	}
}

function _nl2br(str) {
	
	return str.replace(/\r|\n|\r\n/g, "<br/>");
}