var UPIMG = function() {
    /* private variables */
    var ifr = null;
    var startTime = null;
    var infoUpdated = 0;
    
    var writeStatus = function(text,color) {
        var statDiv = document.getElementById("imgStatus");
        if (color == 1 ) {
            statDiv.style.backgroundColor = "#cccccc";
            statDiv.style.color = "black";
        } else if (color == 2 ) {
            statDiv.style.backgroundColor = "#cccccc";
            statDiv.style.color = "black";
        } else if (color == 3 ) {
            statDiv.style.backgroundColor = "#cccccc";
            statDiv.style.color = "black";
        } else {
            statDiv.style.backgroundColor = "#cccccc";
            statDiv.style.color = "black";
        }
        statDiv.innerHTML = text;
    }
    return {
        start: function() {
           ifr = document.getElementById("ifr");
           startTime = new Date();
           infoUpdated = 0;
           $('#imgStatus').show(0);
           this.requestInfo();
           fotoCompleta=false;
        },
        stop: function(files) {
            if (typeof files == 'undefined' || files) {
                 var secs = (new Date() - startTime)/1000;
                 var statusText = "Upload concluido! Demorou " + secs + " segundos. <br/> ";
                 /*                
   				if (infoUpdated > 0) {
                     writeStatus(statusText + "You had " + infoUpdated + " updates from the progress meter, looks like it's working fine",1);
                 } else {
                     statusText += "BUT there were no progress meter updates<br/> ";
                     if (secs < 5) {
                       writeStatus(statusText + "Your upload was maybe too short, try with a bigger file or a slower connection",2);
                     } else {
                       writeStatus(statusText + "Your upload should have taken long enough to have an progress update. Maybe it really does not work...",3);
                     }
                 } 
                 */
                 writeStatus(statusText ,1);                
            } else {
                writeStatus('PHP did not report any uploaded file, maybe it was too large, try a smaller one (max. 300M)',3);
            }
            startTime = null;
         }, 
         tooBig: function() {
             
            writeStatus('Arquivo Grande demais',3);
            startTime = null;
            infoUpdated = 0;
         },
        requestInfo: function() {
        	answer = $.ajax({
        	      url: "/user/infoImage/" + this.myID + "/" + (new Date() - 0),
        	      global: false,
        	      type: "POST",
        	      dataType: "json",
        	      success: function(msg){
        	         if (msg.info=='false'){
        	        	UPIMG.updateInfo()
        	         }else{
        	        	 if (msg.bytes_total<=512000){
        	        		 UPIMG.updateInfo((msg.bytes_uploaded/msg.bytes_total),msg.est_sec)
        	        	 }else{
        	        		 UPIMG.tooBig();
        	        	 }
        	         }
        	      }
        	   }
        	).responseText;
        	//ifr.src="/upload/infoVideo/" + this.myID + "/" + (new Date() - 0);
        },
        updateInfo: function(percent, estimatedSeconds) {
            if (startTime) {
                if (percent) {
                    infoUpdated++;
                    writeStatus("Upload comecou faz " + (new Date() - startTime)/1000 + " segundos. " + Math.floor(percent * 100) + "% concluido, estimamos " + estimatedSeconds + "  segundos restantes"); 
                } else {
                    writeStatus("Upload comecou faz " + (new Date() - startTime)/1000 + " segundos. A barra ainda nao foi atualizada.");
                }
                window.setTimeout("UPIMG.requestInfo()",500);
            }
        }
        
    }
}()

function loadImage(file){
	$('#leftImagem').empty().append('<img src="/midias/temp/images/usuario/' + file + '" />');
	$('#imgStatus').hide(500);
	$('#file').val('');
	$('#image_file').val(file);
	fotoCompleta=true;
//	alert('DONE')
}
