/**
 * @author Jens Martsch
 * 
 */
(function($){
	$.fn.getfilesize = function() {
		
	    return this.each(function() {
			obj = $(this);
			var $links=[];
			// If element is a link
			if (obj.is("a:not([href^='http']):not([href*='#']):not([href*='../'])")){
				$links.push ($(this).attr('href'));
			}
			
			// else find all links in element
			
			$("a:not([href^='http']):not([href*='#']:not([href*='../']))", obj).each(function(){
				if ($(this).attr('href')) {
					$links.push ($(this).attr('href'));
				}
			});
			
			//window.console.log('Found links: '+$links);
			
			if ($links.length > 0) {
				$.ajax({
					type: "POST",
					url: "getfilesize.php",
					async: false,
					dataType: "json",
					data: "files=" + $links,
					success: function(data){
						$.each(data, function(url, filesize){
							if (obj.is('a[href="' + url + '"]')) {
								obj.append('&nbsp;(' + filesize + ')');
							}
							$('a[href="' + url + '"]', obj).append('&nbsp;(' + filesize + ')');
						});
						
					}
				});
			}
	    });
		
	};
})(jQuery);

jQuery(function(){ jQuery('.download a').getfilesize();})
