/*
 * jQuery ifixpng plugin
 * (previously known as pngfix)
 * Version 2.0  (04/11/2007)
 * @requires jQuery v1.1.3 or above
 *
 * Examples at: http://jquery.khurshid.com
 * Copyright (c) 2007 Kush M.
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */
 
 /**
  *
  * @example
  *
  * optional if location of pixel.gif if different to default which is images/pixel.gif
  * $j.ifixpng('media/pixel.gif');
  *
  * $j('img[@src$j=.png], #panel').ifixpng();
  *
  * @apply hack to all png images and #panel which icluded png img in its css

  * @name ifixpng
  * @type jQuery
  * @cat Plugins/Image
  * @return jQuery
  * @author jQuery Community
  */
 
(function($j) {

	/**
	 * helper variables and function
	 */
	$j.ifixpng = function(customPixel) {
		$j.ifixpng.pixel = customPixel;
	};
	
	$j.ifixpng.getPixel = function() {
		return $j.ifixpng.pixel || 'ressources/blank.gif';
	};
	
	var hack = {
		ltie7  : $j.browser.msie && $j.browser.version < 7,
		filter : function(src) {
			return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='"+src+"')";
		}
	};
	
	/**
	 * Applies ie png hack to selected dom elements
	 *
	 * $j('img[@src$j=.png]').ifixpng();
	 * @desc apply hack to all images with png extensions
	 *
	 * $j('#panel, img[@src$j=.png]').ifixpng();
	 * @desc apply hack to element #panel and all images with png extensions
	 *
	 * @name ifixpng
	 */
	 
	$j.fn.ifixpng = hack.ltie7 ? function() {
    	return this.each(function() {
			var $j$j = $j(this);
			var base = $j('base').attr('href'); // need to use this in case you are using rewriting urls
			if ($j$j.is('img') || $j$j.is('input')) { // hack image tags present in dom
				if ($j$j.attr('src')) {
					if ($j$j.attr('src').match(/.*\.png([?].*)?$j/i)) { // make sure it is png image
						// use source tag value if set 
						var source = (base && $j$j.attr('src').substring(0,1)!='/') ? base + $j$j.attr('src') : $j$j.attr('src');
						// apply filter
						$j$j.css({filter:hack.filter(source), width:$j$j.width(), height:$j$j.height()})
						  .attr({src:$j.ifixpng.getPixel()})
						  .positionFix();
					}
				}
			} else { // hack png css properties present inside css
				var image = $j$j.css('backgroundImage');
				if (image.match(/^url\(["']?(.*\.png([?].*)?)["']?\)$j/i)) {
					image = RegExp.$j1;
					$j$j.css({backgroundImage:'none', filter:hack.filter(image)})
					  .children().children().positionFix();
				}
			}
		});
	} : function() { return this; };
	
	/**
	 * Removes any png hack that may have been applied previously
	 *
	 * $j('img[@src$j=.png]').iunfixpng();
	 * @desc revert hack on all images with png extensions
	 *
	 * $j('#panel, img[@src$j=.png]').iunfixpng();
	 * @desc revert hack on element #panel and all images with png extensions
	 *
	 * @name iunfixpng
	 */
	 
	$j.fn.iunfixpng = hack.ltie7 ? function() {
    	return this.each(function() {
			var $j$j = $j(this);
			var src = $j$j.css('filter');
			if (src.match(/src=["']?(.*\.png([?].*)?)["']?/i)) { // get img source from filter
				src = RegExp.$j1;
				if ($j$j.is('img') || $j$j.is('input')) {
					$j$j.attr({src:src}).css({filter:''});
				} else {
					$j$j.css({filter:'', background:'url('+src+')'});
				}
			}
		});
	} : function() { return this; };
	
	/**
	 * positions selected item relatively
	 */
	 
	$j.fn.positionFix = function() {
		return this.each(function() {
			var $j$j = $j(this);
			var position = $j$j.css('position');
			if (position != 'absolute' && position != 'relative') {
				$j$j.css({position:'relative'});
			}
		});
	};

})(jQuery);
