var flag=true;
var data="";

function refreshProgress()
{
    var url=baseUrl+'?t='+new Date().getTime();
    $.ajax({
    	  url: url,
    	  async: true,
    	  success: updateProgress
    	});
}



function updateProgress(response){
	
	if(flag==false||response==100){clearInterval(timeoutRet);return true;}
	var totalWidth=$('#progressBarBox').width();
	
	if(response.replace(/^\s+|\s+$/g, '')=='error'){
		return true;
    }else{
		var uploadInfo = eval('(' + response.replace(/^\s+|\s+$/g, '') + ')');
	    if(uploadInfo!=null&&uploadInfo.totalSize>0){	  
		    var fileIndex = uploadInfo.fileIndex;	
		    var progressPercent = Math.ceil((uploadInfo.bytesRead / uploadInfo.totalSize) * 100);
		    if(progressPercent==100){
		    	clearInterval(timeoutRet);
		    	$('#progressBarText').html('Analyzing the song, this may take more than one minute...');	
				$('#progressBarBoxContent').width(totalWidth-2);
				$('#progressBarText1').html("");
		    }else{	
		        $('#progressBarText').html('Upload in progress: ' + progressPercent + '%');	
		        $('#progressBarBoxContent').width(parseInt((progressPercent/100) * totalWidth));
		        var byteRead=uploadInfo.bytesRead;
		        if(byteRead>1024) byteRead=(byteRead/(1024*1024)).toFixed(2)+"MB";
		        var totalSize=uploadInfo.totalSize;
		        if(totalSize>1024) totalSize=(totalSize/(1024*1024)).toFixed(2)+"MB";
		        else totalSize=totalSize+" Bytes";
		        $('#progressBarText1').html(byteRead+ ' of ' + totalSize + ' at '+uploadInfo.transmitRate +' KB/sec, '+uploadInfo.remainTime+' remaining');
		    }
		}
	}
    return true;
}


var ind=1;
var timeoutRet;

function startProgress(){
	
    $('#progress').show();
    $('#progressBarText').html('Uploading in progress....');
    //wait a little while to make sure the upload has started ..
    timeoutRet=window.setInterval("refreshProgress()",1000);
    return true;
}

