/*
 * easyImgSwap 1.0	http://jlix.net/extensions/jquery/easyimgswap/1.0
 *
 * Copyright (c) 2009 Sander Aarts	(jlix.net)
 * Dual licensed under the MIT (http://jlix.net/MIT.txt)
 * and GPL (http://jlix.net/GPL.txt) licenses.
 *
 * Requires jQuery to work	(jquery.com). Tested with version 1.3.2
 *
 * 2009-10-14
 */
(function($) { // v1.0
	$.fn.extend( {
	
		easyImgSwap: function(click) {
			// Toggles source of matching images between its default (src) and its background-image, as specified in CSS. Works for both <img> and <input type="image">.
			// Args.:	click:	[Boolean | optional]	If true swapping will be triggered on click, otherwise on hover or focus/blur on parent <a> or <button> (default: false)
			function swap($this, type) {
				var imgSwap = $this.data("imgSwap");
				var swapped;
				if (typeof type == "object") {
					$.extend(imgSwap, type);
					swapped = imgSwap.hover || imgSwap.focus ? false : true;
				} else {
					swapped = imgSwap.swapped;
				}
				var src = swapped ? imgSwap.regSrc : imgSwap.altSrc;
				imgSwap.swapped = !swapped;
				$this.data("imgSwap", imgSwap);
				$this.attr("src", imgSwap.regPath + src);
			}
		
			this
				.filter(":image, img")
					.each(function() {
						var $this = $(this);
						var bgImg = $this.css("background-image");
						if (bgImg && bgImg != "none") {
							var bgImgUrl = bgImg.match(/^((url\("?)\s*(.*\/)*)(.+?)(\s*"?\))$/);
							var imgSrc = $this.attr("src").match(/^(.*\/)*(.*)$/);
							var imgSwap = {
								swapped:	false,
								regPath:	imgSrc[1],
								regSrc:	imgSrc[2],
								altSrc:	bgImgUrl[4]
							};
							$this.data("imgSwap", imgSwap);
							if (click) {
								$this.closest("a, button").andSelf()
									.click(function(e) {
										if (this == e.target) {
											swap($this);
										}
									});
							} else {
								$this.closest("a, button").andSelf()
									.hover(
										function() { swap($this, {hover: true}); },
										function() { swap($this, {hover: false}); }
									)
									.focus(function() { swap($this, {focus: true}); })
									.blur(function() { swap($this, {focus: false}); });
							}
						}
					});
			
			return this;
		}
		
	} );
} )(jQuery);<!--gen-->
