/* $Id$ */

function changeLegend(filter,sort) {
  if(!sort) State.filter = filter;
  if(sort) State.sort = sort;
  new Ajax.Updater('navigation-container', ajaxLink('navigation'), { method: 'get', onComplete: function() { $('navigation').scrollUpdate(); } });
  new Ajax.Updater('legend', ajaxLink('legend'), { method: 'get' });
}

function loadProject(project_id) {
  if(State.HomeSlideShow) State.HomeSlideShow.stop();
  if(typeof slideDelay != 'undefined') clearInterval(slideDelay);
  State.project_id = project_id;
  State.image_id = '';
  if(State.mode != 'project') {
    if(!$('content-small').hasClassName('flexcrollactive') && State.safari != true) {
      CSBfleXcroll('content-small');
      CSBfleXcroll('sub-nav');
    }
    if($('about-content')) $('about-content').remove();
    $('cls').show();
  }
  State.mode = 'project';
  new Ajax.Updater('navigation-container', ajaxLink('navigation'), { method: 'get', onComplete: function() { $('navigation').scrollUpdate(); } });
  new Ajax.Updater('sub-nav-container', ajaxLink('project_nav'), { method: 'get', evalScripts: true, onComplete: function() { $('sub-nav').scrollUpdate(); } });
  new Ajax.Updater('content-small-container', ajaxLink('project_desc'), { method: 'get', onComplete: function() { $('content-small').scrollUpdate(); } });
}

function loadAbout(chapter) {
  if(State.HomeSlideShow) State.HomeSlideShow.stop();
  if(typeof slideDelay != 'undefined') clearInterval(slideDelay);
  State.chapter = chapter;
  if(State.mode != 'about') {
    if(!$('content-small').hasClassName('flexcrollactive') && State.safari != true) {
      CSBfleXcroll('content-small');
      CSBfleXcroll('sub-nav');
    }
    State.mode = 'about';
    $('cls').hide();
    new Ajax.Updater('sub-nav-container', ajaxLink('about_info'), { method: 'get', evalScripts: true, onComplete: function() { $('sub-nav').scrollUpdate(); } });
  }
  new Ajax.Updater('content-small-container', ajaxLink('about_nav'), { method: 'get', onComplete: function() { $('content-small').scrollUpdate(); } });
  new Ajax.Updater('container', ajaxLink('about_content'), { method: 'get', onComplete: function() { $('content').scrollUpdate(); } });
}

function loadProjectImage(image_id) {
  State.image_id = image_id;
  new Ajax.Updater('sub-nav-container', ajaxLink('project_nav'), { method: 'get', evalScripts: true, onComplete: function() { $('sub-nav').scrollUpdate(); } });
}

function loadImage(id) {
  var d = document.createElement('div');
  d.style.position = 'absolute';
  d.style.left = Math.round(Math.random() * 80)+'px';
  d.style.top = Math.round(Math.random() * 100)+'px';
  var i = document.createElement('img');
  i.src = 'files/'+id+'.jpg';
  d.appendChild(i);
  $('container').appendChild(d);
}

function loadText(id) {
  var div_id = 'text'+Math.round(Math.random() * 99999);
  var d = document.createElement('div');
  d.id = div_id;
  d.style.position = 'absolute';
  d.style.left = Math.round(Math.random() * 80)+'px';
  d.style.top = Math.round(Math.random() * 100)+'px';
  d.addClassName('project-text');
  $('container').appendChild(d);
  new Ajax.Updater(div_id, ajaxLink('project_text'), { method: 'get', onComplete: function() {
      var combinedHeight = parseInt($(div_id).getStyle('top')) + parseInt($(div_id).getHeight());
      if(combinedHeight > parseInt($('container').getStyle('height'))) $('container').setStyle({height: combinedHeight+'px'});
      $('content').scrollUpdate();
    }
  });
}

function ajaxLink(ajax) {
  var link = '?ajax='+ajax+'&';
  State.keys().each(function(key) {
    link += key+'='+State[key]+'&';
  });
  return link;
}

function cls() {
  $('container').immediateDescendants().each(Element.remove);
  $('container').setStyle({height: 0});
  $('content').scrollUpdate();
}

function slideShow() {
  RandomImagePos++;
  if(RandomImagePos >= RandomImages.length) RandomImagePos = 0;
  loadImage(RandomImages[RandomImagePos]);
}

function startSlideshow() {
   State.HomeSlideShow = new PeriodicalExecuter(slideShow, 5);
}