/**
 * @author jesse
 */

var cache = [];

var features;
var featureTables = [];

var clients;
var clientXML;

function loadFeatures(xml) {
	
	features = $(xml).find('item');
	
	var featureGrid = $('#FeatureGrid');
	
	var idx = 0;
	var HTML = '';
	
	features.each(function() {
		
		HTML = HTML + '<div class="feature-container">';
		HTML = HTML + '<div class="feature-title"><strong>' + $(this).attr('date') + '</strong></div>';
		HTML = HTML + '<div class="feature-content">' + $(this).text() + '</div>';
		HTML = HTML + '</div>';
		
		idx++;	
		
		if (idx % 4 == 0) {
			featureTables[featureTables.length] = HTML;
			HTML = '';
		}	
	});
	
	if(idx % 4 != 0)
		featureTables[featureTables.length] = HTML;
	
	for(var i = 0; i < featureTables.length; i++) {
		var newEl = $('<div id="fc' + i + '" class="fc" />').html(featureTables[i]);
		if(i > 0)
			newEl.addClass('hidden');	
		newEl.appendTo('#FeatureGrid');
	}
	
}

function getFeatures() {
	var link = $('#FeatureData').attr('href');
	$('#FeatureData').hide();
	if (!cache[link]) {
		$.ajax({
	    	type: "GET",
	        url: link,
	        dataType: "xml",
	        success: function(xml) {
				cache[link] = xml;
				loadFeatures(xml);
			 }
		});
	} else {
		loadFeatures(cache[link]);
	}
}

function getClients() {
	var link = $('#WorkData').attr('href');
	$('#WorkData').hide();
	if (!cache[link]) {
		$.ajax({
	    	type: "GET",
	        url: link,
	        dataType: "xml",
	        success: function(xml) {
				cache[link] = xml;
				clientXML = xml;
				clients = $(xml).find('client');
			 }
		});
	} else {
		loadClients(cache[link]);
	}
}
