function Navigate(url) {
	window.location = url;
}

function CreateCookie(name, value, days) {
	var expires;
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		expires = "; expires=" + date.toGMTString();
	}
	else {
		expires = "";
	}
	document.cookie = name + "=" + value + expires + "; path=/";
}

function round(value, decimals) {
	return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
}

function isNumber(value){
	return isFinite(String(value));
}

String.prototype.upperCaseFirst = function() {
	var lower = this.toLowerCase();
	return lower.charAt(0).toUpperCase() + lower.slice(1);
};

function twoDigitZeroPad(input){
	input=input.toString();
	return [!input[1] && '0' || "", input].join('');
}

Date.prototype.oldToString = Date.prototype.toString;
Date.prototype.toString = function(format) {
	if (!format)
		return this.oldToString();
	var me=this;
	return format.replace(/./g, function(match) {
		switch(match){
			case 'd': return twoDigitZeroPad(me.getDate());
			case 'm': return twoDigitZeroPad(me.getMonth()+1);
			case 'y': return me.getFullYear();
			case "H": return twoDigitZeroPad(me.getHours());
			case "i": return twoDigitZeroPad(me.getMinutes());
			case "s": return twoDigitZeroPad(me.getSeconds());
			default: return match;
		}
	});
};

Number.prototype.oldToString=Number.prototype.toString;
Number.prototype.toString=function(input, shouldRound){
	if (!shouldRound)
		return this.oldToString(input);

	return round(this, input);
};

$(function() {
	var panels = $("nav > dl > .sub-pages");
	var goIndicators = $("nav > dl > dt .go");
	var expandIndicators = $("nav > dl > dt .expand");
	panels.hide();
	expandIndicators.show();

	$("nav > dl > dt").click(function(e) {
		var next = $(this).next();
		if (!next.hasClass("sub-pages")) {
			Navigate($(this).children("a").attr("href"));
			return;
		}
		panels.slideUp();
		goIndicators.hide();
		expandIndicators.show();
		if (next.is(":visible")) {
			return;
		}
		next.slideDown();
		$(this).children(".go").show();
		$(this).children(".expand").hide();
	});

	$("nav dd").click(function(e) {
		Navigate($(this).children("a").attr("href"));
	});

	$("nav a").click(function(e) {
		$(this).parent().click();
		return false;
	});

	$("#body").css("padding-bottom", window.innerHeight - $("#buttons-right").offset().top);

	$("form[ajaxForm]").submit(function(e) {
		e.preventDefault();
		form = $(this);
		$.ajax({
			url: form.attr("action"),
			method: form.attr("method"),
			data: form.serialize(),
			success: function(data) {
				var successFunction = form.attr("onsuccess");
				if (successFunction !== undefined && window[successFunction] !== undefined)
					window[successFunction](data);
			},
			error: function() {
				var errorFunction = form.attr("onerror");
				if (errorFunction !== undefined && window[errorFunction] !== undefined)
					window[errorFunction]();
			},
		});
		var postFunction = form.attr("onpost");
		if (postFunction !== undefined && window[postFunction] !== undefined)
			window[postFunction]();
	});

	$("td").each(function() {
		var elem = $(this);
		if (/^[+-]?\d+(\.\d+)?$/.test(elem.text())) {
			elem.addClass("number");
		}
	});
});

$(document).delegate('.allowTabInput', 'keydown', function(e) {
	var keyCode = e.keyCode || e.which;
	console.log(e);
	if (keyCode == 9) {
		e.preventDefault();
		var start = $(this).get(0).selectionStart;
		var end = $(this).get(0).selectionEnd;

		// set textarea value to: text before caret + tab + text after caret
		$(this).val($(this).val().substring(0, start)
				+ "\t"
				+ $(this).val().substring(end));

		// put caret at right position again
		$(this).get(0).selectionStart =
				$(this).get(0).selectionEnd = start + 1;
	}
});