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

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

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

        if( str === "" ) {
            status.setStyle("display", "none");
        } else {
            status.setHTML(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", {styles: {
            position  : "fixed",
            left      : "0px",
            top       : "0px",
            width     : "100%",
            height    : "100%",
            display   : "none",
            background: "#000",
            zIndex    : 12,
            opacity   : 0.5
        }}).inject( document.body );

        this.addFixed( document.$overlay );

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

    redraw: function() {
        // force a redraw, fixes display errors in firefox
        if( !redraw ) {
            redraw = new Element( "div", {styles: {
                width       : "100px",
                height      : "100px",
                display     : "none",
                position    : "absolute",
                left        : "20px",
                top         : "80px"
            }}).inject( document.body );
        }
        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", {styles: {
                whiteSpace  : "pre",
                position    : "absolute",
                display     : "none",
                width       : "200px",
                minHeight   : "100px",
                border      : "1px solid black",
                background  : "rgb( 255,255,220 )",
                zIndex      : "10",
                padding     : "5px"
            }}).inject( document.body );
        }
        event = new Event(event);
        info.setStyles( {
            display : 'block',
            left    : event.page.x + 10,
            top     : event.page.y + 10
        } ).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", {styles: {
            position    : "fixed",
            top         : "100px",
            left        : "0px",
            width       : "0px",
            height      : "0px",
            minWidth    : "1px",
            minHeight   : "20px",
            height      : "0px",
            lineHeight  : "0px",
            fontSize    : "0px"
        }});

        document.$body.adopt( 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", {styles: {
                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.adopt( div );

            document.$body = div;

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

