//Instantiate the object
var json_calls = {
	index: 0,
	callbackSelector: "body",
	logTimeTaken: false
};

//A series of anonymous functions that make AJAX calls that are to be triggered by each others callbacks.
//These methods must return true if an AJAX call is made and false otherwise.
//If true/false are not returned, the next method in line will not be called.
json_calls.methods = [

	//CENtral Science
	function() {
		
		var container = $("#central_science_inner");
		
		if (container.length > 0)
		{
		
			var map = '<div class="heading">'
				+ '<h3><a href="[blogLink]" title="[blogName]">[blogName]</a></h3>'
				+ '</div>'
				+ '<div class="date">[formattedDate]</div>'
				+ '<h4><a href="[link]">[title]</a></h4>'
				+ '<div class="teaser">[truncatedDesc]</div>';
			
			$.getJSON("/acs/jsonify?parent="+CENTRAL_SCIENCE_FOLDER, function(data) {
				var column;
				var NUM_ENTRIES = 6;
				for (var i = 0, node = data[String(i)]; i < NUM_ENTRIES && typeof node !== "undefined"; i++, node = data[String(i)])
				{
					var openColumn = (i%2 == 0);
					var closeColumn = ((i%2 == 1) || (i == NUM_ENTRIES - 1) || (typeof data[String(i+1)] === "undefined"));
					if (openColumn)
					{
						column = $('<div class="column" />');
					}
					var mapped = mapJSON(node, map, false);
					column.append(mapped);
					if (closeColumn)
					{
						container.append(column);
					}
				}
			});
			
			return true;
			
		}
		
		return false;
		
	},

	//Job Listings
	function() {
	
		var container = $("#job_listings_content");
		
		if (container.length > 0)
		{
		
			var map = '<a href="[link]" title="[title]">[title]</a>'
				+ '<div class="location">[location]</div>'
				+ '<div class="text">[description]</div>';
			
			$.getJSON("/acs/jsonify?parent="+JOBLISTINGS_FOLDER, function(data) {
				var NUM_ENTRIES = 3;
				var lastElem = container.find("ul").find("li#last_job");
				for (var i = 0, node = data[String(i)]; i < NUM_ENTRIES && typeof node !== "undefined"; i++, node = data[String(i)])
				{
					lastElem.before($('<li class="link" />').html(mapJSON(node, map, false)));
				}
			});
			
			return true;
			
		}
		
		return false;

	},

	//Tabbox (most popular, most shared, most commented)
	function() {

		var outer_container = $("#most_popular_outer");
		
		if (outer_container.length > 0)
		{
	
			var map = {
				'pageviews': {
					'map': '<a href="[pagePath]" title="[pageTitle]">[pageTitle]</a>',
					'divId': 'popular_viewed'
				},
				'comments': {
					'map': '<a href="[pagePath]" title="[pageTitle]">[pageTitle]</a>',
					'divId': 'popular_commented'
				},
				'shares': {
					'map': '<a href="[pagePath]" title="[pageTitle]">[pageTitle]</a>',
					'divId': 'popular_shared'
				}
			};
			var types = [ 'pageviews', 'comments', 'shares' ];

			var NUM_ENTRIES_PER_TAB = 5;
			
			$.getJSON("/acs/jsonify?parent="+TABBOX_FOLDER, function(data) {
				for (var h = 0; h < types.length; h++)
				{
					var type = types[h];
					var typeObj = data[type];
					var container = outer_container.find("#" + map[type]['divId']).find("ul");
					for (var i = 0, node = typeObj[String(i)]; i < NUM_ENTRIES_PER_TAB && typeof node !== "undefined"; i++, node = typeObj[String(i)])
					{
						container.append($('<li class="link" />').html(mapJSON(node, map[type]['map'], mostPopIsPublish)));
					}
				}
			});
			
			return true;
			
		}
		
		return false;
		
	}
];

//Call this method on document ready...
json_calls.execute = function () {
	if (json_calls.index == 0)
	{
		json_calls.startTime = new Date();
	}
	else if (json_calls.logTimeTaken && typeof console !== 'undefined' && typeof console.log === '')
	{
		console.log('JSON call executed, time took: ' + ((new Date()).getMilliseconds() - json_calls.startTime.getMilliseconds()) + ' milliseconds');
		json_calls.startTime = new Date();
	}
			
	//continue until all the calls have been made
	if (json_calls.index < json_calls.methods.length)
	{
		var madeCall = json_calls.methods[json_calls.index++]();
		//if the previous method made an ajax call, use the ajax callback to trigger the next event
		if (madeCall)
		{
			$(json_calls.callbackSelector).ajaxStop(json_calls.execute);
		}
		//otherwise, immediately trigger the next event
		else
		{
			json_calls.execute();
		}
	}
};
