/*
Flow
*/

/*
Public functions
*/

function Flow(flow, loader, msg) {
  this.flow=document.getElementById(flow);
  this.flow=(typeof this.flow == 'undefined')?null:this.flow;
  this.loader=document.getElementById(loader);
  this.loader=(typeof this.loader == 'undefined')?null:this.loader;
  this.response='';
  this.msg=(msg == null)?'?':msg;

  f=this;
  if (typeof window.onresize!='function') {
    window.onresize = function() { f.onresize(); }
  } else {
    var old_onresize = window.onresize;
    window.onresize = function() { old_onresize(); f.onresize(); }
  }
}

function Flow_show(uri) {
  f=this;
  // Show loader
  if (f.flow) f.flow.style.display = 'none';
  if (f.loader) f.loader.style.display = 'block';
  // Fetch...
  var http = new Flow_HTTP();
  http.open("GET", uri, true);
  http.onreadystatechange = function() {
    f.response = '';
    if (http.readyState == 4) {
      try {
        f.response = http.responseText;
      } catch(e) {
        f.response = 'Oops.';
      }
      // Display
      if (f.response=='')
        f.response = '<span class="noresults">' + f.msg + '</span>';
      f.display();
      // Hide loader
      if (f.flow) f.flow.style.display = 'block';
      if (f.loader) f.loader.style.display = 'none';
    }
  }
  http.send(null);
}
Flow.prototype.show=Flow_show;

function Flow_HTTP() {
  var xmlhttp;
  if (typeof XMLHttpRequest != 'undefined') {
    xmlhttp=new XMLHttpRequest();
  } else {
    if (typeof ActiveXObject !='undefined') {
      try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {}
    }
  }
  return xmlhttp;
}

function Flow_showonload(uri) {
  f=this;
  if (typeof window.onload!="function") {
    window.onload = function() { f.show(uri); }
  } else {
    var old_onload = window.onload;
    window.onload = function() { old_onload(); f.show(uri); }
  }
}
Flow.prototype.showonload=Flow_showonload;

function Flow_onresize() {
  this.display();
}
Flow.prototype.onresize=Flow_onresize;

function Flow_display() {
  if (!this.flow)
    return;
  if (this.response!='') {
    this.flow.innerHTML = this.response;
    $('.flow-header').bind('click', function(evt) { evt.preventDefault(); $(this).next().toggle('slow'); return false; } ).next().hide();
    this.response = '';
  }
}
Flow.prototype.display=Flow_display;
