CmdUtils.CreateCommand({ 
  name: "transparent-message",
  homepage: "http://halobrite.com/verbs/",
  author: { name: "Scott Robbin", email: "srobbin@gmail.com"},
  license: "GPL",
  description: "Overrides the native Javascript alert method to use transparent messages. Can also be used as a Ubiquity command to show yourself transparent messages....but that's not nearly as useful.",
  takes: {"input": noun_arb_text},
  preview: "Sends the input to the browser window, as a transparent message.",
  execute: function(input) {
    TransparentMessage.getInstance().show( input.html );
  }
});

// -----------------------------------------------------------------
// OVERRIDE THE ALERT FUNCTION ON PAGE LOAD
// -----------------------------------------------------------------

function pageLoad_overrideAlert() {
  CmdUtils.getWindowInsecure().alert = function(str) { TransparentMessage.getInstance().show(str); };
};

// -----------------------------------------------------------------
// TRANSPARENT MESSAGE
//  
// Based off of Michael Heilemann's Humanized Messages jQuery Plugin
//      http://code.google.com/p/humanmsg/
//
// Modified for use in Ubiquity by Scott Robbin
// -----------------------------------------------------------------

TransparentMessage =  (function() {
    var uniqueInstance; // Private attribute that holds singleton instance.
    var msgOpacity = 0.8;
    
    function _constructor() {
        // Private members
        var _T_Event;
        var _T_GoAway;
    
        // Private functions
        function _setup(callback) {
            var cssMsg = {  position: "fixed",
                            top: "130px",
                            left: "25%",
                            width: "50%",
                            color: "white",
                            textAlign: "center",
                            opacity: "0",
                            zIndex: "100000",
                            display: "none"
            };
                         
            var cssRBox = { display: "block",
                            zIndex: "-10"
            };
            
            var cssR = [ {margin: "0 5px"},
                         {margin: "0 3px"},
                         {margin: "0 2px"},
                         {margin: "0 1px", height: "2px"}
            ];
            var cssRAll = { display: "block",
                            height: "1px",
                            overflow: "hidden",
                            background: "black"
            };
            
            var cssContent = { width: "100%",
                               background: "black",
            };
            
            var cssContentInner = { display: "block",
                                    color: "white",
                                    textAlign: "center",
                                    margin: "0 auto",
                                    padding: "15px",
                                    fontSize: "22px",
                                    fontFamily: "arial, sans-serif",
                                
            };
            
            // Create the elements
            var msgBox = _createElement("div", "transparent-msg", cssMsg);
            
            var rTop = _createElement("b", "rtop", cssRBox);
            for(i=1; i<=4; i++) {
                var r = _createElement("b", "r" + i, jQuery.extend(cssR[i-1], cssRAll) );
                rTop.appendChild(r);
            }
            msgBox.appendChild(rTop);
            
            var content = _createElement("div", "transparent-msg-content", cssContent);
            var contentInner = _createElement("div", "transparent-msg-content-inner", cssContentInner);
            content.appendChild(contentInner);
            msgBox.appendChild(content);
            
            var rBottom = _createElement("b", "rbottom", cssRBox);
            for(j=4; j>0; j--) {
                var r = _createElement("b", "r" + j, jQuery.extend(cssR[j-1], cssRAll) );
                rBottom.appendChild(r);
            }
            msgBox.appendChild(rBottom);
            
            CmdUtils.getDocumentInsecure().body.appendChild(msgBox);
            
            callback();
        };
        
        function _createElement(type, id, css) {
            var el = CmdUtils.getDocumentInsecure().createElement(type);
            if(id) jQuery(el).attr("id", id);
            if(css) jQuery(el).css(css);
            
            return el;
        }
        
        function _clear() {
            jQuery(window).unbind("mousemove").unbind("click").unbind("keypress");
            Utils.clearTimeout( TransparentMessage.getInstance()._T_GoAway );
            Utils.clearTimeout( TransparentMessage.getInstance()._T_Event );
        }
        
        return {
            show: function(msg) {
                _clear();
                var doc = CmdUtils.getDocumentInsecure();
            
                if(!msg) return;
                if(!doc.getElementById("transparent-msg")) return _setup(function() {
                    TransparentMessage.getInstance().show(msg);
                });
                msgBox = doc.getElementById("transparent-msg");
                contentInner = doc.getElementById("transparent-msg-content-inner");
                msg = msg.replace(/\\n/gi, "<br />"); // Replace the line return with <br /> elements
                jQuery(contentInner).html( msg );
                jQuery(msgBox).show().animate({opacity: "0.8"}, 200);
                
                _T_Event = Utils.setTimeout( function() {
                  jQuery(window).bind("mousemove click keypress", function() { TransparentMessage.getInstance().hide(); } )
                }, 1500);
                _T_GoAway = Utils.setTimeout( function(){ TransparentMessage.getInstance().hide(); }, 5000);
            },
            
            hide: function() {
                _clear();
                var doc = CmdUtils.getDocumentInsecure();
           
                msgBox = doc.getElementById("transparent-msg");
                jQuery(msgBox).animate({opacity: "0.0"}, 500);
                
            }
        };
    };

    return  {
        getInstance: function() {
            if(!uniqueInstance) {
                uniqueInstance = _constructor();
            }
            return uniqueInstance;
        }
    };
    
})();