<?xml version="1.0" encoding="EUC-JP"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">

 <head>
  <!-- 京 -->
  <meta http-equiv="content-type" content="application/xhtml+xml; charset=EUC-JP" />
  <meta http-equiv="content-style-type" content="text/css" />
  <title>mod_uploader</title>
  <link rel="stylesheet" type="text/css" href="../../css/default.css" media="screen" />
  <link rel="shortcut icon" href="../../img/favicon.ico" />
  <script type="text/javascript">
   // <![CDATA[
   var uploadID;
   var uploaderURL;
   var startTime;
   var update_timer;
   var is_no_sccess;
   var bar;

   function init()
   {
       if (document.URL.indexOf('?') == -1) {
           return alert('表示すべきステータスを見つけられませんでした．');
       }

       uploadID         = document.URL.split('?')[1];
       uploaderURL      = document.URL.split('?')[0].replace('/progress/', '/');
       startTime        = getTime();
       bar              = document.getElementById('bar');

       is_no_sccess     = true;
       fail_count       = 0;

       startUpdateProgress();
   }

   function startUpdateProgress()
   {
       // 表示は 1 秒毎に更新する．
       update_timer = setInterval("updateProgress()", 1000);
   }

   function updateProgress()
   {
       var progress;
       var progressArea;

       progressArea = document.getElementById('progress');
       progress = getProgress(uploaderURL + 'progress_data/');

       if (progress == null) {
           if (is_no_sccess) {
               progressArea.innerHTML = "Server Busy!";
           }

           if (++fail_count > 5) {
               clearInterval(update_timer);
           }

           return;
       } else {
           is_no_sccess = false;
           fail_count = 0;
       }

       progressArea.innerHTML = getProgressMessage(progress);

       bar.width = 600 * progress.getPercent() / 100;

       if (progress.readSize == progress.totalSize) {
           clearInterval(update_timer);
       }
   }

   function getProgressMessage(progress)
   {
       if (progress.totalSize == 0) {
           return progress.readSize + ' / アップロードサイズ不明';
       } else {
           return progress.readSize + ' / ' + progress.totalSize +
                  ' (' + progress.getPercent() + '%) @ ' + progress.getSpeed() +
                  ' [残り ' + progress.getRemainTime() + ']';
       }
   }

   function getProgress(url)
   {
       try {
           var request;
           var response;
           var timer;

           request = creatHTTPRequest();

           if (request.readyState != 4) {
               request.abort();
           }

           timer = setTimeout(function () {
               request.abort();
               clearTimeout(timer);
           }, 5000);

           request.open('POST', url + '?' + uploadID, false);
           request.send(null);
           clearTimeout(timer);

           if ((request.status != 200) || (request.responseText.charAt(0) != 's')) {
               return null;
           }

           response = request.responseText.split(' ');

           return new Progress(response[1], response[2]);
       } catch(e) {
           if (typeof e == 'object') {
               alert(e.message);
           } else {
               alert(e);
           }
       }

       return null;
   }

   function creatHTTPRequest()
   {
       var request;

       try {
           request = new ActiveXObject("Msxml2.XMLHTTP");
       } catch(e) {
           try {
               request = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (E) {
               request = false;
           }
       }

       if (!request) {
           if (typeof XMLHttpRequest != 'undefined') {
               request = new XMLHttpRequest();
           }
       }

       return request;
   }

   function normalizeNumber(number)
   {
       if (number*10%10 == 0) {
           number += '.0';
       }
       return number;
   }

   function Progress(totalSize, readSize)
   {
       this.totalSize   = totalSize;
       this.readSize    = readSize;

       this.getPercent = function()
       {
           var percent;

           percent = Math.round(readSize*100*10/totalSize) / 10;

           return normalizeNumber(percent);
       }

       this.getSpeed = function()
       {
           var elapsedTime;
           var bitPerSec;

           elapsedTime = getTime() - startTime;
           if (elapsedTime < 1) {
               return '--------';
           }

           bitPerSec = readSize*8 / elapsedTime;

           if (bitPerSec > 1024*1024*1024) {
               return normalizeNumber(Math.round(bitPerSec*10/(1024*1024*1024))/10) + ' Gbps';
           } else if (bitPerSec > 1024*1024) {
               return normalizeNumber(Math.round(bitPerSec*10/(1024*1024))/10) + ' Mbps';
           } else if (bitPerSec > 1024) {
               return normalizeNumber(Math.round(bitPerSec*10/1024)/10) + ' Kbps';
           } else {
               return normalizeNumber(Math.round(bitPerSec*10)/10) + ' bps';
           }
       }

       this.getRemainTime = function()
       {
           var elapsedTime;
           var bytePerSec;
           var remainSec;
           var hour;
           var min;
           var sec;

           elapsedTime = getTime() - startTime;
           if (elapsedTime < 1) {
               return '--:--:--';
           }

           bytePerSec = readSize / elapsedTime;
           remainSec = (totalSize-readSize) / bytePerSec;

           hour = Math.floor(remainSec/3600);
           if (hour < 10) {
               hour = '0' + hour;
           }

           min = Math.floor((remainSec%3600)/60);
           if (min < 10) {
               min = '0' + min;
           }

           sec = Math.floor(remainSec%60);
           if (sec < 10) {
               sec = '0' + sec;
           }

           return hour + ':' + min + ':' + sec;
       }

       return this;
   }

   function getTime()
   {
       return (new Date()).getTime() / 1000;
   }
   // ]]>
  </script>
 </head>

 <body onload="init()">

  <h1>Upload Progress</h1>

  <div class="description">
   アップロードの進行状況を表示しています．通信速度と残り時間はおおよその目安です．
   [<a href="javascript:javascript:window.close()"><em>閉じる</em></a>]
  </div>

  <div class="progress" id="progress">Loading...</div>

  <div style="background-image: url(../../img/bar_background.png); width: 600px;" class="bar_graph"><img src="../../img/bar_foreground.png" width="1" height="22" alt="" id="bar" /></div>

  <hr class="footer" />

  <div class="footer">
   Copyright(C) 2005 acapulco.dyndns.org
  </div>

  <div class="package_info">
   <a href="http://acapulco.dyndns.org/mod_uploader/">@PACKAGE_NAME@/@PACKAGE_VERSION@ is Free!</a>
  </div>

 </body>

</html>
