var myglobals = new Object();

function StatusBar(ElementId, width, height, maxValue, progressbar)
{
  var self = this;
  var max = progressbar!=null ? width - (12 + progressbar) : width - 8;
  var factor = max / maxValue;
  var oldValue = 0;
  var barElement = null;
  var progressElement = null;
  var percentElement = null;

  myglobals.max = max;
  myglobals.factor = factor;

  construct(ElementId, width, height, progressbar);

  this.setValue = function(value)
  {
    if (value == oldValue)
      return;

    oldValue = value;
    value = value * factor;
    if (!isFinite(value))
      value = 0;

    var width = Math.round(value);

    if (progressElement) {
      progressElement.setAttribute("width", (width > max) ? max : width);
    }
    else if (barElement)
    {
     barElement.setAttribute("width", (width > max) ? max : width);
    }

    var percent = Math.round(100 * value / max)

    if (percent > 100)
      percent = 100;

    if (percentElement)
      percentElement.innerHTML = percent;
  };

  this.setMax = function(value)
  {
    factor = max / value;
    myglobals.factor = factor;
    myglobals.max = value;
  };

  function construct(ElementId, width, height, progressbar)
  {
    var element = document.getElementById(ElementId + "_BAR");

    if (element)
      barElement = createBar(element, width, height, progressbar);

    percentElement = document.getElementById(ElementId + "_PERCENT");
   }

  function createBar(element, width, height, progress)
  {
    var child = document.createElement("table");
    child.width = width;
    child.border = 0;
    child.cellspacing = 0;

    var style = getStyle(".bg0");
    child.style.borderColor = style.backgroundColor;
    child.style.borderStyle = "solid";
    child.style.borderWidth = "1px";
    element.appendChild(child);

    child = child.insertRow(0);
    element = child.insertCell(0);

    var bar = document.createElement("table");
    bar.border = 0;
    bar.cellspacing = 0;

    element.appendChild(bar);

    child = bar.insertRow(0);
    child.setAttribute("height", height);
    child.className = "bar";

    if (progress!=null)
    {
      progressElement = child.insertCell(0);
      progressElement.width = "1"; // cannot be set to 0 in IE
    }

    child = child.insertCell(progress ? 1 : 0);
    child.width = progress!=null ? progress : "100%";

    // status bar
    //style = getStyle(".workingscreen");
    child.className = "workingscreen";

    return bar;
  }

  function getStyle(selector)
  {
    var sheets = document.styleSheets;
    for (var i = 0; i < sheets.length; i++)
    {
      var rules = sheets[i].cssRules;
      if (!rules)
        rules = sheets[i].rules;
      for (var j = 0; j < rules.length; j++)
      {
        if (rules[j].selectorText.toLowerCase() == selector)
        {
          return rules[j].style;
        }
      }
    }
  }
}

function EMPStatusBar(ElementId, url, width, height, idCurrent, idMax, sleep)
{
  var self = this;
  var store = new ValueStore(url);
  var sleep = 500;
  var firstSleep = 2000;
  // wait a while in order to wait for correct max to be set
  var bar;
  construct();

  function MaxListener()
  {
    var self = this;

    this.onSuccess = function(value)
    {
      bar.setMax(parseInt(value));
      setTimeout(self.requestValue, sleep);
    };

    this.onError = function(value)
    {
      //alert("Failed to read max (" + value + ")");
    };

    this.requestValue = function()
    {
      store.getValue(idMax, self);
    };

    this.firstRequestValue = function()
    {
      setTimeout(self.requestValue, firstSleep);
    };

  }

  function ValueListener()
  {
    var self = this;

    this.onSuccess = function(value)
    {
      bar.setValue(parseInt(value));
      setTimeout(self.requestValue, sleep);
    };

    this.onError = function(value)
    {
      //alert("Failed to read current (" + value + ")");
    };

    this.requestValue = function()
    {
      store.getValue(idCurrent, self);
    };

    this.firstRequestValue = function()
    {
      setTimeout(self.requestValue, firstSleep);
    };
  }

  function construct()
  {
    if (!width)
      width = 250;
    if (!height)
      height = 15;
    var parameters = getParameters();
    if (!idMax)
      idMax = parameters["max"];
    if (!idCurrent)
      idCurrent = parameters["current"];
    if (sleep != null)
      firstSleep = sleep;

    bar = new StatusBar(ElementId, width, height, 100, null);
    (new MaxListener()).firstRequestValue();
    (new ValueListener()).firstRequestValue();
  }

  function getParameters()
  {
    var url = window.location.href;
    var lists = url.split("?");
    if (lists.length <= 1)
      return new Object();
    lists = lists[1].split("&");
    var parameters = {};
    for (var i = 0; i < lists.length; i++)
    {
      var parameter = lists[i].split("=");
      parameter[1] = lists[i].slice(lists[i].indexOf("=") + 1);
      parameters[parameter[0]] = parameter[1];
    }
    var str = "";
    for (var i in parameters)
      str += "parameters." + i + " = " + parameters[i] + "\n";
    return parameters;
  }
}

function ValueStore(storeUrl)
{
  var xmlhttp;
  var url = storeUrl;
  var self = this;

  function createXmlHttp()
  {
    var xmlhttp = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
    @end @*/
    if (!this.xmlhttp && typeof XMLHttpRequest != 'undefined')
    {
      mozilla = true;
      xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
  }

  this.getValue = function(id, listener)
  {
    httpGet("getValue?id=" + id, listener);
  };

  this.removeValue = function(id, listener)
  {
    httpGet("removeValue?id=" + id, listener);
  };

  function httpGet(target, listener)
  {
    var xmlhttp = createXmlHttp();
    xmlhttp.onreadystatechange = function()
    {
      if (xmlhttp.readyState == 4)
      {
        if (xmlhttp.status == 200)
        {
          listener.onSuccess(xmlhttp.responseText);
        }
        else
        {
          listener.onError(xmlhttp.status);
        }
      }
    };
    xmlhttp.open("GET", url + target, true);
    xmlhttp.setRequestHeader("Content-type", "text/xml");
    xmlhttp.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 04:58:08 GMT');
    xmlhttp.send(null);
  }
}

function EMPProgressBar(ElementId, width, height, sleep)
{
  var self = this;
  var sleep = sleep;
  var bar;
  var value = 0;
  var factor = 1;

  construct();

  function construct()
  {
    if (!width)
      width = 250;
    if (!height)
      height = 15;
    bar = new StatusBar(ElementId, width, height, 100,  25);
    progress();
  }

  function progress()
  {
    value += factor*2;
    if (value >= 100)
    {
      value = 100;
      factor = -1;
    }
    if (value <= 0) {
      value = 0;
      factor = 1;
    }
    bar.setValue(value);
    setTimeout(progress, sleep);
  }
}