/*
 *	Hack to enable transparent PNGs in IE 6 for both inline and background images
 *
 *	requires sniff.js & utilities.js
 */

var temp = 3;

if (is_ie & !is_ie7up) {
	
	// This must be a path to a blank image. That's all the configuration you need.
	if (typeof blankImg == 'undefined')
	{
		var blankImg = '/files/images/blank.gif';  // TODO: make absolute path
	}
	
	function fnLoadPngs() {
		for (var i = document.all.length - 1, obj = null; (obj = document.all[i]); i--) {
			var test1 = !(obj.event && !/(background|src)/.test(obj.event.propertyName));
			
			// make sure element has background image set or is an image or image submit
			// exclude if background is anthing but no-repeat (has positioning or repeating, skip as this would have to be hacked separately for IE6 anyways)
			var bgImg = obj.currentStyle.backgroundImage || obj.style.backgroundImage;
			var bgRepeat = obj.currentStyle.backgroundRepeat || obj.style.backgroundRepeat;
			var bgPos = obj.currentStyle.backgroundPositionY || obj.style.backgroundPositionY;
			var imgIsPNG = bgImg.match(/\.png/i) != null;
			var test2 = ((bgImg && bgImg != 'none' && bgRepeat && bgRepeat == 'no-repeat' && bgPos && bgPos == '0%' && imgIsPNG) || obj.tagName == 'IMG' || obj.tagName == 'INPUT');
			// exclude any element that has a filter applied
			var test3 = !obj.currentStyle.filter;
			
			if (false && test3 && bgPos && temp > 0) {
				temp--;
				alert("found: " + bgPos + " >> " + obj.outerHTML.substring(0, 20));
			}
			if (test1 && test2 && test3) {
				if (false) {
					alert("applying filter to: " + "(" + obj.outerHTML.substring(0, 20) + ")");
				}
				doPNGFix(obj);
				obj.attachEvent("onpropertychange", fnPropertyChanged);
			}
		}
	}
	
	function fnPropertyChanged() {
		if (window.event.propertyName == "style.backgroundImage") {
			var el = window.event.srcElement;
			if (!el.currentStyle.backgroundImage.match(/^url[("']+(.*\.png)[)"']+$/i)) {
				var bg	= el.currentStyle.backgroundImage;
				var src = bg.substring(5,bg.length-2);
				el.filters.item(0).src = src;
				el.style.backgroundImage = "url(" + blankImg + ")";
			}
		}
	}

/*
function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    ("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
("<li>["+p+"] => "+typeof(theObj)+"</li>");
        ("<ul>")
        print_r(theObj[p]);
        ("</ul>")
      } else {
("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    ("</ul>")
  }
}

*/

	function doPNGFix(obj) {
		// IE5.5+ PNG Alpha Fix v1.0RC4
		// (c) 2004-2005 Angus Turnbull http://www.twinhelix.com
		
		// This is licensed under the CC-GNU LGPL, version 2.1 or later.
		// For details, see: http://creativecommons.org/licenses/LGPL/2.1/
			
		var f = 'DXImageTransform.Microsoft.AlphaImageLoader';
		function filt(obj, s, m)
		{
			if (obj.filters[f])
			{
				obj.filters[f].enabled = s ? true : false;
				if (s)
				{
					with (obj.filters[f])
					{
						obj.src = s;
						sizingMethod = m;
					}
				}
			}
			else if (s)
			{
				obj.style.filter = 'progid:'+f+'(src="'+s+'",sizingMethod="'+m+'")';
			}
		}
	
		var bgImg = obj.currentStyle.backgroundImage || obj.style.backgroundImage;
		var listItemImg = obj.currentStyle.listStyleImage || obj.style.listStyleImage;
	
		if (obj.tagName == 'IMG')
		{
			if ((/\.png$/i).test(obj.src))
			{
				if (obj.currentStyle.width == 'auto' && obj.currentStyle.height == 'auto')
				{
					obj.style.width = obj.offsetWidth + 'px';
				}
				filt(obj, obj.src, 'scale');
				obj.src = blankImg;
			}
			else if (obj.src.indexOf(blankImg) < 0)
			{
				filt(obj);
			}
		}
		else if (bgImg && bgImg != 'none')
		{
			if (bgImg.match(/^url[("']+(.*\.png)[)"']+$/i))
			{
				var s = RegExp.$1;
				if (obj.currentStyle.width == 'auto' && obj.currentStyle.height == 'auto')
				{
					obj.style.width = obj.offsetWidth + 'px';
				}
				obj.style.backgroundImage = 'none';
				filt(obj, s, 'crop');
				// IE link fix.
				for (var n = 0; n < obj.childNodes.length; n++)
				{
					if (obj.childNodes[n].style)
					{
						obj.childNodes[n].style.position = 'relative';
					}
				}
			}
			else
			{
				filt(obj);
			}
		}
		// TODO: fix list-style-image
		
		// add property changed listener in case the image is changed
		obj.attachEvent("onpropertychange", fnPropertyChanged);
	}

	addLoadEvent(fnLoadPngs);
	
	// finally, add behavior to all images that might be included via AJAX or added via javascript
	
}
