/*
 * PNG-TR v1.1.0 RC1
 *
 * Copyright (c) 2006-2008 Takashi Aida http://yungsang.com/
 *
 */

// IE5.5+ PNG Alpha Fix v1.0RC5 Preview 5
// (c) 2004-2008 Angus Turnbull http://www.twinhelix.com

// This is licensed under the GNU LGPL, version 2.1 or later.
// For details, see: http://creativecommons.org/licenses/LGPL/2.1/

// 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/

if (typeof PNGTR == 'undefined') {
//--============================================================================

var PNGTR = {
	blank:  'http://yungsang.com/images/blank.gif',
	filter: 'DXImageTransform.Microsoft.AlphaImageLoader',

	needFix: /MSIE (5\.5|6\.|7\.)/.test(navigator.userAgent),

	needClick: function (tagName) {
		return /A|INPUT|SELECT|TESTAREA|IFRAME|OBJECT/.test((tagName));
	},

	fixit: function (elem, src, method) {
		if (elem.filters[this.filter]) {
			var filter = elem.filters[this.filter];
			filter.enabled = true;
			filter.src = src;
			filter.sizingMethod = method;
		}
		else {
			elem.style.filter = 'progid:' + this.filter +
				'(src="' + src + '",sizingMethod="' + method + '")';
		}
	},

	fixwidth: function(elem) {
		if (elem.currentStyle.width == 'auto' &&
			elem.currentStyle.height == 'auto') {
			elem.style.width = elem.offsetWidth + 'px';
//			elem.style.height = elem.clientHeight + 'px';
//			if (elem.currentStyle.display == 'inline') elem.style.display = 'inline-block';
		}
	},

	fixchild: function(elem, recursive) {
		if (!this.needFix) return;

		for (var i = 0, n = elem.childNodes.length; i < n; i++) {
			var childNode = elem.childNodes[i];
			if (childNode.style /*&& childNode.tagName && this.needClick(childNode.tagName)*/) {
				if (!/absolute|relative/.test(childNode.currentStyle.position)) {
					childNode.style.position = 'relative';
				}
			}
			if (recursive && childNode.hasChildNodes()) {
				this.fixchild(childNode, recursive);
			}
		}
	},

	fix: function(elem, mode, onchange) {  // mode: ['crop' | 'image' | 'scale']
		if (!this.needFix) return;
		
		if (!mode) {
			var bgRepeat =
				elem.currentStyle.backgroundRepeat || elem.style.backgroundRepeat;

			mode = (bgRepeat == 'no-repeat') ? 'crop' : 'scale';
		}

		var fixed = false;
		elem.mode = mode;

		var bgImg =
			elem.currentStyle.backgroundImage || elem.style.backgroundImage;

		if (bgImg) {
			if (bgImg.match(/^url[("']+(.*\.png[^\)"']*)[\)"']+[^\)]*$/i)) {
				var bgImg = RegExp.$1;

				if (!/IMG|INPUT/.test(elem.tagName)) this.fixwidth(elem);
				elem.style.backgroundImage = 'url(' + this.blank + ')';
				this.fixit(elem, bgImg, mode);

				if (elem.tagName == 'A' && elem.style) {
					if (!elem.style.cursor) {
						elem.style.cursor = 'pointer';
					}
				}

				this.fixchild(elem, true);

				fixed = true;
			}
		}


		if (/IMG|INPUT/.test(elem.tagName)) {
			if ((/\.png$/i).test(elem.src)) {
//				this.fixwidth(elem);
				this.fixit(elem, elem.src, 'scale');

				elem.src = this.blank;

				elem.mode = 'scale';
				fixed = true;
			}
		}

		if (fixed) {
			elem.onpropertychange = this.onchange;
		}
		return onchange ? false : 'none';
	},
	
	onchange: function() {
		switch (event.propertyName) {
		case 'style.backgroundImage':
		case 'src':
			break;
		default:
			if ((this._pngtr_className || this.className) && (this._pngtr_className != this.className)) {
				break;
			}
			return;
		}

		this._pngtr_className = this.className;

		switch (event.propertyName) {
		case 'className':
		case 'style.behavior':
			if ((this.style.backgroundImage != '') &&
				(this.style.backgroundImage != ('url(' + PNGTR.blank + ')'))) return;
			var elem = this;
			if (this.timer) clearTimeout(this.timer);
			this.timer = setTimeout(function() {
				elem.timer = null;
				elem.style.backgroundImage = '';
			}, 0);
			return;
		case 'style.backgroundImage':
			if (this.style.backgroundImage == ('url(' + PNGTR.blank + ')')) return;
		case 'src':
			if (this.src == PNGTR.blank) return;
			this.style.filter = '';
			PNGTR.fix(this, this.mode, true);
			return;
		}
	}
};

//--============================================================================
} // end if (typeof PNGTR == 'undefined')

