/**
 * singleton utility class
 */
var Utility = (function() {

    var redraw = null;
    var info = null;
    var status;

    return {
    strGfxPath: '../img/',

    status: function(str) {
        if( !status ) {
            status = new Element("div").setStyle({
                position    : "absolute",
                right       : "10px",
                top         : "10px",
                minWidth    : "100px",
                minHeight   : "100px",
                border      : "1px solid black",
                display     : "none",
                background  : "rgb(255,255,220)"
            });
            document.$body.insert(status);
        }

        if( str === "" ) {
            status.setStyle({display: "none"});
        } else {
            status.update(str).setStyle( {display: "block"} );
            this.redraw(); // fix for opera... giving border to other elements
        }

        return this;
    },

    createOverlay: function() {
        if( document.$overlay ) {
            return this;
        }

        document.$overlay = new Element("div").setStyle({
            position  : "fixed",
            left      : "0px",
            top       : "0px",
            width     : "100%",
            height    : "100%",
            display   : "none",
            background: "#000",
            zIndex    : 12,
            opacity   : 0.5
        });
        $(document.body).insert(document.$overlay);

        this.addFixed( document.$overlay );

        //document.$overlayFix = new OverlayFix( document.$overlay );
        return this;
    },

    showOverlay: function() {
        if( !document.$overlay ) {
            this.createOverlay();
        }

        document.$overlay.setStyle( {display: "block"} );
        return this;
    },

    hideOverlay: function() {
        if( document.$overlay ) {
            document.$overlay.setStyle( {display: "none"} );
        }

        return this;
    },

    redraw: function() {
        // force a redraw, fixes display errors in firefox
        if( !redraw ) {
            redraw = new Element( "div").setStyle({
                width       : "100px",
                height      : "100px",
                display     : "none",
                position    : "absolute",
                left        : "20px",
                top         : "80px"
            });
            $(document.body).insert(redraw);
        }
        redraw.setStyle({display: "block"});
        var i = redraw.offsetWidth;
        redraw.setStyle({display: "none"});

        return this;
    },

    hideinfo: function() {
        if( info ) {
            info.setStyle({display: 'none'});
        }

        return this;
    },

    showinfo: function( text, event ) {
        if( !info ) {
            info = new Element( "div").setStyle({
                whiteSpace  : "pre",
                position    : "absolute",
                display     : "none",
                minWidth    : "80px",
                minHeight   : "20px",
                border      : "1px solid black",
                background  : "rgb( 255,255,220 )",
                zIndex      : "10",
                padding     : "5px"
            });
            $(document.body).insert(info);
        }
        event = new Event(event);
        info.setStyle( {
            display : 'block',
            left    : event.page.x + 10+'px',
            top     : event.page.y + 10+'px'
        } ).setText( text );

        return this;
    },

    dump: function( o ) {
        var m = o + '\n\n';
        for( var p in o ) {
            if( o.hasOwnProperty(p)) {
                m += p + '=' + o[ p ] + '\n';
            }
        }
        return m;
    },

    addFixed: function( element ) {
        if( !document.$body ) {
            this.fixedTest();
        }
        return this;
    },

    fixedTest: function() {
        if( document.$body ) {
            return this;
        }
        document.$body = $(document.body);

        var fixedtest = new Element( "div").setStyle({
            position    : "fixed",
            top         : "100px",
            left        : "0px",
            width       : "0px",
            height      : "0px",
            minWidth    : "1px",
            minHeight   : "20px",
            height      : "0px",
            lineHeight  : "0px",
            fontSize    : "0px"
        });

        document.$body.insert( fixedtest );

        window.$minWidth  = !!fixedtest.offsetWidth;
        window.$minHeight = fixedtest.offsetHeight === 20;

        // test if the browser supports position: fixed
        if( fixedtest.offsetTop != 100 ) {
            window.$fixed = false;
            // create the "new" body element
            var div = new Element( "div").setStyle({
                position    : "absolute",
                left        : "0px",
                top         : "0px",
                width       : "100%",
                height      : "100%",
                overflow    : "auto"
            });

            // copy all the elements
            $A( document.$body.childNodes ).each( function( item ) {
                this.appendChild( item );
            }, div );

            document.$body.setStyle({overflow: "hidden"});
            $(document.documentElement).setStyle({overflow: "hidden"});
            document.$body.insert( div );

            document.$body = div;

            Utility.addFixed = function( element ) {
                $(element).setStyle({position: "absolute"});
                $(document.body).insert( $(element) );
                return this;
            };
        } else {
            window.$fixed = true;
        }
        fixedtest.remove();
        return this;
    },

    fixAlpha: function() {
        if( !window.ie6 ) {
            return;
        }

        $$( document.getElementsByName("alphaimg"), 'img.alphaimg' ).each( function( img ) {
            if( img.src.test(/\.png$/) ) {
                img.setStyle({filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.src + "',sizingMethod='crop')"} );
                img.src = this.strGfxPath + "dummy.gif";
            }
        }.bind(this) );
    },

    fixIE: function() {
        this.fixedTest();

        if( !window.ie6 ) {
            return;
        }

        var setStyle = Element.prototype.setStyle;
        var setProperty = Element.prototype.setProperty;

        Element.extend({
            /**
             * Automatically fix minWidth and maxWidth for IE6
             */
            setStyle: function(property, value){
                property = property.camelCase();

                if( property == 'minWidth' ) {
                    property = 'width';
                } else if( property == 'height' ) {
                    property = 'height';
                }

                setStyle.apply( this, [property, value] );
            },

            setProperty: function(property, value){
                if( property == 'src' && this.name == 'alphaimg' ) {
                    this.setStyle( "filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + value + "',sizingMethod='crop')" );
                    this.src = Utility.strGfxPath + "dummy.gif";
                } else {
                    setProperty.apply( this, [property, value] );
                }
            }
        });

        // this has to be called after Element.extend or the elements will be extended with the wrong set* Functions
        this.fixAlpha();
    }

};}());
