var utils = {
	createCookie: function(name, value, time, unit) {
		if (time) {
			if (!unit)
				unit = "days";
			if (unit === "minutes") {
				time = time * 60 * 1000;
			} else if (unit === "hours") {
				time = time * 60 * 60 * 1000;
			} else if (unit === "days") {
				time = time * 24 * 60 * 60 * 1000;
			}
		
			var date = new Date();
			date.setTime(date.getTime() + time);
			var expires = "; expires=" + date.toGMTString();
		} else {
			var expires = "";
		}
		
		document.cookie = name + "=" + value + expires + "; path=/";
	},

	readCookie: function(name) {
		var nameEQ = name + "=";
		var cookies = document.cookie.split(';');
		for(var i = 0; i < cookies.length; i++) {
			var cookie = utils.trim(cookies[i]);
			if (cookie.indexOf(nameEQ) == 0)
				return cookie.substring(nameEQ.length, cookie.length);
		}
		
		return null;
	},

	qs: function(force) {
		if (this.qsParm && !force)
			return this.qsParm;

		this.qsParm = {};
		var query = window.location.search.substring(1);
		var parms = query.split('&');
		var key = "";
		var val = "";
		for (var i = 0; i < parms.length; i++) {
			var pos = parms[i].indexOf('=');
			if (pos > 0) {
				key = parms[i].substring(0,pos);
				val = parms[i].substring(pos+1);
				this.qsParm[key] = val;
			}
		}
		
		if (val !== "" && val[val.length - 1] === "/") {
			// sometimes the server adds an "/" to the query string
			this.qsParm[key] = val.substr(0, val.length - 1);
		}
		
		return this.qsParm;
	},

	indexOf: function(hay, needle) {
		if (typeof(hay.indexOf) === "function") {
			return hay.indexOf(needle);
		}
		
		var i = hay.length;
		while (i--) {
			if (hay[i] === needle) {
				return i;
			}
		}

		return -1;
	},

	trim: function(str) {
		if (typeof(str.trim) === "function") {
			return str.trim();
		}

		return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	},

	prettyDateDiff: function(date, language) {
		var i18n = {
			"today": {
				"pt": "hoje"
			}, "yesterday": {
				"pt": "ontem"
			}, "{} days ago": {
				"pt": "há {} dias"
			}, "last week": {
				"pt": "semana passada"
			}, "{} weeks ago": {
				"pt": "há {} semanas"
			}, "last month": {
				"pt": "mês passado"
			}, "{} months ago": {
				"pt": "há {} meses"
			}
		}
		var today = new Date();
		// mills to days (86400000 = 1000*60*60*24)
		var diff = (today - date) / 86400000;
		function i18nalize(expression, content) {
			var i18nalized = expression;
			if (language !== "en") {
				i18nalized = i18n[expression][language] || i18nalized;
			}
			if (content) {
				i18nalized = i18nalized.replace("{}", content);
			}
			return i18nalized;
		}
		if (diff < 1) {
			return i18nalize("today");
		} else if (diff < 2) {
			return i18nalize("yesterday");
		} else if (diff < 7) {
			return i18nalize("{} days ago", Math.floor(diff));
		} else if (diff < 15) {
			return i18nalize("last week");
		} else if (diff < 30) {
			return i18nalize("{} weeks ago", Math.floor(diff / 7));
		} else if (diff < 60) {
			return i18nalize("last month");
		} else if (diff < 180) {
			return i18nalize("{} months ago", Math.floor(diff / 30));
		}
		function zeroStr(num) {
			if (num < 10) {
				return "0" + num;
			}
			return "" + num;
		}
		return date.getFullYear() + "/"
			+ zeroStr(date.getMonth()) + "/"
			+ zeroStr(date.getDate());
	}
};

