/* PHP Image Replacement jQuery plugin*/
(function($){
	$.fn.pir = function(opts) {
		opts = $.extend({color:'000',font:'',va:'top'},opts);
		
		return this.each(function(){
			var that = $(this), text = that.text();
			that.empty();
			$.each(text.split(' '), function(){
				that.append(
					$('<img class="pir"/>')
						.attr('alt', this)
						.attr('src', URL_ROOT+'cms-textserver.php?text='+this
							+'&font='+opts.font
							+'&size='+parseInt(that.css('font-size'), 10)
							+'&color='+opts.color
						).css('vertical-align', opts.va)
				).append(' ');
				
				if(opts.hoverColor) {
					var hoverUrl = URL_ROOT+'cms-textserver.php?text='+this
							+'&font='+opts.font
							+'&size='+parseInt(that.css('font-size'), 10)
							+'&color='+opts.hoverColor;
					that.append(
						$('<img class="pir-hover"/>')
							.attr('alt', this)
							.attr('src', hoverUrl).css('vertical-align', opts.va).hide()
					);
					$('<img>').attr('src', hoverUrl);
				}
			});
			
			// Hover color
			if(opts.hoverColor && opts.hoverColor != opts.color) {
				function over(){
					$('img.pir', that).hide();
					$('img.pir-hover', that).show();
				}
				function out(){
					$('img.pir-hover', that).hide();
					$('img.pir', that).show();
				}
				if(that.hasClass('open')) {
					over();
				} else {
					(that.closest('a').length > 0 ? that.closest('a') : that).hover(over, out);
				}
			}
		});
	};
})(jQuery);
