// PNGHack 1.0.1 (last change 2009-11-02) (original 2008-06-18)
// Copyright (c) 2008 Yves Van Goethem and png-hack project contributors
// http://code.google.com/p/png-hack/
// Distributed under MIT X11 License
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
document.write('<!--[if lte IE 6]><style type="text/css">.hack-png{visibility:hidden}</style><![endif]-->');
var PNGHack = function(spacer) {
	var remoteUrl = 'http://png-hack.googlecode.com/svn/trunk/spacer.gif';
	var hackMethod = function(obj, method, that) {
		// browser check
		if (that.checkIE && !navigator.userAgent.match(that.checkIE))
			return;
		
		// settings
		if (!obj) obj = {};
		var d	= obj.scope || document;
		var elm = (!obj.elm && method != 2) ? d.all : d.getElementsByTagName(obj.elm);
		var src = obj.src || 'src';
		var ext = new RegExp((obj.ext) ? '\\w+.'+obj.ext.toUpperCase() : '\.PNG$');
		
		// hack core
		var hack = function(elm, source) {
			var style = elm.style;
			if (!elm.pngsrc)
				that.elements.push(elm);
			elm.pngsrc = elm.getAttribute(source);
			style.width = elm.clientWidth;
			style.height = elm.clientHeight;
			style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+elm.pngsrc+"',sizingMethod='scale')";
			
			// using hosted spacer, set it up to rescue local spacer or cancel remotehost
			if (that.remotehost == true)
				spacer = remoteUrl;
			else if (that.remotehost != false) {
				var rescueSpacer = function() {
					elm.setAttribute(source, remoteUrl);
				};
				var setRescueSpacer = function() {
					if (elm.readyState == 'complete') {
						elm.detachEvent('onerror', rescueSpacer);
						elm.detachEvent('onreadystatechange', setRescueSpacer);
					}
				};
				elm.attachEvent('onerror', rescueSpacer);
				elm.attachEvent('onreadystatechange', setRescueSpacer);
			}
			
			// displaying element / removing hack-png class
			elm.setAttribute(source, spacer || 'images/spacer.gif');
			if (elm.className.match(/\bhack-png\b/))
				var x = setTimeout(function(){elm.className = elm.className.replace(/hack-png/g,'');clearTimeout(x);},300);
		};
		
		// hack method
		if (method == 2) {
			hack(obj.elm, src);
			return;
		}
		
		// go and crush method
		for (var i = 0; i < elm.length; ++i) {
			var isrc = elm[i].getAttribute(src) || '';
			if (method == 0 && elm[i].className.match(/\bhack-png\b/)
			|| (method == 1 && isrc.toUpperCase().match(ext)))
				hack(elm[i], src);
		}
	};
	return {
		hackClass : function(obj) {
			hackMethod(obj, 0, this);
		},
		hackExtension : function(obj) {
			hackMethod(obj, 1, this);
		},
		hackElement : function(obj) {
			hackMethod(obj, 2, this);
		},
		elements : [],
		checkIE : 'MSIE 5\.5|MSIE 6',
		remotehost : null
	};
};