HTMLElement.prototype.getrel = function(){
  var rel = this.getAttribute("rel");
  var ret = {};
  if(rel){
    rel = rel.split('&');
    for(var i=0; i<rel.length; i++){
      var spl = rel[i].split('=');
      ret[spl[0]] = spl[1];
    }
  }
  return ret;
};
HTMLElement.prototype.childClean = function(){
  var cn = this.childNodes;
  for(var i=0; i<cn.length; i++){
    if(cn[i].nodeType == 3){
      this.removeChild(cn[i]);
      i--;
    }
  }
}
var Engine = {
  host: null,
  index: null,
  module: null,
  all: null,
  wrap: null,
  height: 0,
  page: location.href,
  host: null,
  
  effect: {
    conf: {from: 0, to: 100},
    Pre: function(){
      this.el.style.top = this.from+"px";
    },
    Run: function(){
      var h = (this.from+this.ratio*(this.to-this.from));
      this.el.style.top = h+"px";
      kropker.set(h);
    },
    Post: function(){
      this.el.style.top = this.to+"px";
    }
  },
  
  Lock: {
    lock: 0,
    run: function(call){
      if(this.lock){
        return setTimeout(function(){Engine.Lock.run(call)}, 40);
      }
      if(typeof(call) == "function"){
        call();
      }
    }
  },
  
  Init: function(){
    var url = location.href;
    url = url.substring(0, url.indexOf('.com/')+5);
    this.host = new RegExp("^"+url);
    this.index = new RegExp("^"+url+"$");
    this.module = module;
    this.all = document.getElementById("in");

    this.Hrefy();
    setInterval(function(){ Engine.CheckChange(); }, 100);
  },
  Hrefy: function(){
    var els = document.getElementsByTagName("A");
    for(var i=0; i<els.length; i++){
      var el = els[i];
      if(!el.engined && this.host.test(el.href)){
        el.onclick = function(e){
          return Engine.click(this);
        }
        el.engined = true;
      }
    }
  },
  
  click: function(el){
    if(el.getrel().ext 
      || el.href.indexOf("full.jpg") != -1 
      || el.href.indexOf("#") != -1 
      || el.href.indexOf("/filemanager/userfiles/") != -1
      || (/[0-9a-f]{12,16}/).test(el.href)
    ) return true;
    if(this.Lock.lock) return false;
    var index = (this.module == "index") || this.index.test(el.href);
    var url = "/ajax/"+el.href.replace(this.host, "");
    if(index){
      this.Lock.lock+=2;
      callMe(url+"?index=1", function(o){
        new Preloader(o.html, function(){ Engine.gotIndex(o); });
      });
      RunEffect(document.body, "fadeOut", {callback: function(e){
        Engine.Lock.lock--;
      }});
    }else{
      this.wrap = document.getElementById("wrap_all");
      this.wrap.childClean();
      this.height = Math.max(document.getElementById('all').offsetHeight-250, this.wrap.offsetHeight-100);
      this.wrap.style.height = this.height+"px";
      this.wrap.style.overflow = "hidden";
      this.Lock.lock+=2;
      callMe(url, function(o){
        new Preloader(o.html, function(){ Engine.gotNoIndex(o); });
      });
      this.addGradients();
      RunEffect(this.wrap.firstChild, this.effect, {from: 0, to: this.height+50, callback: function(e){
        Engine.Lock.lock--;
        kropker.start(20);
      }});
    }
    return false;
  },
  gotIndex: function(o){
    if(this.Lock.lock != 1){
      setTimeout(function(){ Engine.gotIndex(o); }, 40);
      return false;
    }
    this.metafy(o.header);
    this.module = o.module;
    this.all.innerHTML = o.html;
    document.body.parentNode.className = this.module=="index"?"index":"subsite";
    if(this.module == "index"){
      this.all.style.bottom = "";
      mainer();
    }
    this.Hrefy();
    RunEffect(document.body, "fadeIn", {callback: function(e){
      Engine.Lock.lock--;
      FB.XFBML.parse();
      gapi.plusone.go();
    }});
    kropker.Init();
    InitRowers();
  },
  gotNoIndex: function(o){
    if(this.Lock.lock != 1){
      setTimeout(function(){ Engine.gotNoIndex(o); }, 40);
      return false;
    }
    this.metafy(o.header);
    this.module = o.module;
    var el = document.createElement("div");
    el.className = "inner";
    el.innerHTML = o.html;
    el.style.visibility = "hidden";
    this.wrap.innerHTML = "";
    this.wrap.appendChild(el);
    this.height = el.offsetHeight;
    el.style.top = -this.height+"px";
    el.style.visibility = "";
    this.Hrefy();
    kropker.stop();
    
    var whiter = document.getElementById("whiter");
    if(o.whiter && whiter.style.display == "none"){
      changeOpac(whiter, 0);
      whiter.style.display = "";
      RunEffect(whiter, "fadeIn");
    }else if(!o.whiter && whiter.style.display != "none"){
      RunEffect(whiter, "fadeOut", {callback: function(){
        whiter.style.display = "none";
      }});
    }
    
    RunEffect(this.wrap.firstChild, this.effect, {from: -this.height, to: 0, callback: function(e){
      Engine.wrap.style.height = "";
      Engine.Lock.lock--;
      Engine.removeGradients();
      kropker.stop();
      FB.XFBML.parse();
      gapi.plusone.go();
    }});
    InitRowers();
  },
  metafy: function(meta){
    var head = document.getElementsByTagName("HEAD")[0];
    var ar = head.getElementsByTagName("META");
    for(var i=0; i<ar.length; i++){
      if(ar[i].getAttribute("property") && ar[i].getAttribute("property").substring(0,2) == "og"){
        head.removeChild(ar[i]);
        i--;
      }
    }
    if(meta){
      var el = document.createElement("div");
      el.innerHTML = meta;
      for(var i=0; i<el.childNodes.length; i++){
        head.appendChild(el.childNodes[i]);
      }
    }
  },
  rows: function(){
    return parseInt(document.getElementById("all").offsetWidth/200);
  },
  gradients: [],
  addGradients: function(){
    for(var i=0; i<this.rows(); i++){
      var top = document.createElement("div");
      var bot = document.createElement("div");
      top.className = "grad-top";
      bot.className = "grad-bot";
      top.style.left = (200*i)+"px";
      bot.style.left = (200*i)+"px";
      this.all.appendChild(top);
      this.all.appendChild(bot);
      this.gradients.push(top);
      this.gradients.push(bot);
    }
  },
  removeGradients: function(){
    for(var i=0; i<this.gradients.length; i++){
      this.all.removeChild(this.gradients[i]);
    }
    this.gradients = [];
  },
  CheckChange: function(){
    if(this.Lock.lock) return false;
    if(this.page != location.href){
      var el = {href: location.href, getrel: function(){ return ""; }};
      var url = location.href.split('#');
      if(url[1]){
        url[0] = url[0].replace(this.host, '').split('/');
        url[0].pop();
        url[0].pop();
        url[0].push(url[1].substring(1));
        url[0] = url[0].join('/');
      }
      el.href = url[0];
      this.click(el);
    }
  }
};
AddLoad(function(){ Engine.Init(); });
