jQuery(function($){
	
	// addSizes was written by Natalie Downe 
	// http://natbat.net/2008/Aug/27/addSizes/
	
	// Copyright (c) 2008, Natalie Downe under the BSD license
	// http://www.opensource.org/licenses/bsd-license.php
	
	$('#centre a[href$=".pdf"], #centre a[href$=".doc"], #centre a[href$=".mp3"], #centre a[href$=".zip"], a[href$=".xls"]').not(':has(img)').each(function(){
		var link = $(this);
		var bits = this.href.split('.');
		var type = bits[bits.length -1];
		
	//	if ()
	//	{
	//		console.log($('link:has(img)'));
	//	}
		
		var url= "http://json-head.appspot.com/?url="+encodeURIComponent (this.href)+"&callback=?";
	
		$.getJSON(url, function(json){
			if(json.ok && json.headers['content-length']) {
				var length = parseInt(json.headers['content-length'], 10);
				
				var units = [
					[1024 * 1024 * 1024, 'GB'],
					[1024 * 1024, 'MB'],
					[1024, 'KB'],
					[1, 'bytes']
				];
				
				for(var i = 0; i < units.length; i++){
					
					var unitSize = units[i][0];
					var unitText = units[i][1];
					
					if (length >= unitSize) {
						length = length / unitSize;
						// 1 decimal place
						length = Math.ceil(length * 10) / 10;
						var lengthUnits = unitText;
						break;
					}
				}
				
				link.after(' <span class="file_size">(' + type + ' ' + length + ' ' + lengthUnits + ')</span>');
				link.addClass(type);
			}
		});
	});
});