// See COPYING for copyright and license details
(function () {
    Object.defineProperties(util, 
    { 
        "clone" : {
            value : function(o)  {
                return JSON.parse(JSON.stringify(o));
            }
        },
        "getSelection" : 
        {
            value : function() 
            {
                var frames = tabs.current.allFrames;
                for (var i=frames.length-1; i>=0; --i) {
                    var selection = JSON.parse(frames[i].inject("return document.getSelection().toString()"));
                    if (selection && selection.length > 0)
                        return selection;
                }
                return null;
            }
        },
        "uncamelize" : 
        {
            value : function(text) 
            {
                if (! text || text.length === 0)
                    return text;
                return text.replace(/(.)?([A-Z])/g, function(x, s, m) { 
                    return s ? s + "-" + m.toLowerCase() : m.toLowerCase(); 
                });
            }
        },
        "camelize" : 
        {
            value : function(text) 
            {
                if (! text || text.length === 0)
                    return text;
                return text.replace(/[-_]+(.)?/g, function(a, b) { 
                    return b ? b.toUpperCase() : ""; 
                });
            }
        },
        "mixin" : 
        {
            value : function(arg)
            {
                var i, l, key, o;
                var self = arg || {};
                for (i=1, l=arguments.length; i<l; i++)
                {
                    o = arguments[i];
                    for (key in o)
                    {
                        if (!self.hasOwnProperty(key)) 
                        {
                            self[key] = o[key];
                        }
                    }
                }
                return self;
            }
        }, 
        "encodeUTF8" : 
        {
            value : function(string)
            {
                return unescape(encodeURIComponent(string));
            }
        },
        "decodeUTF8" : 
        {
            value : function(string)
            {
                return decodeURIComponent(escape(string));
            }
        },
        "base64Encode" : 
        {
            value : function(string)
            {
                return util._base64Encode(util.encodeUTF8(string));
            }
        },
        "base64Decode" : 
        {
            value : function(string)
            {
                return util.decodeUTF8(util._base64Decode(string));
            }
        },
        "normalMode" : 
        {
            value : function(string)
            {
                return util.changeMode(Modes.NormalMode);
            }
        },
        "insertMode" : 
        {
            value : function(string)
            {
                return util.changeMode(Modes.InsertMode);
            }
        },
        "caretMode" : 
        {
            value : function(string)
            {
                return util.changeMode(Modes.CaretMode);
            }
        },
         "hereDoc" : 
         {
             value : function(func)
             {
                 return this.getBody(func).replace(/\s*\/\*HEREDOC([\s\S]*)\s*HEREDOC\*\/\s*;?/, "$1");
             }
         }, 
         search : {
             value : function(keyword, searchString, newTab) {
                 var command = newTab ? "tabopen" : "open";
                 data.parse("searchEngines").some(function(engine) {
                     if (engine.keyword == keyword) {
                         execute(command + " " + engine.keyword + " " + searchString);
                         return true;
                     }
                     return false;
                 });
             }
         }
    });
    Object.freeze(util);
    
    if (Object.prototype.forEach === undefined) 
    {
        Object.defineProperty(Object.prototype, "forEach", 
        { 
            value : function (callback) 
            {
                var key;
                for (key in this) 
                {
                    if (this.hasOwnProperty(key))
                        callback(key, this[key], this); 
                }
            }
        });
    }
})();
