/**
 * @namespace
 * This is the global MapQuest namespace encaspulating all functionality used on the MapQuest sites.  The namespace 
 * is named m2 to account for versioning of the namespace to prevent conflicts with older versions on the namespace 
 * while in development.
 */
var m2 = {};

(function() { 
	var _ua = navigator.userAgent.toLowerCase();
	var _av = navigator.appVersion.toLowerCase();


	m2 = {
		/**
		 * @namespace
		 * This namespace is used to attach / hold on to misc page elements within the mapquest site
		 */
		page : {},
		
		/**
		 * @namespace
		 * This namespace is used to attach util functions
		 */
		util : {},
		
		/**
		 * This function is a generic replacement for the getElementById() function.  It takes a string or array of 
		 * Strings and returns the elements for them.  Will return the element passed in if it is not a string or array.
		 * 
		 * @param  {String|Array|HTMLElement} el   the identifier(s) that we will use to retrieve HTMLElement(s).
		 * @return {HTMLElement|Array}             A DOM reference to an HTML element or an array of HTMLElements.
		 */
		$ : function(el) {
			// string
		    if (m2.util.isString(el)) { 
		        return document.getElementById(el);
		    }
		
			// array
		    if (m2.util.isArray(el)) {
		        var a=[],i=0;
		        for (; i < el.length; i++) {
		            a[a.length] = m2.$(el[i]);
		        }
		        return a;
		    }
		
			// some object ... just pass it back
		    return el; 
		},
		dName  : function(el) {
			// string
		    if (m2.util.isString(el)) { 
		        return document.getElementsByName(el);
		    }
		
			// array
		    if (m2.util.isArray(el)) {
		        var a=[],i=0;
		        for (; i < el.length; i++) {
		            a[a.length] = m2.dName(el[i]);
		        }
		        return a;
		    }

			// some object ... just pass it back
		    return el; 
		},
		/**
		 * indicates if the operating system is a mac
		 * @type Boolean  
		 */
		isMac : (_ua.indexOf("macintosh") >= 0) ? true : false,
		
		/**
		 * indicates if the operating system is windows
		 * @type Boolean  
		 */
		isWindows : (_ua.indexOf("windows") >= 0 || _ua.indexOf("win32") >= 0) ? true : false,
		
		/**
		 * indicates if the browser is Internet Explorer and what version it is
		 * @type int
		 */
		isIE : (document.all && _ua.indexOf("opera") < 0) ? parseFloat(_av.split("msie ")[1].split(";")[0]) : 0,
		
		/**
		 * indicates if the browser is konqueror
		 * @type int
		 */
		isKhtml : (_av.indexOf("Konqueror") >= 0 || _av.indexOf("Safari") >= 0) ? parseFloat(_av) : 0,
		
		/**
		 * indicates if the browser is mozilla
		 * @type int
		 */
		isMoz : (_ua.indexOf("Gecko") >= 0 && !isKhtml) ? parseFloat(_av) : 0,
		
		/**
		 * indicates if the browser is firefox
		 * @type int
		 */
		isFF : (_ua.indexOf("firefox") >= 0) ? parseFloat(_ua.split("firefox/")[1].split(" ")[0]) : 0,
		
		/**
		 * indicates if the browser is Safari and what version it is
		 * @type int
		 */	
		isSafari : (_av.indexOf("safari") >= 0) ? parseFloat(_av.split("version/")[1]) || 2 : 0,
		
		/**
		 * indicates if the browser is Safari and what version it is
		 * @type int
		 */	
		isOpera : (_ua.indexOf("opera") >= 0) ? parseFloat(_av) : 0,

		/**
		 * Function determines the server purpose
		 */
	    getServerPurpose : function() {
	    	var devPurpose;
			var acct = (typeof s_accountCL != "undefined") ? s_accountCL : "";
	    	
	        switch(acct) {
	            case "aolwpmqdev2":
	    			devPurpose = "dev";
	    	        break;
	         	case "aolwpmqqa2":
	    			devPurpose = "qa";
	    	        break;
	         	case "aolwpmqstg2dev":
	    			devPurpose = "staging";
	    	        break;
	         	case "aolwpmq":
	    			devPurpose = "production";
	    	        break;
	         	default:
	    			devPurpose = "";
	    	}
	
	    	return devPurpose;
	    }
	};
	
	/*
	 * Stub out a console if it does not already exist.
	 */
	if (! ("console" in window) || !("firebug" in console)) {
	    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group"
			 , "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	    window.console = {};
	    for (var i = 0; i < names.length; ++i) window.console[names[i]] = function() {};
	}
})();

