jQuery(function($){
	$('a[href$=".pdf"], a[href$=".doc"], a[href$=".mp3"], a[href$=".m4u"], a[href$=".xls"]').each(function(){
		var link = $(this);
		var bits = this.href.split('.');
		var type = bits[bits.length -1].toUpperCase();
		
		var url= "http://json-head.appspot.com/?url="+encodeURI($(this).attr('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;
						length = Math.ceil(length * 10) / 10;
						var lengthUnits = unitText;
						break;
					}
				}
				
				link.after(' <small>(' + type + ' ' + length + lengthUnits + ')</small>');
				link.addClass(type);
				switch(type) {
				case "PDF":
					link.attr("type","application/pdf")
					break;    
				case "DOC":
					link.attr("type","application/msword")
					break;
				case "XLS":
					link.attr("type","application/vnd.ms-excel")
					break;
				}
			}
		});
	});
});