/*	
 * LBi.Common v 1.0
 * for jQuery 1.2.6
*/

String.prototype.trim = function () {
    return this.replace(/^\s*|\s*$/g,'');
}

if ( typeof LBi == 'undefined' ) { self.LBi = {}; }
LBi.Common = {	
	version					: 1.0,	
	identJSClass			: 'hasJS',
	dynamicInputSelector	: 'input.dyna-text',
	ajaxQueryParam			: 'rc=1',
	newWindowClass			: 'newwin',
	newWindowText			: 'This link will open a new window',	
	
	identJS: function() {
		// Add class to body to identify presence of JS
		// Use hasJS.css instead to control load flicker etc
		$('body').addClass(LBi.Common.identJSClass);
	},
	sortNumeric: function(b,a) {
		return a < b ? -1 : (a > b) ? 1 : 0;
	},
	isUnsignedInteger: function (s) {
		return (s.toString().search(/^[0-9]+$/) == 0);
	},
	
	getStringQueries: function(theString) {
		// returns object of all queries within a string
		if(theString.indexOf('?')>-1) {
			theString = theString.split('?')[1];
		}
		var qs = theString, qsKey, qsValue, stringElements = {};
		qs = qs.split("&");
		for (var i = qs.length-1; i>=0; i--) {
			qsKey = qs[i].split("=")[0];
			qsValue = qs[i].split("=")[1];
			stringElements[qsKey] = qsValue;
		}
		return stringElements;
	},
	urlGetAnchor: function(anchor) {
		// strip out and return local page anchor from url
		if ( anchor.href.indexOf("#") >= 0 ) { //Check for anchorness
			anchor.destination = anchor.href.substring(anchor.href.indexOf("#")+1);
			return anchor.destination;
		} else {
			return false;
		}
	},
	urlAjaxify: function(url) {
		// takes a string (url expected) and appends parameter to identify as AJAX only content required
		var prefix =(!url.contains('?')) ? "?" : "&" ;
		var ajaxURL = url + prefix + this.ajaxQueryParam;
		return ajaxURL;
	},
	
	/* New window/popup functions
	---------------------------------------*/	
	getNewWindowLinks: function() { 
		// Add the openInNewWindow function to the onclick event of links with a specified class name
		$('a').each(function (i) {
			if ($(this).hasClass(LBi.Common.newWindowClass)) {
				$(this).title = $(this).title + " " + LBi.Common.newWindowText;
				$(this).click(function () {
			    	var newWindow = window.open(this.getAttribute('href'), '_blank');
					if (newWindow) {
						if (newWindow.focus) {
							newWindow.focus();
						}
					}
					return false;
			    });
			}
		});
	},
	
	/* Input field text population
	 * Takes current value of form INPUTS and handles clearing and repopulating on focus
	-------------------------------------------------------------------------------------*/
	dynamicInputText: function() {
		$(LBi.Common.dynamicInputSelector).each(function (i) {
			var target = $(this);
			target.savedText = target.value; // keep track of the original input value
			target.onfocus=function(){
				this.value = this.value.trim()
				if (this.value == this.savedText) {
					this.value = "";
				}
			}
			target.onblur=function(){
				this.value = this.value.trim()
				if (this.value == "") {
					this.value = this.savedText;
				}
			}
		});
	}

}/* /LBi.Common */
