// Copyright 2010 OverInteractive Media Inc. All rights reserved.

/*global $, DR: true, window, Arcade */
/*jslint devel: true */

/**
 * Main file to create the dimeRocker Framework namespace (DR). The conventions
 * and organization of this framework is inspired by the Facebook Connect-JS
 * layout.
 *
 * Extending this namespace for your build:
 *
 * DR.extend('App.Something', overwrite, source);
 *   - overwrite: true/false (optional)
 *   - source: contains new functionality { myfunc : function(...) }
 *   - Results: DR.App.Something.myfunc() ...
 *
 * Notes on `overwrite`
 *
 * DR.extend() has argument over-loading. If the 2nd param is an object
 * overwrite will default to False and the obj. will be used instead.
 *
 * See: http://github.com/facebook/connect-js/blob/master/src/core/prelude.js
 *
 *
 * For readability, code should adhere to the styling described at
 * http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
 *
 * Code should pass both JSLint (http://www.jslint.com/) and Google's Closure
 * Linter (http://code.google.com/closure/utilities/).
 *
 */
if (!window.DR) {
    DR = {
        /**
         * Copies members from source into target.
         *
         * @private
         * @param {Object}  target      Where source will be copied to.
         * @param {Object}  source      The source.
         * @param {Boolean} overwrite   Overwrite target with source.
         */
        copy: function(target, source, overwrite) {
            for (var k in source) {
                if (overwrite || typeof target[k] === 'undefined') {
                    target[k] = source[k];
                }
            }
        },

        /**
         * Creates an object in the namespace.
         *
         * @param   {String}    name    The name of the object (i.e. App.api).
         * @param   {Object}    value   Object to set (optional).
         * @return {Object} The new object.
         */
        create: function(name, value) {
            var base = window.DR,
                nameParts = name ? name.split('.') : [],
                c = nameParts.length;

            for (var i = 0; i < c; i++) {
               var part = nameParts[i],
                   nso = base[part]; // The namespace obj

               // Create the namespace
               if (!nso) {
                   nso = (value && i + 1 === c) ? value : {};
                   base[part] = nso;
               }

               // Move forward
               base = nso;
            }

            // The final created object
            return base;
        },

        /**
         * Logs messages to the console, if the browser has one.
         */
        log: function() {
            if (Arcade.debug && window.console && window.console.log) {
                console.log(Array.prototype.slice.call(arguments));
            }
        },

        /**
         * Extends the DR namespace with a new object.
         *
         * @param {Object|String}   target      The target object to copy to.
         * @param {Boolean}         overwrite   Overwrite target (optional).
         * @param {Object}          source      Source object to copy from.
         * @return {Object} The class.
         */
        extend: function(target, overwrite, source) {
            // Overloading so we can use 2 params instead of 3
            if (typeof overwrite === 'object') {
                source = overwrite;
                overwrite = false;
            }

            // Convert it to an object
            if (typeof target === 'string') {
                target = DR.create(target);
            }

            return DR.copy(target, source, overwrite);
        }
    };
}

