// The'name' of the managed store used by wp-offline
var STORE_NAME = 'wp-offline-managedstore';

function setError(s) {
  alert(s);
}

function wpo_ProgressBar(parent, width, fontSize, complete, cancel, barClass) {
  // ProgessBar constructor - Width and fontsize in pixels
  this.fillPixels = width - 2;
  this.step = 0;
  this.completeCallback = complete;

  this.outerDiv = document.createElement('div');
  this.outerDiv.width = width + 'px';
  parent.appendChild(this.outerDiv);

  this.outerDiv.appendChild(document.createTextNode("Downloading..."));
  this.outerDiv.style.fontFamily = 'arial, verdana';
  this.outerDiv.style.fontSize   = fontSize + 'px';
  this.outerDiv.style.lineHeight = fontSize + 'px';
  this.outerDiv.style.color      = '#000000';
  this.outerDiv.style.textAlign  = 'left';

  this.borderDiv = document.createElement('div');
  this.borderDiv.style.border     = '1px solid #949DAD';
  this.borderDiv.style.background = '#FFFFFF';
  this.borderDiv.style.width      = width + 'px';
  this.borderDiv.style.height     = '4px';
  this.borderDiv.style.margin     = '2px 0px';
  this.outerDiv.appendChild(this.borderDiv);

  this.messageDiv = document.createElement('div');
  this.messageDiv.innerHTML = "0% completed. Please wait...";
  this.outerDiv.appendChild(this.messageDiv);

  this.fillDiv = document.createElement('div');
  this.fillDiv.style.overflow = 'hidden';
  this.fillDiv.style.width    = '0px';
  this.fillDiv.style.height   = '2px';
  this.fillDiv.style.border   = '1px solid #FFFFFF';
  if(barClass) {
    this.fillDiv.className = barClass;
  } else {
    this.fillDiv.style.background = '#00008B';
    this.fillDiv.style.border     = '1px solid #FFFFFF';
    this.fillDiv.style.color      = '#FFFFFF';
  }
  this.borderDiv.appendChild(this.fillDiv);

  this.cancelA = document.createElement('a');
  this.cancelA.style.position = 'absolute';
  this.cancelA.style.bottom   = '3px';
  this.cancelA.style.right    = '3px';
  this.cancelA.href = '#';
  this.cancelA.onclick = cancel;
  this.cancelA.innerHTML = 'Cancel';
  this.outerDiv.appendChild(this.cancelA);
}

wpo_ProgressBar.prototype.setPercent = function(pct) {
  this.step = pct;
  // expects pct between 0.0 and 1.0
  var fillPixels;
  if(pct < 1.0) {
    fillPixels = Math.round(this.fillPixels * pct);
  } else {
    // Avoid rounding errors
    pct = 1.0;
    fillPixels = this.fillPixels;
  }
  this.messageDiv.innerHTML = Math.round(100 * pct) + "% completed. Please wait...";
  this.fillDiv.style.width  = fillPixels + 'px';

  if( pct >= 1.0 && this.completeCallback ) {
    this.completeCallback();
  }
}

