var grid = [5,2];
var start = 0;
var portel;
var portbody = [120, 405];
var Lock = {
  lock: 0,
  run: function(call){
    if(this.lock){
      return setTimeout(function(){Lock.run(call)}, 40);
    }
    if(typeof(call) == "function"){
      call();
    }
  }
};

function InitEngine(){
  DOMChanged();
  StartShow();
}
AddLoad(InitEngine);

function StartShow(){
  if(IE){
    document.getElementById("blackie").style.zIndex = "0";
    return;
  }
  Lock.lock++;
  var el = document.getElementById("mainbox");
  var child, els = [], blackel;
  function Iterate(e){
    for(var i=0; i<e.childNodes.length; i++){
      child = e.childNodes[i];
      if(child.nodeType != 3){
        if(child.className == "in"){
          Iterate(child);
        }else if(child.className != "black"){
          changeOpac(child, 0);
          els.push(child);
        }else{
          blackel = child;
        }
      }
    }
  }
  Iterate(el);
  el = document.getElementById("port");
  if(el){
    el.style.display = "none";
    Lock.run(function(){
      Projector.project = 1;
      while(el.firstChild.nodeType == 3){
        el.removeChild(el.firstChild);
      }
      el = el.firstChild;
      changeOpac(el, 0);
      el.parentNode.style.display = "";
      while(el.lastChild.nodeType == 3){
        el.removeChild(el.lastChild);
      }
      while(el.firstChild.nodeType == 3){
        el.removeChild(el.firstChild);
      }
      Projector.el = el;
      RunEffect(el, "fadeIn", {speed: 200});
    });
  }
  document.body.removeChild(document.getElementById("hider"));
  blackel.style.zIndex = "0";
  Lock.lock += els.length;
  run = function(){
    var e = els.shift();
    if(!e) return;
    RunEffect(e, "fadeIn", {speed: 1000, callback: function(){Lock.lock--;}});
    run();
  };
  setTimeout(run, 1000);
  Lock.lock--;
}

function DOMChanged(){
  var as = document.links, a;
  for(var i=0; i<as.length; i++){
    a = as[i];
    if(!a.engined){
      a.onclick = ResolveA;
      a.engined = true;
    }
  }
  _typeface_js.renderDocument();
}
function ResolveA(e){
  if(Lock.lock) return false;
  Lock.lock++;
  if(!e) e = window.event;
  var mod = 'index';
  var nowmod = 'index';
  var rel = ParseRel(this);
  if(rel.ext || IE){
    Lock.lock--;
    return true;
  }else{
    if(rel.project){
      Projector.Show(this);
    }else if(rel.util == "close"){
      Projector.Close();
    }else if(rel.util == "navi"){
      Projector.Navi(this, rel.side);
    }
  }
  Lock.lock--;
  return false;
}
function ParseRel(el){
  var rel = el.getAttribute('rel');
  if(!rel) return {};
  rel = rel.split('&');
  var ret = {}, t;
  for(var i=0; i<rel.length; i++){
    t = rel[i].split('=');
    ret[t[0]] = t[1];
  }
  return ret;
}

var Projector = {
  project: 0,
  el: null,
  lock: 0,
  
  Show: function(a){
    if(this.project){
      return this.Close(function(){ Projector.Show(a); });
    }
    a.blur();
    Lock.lock++;
    var rel = ParseRel(a);
    this.project = 1;

    callMe("ajax/"+a.href.split("/").pop(), function(o){ new Preloader(o, function(){ Projector.Loaded(o) }); });
    
    this.el = document.createElement('div');
    this.el.id = "port";
    var el = document.createElement('div');
    el.className = "in";
    changeOpac(el, 0);
    this.el.appendChild(el);
    document.body.appendChild(this.el);
    this.el = el;
    this.lock++;
    RunEffect(this.el, "fadeIn", {speed: 200, callback: function(){
      Projector.lock--;
    }});
  },
  Navi: function(a, side){
    Lock.lock++;
    this.lock++;
    callMe("ajax/"+a.href.split("/").pop(), function(o){ new Preloader(o, function(){ Projector.Loaded(o, side) }); });
    RunEffect(this.el.firstChild, "moveOut", {speed: 200, side: side==1?"left":"right", callback: function(e){
      e.parentNode.removeChild(e);
      Projector.lock--;
    }});
  },
  Loaded: function(o, side){
    if(this.lock){
      return setTimeout(function(){ Projector.Loaded(o) }, 40);
    }
    this.el.innerHTML = o.html;
    while(this.el.firstChild.nodeType == 3){
      this.el.removeChild(this.el.firstChild);
    }
    while(this.el.lastChild.nodeType == 3){
      this.el.removeChild(this.el.lastChild);
    }
    this.el.firstChild.style.display = "";
    if(side == 1 || side == -1){
      this.el.firstChild.style.display = "";
      RunEffect(this.el.firstChild, "moveIn", {speed: 200, side: side==1?"right":"left", html: this.el.firstChild.innerHTML, callback: function(){
        Lock.lock--;
        DOMChanged();
      }});
    }else{
      DOMChanged();
      changeOpac(this.el.firstChild, 0);
      this.el.firstChild.style.display = "";
      RunEffect(this.el.firstChild, "fadeIn", {speed: 200, callback: function(){
        Lock.lock--;
      }});
    }
  },
  Close: function(call){
    if(!this.project){
      if(typeof(call) == "function"){
        call();
      }
      return;
    }
    Lock.lock++;
    this.project = 0;
    RunEffect(this.el, "fadeOut", {speed: 400, callback: function(){
      Projector.el.parentNode.parentNode.removeChild(Projector.el.parentNode);
      Projector.el = null;
      Lock.lock--;
      if(typeof(call) == "function"){
        call();
      }
    }});
    curURL = curURL.replace(/,\d+/,'');
    location.href = "#"+curURL;
  }
}
