﻿function appendFileSize()
{
    var fileUrls;
    $(".ShowFileSize a").filter(
            function()
            {
                return this.href.match(/\.pdf|\.doc|\.docx|\.xls|\.xlsx|\.ppt|\.pptx|\.txt|\.rtf/i);
            }
        ).each(
            function(i)
            {
		if(i==0)
		{	fileUrls = "{fileAbsoluteUrls:[";
			fileUrls = fileUrls + "'" + this.href + "'";
		}
		else
			fileUrls = fileUrls + ",'" + this.href + "'";
            }
        )
	if(fileUrls!="")
	{
		fileUrls = fileUrls + "]}";
		$.ajax({
            		url:"/_layouts/DIISR/filesize/filesize.asmx/GetFileSize",            
            		success:getFileSizeCallBack,
	    		data:fileUrls,
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			type:"POST"
            	});
	}
}


function getFileSizeCallBack(response)
{
    $(".ShowFileSize a").filter(
            function()
            {
                return this.href.match(/\.pdf|\.doc|\.docx|\.xls|\.xlsx|\.ppt|\.pptx|\.txt|\.rtf/i);
            }
        ).each(
            function(i)
            {
		var fileType=(this.href.match(/\.pdf|\.doc|\.docx|\.xls|\.xlsx|\.ppt|\.pptx|\.txt|\.rtf/gi))[0];
		fileType=fileType.substring(1);
		if(response.d[i]>=0){
			if(response.d[i]>1000000)
				$(this).append(" (" +fileType.toUpperCase()+"  "+ (Math.round((response.d[i]/1048576)*100))/100 + "MB)");
			else if(response.d[i]>124)
				$(this).append(" ("+fileType.toUpperCase()+"  " + (Math.round((response.d[i]/1024)*100))/100 + "KB)");
			else
				$(this).append(" ("+fileType.toUpperCase()+"  " + response.d[i] + "B)");}
            }
        )
}

_spBodyOnLoadFunctionNames.push("appendFileSize");
