var videoboysTotalCount = 0;
var videoboysStart = 0;

var squirtzTotalCount = 0;
var squirtzStart = 0;

var videoboys = {
	startup: function() {
		new Ajax.Updater('videoboys-ajax-result','/ajaxproxy.php', { method:'get', parameters: {action: 'fetch', site: "videoboys"} });
	},
	
	goforward: function() {
		videoboysStart = videoboysStart + 5;
		if (videoboysTotalCount <= videoboysStart) { videoboysStart = 0; }

		new Effect.Fade('videoboys-ajax-result', {
			duration: 0.2,
			afterFinish: function() {
				new Ajax.Updater('videoboys-ajax-result','/ajaxproxy.php', { method:'get', parameters: {action: 'fetch', site: "videoboys", start: videoboysStart},
				onSuccess: function() {
					new Effect.Appear('videoboys-ajax-result', {duration: 0.2, queue: 'end'});
				}});
			}
		});
	},
	
	goback: function() {
		videoboysStart = videoboysStart - 5;
		if (videoboysStart < 0) { videoboysStart = (videoboysTotalCount - 5);}
		new Effect.Fade('videoboys-ajax-result', {
			duration: 0.2,
			afterFinish: function() {
				new Ajax.Updater('videoboys-ajax-result','/ajaxproxy.php', { method:'get', parameters: {action: 'fetch', site: "videoboys", start: videoboysStart},
				onSuccess: function() {
					new Effect.Appear('videoboys-ajax-result', {duration: 0.2, queue: 'end'});
				}});
			}
		});
	}
}

var squirtz = {
	startup: function() {
		new Ajax.Updater('squirtz-ajax-result','/ajaxproxy.php', {method:'get', parameters: {action: 'fetch', site: "squirtz"} });
	},
	
	goforward: function() {
		squirtzStart = squirtzStart + 6;
		if (squirtzTotalCount <= squirtzStart) { squirtzStart = 0; }
		new Effect.Fade('squirtz-ajax-result', {
			duration: 0.2,
			afterFinish: function() {
				new Ajax.Updater('squirtz-ajax-result','/ajaxproxy.php', { method:'get', parameters: {action: 'fetch', site: "squirtz", start: squirtzStart},
				onSuccess: function() {
					new Effect.Appear('squirtz-ajax-result', {duration: 0.2, queue: 'end'});
				}});
			}
		});
	},
	
	goback: function() {
		squirtzStart = squirtzStart - 6;
		if (squirtzStart < 0) { squirtzStart = (squirtzTotalCount - 6);}
		new Effect.Fade('squirtz-ajax-result', {
			duration: 0.2,
			afterFinish: function() {
				new Ajax.Updater('squirtz-ajax-result','/ajaxproxy.php', { method:'get', parameters: {action: 'fetch', site: "squirtz", start: squirtzStart},
				onSuccess: function() {
					new Effect.Appear('squirtz-ajax-result', {duration: 0.2, queue: 'end'});
				}});
			}
		});
	}
}

var frontpage = {
	prepare: function() {
		// Show most recent updates when page loads
		videoboys.startup();
		squirtz.startup();
		
		// Get total amount of updates from each website so we can calculate the loop point,
		// and strip modulus from the number of updates shown on the frontpage to keep the frontpage tidy and filled with content.
		// This means you will never see an empty spot on the frontpage even if the total amount of updates cannot be divided by the number of spots on the frontpage.
		new Ajax.Request("/ajaxproxy.php", { method: 'get', parameters: {action: 'gettotal', site: 'videoboys'}, 
			onSuccess: function(transport) { 
				videoboysTotalCount = transport.responseText;
				videoboysTotalCount = (videoboysTotalCount - (videoboysTotalCount % 5));
			} 
		});
		new Ajax.Request("/ajaxproxy.php", { method: 'get', parameters: {action: 'gettotal', site: 'squirtz'}, 
			onSuccess: function(transport) { 
				squirtzTotalCount = transport.responseText;
				squirtzTotalCount = (squirtzTotalCount - (squirtzTotalCount % 6));
			} 
		});
	}
}