jQuery(function() {

	//retrieve comments to display on page  
    jQuery.getJSON("/api/v2/contributors/country.json?hours=720&callback=?", function(data)
	{
	    //loop through all items in the JSON array 
		if(data)
		 {
			var html = '<table class="boxlist"><thead><tr><th width="10%" style="text-align:center">Rank</th><th width="10%" style="text-align:center">Count</th><th style="text-align:left" width="70%">Country</th><th style="text-align:center" width="10%">Flag</th></tr></thead>';
        
			var contributors = data.contributors; // second element is the list of trends
	        for (var x = 0; x < contributors.length; x++) {  
				html += formatContributorText(contributors[x], x);
			}
		
			html += "</table>"
			
			// add trend items to panel
	 		jQuery("#country_contributors").append(html);  
			// Format dates
			//jQuery('.date').timeago();			
		}
		else
		{
			jQuery("#fastest_contributors").append("No contributors available");
		}          
    });
	
	function formatContributorText(contributor, index)
	{		
		var html = '<tr><td style="font-size:140%;font-weight:bold;text-align:center">#' + (index + 1) +'</td>';
		html += '<td style="text-align:center">' + contributor.count + '</td>';
		html += '<td style="text-align:left">' + contributor.country + '</td>';
		html += '<td style="text-align:center"><img src="' + countryToFlag(contributor.country) + '" alt="' + contributor.country + '" title="' + contributor.country + '"></td>';
		html += '</tr>';
			
		return html;	
	}
	
});
