//
// Javascript functions to provide progress bar
//
var ProgressBar = Class.create({

  initialize: function(request_json, oncomplete, barid)
  {
    this._request_url = request_json.url;
    this._oncomplete = oncomplete;
    this._freq = request_json.delay;
    this._linkmode = request_json.linkmode;
    this._extralinktext = request_json.extralinktext;
    this._extralinkdate = request_json.extralinkdate;
    this._barid = barid;
    this._complete = false;
    this._nohideoncompletion = request_json.nohideoncompletion;
    this.nocollectbutton = false;
    window.setTimeout(this.update_progress_info.bind(this), request_json.initialDelay || this._freq);
    if(document.getElementById('progress-bar_' + this._barid))
    {
      document.getElementById('progress-bar_' + this._barid).style.marginLeft = 0;
    }
  },

  // Update progress bar
  update_progress_info: function() 
  {
    new Ajax.Request(this._request_url, {
      method: "get",
      onSuccess: this._OnLoad.bind(this)

    });
  },

  _OnLoad: function(data)
  {
    res = data.responseJSON;
    if (res.innerHTML && $('progress-container_' + this._barid)) {
      $('progress-container_' + this._barid).update(res.innerHTML);
    } else {
      var progress = parseInt(res.progress);
      var progress_width = 0;
      if(document.getElementById('progress-surround_' + this._barid))
      {
        document.getElementById('progress-surround_' + this._barid).style.display='';
      }
      if(document.getElementById('progress-bar_' + this._barid))
      {
        document.getElementById('progress-bar_' + this._barid).style.width = progress + '%';
      }
      if(document.getElementById('progress-info_' + this._barid))
      {
        var strVal = "Unknown";
        if(res.message)
        {
          strVal = res.message;
        }
        if(res.link)
        {
          if(res.linktext)
          {
            strVal = res.linktext;
          }
          if(this._extralinktext && res.extratext)
          {
            strVal += " - " + res.extratext;
          }
          if(this._extralinkdate && res.time)
          {
            strVal += "<small> - " + res.time + "</small>";
          }
          if(this._linkmode == "imsbutton")
          {
            var objButton = IU.genIMSButton(strVal, {onclick: function(){window.location.href=res.link;}, submit: true});
            if($('progress-button_' + this._barid))
            {
              $('progress-button_' + this._barid).appendChild(objButton);
              $('progress-info_' + this._barid).innerHTML = res.message;
            }
            else if (this.nocollectbutton) {
              $('progress-info_' + this._barid).innerHTML = res.message;
            }
            else
            {
              $('progress-info_' + this._barid).appendChild(objButton);
            }
          }
          else
          {
            if(this._linkmode == "button")
            {
              strVal = "<input class=\"imsbutton\" value=\"" + strVal + "\" name=\"htmlbutton\" id=\"\" onclick=\"window.location.href='" + res.link + "';\" type=\"button\">";
            }
            else
            {
              strVal = "<a href=\"" + res.link + "\">" + strVal + "</a>";
            }

            if(document.getElementById('progress-surround_' + this._barid) && !this._nohideoncompletion)
            {
              document.getElementById('progress-surround_' + this._barid).style.display='none';
            }
            if(document.getElementById('progress-button_' + this._barid))
            {
              document.getElementById('progress-button_' + this._barid).innerHTML = strVal;
              document.getElementById('progress-info_' + this._barid).innerHTML = res.message;
            }
            else if (this.nocollectbutton) {
              document.getElementById('progress-info_' + this._barid).innerHTML = res.message;
            }
            else
            {
              document.getElementById('progress-info_' + this._barid).innerHTML = strVal;
            }
          }
        }
        else
        {
          document.getElementById('progress-info_' + this._barid).innerHTML = strVal;
        }
      }
      var dismissEl = $("download-dismiss_"+this._barid);
      if (dismissEl) 
      {
        if (res.allowdismiss) dismissEl.show();
        else dismissEl.hide();
      }
    }  
    if(res.complete && !this._complete)
    {
      this._complete = true;
      eval(this._oncomplete);
    }
    if(res.nextrequest > 0)
    {
      window.setTimeout(this.update_progress_info.bind(this), res.nextrequest);
    }
    else if(!this._complete)
    {
      window.setTimeout(this.update_progress_info.bind(this), this._freq);
    }
  }
});

