var console = console || {
log:function(){},
warn:function(){},
error:function(){}
};
// Convert from device-independent px to device px.
var get_device_px = function(px) {
return (Application.client.ios || navigator.userAgent.match(/Intel Mac/i)) ? px : (window.outerWidth / 320) * px;
};
var get_viewport_dims = function(){
var dim = {
'width': (window.innerWidth || document.documentElement.clientWidth || document.body.offsetWidth),
'height': (window.innerHeight || document.documentElement.clientHeight || document.body.offsetHeight)
};
return dim;
};
// 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 string and then remove
// the tags here.
return html.replace(/<(\/){0,1}tmp>/g, '');
}
// Convert from device-independent px to device px.
var get_device_px = function(px) {
return (Application.client.ios || navigator.userAgent.match(/Intel Mac/i)) ? px : (window.outerWidth / 320) * px;
};
var get_viewport_dims = function(){
var dim = {
'width': (window.innerWidth || document.documentElement.clientWidth || document.body.offsetWidth),
'height': (window.innerHeight || document.documentElement.clientHeight || document.body.offsetHeight)
};
return dim;
};
// A dumb method to flip Plural => Singular.
String.prototype.singularize = function() {
return this.slice(0, -1);
};
String.prototype.contains = function(str){
return (this.indexOf(str) !== -1) ? true : false;
};
// zepto/querySelector, understandably, can't deal with periods in the id attr.
String.prototype.dotToBar = function() {
return this.replace(/\./g,"_");
};
String.prototype.barToDot = function() {
return this.replace(/_/g, '.');
};
// ======================
// = Element Prototypes =
// ======================
// highlight changes
Element.prototype.hilite = function() {
var el = this;
$(el).addClass('hilite');
// the 3rd param is the context. does not work in IE but we don't care. cstuart 2010-03-18
window.setTimeout(function() {
$(el).removeClass('hilite');
}, 2200, el);
};
// =====================
// = Number Prototypes =
// =====================
Number.prototype.isInt = function() {
return this % 1 === 0;
};
// =====================
// = Utility Functions =
// =====================
// Takes an object and returns the type.
// var type_of = function(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";
// if (v.constructor == (new String).constructor) return "string";
// if (v.constructor == (new Number).constructor) return "number";
// return "object";
// }
// return typeof(v);
// };
$.fixImage = function(elem, src) {
if (!elem.fixed) {
elem.src = src;
} else {
elem.style.visibility = 'hidden';
}
elem.fixed = true;
};
var macrojungle = function(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 string and then remove
// the tags here.
return html.replace(/<(\/){0,1}tmp>/g, '');
};