var console = console || {
     log:function(){},
     warn:function(){},
     error:function(){}
};

function get_px(px) {
    return ($.client.ios) ? px : (window.outerWidth / 320) * px;
}

// Take a query string and return and object.
String.prototype.toParameters = function() {
    if (this.indexOf('=') == -1) {
        return this;
    }
    var params = {};

    this.split("&").forEach(function(pair) {
        var s = pair.split("=");
        params[decodeURIComponent(s[0].replace(/\+/g, ' '))] = decodeURIComponent(s[1].replace(/\+/g, ' '));
    });
    return params;
}


// Replace values in a string, e.g. `"foo {boo}".format({boo: 'hoo'}) = 'foo hoo'`.
//
//     @param {Object} object
String.prototype.format = function(hash, translate_key) {
    translate_key = translate_key || false;
    var s = this;
    for (var key in hash) {
        var re = new RegExp('\\{' + (key) + '\\}','gm');
        s = s.replace(re, hash[key]);
    }
    return s;
}

// Capitalize the first character of a string.
String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.substr(1).toLowerCase();
}

// Sets the object to which `this` refers to.
Function.prototype.bind = function() {
    if (arguments.length < 2 && typeof arguments[0] == "undefined") return this;
    var __method = this, args = $A(arguments), object = args.shift();
    return function() {
        return __method.apply(object, args.concat($A(arguments)));
    }
}

// Take a DOM node collection and return an `Array`
function $A(arraylike) {
    if (!arraylike) return [];
    var length = arraylike.length || 0, results = new Array(length);
    while (length--) results[length] = arraylike[length];
    return results;
}

// Takes an object and returns the type.
function type_of(v) {
  if (typeof(v) == "object") {
    if (v === null) return "null";
    if (v.constructor == (new Array).constructor) return "array";
    if (v.constructor == (new Date).constructor) return "date";
    if (v.constructor == (new RegExp).constructor) return "regex";
    return "object";
  }
  return typeof(v);
}

function macrojungle(tmpl) {
    // This is kind of stupid.
    // But at least it's only written once.
    var html = $('#tmp').empty().microjungle(tmpl).html();
    // If we don't actually want to get any html back (just a string),
    // we wrap the string in <tmp>string</tmp> and then remove
    // the tags here.
    return html.replace(/<(\/){0,1}tmp>/g, '');
}