// ISSUES
// 
// If you run into a problem where you can not click on a link or an input
// that is within a div with a png background, then create a nested div with
// the position style set to reltive (tested) or absolute (not tested)

var bgsleight	= function() {
	
	function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			}
		}
	}
	
	function fnLoadPngs() {
		var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
		var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

        // do the images		
	    for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
		    if (itsAllGood && img.src.match(/\.png$/i) != null) {
			    fnFixPng(img);
			    img.attachEvent("onpropertychange", fnPropertyChanged);
		    }
		    img.style.visibility = "visible";
	    }

        // do the inputs
	    var nl = document.getElementsByTagName("INPUT");
	    for (var i = nl.length - 1, e = null; (e = nl[i]); i--) {
	    
		    if (e.className && e.className.match(/\bimage\b/i) != null) {
			    if (e.src.match(/\.png$/i) != null) {
				    fnFixPng(e);
				    e.attachEvent("onpropertychange", fnPropertyChanged);
			    }
			    e.style.visibility = "visible";
		    } else if (e.type && e.type.match(/\bimage\b/i) != null) {
			    if (e.src && (e.src.match(/\.png$/i) != null)) {
				    fnFixPng(e);
				    e.attachEvent("onpropertychange", fnPropertyChanged);
			    }
			    e.style.visibility = "visible";		    
			}
	    }		
		
		// do the backgrounds
		for (var i = document.all.length - 1, obj = null; (obj = document.all[i]); i--) {
			if (itsAllGood && obj.currentStyle.backgroundImage.match(/\.png/i) != null) {
			
				fnFixBackgroundPng(obj);
				obj.attachEvent("onpropertychange", fnPropertyChanged);
			}
		}
	}

	function fnPropertyChanged() {
		if (window.event.propertyName == "style.backgroundImage") {
			var el = window.event.srcElement;
			if (!el.currentStyle.backgroundImage.match(/spacer\.gif/i)) {
				var bg	= el.currentStyle.backgroundImage;
				var src = bg.substring(5,bg.length-2);
				if (el.filters.item(0)) {
				    el.filters.item(0).src = src;
				}
				el.style.backgroundImage = "url(/images/spacer.gif)";
			}
		} else if (window.event.propertyName == "src") {
		    var el = window.event.srcElement;
		    if (!el.src.match(/spacer\.gif$/i)) {
		        if (el.filters.item(0)) {
			        el.filters.item(0).src = el.src;
			    }
			    el.src = "/images/spacer.gif";
		    }	
		}
	}

    function fnFixPng(img) {
	    var src = img.src;
	    img.style.width = img.width + "px";
	    img.style.height = img.height + "px";
	    if (img.id.indexOf('__') == 0)
	    {
	        var myArray = img.id.split("__");
	        img.style.width = myArray[1] + "px";
	        img.style.height = myArray[1] + "px";	    
	    }
	    img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
	    img.src = "/images/spacer.gif";
    }

	function fnFixBackgroundPng(obj) {

		var bg	= obj.currentStyle.backgroundImage;
		var src = bg.substring(5,bg.length-2);
		obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
		obj.style.backgroundImage = "url(/images/spacer.gif)";
		
	}
	
	
	
	
	return {
		
		init: function() {
        
			
			if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
				addLoadEvent(fnLoadPngs);
			}
			
		}
	}
	
}();

bgsleight.init();


