if (typeof Pubsmith == "undefined" || !Pubsmith) {
    var Pubsmith = {};
}

/**
 * From Yahoo YUI
 * @method namespace
 * @static
 * @param  {String*} arguments 1-n namespaces to create
 * @return {Object}  A reference to the last namespace object created
 */
Pubsmith.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=(""+a[i]).split(".");
        o=Pubsmith;

        // Pubsmith is implied, so it is ignored if it is included
        for (j=(d[0] == "Pubsmith") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }

    return o;
};

Pubsmith.events = $({});

function $$(tagName) {

    var max = arguments.length - 1, options = {}, $ele = null, match = null;
    if (typeof arguments[max] == 'object' && !(arguments[max].html)) {
        options = arguments[max--];
    }

    if (match = /^([\w-]+)(#([\w-]+))?((\.[\w-]+)*)$/.exec(tagName)) {

        var $ele = jQuery(document.createElement(match[1]));

        if (match[3]) $ele[0].id = match[3];
        if (match[4]) $ele[0].className = match[4].replace(/\./g, ' ');
        //console.log(options);
        for (var k in options) {
            if (k == 'hover' && options[k] instanceof Array) {
                $ele.hover(options[k][0], options[k][1]);
            } else if (jQuery.isFunction(options[k])) {
                $ele.bind(k, options[k]);
            } else {
                $ele.attr(k, options[k])
            }
        }

        for (var i = 1; i <= max; i++) $ele.append(arguments[i]);

    }

    return $ele;

};

/** utility from http://stackoverflow.com/a/2441972 */
Pubsmith.stringToFunction = function(str) {
  var arr = str.split(".");

  var fn = (window || this);
  for (var i = 0, len = arr.length; i < len; i++) {
    fn = fn[arr[i]];
  }

  if (typeof fn !== "function") {
    throw new Error("function not found");
  }

  return  fn;
};
