/*!
 * jQuery Tools
 *
 *   http://wiki.activeids.nl/index.php/JQuery_Tools
 */

/**
 * log renders a string in the console window or optionally in an alert window if console is unavailable
 * @param arg (string): string to be outputed
 * @param alt (boolean): if true show output in alert window when console is unavailable
 */
jQuery.log = function (arg, alt) {
    if (window.console && console.debug) {
        console.log(arg);
    } else if(alt){
        alert(arg);
    }
};

/**
 * isDefined checks if the given object is defined and returns this as boolean.
 * @param obj (object): object to be checked
 * @return (boolean): if true the given object is defined
 * @type boolean
 */
jQuery.isDefined = function (obj) {
    if (typeof(obj) == "undefined") {
        return false;
    }
    if (obj == null) {
    	return false;
    }
    return true;
};


/**
 * getUrlVars returns a URL's parameters as array
 * @param url (string): URL formatted as absolute path
 * @return (array):
 * @type array
 */
jQuery.getUrlVars = function(url){
  var vars = [], hash;
  var hashes = url.slice(url.indexOf('?') + 1).split('&');
  for(var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
  }
  return vars;
};

/**
 * getUrlVar returns the value of a single URL parameter
 * @param url (string):  URL formatted as absolute path
 * @param name (string): name of parameter
 * @return (mixed): value of parameter (string or number)
 * @type mixed
 */
jQuery.getUrlVar = function(url,name){
  return $.getUrlVars(url)[name];
};

jQuery.browserVersion = function() {
	var userAgent = navigator.userAgent.toLowerCase();
	$.browser.chrome = /chrome/.test(userAgent);
	var version = 0;

	// Is this a version of IE?
	if($.browser.msie){
		userAgent = $.browser.version;
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));	
		version = userAgent;
	}

	// Is this a version of Chrome?
	if($.browser.chrome){
		userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));	
		version = userAgent;
		// If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
		$.browser.safari = false;
	}

	// Is this a version of Safari?
	if($.browser.safari){
		userAgent = userAgent.substring(userAgent.indexOf('safari/') +7);	
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;	
	}

	// Is this a version of Mozilla?
	if($.browser.mozilla){
		//Is it Firefox?
		if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
			userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
			userAgent = userAgent.substring(0,userAgent.indexOf('.'));
			version = userAgent;
		}else{
			// If not then it must be another Mozilla
		}
	}

	// Is this a version of Opera?
	if($.browser.opera){
		userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
	}
	return version;
};

jQuery.klog = function (text) {
	$("#Log").prepend("<div>" + text + "</div>");
	$.log(text);
};


Array.prototype.unique = function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };
