/*
 * PNG-TR v1.1.0 RC2
 *
 * 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 = {
// You should change the following line to a transparent gif (1x1px) on your server.
	blank:  '../img/blank.gif',

	filter: 'DXImageTransform.Microsoft.AlphaImageLoader',

// You can delete the '|7' string, if you don't want this to fix at IE7.
	needFix: /MSIE (5\.5|6|7)/.test(navigator.userAgent),

// To fix mouse events, but not used at this time. (from IEPNGFIX.htc)
	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';
			if (!elem.style.zoom) elem.style.zoom = 1;
// I'm not sure if these are needed or not. (from IEPNGFIX.htc)
//			elem.style.height = elem.clientHeight + 'px';
//			if (elem.currentStyle.display == 'inline') elem.style.display = 'inline-block';
		}
	},

// To fix mouse events within the element
	fixchild: function(elem, recursive) {
		if (!this.needFix) return;
		
		var isPositioningInside = false;

		for (var i = 0, n = elem.childNodes.length; i < n; i++) {
			var childNode = elem.childNodes[i];

			if (recursive && childNode.hasChildNodes()) {
				if (this.fixchild(childNode, recursive)) {
					isPositioningInside = true;
					continue;
				}
			}

			if (childNode.style /*&& childNode.tagName && this.needClick(childNode.tagName)*/) {
				if (/absolute|relative|fixed/.test(childNode.currentStyle.position)) {
					isPositioningInside = true;
				}
				else {
					childNode.style.position = 'relative';
					if (!childNode.style.zIndex) childNode.style.zIndex = 1;
				}
			}
		}
		
		return isPositioningInside;
	},

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

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

		var fixed = false;

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

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

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

			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) && (/\.png$/i).test(elem.src))) {
//			this.fixwidth(elem);
			this.fixit(elem, elem.src, 'scale');
			elem.src = this.blank;
			fixed = true;
		}

		if (fixed) {
			elem.onpropertychange = this.onchange;
		}
		return onchange ? false : 'none';
	},

// To support changing properties and class-name by CSS and JavaScript.
	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')