/*
 * 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
  * $.ifixpng('media/pixel.gif');
  *
  * $('img[@src$=.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(jq) {

	/**
	 * helper variables and function
	 */
	jq.ifixpng = function(customPixel) {
		jq.ifixpng.pixel = customPixel;
	};
	
	var hack = {
		ltie7  : jq.browser.msie && jq.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
	 *
	 * jq('img[@src$=.png]').ifixpng();
	 * @desc apply hack to all images with png extensions
	 *
	 * jq('#panel, img[@src$=.png]').ifixpng();
	 * @desc apply hack to element #panel and all images with png extensions
	 *
	 * @name ifixpng
	 */
	 
	jq.fn.ifixpng = hack.ltie7 ? function(pixelUrl) {
    	return this.each(function() {
			var jqjq = jq(this);
			var base = jq('base').attr('href'); // need to use this in case you are using rewriting urls
			if (jqjq.is('img') || jqjq.is('input')) { // hack image tags present in dom
				if (jqjq.attr('src')) {
					if (jqjq.attr('src').match(/.*\.png([?].*)?$/i)) { // make sure it is png image
						// use source tag value if set 
						var source = (base && jqjq.attr('src').substring(0,1)!='/') ? base + jqjq.attr('src') : jqjq.attr('src');
						// apply filter
						jqjq.css({filter:hack.filter(source), width:jqjq.width(), height:jqjq.height()})
						  .attr({src:pixelUrl})
						  .positionFix();
					}
				}
			} else { // hack png css properties present inside css
				var image = jqjq.css('backgroundImage');
				if (image.match(/^url\(["']?(.*\.png([?].*)?)["']?\)$/i)) {
					image = RegExp.$1;
					jqjq.css({backgroundImage:'none', filter:hack.filter(image)})
					  .children().children().positionFix();
				}
			}
		});
	} : function() { return this; };
	
	/**
	 * Removes any png hack that may have been applied previously
	 *
	 * $('img[@src$=.png]').iunfixpng();
	 * @desc revert hack on all images with png extensions
	 *
	 * $('#panel, img[@src$=.png]').iunfixpng();
	 * @desc revert hack on element #panel and all images with png extensions
	 *
	 * @name iunfixpng
	 */
	 
	jq.fn.iunfixpng = hack.ltie7 ? function() {
    	return this.each(function() {
			var jqjq = jq(this);
			var src = jqjq.css('filter');
			if (src.match(/src=["']?(.*\.png([?].*)?)["']?/i)) { // get img source from filter
				src = RegExp.$1;
				if (jqjq.is('img') || jqjq.is('input')) {
					jqjq.attr({src:src}).css({filter:''});
				} else {
					jqjq.css({filter:'', background:'url('+src+')'});
				}
			}
		});
	} : function() { return this; };
	
	/**
	 * positions selected item relatively
	 */
	 
	jq.fn.positionFix = function() {
		return this.each(function() {
			var jqjq = jq(this);
			var position = jqjq.css('position');
			if (position != 'absolute' && position != 'relative') {
				jqjq.css({position:'relative'});
			}
		});
	};

})(jQuery);