/**
 *  progress.js
 *  This is the progress script-file for FrontWeb Express welcomepage
 *  Date:				2005-09-07
 *  version:		1.0.0
 *  version-notes:
    1.0.1       patches
 *  author:       Crimson AB, linus.glansholm@frontlinesystem.com
 *  Crimson:      copyright AB, Frontline, Frontweb, All rights reserved, 2005
 */


//alert("progress.js");
var progressBar;
var progressStarted = false;


function makeProgress() {
	//document.progressBarForm.submit();

	var waitBlock = getId("waitBlock");
    var scrollComp = document.getElementsByTagName("html").item(0).scrollTop;
    getId("pageDimmer").style.visibility = "visible";
    waitBlock.style.top = 100 + scrollComp + "px";
	waitBlock.style.visibility = "visible";




  //progressBar = getId("progresser");
  document.uploadForm.submit();
  //progress.clockTick();

}

//var safari = false;
//function makeProgress() {
//  if (safari) {
//  } else {
//    
//    var src = scriptPath + "/" + sessionId + "/progress/start";
//    getId("progressIFrame").src = src;
//	  var waitBlock = getId("waitBlock");
//    var scrollComp = document.getElementsByTagName("html").item(0).scrollTop;
//    getId("pageDimmer").style.visibility = "visible";
//    waitBlock.style.top = 100 + scrollComp + "px";
//	  waitBlock.style.visibility = "visible";
//	  setTimeout("setIFrameSrc('" + src + "')", 200);
//    progressBar = getId("progresser");
//    progress.clockTick();

//	  return true;
//  }
//}



function loadScript(url) {
  
  parent        = document.getElementsByTagName("head")[0];
  script        = document.createElement('script');
  script.type   = "text/javascript";
  script.src    = url;
  parent.appendChild(script);
}


/**
 * Progress bar functions 
 */
 
 
var progress = createProgress();




function createProgress() {
  var obj         = new Object();
  obj.state       = '';
  obj.setState    = progress_setState;
  obj.setData     = progress_setData;
  obj.adjustData  = progress_adjustData;
  obj.display     = progress_display;
  obj.displayTime = progress_displayTime;
  obj.clockTick   = progress_clockTick;
  obj.timeToLive  = 20;
  obj.perCent     = 0;
  obj.upload      = false;
  return obj;

}

function progress_setState(state) {
  this.state = state;
  return this;
}

 

function progress_setData(length, read, bps) {
  if (length == undefined) {
    length = 0;
    read = 0;
    bps = 0;
  }
  this.upload   = true;
  this.length   = length;
  this.read     = read;
  this.bps      = bps;
  this.remain   = length - read;
  this.elapsed  = Math.round(read / bps);
  this.perCent  = Math.round(100 * read / length);
  this.secToGo  = Math.round(this.remain / bps);
  this.timeToLive = 20;
  if (CLOCK_STOP) {
    CLOCK_STOP = false;
    this.clockTick();
  }
  this.adjustData();
  return this;
}

function progress_clockTick() {
  if (--this.timeToLive <= 0) {
   this.secToGo = "?";
   CLOCK_STOP = true;
  }
  if (!CLOCK_STOP) { 
    this.secToGo--;
    if (this.secToGo < 0) this.secToGo = 0;
    this.elapsed++;
    this.adjustData();
    this.displayTime();
    setTimeout("progress.clockTick()", 1000);
  }
}



function progress_adjustData() {
	this.txt          = new Object(); 
	
	this.txt.secToGo  = setTimeUnits(parseInt(this.secToGo));
	this.txt.elapsed  = setTimeUnits(parseInt(this.elapsed));
	this.txt.length   = setByteUnits(this.length);
	this.txt.read     = setByteUnits(this.read);
	this.txt.bps      = setByteUnits(this.bps) + "/" + SEK;
	this.txt.remain   = setByteUnits(this.remain);
}

function setTimeUnits(v) {
	var text = ""; 
	if (isNaN(v % 60)) {
		text = "-";
		//alert("im protecting sek");
	} else {
		s = v % 60;
		if (Math.floor(v / 60) >= 1) {
			text += Math.floor(v / 60) + " " + MIN + " ";
  		if (s < 10) s = "0" + s;
		} 
		text += s + " " + SEK;
	}
	return text;
}

function setByteUnits(value) {
  var txt = "";
  if (1024 > value) {
	  txt = value  + " b";
  } else if (1048576 > value) {
	  txt = Math.round(value / 1024) + " kb";
  } else {
	  if (10 > Math.round(value / 1048576)) {
		  txt = (Math.round((value / 1048576) * 100) / 100) + " mb";
	  } else if (100 > Math.round(value / 1048576)) {
		  txt = (Math.round((value / 1048576) * 10) / 10) + " mb";
	  } else {
		  txt = Math.round(value / 1048576)  + " mb";
	  }
  }
	return txt;			
}

var CLOCK_STOP = false;
 
 function getId(id) {
  return document.getElementById(id);
}

 
function progress_display() {
  //alert("debug code karlk11;");
  //document.title = progress.state;
  if (!progressStarted && progress.state == "uploading") {
    //alert("debug code karlk2;");
    getId('waitingForProgressBar').style.display = "none";
    getId('progressBarHolder').style.backgroundImage = "none";
    getId('dataTable').style.display = "block";
    progressStarted = true;
  }

  if (progress.state == "uploading") {
    //alert("state uploading");
		//getId("progresser").style.width       = this.perCent + "%";
		getId("perCent").innerHTML            = this.perCent + "%";
		getId("speed").innerHTML              = this.txt.bps;
		getId("totalAmount").innerHTML        = this.txt.length;
		getId("transferredAmount").innerHTML  = this.txt.read;
		getId("remainingAmount").innerHTML    = this.txt.remain;
    progressBar.style.width       = this.perCent + "%";	
		
  } else if (this.upload) {
    //alert("state not uploading");
    PROGRESS_STOP = true;
		getId("waitHeader").style.display = "none";
		getId("prepareHeader").style.display = "block";
		getId("waitParagraph").style.display = "none";
		getId("prepareParagraph").style.display = "block";
		getId("dataTable").style.color = "#aaaaaa";
		progressBar.style.width = "100%";
		getId("perCent").innerHTML = "100%";
		getId("transferredAmount").innerHTML  = this.txt.length;
		getId("timeToGo").innerHTML = "-";
		//getId("timeToGo2").innerHTML = "-";
		getId("speed").innerHTML = "-";
		getId("remainingAmount").innerHTML = "0";
	} else {
	  // wait;
	}
}

function progress_displayTime() {
  if (progress.state == "uploading") {
		getId("timeToGo").innerHTML           = this.txt.secToGo;
		//getId("timeToGo2").innerHTML           = this.txt.secToGo;
    document.title                        = this.txt.secToGo;
		getId("elapsedTime").innerHTML        = this.txt.elapsed;
  } else if (this.upload) {
    //alert("state not uploading");
    CLOCK_STOP = true;
	} else {
	  // wait;
	}
}


function cancelUpload(msg, path, session) {

  if (confirm(msg)) {
    window.location = path + '/' + session + '?reload=true';
  }
}

