/**
 * An alpha transparent png support library for Internet Explore (other browsers
 * support this functionality natively) now compatible with Mootools.
 *
 * @author Toby Miller <tmiller@tobymiller.com>
 * @copyright Copyright (C) 2005, Toby Miller
 * @license MIT
 */
var AlphaPng = new Class({
	/**
	 * defaultOptions
	 * Options used in typical implementations
	 *
	 * debug:			0 = production, 1 = development
	 * clearImage:		url location of a transparent image
	 * backgroundTags:	allowable html tags for alpha transparent backgrounds
	 */
	 defaultOptions: {
		'debug':			0,
		'clearImage':		'/images/global/spacer.gif',
		'backgroundTags':	['div', 'table', 'td', 'a']
	},

	/**
	 * initialize
	 * Initialize an instance of the AlphaPng object
	 *
	 * @param mixed array or object representation of options
	 * @return void
	 */
	initialize: function(options) {
		if (window.ie6)
		{
			// Merges the default options with the ones given as parameters
			this.setOptions($merge(this.defaultOptions, options));
			
			// Execute
			this.fixImages();
			this.fixBackgrounds();
		}
	},

	/**
	 * fixImages
	 * Fixes foreground images that are using alpha transparent pngs
	 *
	 * @param void
	 * @return void
	 */
	fixImages: function()
	{
		if (window.ie6)
		{
			$ES('img[src$=png]').each(function(img){
				img.setStyles({
					'filter':	'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=\'true\', src=\'' + img.getProperty('src') + '\', sizingMethod=\'scale\')',
					'width':	img.getProperty('width'),
					'height':	img.getProperty('height')
				});
				img.setProperty('src', this.options.clearImage);
			}.bind(this));
		}
	},
	
	/**
	 * fixBackgrounds
	 * Fixes background images that are using alpha transparent pngs
	 *
	 * @param void
	 * @return void
	 */
	fixBackgrounds: function()
	{
		if (window.ie6)
		{
			var reBackground = new RegExp('[^\(\)"\']+\.png', 'i');
			for (var i = 0; i < document.styleSheets.length; i++){
				for (var j = 0; j < document.styleSheets[i].rules.length; j++){
					var background = reBackground.exec(document.styleSheets[i].rules[j].style.backgroundImage);
					if (background){
						document.styleSheets[i].rules[j].style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=\'true\', sizingMethod=\'scale\', src=\'' + document.styleSheets[i].rules[j].style.backgroundImage + '\')';
						document.styleSheets[i].rules[j].style.backgroundImage = 'none';
					}
				}
			}

			$ES(this.options.backgroundTags.join(',')).each(function(tag){
				if (new RegExp('\\.png["\']\\)', 'i').exec(tag.getStyle('background-image'))){
					tag.setStyle('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=\'true\', sizingMethod=\'scale\', src=\'' + tag.getStyle('background-image') + '\')');
					tag.setStyle('background-image', 'none');
				}
			}.bind(this));
		}
	}
});

// Adds options management support
AlphaPng.implement(new Events, new Options);
new AlphaPng();