/*
* jQuery ifixpng plugin
* (previously known as pngfix)
* with another plugin
* Version 1.9  (27/09/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($) {

    /**
     * helper variables and function
     */
    $.ifixpng = function(customPixel) {
        $.ifixpng.pixel = customPixel;
    };

    $.ifixpng.getPixel = function() {
        return $.ifixpng.pixel || '/images/showcase/ui/blank.gif';
    };

    var hack = {
        ltie7: $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent),
        filter: function(src) {
            return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='" + src + "')";
        }
    };

    /**
     * Applies ie png hack to selected dom elements
     *
     * $('img[@src$=.png]').ifixpng();
     * @desc apply hack to all images with png extensions
     *
     * $('#panel, img[@src$=.png]').ifixpng();
     * @desc apply hack to element #panel and all images with png extensions
     *
     * @name ifixpng
     */

    $.fn.ifixpng = hack.ltie7 ? function() {
        return this.each(function() {
            var $$ = $(this);
            var base = $('base').attr('href'); // need to use this in case you are using rewriting urls
            if ($$.is('img') || $$.is('input')) { // hack image tags present in dom
                if ($$.attr('src')) {
                    if ($$.attr('src').match(/.*\.png([?].*)?$/i)) { // make sure it is png image
                        // use source tag value if set
                        var source = (base && $$.attr('src').substring(0, 1) != '/') ? base + $$.attr('src') : $$.attr('src');
					//1.
						var $width = $$.attr('width') == undefined ? $$.width() : $$.attr('width'); // <-- Modified this line 10/29/2008 [Jason]
					//2.
						var $height = $$.attr('height') == undefined ? $$.height() : $$.attr('height'); // <-- Added this line 10/28/2008 [Jason]
						
					//3. Start code block added on 10/28/2008 [Jason]
					if ($height == 0 || $width == 0) { // uses img tag w/h, loaded w/h, if fails: puts image in var then gets w/h
						img = new Image();
						img.src = this.src;
						$width = img.width;
						$height = img.height;
					}
					//3. End code block added on 10/28/2008 [Jason]

				// apply filter
					
					//4. Commented out on 10/28/2008 [Jason] $$.css({ filter: hack.filter(source), width: $$.attr('width') + "px", height: $$.attr('height') + "px" })
					//5. Copied and modified line above on 10/28/2008 [Jason]
						$$.css({ filter: hack.filter(source), width: $width, height: $height })
                                .attr({ src: $.ifixpng.getPixel() })
            //.positionFix();
                    }
                }
            } else { // hack png css properties present inside css
                var image = $$.css('backgroundImage');
                if (image.match(/^url\(["']?(.*\.png([?].*)?)["']?\)$/i)) {
                    image = RegExp.$1;
                    $$.css({ backgroundImage: 'none', filter: hack.filter(image) })
                            .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
     */

    $.fn.iunfixpng = hack.ltie7 ? function() {
        return this.each(function() {
            var $$ = $(this);
            var src = $$.css('filter');
            if (src.match(/src=["']?(.*\.png([?].*)?)["']?/i)) { // get img source from filter
                src = RegExp.$1;
                if ($$.is('img') || $$.is('input')) {
                    $$.attr({ src: src }).css({ filter: '' });
                } else {
                    $$.css({ filter: '', background: 'url(' + src + ')' });
                }
            }
        });
    } : function() {
        return this;
    };

    /**
     * positions selected item relatively
     */

    $.fn.positionFix = function() {
        return this.each(function() {
            var $$ = $(this);
            var position = $$.css('position');
            if (position != 'absolute' && position != 'relative') {
                $$.css({ position: 'relative' });
            }
        });
    };

})(jQuery);

 jQuery(function(){ /* Fixes IE PNG bug */
$('img[src$=".png"], input[type=image], .pngfix').not('.nofix').ifixpng();
});


function isPNGSupported() {
	var supported = true;
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	// *** BROWSER VERSION ***
	if (is_ie) {
		var t = (agt.indexOf("msie"));
		var version = parseInt(agt.substr(t+5,1));
		if (version <= 6) {
		    supported = false;
		}
	} else {
		supported = true;
	}
	return supported;
}

