// CONFIG
var startPage='step1';
var rootPath='/order2/loopqual';
var xmlPath=rootPath+'/xml';
var jsPath=rootPath+'/js';

// OBJECTS
var debug=new DebugTools(false);
var pageMgr=new PageManager();
var errMgr=new ErrorManager();
var loadMgr=new AsyncLoadManager();
var currentJs=null;
var currentKey=null;

var dataMap=new HashMap();
dataMap.readString(top.window.location.search.substr(1));
dataMap.set('domain',window.location.host);

// Run Time
loadMgr.setCallback(function(){top.mainFrameLoaded=true;});
loadMgr.loadingBegin();
window.onload=function(){loadMgr.loadingEnd();}
window.onunload=function(){top.mainFrameLoaded=false;}

// Functions
function loadPage(pName){
  debug.printLn('loadPage: '+pName);
  pageMgr.get(currentKey).completed=true;
  if(pageMgr.get(pName)!=null)pageMgr.get(pName).needParsing=true;
  top.historyFrame.location='./index_history.vm?page='+pName;
}
function reloadPage(pName){
  debug.printLn('reloadPage: '+pName);
  pageMgr.get(currentKey).completed=true;
  if(pageMgr.get(pName)!=null)pageMgr.get(pName).needParsing=true;
  pageHandler(pName);
}
function refreshPage(pName){
  debug.printLn('refreshPage: '+pName);
  pName=pName||startPage;
  document.body.removeChild(pageMgr.get(pName).container);
  pageMgr.remove(pName)
  fetch(pName);
}
function pageHandler(pName){
  debug.printLn('pageHandler: '+pName);
  debug.printLn('currentKey: '+currentKey);
  pageMgr.hideAll();
  pName=pName||startPage;
  if(currentKey==null){
    fetch(startPage);
  }else if(pageMgr.get(pName)!=null){
    pageMgr.showPage(pName);
  }else if(pageMgr.get(currentKey).completed==false){
    pageMgr.showPage(currentKey);
  }else{
    fetch(pName);
  }
}
function fetch(pName){
  loadMgr.loadingBegin();
  if(pName==null)return;
  var url=xmlPath+'/'+pName+'.xml?rnd='+Math.random();
  var http=new HttpRequest();
  http.getXml('GET',url,execute);
  delete http;
  function execute(xml){
    if(xml==null)return;
    var xm=new XmlManager(xml);
    // Create Page Object;
    var page=pageMgr.set(pName);
    page.setHtml(xm.getFirstValue('/content/html'));
    page.setTitle(xm.getFirstValue('/content/title'));
    page.setJs(eval(xm.getFirstValue('/content/js')));
    page.show();
    loadMgr.loadingEnd();
  }
}
function PageManager() {
  var pageMap=new HashMap();
  this.set=function(pName){
    var page=new Page(pName);
    pageMap.set(pName,page);
    return page;
  }
  this.get=function(pName){
    return pageMap.get(pName);
  }
  this.remove=function(pName){
    return pageMap.remove(pName);
  }
  this.size=function(){
    return pageMap.size();
  }
  this.hideAll=function(){
    var pages=pageMap.getValues();
    for(var i=0;i<pages.length;i++){
      pages[i].hide();
    }
  }
  this.showPage=function(pName){
    pageMap.get(pName).show();
  }
  function Page(key){
    this.haveParsing=true;
    this.needParsing=true;
    this.completed=false;
    this.key=key;
    this.setHtml=function(x){
      this.html=x;
    }
    this.setTitle=function(x){
      this.title=x;
    }
    this.setJs=function(js){
      this.js=js;
      this.js.page=this;
    }
    this.hide=function(){
      if(this.container==null)return;
      this.container.style.display='none';
    }
    this.parseHtml=function(){
      if(!this.haveParsing) return;
      if(!this.needParsing) return;
      var html=this.html;
      var count=0;
      var matched=html.match(new RegExp('\\${[^}]+}','g'));
      if(matched!=null){
        for(var i=0;i<matched.length;i++){
          var name=matched[i].substring(2,(matched[i].length-1));
          try{var value=eval(name);}catch(e){continue;};
          html=html.replace(matched[i],value);
          count++;
        }
      }
      if(count==0)this.haveParsing=false;
      if(this.container!=null) {
        document.body.removeChild(this.container);
      }
      this.container=document.createElement('div');
      document.body.appendChild(this.container);
      this.container.innerHTML=html;
      debug.printLn(this.key+'.haveParsing: '+this.haveParsing);
      this.needParsing=false;
    }
    this.show=function(){
      window.scroll(0,0);
      currentJs=this.js;
      currentKey=this.key;
      var init=this.js.init();
      if(init==null)init=true;
      if(!init)return;
      this.parseHtml();
      this.js.onload();
      top.document.title=this.title;
      top.historyFrame.document.title=this.title;
      this.container.style.display='block';
    }
  }  
}
