String.prototype.format = String.prototype.f = function () {
	var s = this,
		i = arguments.length;

	while (i--) {
		s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
	}
	return s;
};

function navigateTo(url) {
	window.location.href = url;
}

function timeSince(date, returnSeconds) {

	var seconds = Math.floor((new Date() - date) / 1000);

	if (returnSeconds) {
		return seconds;
	}

	var interval = Math.floor(seconds / 31536000);

	if (interval > 1) {
		return interval + " years";
	}
	interval = Math.floor(seconds / 2592000);
	if (interval > 1) {
		return interval + " months";
	}
	interval = Math.floor(seconds / 86400);
	if (interval > 1) {
		return interval + " days";
	}
	interval = Math.floor(seconds / 3600);
	if (interval > 1) {
		return interval + " hours";
	}
	interval = Math.floor(seconds / 60);
	if (interval > 1) {
		return interval + " minutes";
	}
	return Math.floor(seconds) + " seconds";
}


var ajax = {};
ajax.x = function () {
	if (typeof XMLHttpRequest !== 'undefined') {
		return new XMLHttpRequest();
	}
	var versions = [
		"MSXML2.XmlHttp.5.0",
		"MSXML2.XmlHttp.4.0",
		"MSXML2.XmlHttp.3.0",
		"MSXML2.XmlHttp.2.0",
		"Microsoft.XmlHttp"
	];

	var xhr;
	for (var i = 0; i < versions.length; i++) {
		try {
			xhr = new ActiveXObject(versions[i]);
			break;
		} catch (e) {
		}
	}
	return xhr;
};

ajax.send = function (url, callback, method, data, async) {
	if (async === undefined) async = true;
	var x = ajax.x();
	x.open(method === 'POST-RAW' ? 'POST': method, url, async);
	x.onreadystatechange = function () {
		if (x.readyState == 4) {
			if (callback) {
				callback(x.responseText, x.status)
			}
		}
	};
	if (method == 'POST') {
		x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	}
	x.send(data)
};

ajax.get = function (url, params, callback, async) {
	var query = [];
	for (var key in params) {
		query.push(encodeURIComponent(key) + '=' + encodeURIComponent(params[key]));
	}
	ajax.send(url, callback, 'GET', null, async);
};

ajax.post = function (url, data, callback, async) {
	ajax.send(url, callback, 'POST', data, async);
};

jQuery.div = function (cl, content) {
	if (!content) content = "";

	var $div = $('<div />');
	$div.addClass(cl);
	if (content) $div.append(content);
	return $div;
};

jQuery.img = function (asset) {
	return $('<img src="{0}"/>'.f(SHARED_ROOT + "assets/" + asset + '.png'));
};


jQuery.btn = function (content, click) {
	var $btn = $.div('btn');
	$btn.append(content);
	if (typeof(click) === 'function') {
		$btn.click(click);
	}
	return $btn;
};


function findDups(arr, key) {
	var dupArr = [];
	var groupedByCount = _.countBy(arr, function (item) {
		return item[key];
	});

	var checker = {};
	checker[key] = key;

	for (var item in groupedByCount) {
		if (groupedByCount[item] > 1) {
			checker[key] = item;
			_.where(arr, checker).map(function (item) {
				dupArr.push(item);
			});
		}
	}

	return dupArr;
}

