// JavaScript Document

// PNGHack 1.0 beta 5 (2008-05-01)
// 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 d = document;
        var remoteUrl = 'http://pnghack.googlepages.com/spacer.gif';
        
        var hackMethod = function(obj, method, that) {
                if (!obj) obj = {};
                var elm = (!obj.elm && method != 2) ? d.all : d.getElementsByTagName(obj.elm);
                var src = obj.src || 'src';
                var ext = obj.ext || '\.PNG$';
                
                // browser check
                var regExp = '('+that.checkIE+')';
                if (!navigator.userAgent.match('MSIE '+regExp) && that.checkIE)
                        return;
                
                // 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
                        elm.setAttribute(source, spacer || 'img/spacer.gif');
                        if (elm.className.match(/\bhack-png\b/))
                                var x = setTimeout(function(){style.visibility = 'visible';clearTimeout(x);},300);
                };
                
                // hack method
                if (method == 2) {
                        hack(obj.elm, src);
                        return;
                }
                
                // go and crush method
                var ext = new RegExp('\\w+.'+ext.toUpperCase());
                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 {
                go : function(obj) {
                        hackMethod(obj, 0, this);
                },
                crush : function(obj) {
                        hackMethod(obj, 1, this);
                },
                hack : function(obj) {
                        hackMethod(obj, 2, this);
                },
                elements : [],  
                checkIE : '5\.5|6',
                remotehost : null
        };
};
