window.Debug = new function()
{
	this.logged_times = {};
	this.log_file = [];
	
	this.use_timer = false;
	
	this.log = function(a, b, func)
	{
		/* Sends a message to console.log (or console.info, etc., in case
		 * a different function is requested). This prints out the message
		 * in the browser's console. */
		
		if (func == null) {
			func = "log";
		}
		if (b == null) {
			b = [];
		}
		if (b.constructor.toString().indexOf("Array") == -1) {
			b = [b];
		}
		if (this.use_timer) {
			b.unshift(this.get_timer());
			a = "%d: "+a;
		}
		b.unshift(a);
		console[func].apply(console, b);
	}
	
	/* Disable the console function in case there is no console available. */
	if (!window["console"]) {
		this.log = function(a, b, func)
		{
			return false;
		}
	}
	
	this.info = function(a, b)
	{
		/* Shortcut for this.log(a, b, "info"). */
		return this.log(a, b, "info");
	}
	
	this.warn = function(a, b)
	{
		/* Shortcut for this.log(a, b, "warn"). */
		return this.log(a, b, "warn");
	}
	
	this.error = function(a, b)
	{
		/* Shortcut for this.log(a, b, "error"). */
		return this.log(a, b, "error");
	}
	
	this.timer_start = function(name)
	{
		/* Starts a named timer. This can be used to see how long a particular
		 * set of actions takes to complete. To get the result, call timer_stop()
		 * with the same argument. */
		this.logged_times[name] = this.get_timer();
	}
	
	this.timer_stop = function(name)
	{
		/* Returns the difference between now and the moment this timer was started. */
		return this.get_timer() - this.logged_times[name];
	}
	
	this.get_timer = function()
	{
		/* Returns the amount of milliseconds since this page was loaded. */
		return new Date().getMilliseconds();
	}
}

Debug.info("Debug class initialized.");

$(function()
{
	$("a[href*=readmore]").live("click", function()
	{
		var $readmore = $(this).parents("p").siblings(".readmore");
		$readmore.slideToggle();
	});
	
	window.abcd_data = {
		loading_new_blog: false,
		blog_offset: window.col_offset,
		loading_new_project: false,
		project_offset: window.col_offset
	};
	
	setInterval(function()
	{
		var $div = $("#projects");
		var bottom_point = $("#cases").height() - ($(window).height() / 2);
		var scroll_y = $(document).scrollTop();
		var max_scroll = $(document).height() - $(window).height();
		if ((scroll_y > bottom_point || scroll_y >= max_scroll) && window.abcd_data.loading_new_project == false) {
			Debug.log("Getting new project post; scroll_y: %o, bottom_point: %o, max_scroll: %o (doc h: %o) | (%o || %o) && %o", [scroll_y, bottom_point, max_scroll, $(document).height(), scroll_y > bottom_point, scroll_y >= max_scroll, window.abcd_data.loading_new_project == false]);
			window.abcd_data.loading_new_project = true;
			$.getJSON(window.site_url+"/json/", {type: "project", offset: window.abcd_data.project_offset}, function(data)
			{
				if (data.has_posts == true) {
					$("#projects").append(data.posts.join(""));
					window.abcd_data.loading_new_project = false;
					window.abcd_data.project_offset += data.return_amount;
				}
			});
		}
	}, 500);
	
	setInterval(function()
	{
		var $div = $("#blog");
		var bottom_point = $("#news").height() - ($(window).height() / 2);
		var scroll_y = $(document).scrollTop();
		var max_scroll = $(document).height() - $(window).height();
		if ((scroll_y > bottom_point || scroll_y >= max_scroll) && window.abcd_data.loading_new_blog == false) {
			Debug.log("Getting new blog post; scroll_y: %o, bottom_point: %o, max_scroll: %o (doc h: %o) | (%o || %o) && %o", [scroll_y, bottom_point, max_scroll, $(document).height(), scroll_y > bottom_point, scroll_y >= max_scroll, window.abcd_data.loading_new_blog == false]);
			window.abcd_data.loading_new_blog = true;
			$.getJSON(window.site_url+"/json/", {type: "blog", offset: window.abcd_data.blog_offset}, function(data)
			{
				if (data.has_posts == true) {
					$("#blog").append(data.posts.join(""));
					window.abcd_data.loading_new_blog = false;
					window.abcd_data.blog_offset += data.return_amount;
					var curr_items_html = $("#news p.index_items").html();
					$("#news p.index_items").html(curr_items_html+data.items);
				}
			});
		}
	}, 500);
});
