/*	
	Javascript & PHP Image Replacement 
	Created by Tab Atkins Jr.
*/
(function($j){

$j.fn.pir = function( options ) {

	return $j(this).hide().each(function(){ $j.pir(this,options); }).show();
};

$j.pir = function( elem, options ) {
	//Settings
	var $je = $j(elem),
		meta = ($j.metadata)?$j(elem).metadata():{},
		o = $j.extend(
				{ size: $je.css("font-size"), color: $je.css("color"), casing: $je.css("text-transform"), text: $je.text().split("'").join("&#039;") },
				$j.pir.options,
				meta,
				options),
		e = encodeURIComponent;
	

	
	$je.html(""); //clear out the current contents
	$j.each( o.wrap ? o.text.split(" ") : [o.text], function() {
		if( $j.trim(this) != "" ) {
			//have to manually tweak the vertical-align so that it works properly when inlined
			var $jimg = $j("<img alt='" + this.replace("'", "&rsquo;") + "' src='" + o.php + "?text=" + e(this) + "&font=" + e(o.font) + "&size=" + e(o.size) + "&color=" + e(o.color) + "&casing=" + e(o.casing) + "'>")
					.css({"vertical-align":"bottom"});
			$je.append( $jimg );
			$je.append( " " );
		}
	});

	//this option *removes* the pir images when you print, returning to the original text, because images don't always look great when printed
	if( o.prettyPrint ) {
		$j("img", $je).addClass("pir-prettyprint-image");
		$j("<span>" + o.text + "</span>").addClass("pir-prettyprint-text").appendTo($je);
		$j("<style type='text/css' media='print'></style>").text(".pir-prettyprint-image { display: none; }").appendTo("head");
		$j("<style type='text/css' media='screen'></style>").text(".pir-prettyprint-text { display: none; }").appendTo("head");
	};
	return $je;
};

//Defaults
$j.pir.options = {
	php: "/pir.php",
	font: "denmark.ttf",
	wrap: false,
	prettyPrint: false
};

//Version
$j.pir.version = "0.1";

//Auto-run
$j(function(){ $j(".pir").pir(); });

})(jQuery);