(function($){

	$.fn.pir = function(options) {
		if(options == undefined) options = {};
		
		return this.each(function(){ $.pir_sort(this,options) });
	};
	
	$.pir_sort = function(elem,options) 
	{
		return $(elem).each(function()
		{
			var parent_elem = this;
			
			var kids = $(this).children().length;
			
			if($(this).is('a') && kids < 1)
			{
				$.pir_inverse(this,this,options);
			}
			else if(kids < 1)
			{
				$.pir_create(this,options);
			}
			else
			{
				$(this).contents().each(function()
				{
					// link
					if($(this).is('a'))
					{
						$.pir_inverse(this,parent_elem,options);
					}
					// text
					else if(this.nodeType == 3)
					{
						var new_el = $("<span>" + this.nodeValue + "</span>");						
						$(this).replaceWith(new_el);
												
						$.pir_create(new_el,options);
					}
					// something else
					else if(this.nodeType != 3)
					{
						$.pir_create(this,options);
					}
				});				
			}
		});
	};
	
	$.pir_inverse = function(elem,parent_elem,options)
	{
		
		$.pir_create(elem,options);
		
		$(elem).hover(
			function () 
			{
				if($(elem).data('on_state') == undefined)
				{
					// get the non hover state before changing the image
					$(elem).data('off_state', {"background-image" : $(elem).css("background-image"), "height" : $(elem).css("height"), "width" : $(elem).css("width")});
					$.pir_create(elem,options);						
				}
				else
				{
					$(elem).css($(elem).data('on_state'));
				}
			}, 
			function () 
			{
				// get the hover state before changing the image
				$(elem).data('on_state', {"background-image" : $(elem).css("background-image")});
				$(elem).css($(elem).data('off_state'));
			}
		);
	};
	
	
	$.pir_create = function( elem, options ) {
		
		$(elem).addClass('pir-replaced');
		
		if($(elem).parent().is('h1, h2, h3') || $(elem).is('h1, h2, h3'))
		{	
			options.size = "60px";	
		}
			
		// fulltext of parent for setting true height
		var el_text = $(elem).text();
		
		var fulltext = $(elem).parent(".pir").text();
		if(fulltext == false) fulltext = el_text;
		
		// options from css
		css_options = {
			color: 		$(elem).css("color"),
			bgColor: 	$(elem).css("background-color")			
		};

		// font family from css
		//var font = $(elem).css('font-family').split(',').shift();
		//if(font) css_options.font = font;
	
		// metadata (plugin)	
		var meta = {}; //($.metadata)?$(elem).metadata():{};
		
		// set the options
		var o = $.extend($.pir_create.options,css_options,{text: el_text, fulltext: fulltext},meta,options);
		
		//logger(o);
		
		var url = 'http://www.anne-davis.co.uk' + o.php + "?text=" + escape(o.text) + "&font=" + o.font + "&size=" + o.size + "&color=" + escape(o.color) + "&bgcolor=" + escape(o.bgColor) + "&fulltext=" + escape(o.fulltext) + "&cache=" + o.cache;
//alert(url);		

		if(! o.retrieve)
		{
			// new image object 
			var img = new Image(); 
			
			// image onload 
			$(img).load(function () {
				var width = $(img).width();
				var height = $(img).height();
				if(! o.preload) $(elem).css({'background-image': "url('" + url + "')", 'width': width, 'height' : height});
				$(img).remove();
				return $(elem); 
			}).error(function () { 
				$(elem).removeClass('pir-replaced');
				$(img).remove();
				return false;
			}).attr('src', url).hide().appendTo($("body")); 
		}
		else
		{
			$(elem).css({'background-image': "url('" + url + "')"});
			return $(elem);	
		}
	};
	
	//Defaults
	$.pir_create.options = {
		php: "/utils/pir.php",
		font: "Windsong",
		size: "32px",
		preload: false,
		retrieve: false,
		cache: true,
		transparent: true
	};
	
	//Version
	$.pir_create.version = "0.1";
	
	//Auto-run
	$(function(){ $(".pir").pir(); });
	
})(jQuery);
