$(document).ready(function(){
  var isMSIE=/*@cc_on!@*/false; //http://dean.edwards.name/weblog/2007/03/sniff/
  // disable colorbox in IE6
  if(isMSIE&&(typeof document.body.style.maxHeight=='undefined')) return;

  $('body.ourStory ul.personnel a').colorbox({
    transition:'none',width:'660px',height:'600px',current:'',
    onLoad:function(){
      this.href+=(this.href.indexOf('?')==-1?'?':'&')+'ajax=yes';},
    onComplete:function(){this.href=this.href.replace(/[\?&]ajax=\w+$/,'')}
  });

  var timeline={
    cache:{},
    event:function(){
      var which=$('img',this).attr('alt').toLowerCase();
      if(timeline.cache[which]) {
        timeline.update(which);
      } else {
        $.get(this.href+(this.href.indexOf('?')==-1?'?':'&')+'ajax=2',
          timeline.callback);
        $(this).parent('li').addClass('loading');
        timeline.pulseDot();
      }
      return false;
    },
    callback:function(data,status,req){
      data=$(data);
      var which=data.find('img').attr('alt').toLowerCase();
      timeline.cache[which]=data;
      timeline.update(which);
      $('ul.timeline li.loading').removeClass('loading')
        .find('img').stop(true).fadeIn(1);
    },
    update:function(which){
      $('div.timelineBox').find('h1.year,div.photos,ul.descriptions').remove();
      timeline.cache[which].prependTo('div.timelineBox');
      $('ul.timeline li').removeClass('current').
        filter((/\d+/.test(which)?'.y':'.')+which).addClass('current');
    },
    pulseDot:function(level,count){
      if(isMSIE) return; // to avoid an IE alpha filter bug
      if(typeof(level)=='undefined') level=.5;
      if(typeof(count)=='undefined') count=24;
      else if(count < 1) {
        $('ul.timeline li.loading').removeClass('loading')
          .find('img').stop(true).fadeIn(1);
        return;
      }
      $('ul.timeline li.loading img').fadeTo(600,level,function(){
        timeline.pulseDot((level<=.5)?1.0:.5,--count);
      });
    }
  }
  $('#viewTimeline').colorbox({
    transition:'none',innerWidth:'879',innerHeight:'513',current:'',
    scrolling:false,
    onLoad:function(){
      this.href+=(this.href.indexOf('?')==-1?'?':'&')+'ajax=1';
      $('#colorbox').addClass('timeline');
      if(basicSlideShow) basicSlideShow.pause();
    },
    onComplete:function(){
      $('#cboxLoadedContent ul.timeline a').bind("click mouseover",timeline.event);
      this.href=this.href.replace(/[\?&]ajax=\w+$/,'');
    },
    onClosed:function(){
      if(basicSlideShow && !basicSlideShow.disabled) basicSlideShow.pause();
      $('#colorbox').removeClass('timeline');
    }
  });
  
  $('body.contact ul.personnel a[rel~=bios]').colorbox({
    transition:'none',width:'540px',height:'500px',current:'',
    onLoad:function(){
      this.href+=(this.href.indexOf('?')==-1?'?':'&')+'ajax=yes';},
    onComplete:function(){this.href=this.href.replace(/[\?&]ajax=yes$/,'')}
  });
});

