var upload_type = '';
var items = 0;
var uploadLimit = 5;
var fileList = [];  
var startDate = '';
var upload_to = '';
var progressBarSize = 0;

function setFileList(arrayFL) {
    fileList = arrayFL[0];
    var amount = arrayFL[0].length;
    items = amount;
    var FL=document.getElementById("selectedFiles");
    var FLtext = "";

    if (amount>0)
    {
	  FLtext = '<h4>Upload Queue</h4>';
      var tmptext;
      for (i=0;i<amount;i++){
        FLtext += "<li>"+arrayFL[0][i]+" "+Math.round(arrayFL[1][i]/1024)+" Kbytes  <a href='javascript:removeFile("+i+")'>remove</a></li>";
      }
//      FLtext += "<P><a href='javascript:clearFileList();'>Clear All</a></P>";
//      FLtext += "<br><strong>YOU MUST CLICK UPLOAD IN ORDER TO UPLOAD YOUR FILES</strong><br>";
      document.getElementById("fileSelectedStatus").style.display = 'none';
    }
    else
    {
      document.getElementById("fileSelectedStatus").style.display = '';
    }
    FL.innerHTML=FLtext;
    //return ">> OK";
  }
    
  function clearFileList() {
    //alert("clearFileList");
    movie("upload").clearFileList();
  }
  
  function cancelUpload() {
    //alert("cancelUpload");
    movie("upload").cancelUpload();
  }
  
  function browse() {
    if (fileList.length < uploadLimit) {
      movie("upload").__browse(uploadLimit);
    } else {
      overLimit();
    }
  }
  
  function removeFile(number) {
    //alert("removeFile");
    movie("upload").removeFile(number);
  }
  
  function uploadFiles(url,type) {
    if (type == 'public' && document.getElementById('agree_to_terms').checked == false) {
        alert('You must agree to our terms and conditions before continuing.');
        return false;
    }

	progressBarSize = document.getElementById("status_file_border").style.width.substring(0,document.getElementById("status_file_border").style.width.indexOf('px'))/100;

    if(items > 0) {
        var FL=document.getElementById("fileList");
        document.getElementById("selectedFiles").style.display = "none";
        document.getElementById("select_files").style.display = "none";
       
//        var FLtext = "wait...";
//        FL.innerHTML=FLtext;
        upload_type = type;
//        var usize = '';
//        for (var i = 0; i < usizes.length; i++) {
//         if (usizes[i].checked) {
//            usize = escape(usizes[i].value);
//          }
//        }
// 		url = url + "&action=flash&usize="+usize;
        url = url + "&action=flash";
        if(type == 'private') {
	  var usizes    = document.getElementsByName('upload[thumbnail_size]');
      overwrite_option  = escape(document.getElementsByName('overwrite_option')[0].value);
      upload_to  = escape(document.getElementsByName('upload_to')[0].value);
      create_thumbnails = Number (document.getElementById('create_thumbnails').checked);
      if(document.getElementById('thumbnail_size_small').checked) {
        thumbnail_size  = 'small';
      } else {
        thumbnail_size  = 'large';
      }
      create_img_tags  = Number (document.getElementById('create_img_tags').checked);
      extract_zip_files  = Number (document.getElementById('extract_zip_files').checked);
      url = url + "&overwrite_option="+overwrite_option+"&upload_to="+upload_to+"&create_thumbnails="+create_thumbnails+"&thumbnail_size="+thumbnail_size+"&create_img_tags="+create_img_tags+"&extract_zip_files="+extract_zip_files;
      }
	startDate = new Date();
        movie("upload").uploadFiles(url);
    }
    else {
       alert("Please select files first");
    }
  }

  function movie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
      return window[movieName]
    }
    else {
      return document[movieName]
    }
  }
  
  function setProgress(fn, fl, ft, tl, tt, loa, rem)
  {
    var FL=document.getElementById("fileList");
    var speed = calcSpeed(tl);
    document.getElementById("status_div").style.display = "";
    document.getElementById("status_file").style.width =  Math.round(fl*100/ft*progressBarSize) + "px";
    document.getElementById("status_total").style.width =  Math.round(tl*100/tt*progressBarSize) + "px";
    document.getElementById("status_total_num").innerHTML =  Math.round(tl*100/tt) + "%";
    document.getElementById("status_file_num").innerHTML =  Math.round(fl*100/ft) + "%";
    document.getElementById("status_total_kb_left").innerHTML =  Math.round(tt/1024) + "";
    document.getElementById("status_file_kb_left").innerHTML =  Math.round(ft/1024) + "";
    document.getElementById("status_total_kb_done").innerHTML =  Math.round(tl/1024) + "";
    document.getElementById("status_file_kb_done").innerHTML =  Math.round(fl/1024) + "";
    document.getElementById("status_total_speed").innerHTML =  Math.round(speed/1024) + "";


    
    var FLtext = "";
    FLtext="<div width=\"100%\" height=\"60\" style=\"background-color: #555555\"></div><br><br>";
    FLtext+="<b>File data: " + fn + "</b><br>";
    FLtext+="<table border=1><tr><th width=200>Bytes Loaded</th><th width=200>Bytes Total</th><th width=100>Progress</th></tr><tr><td>"+fl+"</td><td>"+ft+"</td><td>"+Math.round(fl*100/ft)+"%</td></tr></table><br><br>";
    FLtext+="<b>Total data</b><br>";
    FLtext+="<table border=1><tr><th width=200>Bytes Loaded</th><th width=200>Bytes Total</th><th width=100>Progress</th><th width=100>Loaded files</th><th width=100>Remain files</th></tr><tr><td>"+tl+"</td><td>"+tt+"</td><td>"+Math.round(tl*100/tt)+"%</td><td>"+loa+"</td><td>"+rem+"</td></tr></table><br><a href='javascript:cancelUpload();'>Cancel Upload</a><br>";
//    FL.innerHTML=FLtext;
  }
  function complete()
  {
    var FL=document.getElementById("fileList");
    var FLtext = "";
    FLtext="UPLOADING COMPLETE";
//    FL.innerHTML=FLtext;
    if(upload_type == "public") {
        document.location="/public.php?action=upload";
    } else {
        document.location="/upload.php?action=upload&upload_to=" + upload_to;
    }
  }
  function showMessage(msg)
  {
//    var H=document.getElementById("history");
//    H.innerHTML+="<br>UPLOADER: "+msg;
  }

  function overLimit() {
      alert("You cannot upload more then " + uploadLimit + " files");
  }


	function calcSpeed(progr)
	{
		var now = new Date();
		var nms = now.getMilliseconds();
		var ns = now.getSeconds();
		var nm = now.getMinutes();
		var nh = now.getHours();
		var nd = now.getDay();
		
		//alert(1);
		var sms = startDate.getMilliseconds();
		var ss = startDate.getSeconds();
		var sm = startDate.getMinutes();
		var sh = startDate.getHours();
		var sd = startDate.getDay();
		
		if (nd>sd)
		{
			nh = nh+24*(nd-sd);
		}
		if (nh>sh)
		{
			nm = nm+60*(nh-sh);
		}
		if (nm > sm)
		{
			ns = ns+60*(nm-sm);
		}
		if (ns > ss)
		{
			nms=nms+1000*(ns-ss);
		}
		var totms = nms-sms;
		seconds_ = totms / 1000;
		//alert(totms);
		//alert(progr[0]);
		var speed = Math.round((progr/(totms/1000)));
		return speed;
		
	}
