﻿// NOTE: This code was taken from: http://24ways.org/2007/supersleight-transparent-png-in-ie6
// It seems better than the "iepngfix.htc" fix, which messes up DIV sizes when no fixed width has been set.
// Only slight flaw is a delay before images get made transparent. Can it be sped up somehow?
var supersleight = function() {

	var root = false;
	var applyPositioning = true;

	// Path to a transparent GIF image
	var supersleight_shim = '/Images/Common/spacer.gf';
	
	var shim = supersleight_shim; //'Images/Blank.gif';

	// RegExp to match above GIF image name
	var shim_pattern = /x\.gif$/i;

	var fnLoadPngs = function() {
		if (root) {
			root = document.getElementById(root);
		} else {
			root = document;
		}
		
		var oTags = new Array("div", "td");
		
		var oObjs = new Array();
		var nSize = 0;
		for(var i = oTags.length - 1;  i >= 0; --i) {
			var oSubObjs = document.getElementsByTagName(oTags[i])
			for(var j = oSubObjs.length - 1; j >= 0; --j) {
				oObjs[nSize++] = oSubObjs[j];
			}
		}
		
		for (var i = oObjs.length - 1, obj = null; (obj = oObjs[i]); i--) {
//			if(obj && obj.id && obj.id == "oMenu") continue;
			
			// background pngs
			if (obj.currentStyle.backgroundImage.match(/\.png/i) !== null) {
				bg_fnFixPng(obj);
			}
			// image elements
			if (obj.tagName == 'IMG' && obj.src.match(/\.png$/i) !== null) {
				el_fnFixPng(obj);
			}
			// apply position to 'active' elements
			if (applyPositioning && (obj.tagName == 'A' || obj.tagName == 'INPUT') && obj.style.position === '') {
				obj.style.position = 'relative';
			}
		}
	};

	var bg_fnFixPng = function(obj) {
		var mode = 'scale';
		var bg = obj.currentStyle.backgroundImage;
		var src = bg.substring(5, bg.length - 2);
		if (obj.currentStyle.backgroundRepeat == 'no-repeat') {
			mode = 'crop';
		}
		obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')";
		obj.style.backgroundImage = 'url(' + shim + ')';
	};

	var el_fnFixPng = function(img) {
		var src = img.src;
		img.style.width = img.width + "px";
		img.style.height = img.height + "px";
		img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
		img.src = shim;
	};

	var addLoadEvent = function(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			};
		}
	};

	return {
		init: function() {
			addLoadEvent(fnLoadPngs);
		},

		limitTo: function(el) {
			root = el;
		},

		run: function() {
			fnLoadPngs();
		}
	};
} ();

// limit to part of the page ... pass an ID to limitTo:
// supersleight.limitTo('header');

supersleight.init();
