/*
 *
 * Copyright (c) 2006/2007 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 */

 
/*
 * jQuery Image Replacement. An alternative to using CSS hacks
 * The id attribute (or class) is used for the filename
 *
 * @name     jQIR
 * @param    format  Image format/file extension (e.g. png, gif, jpg) - ignored if specifying the filename in the class
 * @param    path    Path to images folder (optional)
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @example  $(".jqir").jQIR("png", "images/");
 * @before   <h1 id="heading1" class="jqir">Heading 1</h1>
 *           <h2 class="jqir {src:heading2.png}">Heading 2</h2>
 * @result   <h1 id="heading1" class="jqir"><img alt="Heading 1" src="images/heading1.png"></h1>   
 *           <h2 class="jqir {src:heading2.png}"><img alt="Heading 2" src="images/heading2.png"></h2>   
 * @example  $(".jqir").jQIR("gif"); // use same folder as page
 * @before   <h1 id="heading1" class="jqir">Heading 1</h1>
 * @result   <h1 id="heading1" class="jqir"><img alt="Heading 1" src="heading1.gif"></h1>   
 *
 */
jQuery.fn.jQIR = function(format, path, active)
{
	if(!document.images) return this;
	path = path || "";
	this.each(function(){
		var img = $("<img>"), self = jQuery(this);
		var file;
		var filename = self.text().toLowerCase();
		var re = new RegExp(" ", "g");
		filename = filename.replace(re, "_");
		var on = "";
		if(active)
		{
			on = "ON";
		}
		file = path + filename + on + "." + format;
		jQuery(img).attr(
		{
			src: file,
			alt: self.text(),
			title:  self.text()
		});
		$.ajax({
		  type: "GET",
		  url: file,
		  complete: function(request){
			if( request.status == 200 ) {
				self.empty().append(img);
			}
			self.css("visibility", "visible");
		  }
		});
	});
	return this;
}
