/*
 * utils
 *
 * Copyright (c) 2011 Justin Robert Wehrman
 *
 * Version: 2.0.0 (3/5/2011)
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 */

// Utils

/*--	||-- listFunctions(objName); --||
			Returns a list of all methods/functions within object passed via 'objName' param
		----------------------------------------------------------------------------------------*/

/*--	||-- queryParam('PARAMETER'); --||
			EXAMPLE: http://www.foosite.bar?foo=bar&bar=foo#foobar
				Utils.queryParam('foo');		:::: 	Utils.queryParam('bar');
				Returns "bar"					:::: 	Returns "foo"
			*NOTE - anchor tags (#foobar) are excluded from result
		----------------------------------------------------------------------------------------*/
		
/*--	||-- addParam('PARAMETER'); --||
			Appends parameter to url. If additional parameters exist it is pushed to the end.
			If a hash exists, it will not be affected.
		----------------------------------------------------------------------------------------*/

/*--	||-- setLogging(); --||
			EXAMPLE: http://www.foosite.bar?showlogs=true&foo=bar&bar=foo#foobar
					 http://www.foosite.bar?foo=bar&bar=foo#foobar&showlogs=true
				a: 	Used to control display of logging information in console window if
					console window exists, otherwise fail gracefully.
				b:	Enables ability to leave -log(foo)- items in scripts and display only
					if isLogging is set to true via showlogs parameter in url
			*NOTE - If parameter 'showlogs=true' is not equal to true or does not exist then
					there will be no output into console window.
		----------------------------------------------------------------------------------------*/
		
/*--	||-- isMobile(); --||
			Returns true if mobile device is matched, otherwise undefined.
			*NOTE - This is a simple list and far from definitive.
		----------------------------------------------------------------------------------------*/
		
/*--	||-- loadGoogleMapsAPI(PARAMETER, 'PARAMETER'); --||
			Load the google maps api
				a. Parameter 1 sets the sensor parameter. true or false (REQUIRED)
				b. Parameter 2 sets the callback fired once the api is fully loaded (OPTIONAL)
		----------------------------------------------------------------------------------------*/

var Utils = {
	
	listFunctions: function (objName) {
		if (typeof objName !== 'object') {
			throw 'ERROR caused by ' + objName + ': Expected object, was passed a ' + typeof objName;
		}
		var fList = [], l;
		for (l in objName) { 
			if (typeof objName[l] === 'function') {
				fList.push(l);
			}
		} 
		return fList;
	}, //-- listFunctions()
	
	queryParam: function (paramName) {
		var query = {}, m, v, k; 
		for (m, v = location.href.split(/[?&]/), k = v.length - 1; k > 0; k -= 1) {
			query[(m = v[k].split(/[=#]/))[0]] = m.length > 1 ? decodeURI(m[1]) : "";
		}
		return query[paramName];
	}, //-- queryParam()
	
	addParam: function (paramName, paramValue) {
		var currentParam = location.search, paramDivider;
		if (currentParam === '') {
			paramDivider = '';
		} else {
			paramDivider = '&';
		}
		location.search = currentParam + paramDivider + paramName + '=' + paramValue;
	}, //-- addParam()
	
	setLogging: function () {
		var isLogging = Utils.queryParam('showlogs');
		window.log = function (string) {
			if (typeof console === 'object') {
				console.log(string);
			}
		};
		if (typeof console === "undefined") {
			console = { log: function () {} };
		} else if (isLogging !== 'true' || typeof console.log === 'undefined') {
			console.log = function () {};
		}
	}, //-- setLogging()
	
	isMobile: function () {
		var d, devices = ['mozilla', 'ipod', 'iphone', 'ipad', 'series60', 'symbian', 'android', 'windows ce', 'blackberry', 'palm'];
		for (d = 0; d < devices.length; d += 1) {
			if (navigator.userAgent.match(new RegExp(devices[d], "gi"))) {
				return true;
			}
		}
	}, //-- isMobile()
	
	loadGoogleMapsAPI: function ( sensorValue, callback ) {
		var script = document.createElement("script"), callBackItem = '';
		if (callback) {
			callBackItem = "&callback=" + callback;
		}
		script.type = "text/javascript";
		script.src = 'http://maps.google.com/maps/api/js?sensor=' + sensorValue + callBackItem;
		void(document.body.appendChild(script));
	} //-- loadGoogleMapsAPI
	
};
Utils.setLogging();
