// jQuery Detector Overkill
// version 0.5, 2010-01-16
// Copyright (c) 2008-2010 Pyrolupus
// http://pyrolupus.com
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
// Original author: Paul Bakaus, http://paulbakaus.com
// Augmentation ideas: Karl Swedberg, http://learningjquery.com and http://englishrules.com
// 
// Displays an icon fixed to the bottom right of the viewport.
// If jQuery is loaded on the page:
// 	* the icon image is the jQuery logo
// 	* the image's title attribute is the jQuery version
// 	* clicking the image shows the jQuery version in a div.
// If jQuery is NOT on the page:
// 	* the icon is a bland, generic image
// 	* clicking the image loads jQuery
//
// Change Log:
// 0.5 2010-01-16
// Resized images and made transparent w/ dark matte (did not require change to userscript).
// Changed background of div to match new jquery.com design.
// 0.4 2009-12-04
// Stopped leeching images from jquery.com (because they moved them).
// Created images based upon jQuery.com favicon.
// 0.3 2008-07-30
// Cleaned and commented and other stuff I can't remember.
//
// TODO:
// Create smaller image w/ transparency.
(function() {
	var version=0.5,	//to easily and cheaply tell iteration when compressed
		b=document.getElementsByTagName('body')[0],
		el=document.createElement('div'),
		img = document.createElement('img'),
		els = el.style, ims = img.style,
		jqimg='http://pyrolupus.com/img/jq.png';	//"jquery is present" image

	// style info div
	els.position='fixed';
	els.bottom='2px';
	els.right='20px';
	els.width='200px';
	els.height='24px';
	els.margin='0 auto 0 auto';
	els.padding='5px 10px 5px 10px';
	els.backgroundColor='#0f1923';
	els.color='#fdfdfd';
	els.fontSize='8pt';
	els.textAlign='center';
	els.display='none';

	// style jquery detector icon
	ims.position='fixed';
	ims.bottom='2px';
	ims.right='2px';
	ims.zIndex='5';
	ims.cursor='pointer';
	ims.border='0';
	
	b.appendChild(img);
	b.appendChild(el);

	// function overhead in order to rebind click event after loading jQuery
	// showJq(): img.click = display version of jQuery currently loaded
	function showJq() {
		var jq=jQuery, v = jq.fn.jquery;
		if(img.src!=jqimg) img.src=jqimg;	//"if" protects jquery.com from needless rechecking
		img.title=v;
		el.innerHTML='Using jQuery v'+v;
		jq(img).click(function(){
			els.display='block';
			window.setTimeout(function() {
				jq(el).fadeOut('slow');
			}, 1000);
		});
	}
	if (typeof jQuery != 'undefined') {	//jquery exists; display version on icon click
		showJq();
	} else {	//no jquery; show no-jQuery icon and load jquery on click
		img.src='http://pyrolupus.com/img/jqx.png';
		img.title='Load jQuery';
		img.onclick=function(){	//todo: rebind click event to above
			var s=document.createElement('script');
			s.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js');
			document.getElementsByTagName('head')[0].appendChild(s);
			var i = 0;
			// loadJQuery(): recursive calls until jQuery finishes loading
			function loadJQuery() {
				el.innerHTML='jQuery Loading...';els.display='block';
				if (++i < 10 && typeof jQuery == 'undefined') {
					window.setTimeout(loadJQuery, 50);	//recheck every x milliseconds
				} else if (i >= 10){	//don't try rechecking more than y times
					el.innerHTML='Load jQuery failed<br/>(try again?)';
				} else {	//finished loading; show version and fade out
					img.onclick=showJq;	//rebind to just show version (so as not to keep rehitting ajax.google)
					el.innerHTML='jQuery loaded, v'+jQuery.fn.jquery;
					img.src=jqimg;	//reset img url to jquery icon
					window.setTimeout(function() {
						jQuery(el).fadeOut('slow');
					}, 1000);
				}
			}
			loadJQuery();	//start loading jquery
		};
	}
})();//define and execute the function all at once