﻿(function ()
{
    var _root_namespace = 'Publicis';
    var _init_methods = [];

    window[_root_namespace] = window[_root_namespace] || {};

    window[_root_namespace].namespace = function (namespace_str, data, initName)
    {
        var spaces = namespace_str.split('.');
        var spaceStack = [window];
        for (var i = 0; i < spaces.length; i++)
        {
            spaceStack[spaceStack.length - 1][spaces[i]] = spaceStack[spaceStack.length - 1][spaces[i]] || {};
            spaceStack.push(spaceStack[spaceStack.length - 1][spaces[i]]);
        }

        var data_obj = null;
        if (typeof data === 'function')
            data_obj = data();
        else if (typeof data === 'object')
            data_obj = data;

        if (data_obj)
        {
            var destination_space = spaceStack.pop();
            for (var key in data_obj)
            {
                destination_space[key] = data_obj[key];
            }

            if (initName && typeof data_obj[initName] === 'function')
            {
                _init_methods.push(data_obj[initName]);
            }
        }
    };

    if (typeof Function.prototype.extend !== 'function')
    {
        Function.prototype.extend = function (superClass)
        {
            var F = function () { };
            F.prototype = superClass.prototype;
            this.prototype = new F();
            this.prototype.constructor = this;

            this.superclass = superClass.prototype;
            if (superClass.prototype.constructor == Object.prototype.constructor)
                superClass.prototype.constructor = superClass;
        };
    }

    if (typeof Function.prototype.method !== 'function')
    {
        Function.prototype.method = function (name, func)
        {
            this.prototype[name] = func;
            return this;
        };
    }

    $(document).ready(function ()
    {
        if (_init_methods.length > 0)
        {
            $.each(_init_methods, function (idx, val)
            {
                val();
            });
        }
    });
})();

