var bad_browser;
var pixel = new Image();

/**
 * Finds all pngs in document and calls appropriate fix function
 */
$(document).ready(function(){
	bad_browser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == 'Win32');
	pixel.src = 'system/application/assets/images/pixel.gif';

	if (bad_browser)
	{
		// get all pngs
		$('img[src$=.png]').each(function(){
			if ( ! this.complete)
			{
				this.onload = function()
				{
					fix_png(this)
				};
			}
			else
			{
				fix_png(this);
			}
		});

		$('.background_png').each(function(){
			fix_png_background(this);
		});
	}
});

/**
 * Manipulates png to display alpha channel correctly in ie6
 */
function fix_png(png)
{
	var src = png.src;

	if ( ! png.style.width)
	{
		png.style.width = $(png).width();
	}

	if ( ! png.style.height)
	{
		png.style.height = $(png).height();
	}

	// replace by pixel image
	png.src = pixel.src;
	png.onload = function() {};

	// set filter ( display original image )
	png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}


/**
 * Manipulates background png to display alpha channel correctly in ie6
 */
function fix_png_background(element)
{
	background_img = $(element).css('background-image');

	if (background_img.indexOf('.png') != -1)
	{
		src = background_img.replace('url("', '').replace('")', '');
		$(element).css('background-image', 'none');

		// set filter ( display original image )
		element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
	}
}

