var etfile = {};
etfile.create = function (file, downloadItemId, noflashInfo) {
  var webUrl = location.protocol + "//" + location.host;
  // Globals
  // Major version of Flash required
  var requiredMajorVersion = 9;
  // Minor version of Flash required
  var requiredMinorVersion = 0;
  // Minor version of Flash required
  var requiredRevision = 28;

  // Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
  var hasProductInstall = DetectFlashVer(6, 0, 65);

  // Version check based upon the values defined in globals
  var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

  if ( hasProductInstall && !hasRequestedVersion ) {
  // DO NOT MODIFY THE FOLLOWING FOUR LINES
  // Location visited after installation is complete if installation is required
   	var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
   	var MMredirectURL = window.location;
    document.title = document.title.slice(0, 47) + " - Flash Player Installation";
    var MMdoctitle = document.title;

   	AC_FL_RunContent(
	  "src", "playerProductInstall",
      "FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"&",
      "width", "100%",
      "height", "100%",
      "align", "middle",
      "id", downloadItemId,
      "quality", "high",
      "bgcolor", "#f1fbfd",
      "name", "easyflvplayer",
      "allowScriptAccess","sameDomain",
      "allowFullScreen","true",
      "type", "application/x-shockwave-flash",
      "pluginspage", "http://www.adobe.com/go/getflashplayer"
    );
  } else if (hasRequestedVersion) {
    // if we've detected an acceptable version
    // embed the Flash Content SWF when all tests are passed
    AC_FL_RunContent(
      "src", webUrl + "/js/etfile/etfile",
      "width", "96",
      "height", "32",
      "align", "middle",
      "id", downloadItemId,
      "quality", "high",
      "bgcolor", "#ffffff",
      "name", downloadItemId,
      "allowScriptAccess","sameDomain",
      "allowFullScreen","true",
      "type", "application/x-shockwave-flash",
      "wmode", "transparent",
      "pluginspage", "http://www.adobe.com/go/getflashplayer",
      "flashVars", "file=" + file + "&process=etfile.updateProcessBarState&complete=etfile.completeHander&open=etfile.openHander&error=etfile.errorHander"
    );
  } else {  // flash is too old or we can't detect the plugin
	if (noflashInfo) {
	  document.write(noflashInfo);
	  return;
	}
    var alternateContent = '检测到您没有安装Flash控件，请您点击此处安装：'
      + '<a href=http://www.adobe.com/go/getflash/ target="_blank">安装Flash控件</a>';
    document.write(alternateContent);  // insert non-flash content
  }
};
/**
 * 文件下载类初始化
 *
 * @param file 要下载的文件名
 * @param downloadItemId 下载控件放置在页面上的节点Id号
 * @param processBarId 下载进度条放置在页面上的节点Id号
 * @param stopItemId 停止下载按钮放置在页面上的节点Id号
 * @param handers 用户注册的事件处理回调函数集合
 */
etfile.init = function (file, downloadItemId, processBarId, cancelItemId,
						handers) {
  etfile.file = file;
  etfile.downloadItem = document.getElementById(downloadItemId);
  etfile.processBar = document.getElementById(processBarId);
  etfile.cancelItem = document.getElementById(cancelItemId);
  etfile.handers = handers;

  etfile.processBar.style.display = "none";
  etfile.cancelItem.style.display = "none";
  etfile.cancelItem.onclick = etfile.cancel;
};

etfile._get_lever = function (num, base) {
  var returnValue = 0;
  while (num >= base) {
	num /= base;
	++returnValue;
  }
  return returnValue;
};

/**
 * 更新进度条，不要直接调用
 *
 * @param load 已下载的字节数
 * @param total 文件总字节数
 */
etfile.updateProcessBarState = function (load, total) {
  var offsetTime = ((new Date().getTime()) - etfile.beginTime) / 1000;
  var speed = load / offsetTime;
  var remainTime = Math.floor((total - load) / speed);
  var part = load / total;
  var persent = (part * 100).toFixed(2);
  var position = Math.floor(part * 400) - 800;  // 400是进度条的长度，800是背景图的长度

  var loadShow = "";
  var totalShow = "";
  var speedShow = "";
  var persentShow = persent + "%";
  var remainTimeShow = "";

  switch (etfile._get_lever(load, 1024)) {
  case 0: loadShow = load + "B"; break;
  case 1: loadShow = Math.floor(load / 1024) + "KB"; break;
  case 2: loadShow = (load / 1024 / 1024).toFixed(2) + "MB"; break;
  case 3: loadShow = (load / 1024 / 1024 / 1024).toFixed(2) + "GB"; break;
  default: loadShow = (load / 1024 / 1024 / 1024 / 1024).toFixed(2) + "TB"; break;
  }
  switch (etfile._get_lever(total, 1024)) {
  case 0: totalShow = total + "B"; break;
  case 1: totalShow = (total / 1024).toFixed(2) + "KB"; break;
  case 2: totalShow = (total / 1024 / 1024).toFixed(2) + "MB"; break;
  case 3: totalShow = (total / 1024 / 1024 / 1024).toFixed(2) + "GB"; break;
  default: totalShow = (total / 1024 / 1024 / 1024 / 1024).toFixed(2) + "TB"; break;
  }

  switch (etfile._get_lever(speed, 1024)) {
  case 0: speedShow = speed + "B/秒"; break;
  default: speedShow = Math.floor(speed / 1024) + "KB/秒";
  }

  switch (etfile._get_lever(remainTime, 60)) {
  case 0: remainTimeShow = remainTime + "秒"; break;
  case 1: remainTimeShow = Math.floor(remainTime / 60) + "分"
	  + (remainTime % 60) + "秒"; break;
  default: remainTimeShow = Math.floor(remainTime / 3600) + "小时"
	  + (remainTime % 60) + "分" + (remainTime % 3600) + "秒"; break;
  }

  etfile.processBar.style.backgroundPosition = position + "px 0px";
  etfile.processBar.innerHTML = "剩余时间:" + remainTimeShow + "(已下载" + loadShow + ",共" + totalShow + ")&nbsp;速度" + speedShow;

  if (etfile.handers != null
	  && typeof(etfile.handers.processHander) == "function") {
	etfile.handers.processHander(load, total);
  }
};

etfile.openHander = function () {
  etfile.beginTime = (new Date()).getTime();

  etfile.downloadItem.width = 1;
  etfile.downloadItem.height = 1;
  etfile.processBar.style.display = "block";
  etfile.cancelItem.style.display = "block";

  if (etfile.handers != null
	  && typeof(etfile.handers.openHander) == "function") {
	etfile.handers.openHander();
  }
};

etfile.completeHander = function (fileName) {
  etfile.cancelItem.style.display = "none";
  etfile.processBar.style.display = "none";

  if (etfile.handers != null
	  && typeof(etfile.handers.completeHander) == "function") {
	etfile.handers.completeHander(fileName);
  }
};

etfile.cancel = function () {
  etfile.downloadItem.cancelDownload();
  etfile.cancelItem.style.display = "none";
  etfile.processBar.style.display = "none";
  etfile.downloadItem.width = 96;
  etfile.downloadItem.height = 32;

  if (etfile.handers != null
	  && typeof(etfile.handers.cancelHander) == "function") {
	etfile.handers.cancelHander();
  }
};

etfile.errorHander = function (message) {
  alert("发生了错误，请您重试或联系网站管理员。错误描述:" + message);
};