/*	$.fn.log([String]|[Object]|[Array]){};
 *	:random
 *	$.urlVars([String]){};
 *	$.fn.urlVars([String]){};
 */

(function($){

	/* Add a helper method for console.log to cottle IE6
	 * Looks to $.debug to see if it should run or not.
	 */
	$.fn.log = function($msg) {
		if($.debug) {
			/* if window.console exists, use it */
			if(window.console && window.console.firebug) console.log("%s: %o", $msg, this);
			else if(window.console && !window.console.firebug) console.log($msg+": "+this);
			/* otherwise, just give me an on-screen alert (but not a naggy alert box!) */
			else {
				if($('#jq__console').size() == 0) $('body').append('<div id="jq__console"></div>');
				$console = $('#jq__console');
				$console.css({
					fontFamily: 'Inconsolata, "Courier New", Courier, fixed-width',
					fontSize: '1.25em', 
					position: 'absolute',
					top: '0', left: '0', 
					width: '100%', 
					borderBottom: '1px solid black',
					height: '6em', 
					overflow: 'auto',
					padding: '.25em 1em', 
					opacity: .25, 
					backgroundColor: 'black', 
					color: 'white'
				});
				$console.hover(function(){
					$(this).stop().animate({ opacity: .9 }, 'slow', 'easeOutQuad');
				}, function(){
					$(this).stop().animate({ opacity: .25 }, 'slow', 'easeInQuad');
				});
				$console.append("<p>"+$msg+": "+this+"</p>");
			} 
		} return this;
	};
	
	$.log = $.fn.log;

	/* Add a :random selector to jQuery's CSS selector arsenal
	 */
	$.jQueryRandom = 0;  
	$.extend($.expr[":"], {
		random: function(a, i, m, r) {
			if (i == 0) $.jQueryRandom = Math.floor(Math.random() * r.length);
			return i == $.jQueryRandom;  
		}
	});
	
	/* Add a quick & dirty debugging outliner
	 * Looks to $.debug to see if it should run or not.
	 */
	$.fn.outline = function(){
		if($.debug) this.each(function(){
			$(this).css('border', '1px solid red');
		}); 
		return this;
	}
	
	/* Add a method to parse arguments from URL and provide them as variables.
	 * Accepts a prefix for the variable names, to avoid conflicts.
	 */
	$.urlVars = function(prefix) {
		if (typeof prefix == 'undefined') prefix = '$';
		var qString = top.location.search.substring(1);
		var pairs = qString.split(/\&/);
		for (var i in pairs) {
			var nameVal = pairs[i].split(/\=/);
			window[prefix + unescape(nameVal[0])] = unescape(nameVal[1]);
		}
	}
	
	/* Add a method to parse arguments from URL within a method chain.
	 * Returns an array to the next chained method, thus...
	 * NOTE: this method does not return the original object!
	 */
	$.fn.urlVars = function(prefix) {
		$result = new Array();
		if (typeof prefix == 'undefined') prefix = '$';
		var qString = top.location.search.substring(1);
		var pairs = qString.split(/\&/);
		for (var i in pairs) {
			var nameVal = pairs[i].split(/\=/);
			$result[prefix + unescape(nameVal[0])] = unescape(nameVal[1]);
		} return $result;
	}
	
})(jQuery);