// JavaScript Document

/* firebugxCustom.js courtesy of http://stackoverflow.com/questions/4901196/altering-firebugx-js-to-accomodate-ie-developer-tools/4984463 */
if (!window.console) {
  window.console = {};
}
if (!window.console.firebug) {
  var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

  for (var i = 0; i < names.length; ++i) {
    if (!window.console[names[i]]) {
      window.console[names[i]] = function () { }
    }
  } 
}


/* Code By Christian */
function isIE() {
	if(document.all && navigator.userAgent.indexOf("Opera") == -1) return true;
	return false;
}
function isSafari() {
	if((/Safari|Konqueror|KHTML/gi).test(navigator.userAgent)) { return true; }
	return false;
}

// crockford Object.create formulation
if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}
// foo = {one: 1, two: 2};
// foo2 = Object.create(foo);
// 

function urlParameterToBodyClass(paramName) {
	var myHref = window.location.href
	strReturn = '';
	if (myHref.indexOf("&") > -1) {
		var strQueryString = myHref.substr(myHref.indexOf("&")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if ( aQueryString[iParam].indexOf(paramName + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	if(!(strReturn == '')) { addBodyClass(strReturn); }
}

function addBodyClass(newClass) {
	theBody = document.getElementsByTagName('body')[0];
	oldClasses = '';
	if (theBody.className) { oldClasses = theBody.className + ' '; }
	theBody.className = oldClasses + newClass;
}
function getURLParamValue(paramName, myHref) {
	if (typeof myHref === 'undefined') {
		var myHref = window.location.href;
	}
	var paramValue;
	if (myHref.indexOf("?") > -1) {
		var urlParams = myHref.substr(myHref.indexOf("?") + 1);
		if (myHref.indexOf("#") > -1) {
			var urlParams = urlParams.substr(0, urlParams.indexOf("#")); // try to get the value to the left of the # first
		}
		paramArr = urlParams.split("&");
		for ( var iParam = 0; iParam < paramArr.length; iParam++ ){
			var thisParamParts = paramArr[iParam].split("=");
			var thisParamName = thisParamParts[0];
			var thisParamValue = thisParamParts[1];
			if (thisParamName === paramName){
				var aParam = paramArr[iParam].split("=");
				paramValue = thisParamValue;
			}
		}
	}
	if (myHref.indexOf("#") > -1) {	//check if the target exists after the # and overwrite value if found
		//var urlParams = myHref.substr(myHref.indexOf("#")).toLowerCase();
		var urlParams = myHref.substr(myHref.indexOf("#"));
		var pparamArr = urlParams.split("&");
		for ( var iParam = 0; iParam < pparamArr.length; iParam++ ){
			if ( pparamArr[iParam].indexOf(paramName + "=") > -1 ){
				var aParam = pparamArr[iParam].split("=");
				paramValue = aParam[1];
			}
		}	
	}
	return paramValue;
}

function updateParameterInURL(parameter, newValue, url) {
	if(typeof url === 'undefined') {
		url = window.location.href;
	}
	var curVal = getURLParamValue(parameter);
	if(!!curVal) {
		return url.split(parameter + "=" + curVal).join(parameter + "=" + newValue);
	} else {
		connector = (url.indexOf("?") > -1) ? "&" : "?";
		return url + connector + parameter + "=" + newValue;
	}
}
function removeParameterFromURL(parameter, url) {
	if(typeof url === 'undefined') {
		url = window.location.href;
	}
	if(url.indexOf("?"+parameter+"=") > -1) {
		// if it is the first parameter
		var cutMe = "?"+parameter+"="+getURLParamValue(parameter, url);
		url = url.split(cutMe).join("?").split("?&").join("?");
		// call recursively in case parameter is in URL more than once
		return removeParameterFromURL(parameter, url);
	} else if(url.indexOf("&"+parameter+"=") > -1) {
		var cutMe = "&"+parameter+"="+getURLParamValue(parameter, url);
		url = url.split(cutMe).join("");
		return removeParameterFromURL(parameter, url);
	}
	return url;
}
function go(where) {
	window.location.href = where;
}

function loadJS(url) {
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e);
}

Array.prototype.intersect = function() {
	if (!arguments.length)
		return [];
	var a1 = this;
	var a = a2 = null;
	var n = 0;
	while(n < arguments.length) {
		a = [];
		a2 = arguments[n];
		var l = a1.length;
		var l2 = a2.length;
		for(var i=0; i<l; i++) {
			for(var j=0; j<l2; j++) {
				if (a1[i] === a2[j])
					a.push(a1[i]);
			}
		}
		a1 = a;
		n++;
	}
	return a.unique();
};

function theTime() {
	var hours;
	var mins;
	var secs;
	var time;
	stamp = new Date();
	hours = stamp.getHours();
	if (hours >= 12) { time = " P.M."; }
	else { time = " A.M."; }
	if (hours > 12) { hours -= 12; }
	if (hours == 0) { hours = 12; }
	mins = stamp.getMinutes();
	if (mins < 10) { mins = "0" + mins; }
	secs = stamp.getSeconds();
	if(secs < 10) { secs = "0" + secs}
	ms = stamp.getMilliseconds();
	currentTime = hours + ':' + mins + ':' + secs + '.' + ms + time;
	return currentTime
}

