SlideProjects = Class.create(Effect.Base, {
  initialize: function(from, to) {
    this.companycontainer = $('companycontainer');
    this.projectscontainer = $('projectscontainer');
    this.wallcontainer = $('wallcontainer');
    this.company = $('company');
    this.bg = $$('#company .bg').first();
    this.wall = $('wall');

    var args = $A(arguments);
    var options = args.last();

    this.start(Object.extend({ from: from, to: to }, options));
  },
  update: function(position) {
    var w = this.company.getWidth() - 95;
    this.wallcontainer.setStyle({
      right: Math.round(95 + (1 - position) * w)+'px'
    });

/*    this.bg && this.bg.setStyle({
      backgroundSize: (50+position*50) +'% 100%'
    });*/
  }
});
var state, initialState; 
var lock = false;
var content_cache = {};
var queue = 0;
function toggleProjects() {
  if (lock) {
    queue ++;
  } else {
    lock = true;
    var sp = new SlideProjects(state, 1-state, {
      duration: 2,
      afterFinish: function() {
        lock = false;
        if (queue > 0) {
          queue --;
          toggleProjects();
        }
      }
    });
    var wsl = $$('#wall a').first();
    wsl.fade({
      afterFinish: function() {
        $$('#company .fillable').invoke('toggle');
        $$('#projects .fillable').invoke('toggle');

        wsl.toggleClassName('projects-link');
        wsl.toggleClassName('company-link');
        if (wsl.hasClassName('projects-link')) {
          wsl.href = '/projects';
        } else {
          wsl.href = '/';
        }

        wsl.appear();
      }});
    state = 1-state;
  }
}
function _content_for(href, targetState) {
  var sel = targetState == 0 ? '#projects .fillable' : '#company .fillable';
  if (content_cache[href]) {
    window.History.replaceState({state: targetState}, content_cache[href].title, href);
    $$(sel).invoke('update', content_cache[href].html)
    processContentAfterReload();
  } else {
    new Ajax.Request(href, {
      onSuccess: function(tr) {
        var d = new Element('div').update(tr.responseText);
        var html = d.select(sel).first().innerHTML;
        content_cache[href] = {};
        var dom_title = d.select('title').first();
        if (!dom_title) {
          content_cache[href].title = new RegExp('<title>(.*)</title>').exec(tr.responseText)[1];
        } else {
          content_cache[href].title = dom_title.innerHTML;
        }
        content_cache[href].html = html;
        window.History.replaceState({state: targetState}, content_cache[href].title, href);
        $$(sel).invoke('update', html)
        processContentAfterReload();
      }
    })
  }
  if (state != targetState) {
    toggleProjects();
  }
}
function processContentAfterReload() {
  if (!arguments.callee.bgimage) {
    var msg_count = 7;
    arguments.callee.bgimage = 1 + Math.floor(msg_count*Math.random())
  }
  $$('#company > .fillable > .bg').invoke('setStyle', {backgroundImage: "url('/main-image-"+ arguments.callee.bgimage +".jpg')"});

  $$('#company a[href^=/projects/]').invoke('addClassName', 'projects-link');
  $$('#projects a[href^=/news/]').invoke('addClassName', 'company-link');
}
document.observe('dom:loaded', function() {
  processContentAfterReload();

  initialState = state = $$('.padded.showright').length == 0 ? 1 : 0;
  $$('.projects-link, .company-link').invoke('observe', 'click', function(ev) {
    ev.stop();

    if (!content_cache[document.location.href]) {
      content_cache[document.location.href] = {};
      content_cache[document.location.href].title = document.title;
      content_cache[document.location.href].html = $$(state == 0 ? '#projects .fillable' : '#company .fillable').first().innerHTML;
    }

    /*_content_for(ev.findElement('a').href, ev.target.hasClassName('projects-link') ? 0 : 1);*/
    var a = ev.findElement('a');
    window.History.pushState({state: a.hasClassName('projects-link') ? 0 : 1}, null, a.href);
  })
});

(function(window,undefined){
  var hst = window.History;
  if ( !hst.enabled ) {
    return false;
  }

  hst.Adapter.bind(window,'statechange',function(){
    var st = hst.getState();
    _content_for(st.cleanUrl, st.data.state == undefined ? initialState : st.data.state);
  });
})(window);

function goto_tab(t) {
  $$('.projects-slice').invoke('hide');
  $$('.'+ t +'-projects').invoke('show');
  $$('.projects-slice-link').invoke('up').invoke('removeClassName', 'selected');
  $$('#'+ t +'-projects-link').invoke('up').invoke('addClassName', 'selected');
  return false;
}

Event.observe(window, 'scroll', function(e) {
  var sy = document.viewport.getScrollOffsets().top;
  window.zp_scrollables = window.zp_scrollables || $$('.slowly_scrolling:not(#projectscontainer)');
  window.zp_scrollables.invoke('setStyle', {
    backgroundPositionY: sy/5 + 'px'
  })
  window.zp_lockables = window.zp_lockables || $$('#projectscontainer');
  window.zp_lockables.invoke('setStyle', {
    backgroundPositionY: sy + 'px'
  })
})

