document.observe('dom:loaded', function() {setTimeout("$('flash').fade()", 2500)});

var AnchorObserver = { enabled:  true, interval: 0.1 };

AnchorLink = Class.create({
  initialize: function(func) {    
    this.callback = func;
    this.observeLinks();
  },
  
  observeLinks: function() {
    document.observe("anchor:changed", this.showAnchor.bind(this));
    /* Adds an observer that polls to see if the URL anchor has chnaged. */
    document.observe("dom:loaded", function() {
      var lastAnchor = "";
      function poll() {
        var anchor = (window.location.hash || "").slice(1);
        if (anchor != lastAnchor) {
          document.fire("anchor:changed", { to: anchor, from: lastAnchor });
          lastAnchor = anchor;
        }
      }
    
      if (AnchorObserver.enabled) {
        setInterval(poll, AnchorObserver.interval * 1000);
      }
    });
  },
  
  getAnchor: function() {
    return window.location.hash.split('#')[1];
  },
  
  showAnchor: function() {
    if(window.location.hash) {
      var a = this.getAnchor();
      this.callback(a);
    }
  }
});

AnchorLink.set = function(anchor) {
  window.location.hash = '#'+anchor;
}

ScreenLinks = Class.create({

  initialize: function() {    
    this.observeDOM();
    this.screenLinks = null;
  },
  
  observeDOM: function() {
    document.observe('dom:loaded', this.observeScreenLinks.bind(this));
  },
  
  observeScreenLinks: function() {
    this.screenLinks = $$('#screenshot .screen_link');
    this.screenLinks.each(function(link) {
      link.observe('click', this.visitLink.bind(this));
    }.bind(this));
  },
  
  visitLink: function(link) {
    window.location = link.target.getAttribute('rel');
  }
  
});



SlideShow = function(classid) {
  var root = $('screenshot');
  var current = $$('#screenshot .viewport.current').first();

  // If this function is called and we don't have a viewport, abort  
  if (!root || !current)  {
    return;
  }

  var next = $$('#screenshot .viewport.'+classid).first();
  if (current.identify() == next.identify()) { return; }

  next.addClassName('current');
  current.removeClassName('current');
  
  current.setStyle('z-index:0');
  next.setStyle('z-index:1');

  current.fade({duration:0.2});
  next.appear({duration:2.0});
  
  if ($$('#'+classid+'-link').first()) {
    clearPageMenu();
    $(classid+'-link').addClassName('active');
  }

  if ($$('#'+classid+'-pager').first()) {
    $$('#pager .links').each(function(d){d.hide()});
    $(classid+'-pager').show();
    $('footer').setStyle('margin-top:0px');
  }
  $$('#pager .links a').each(function(d){d.removeClassName('active')});
  if ($(classid+'-page-link')) {
    $(classid+'-page-link').addClassName('active');
  }
}

CaseStudies = function(classid) {
  var root = $('screenshot');
  var current = $$('#screenshot .viewport.current').first();

  // If this function is called and we don't have a viewport, abort  
  if (!root || !current)  {
    return;
  }

  var next = $$('#screenshot .viewport.'+classid).first();
  if (current.identify() == next.identify()) { return; }
  
  current.removeClassName('current');
  next.addClassName('current');
  
  current.setStyle('z-index:0');
  next.setStyle('z-index:1');

  current.fade({duration:0.2});
  next.appear({duration:2.0});

  
  if ($$('#'+classid+'-link').first()) {
    clearSectionMenu();
    $(classid+'-link').addClassName('active');
  }

  if ($$('#'+classid+'-pager').first()) {
    $$('#pager .links').each(function(d){d.hide()});
    $(classid+'-pager').show();
  }
  $$('#pager .links a').each(function(d){d.removeClassName('active')});
  if ($(classid+'-page-link')) {
    $(classid+'-page-link').addClassName('active');
  }
}

clearSectionMenu = function() {
  $$('#sectionmenu a').each(function(a){a.removeClassName('active')});
}


clearPageMenu = function() {
  $$('#pagemenu a').each(function(a){a.removeClassName('active')});
}

About = function(person) {
  $('photo').hide();
  $('photo').className = '';
  $('photo').className = person+'-photo';
  $$('#bio .person').each(function(p){if (p.identify()!=$(person).identify()){p.hide();}});
  $('photo').appear();
  $(person).appear();
  clearPageMenu();
  $(person+'-link').addClassName('active');
}

HomePage = function(classid) {
  var root = $('screenshots');
  var current = $$('#screenshots .screenshot.active').first();

  // If this function is called and we don't have a viewport, abort  
  if (!root || !current)  {return;}

  var next = $$('#screenshots .screenshot.'+classid).first();
  if (current.visible() && current.identify() == next.identify()) { return; }

  current.removeClassName('active');
  next.addClassName('active');
  
  current.setStyle('z-index:0');
  next.setStyle('z-index:1');

  if (current.visible()){current.fade({duration:0.2});}  
  next.appear({duration:2.0});
}


