var Site = {

  start: function() {
    
    Site.setDefaults();
    
    if($$('.home')[0]) {
      Site.setSlideshow();
      Site.setAboutTab();
    }
        
    if($('imageNav')) {
      Site.setImageBrowser();
    }

    if($('imageNav')) {
      Site.setImageBrowser();
    }
    
    if($('directory-list')) {
      Site.setDirectory();
    }
    
    if($('video')) {
      Site.setVideo();
    }
    
  },
  
  // Set up page defaults
  setDefaults: function(){
    // load CSS javascript
    var myCSS = new Asset.css('/css/script.css')

    // tooltips
    if (!Browser.Engine.trident) {
      var tipz = new Tips('.tips, .essay img, a[title]',{
        className: 'tip',
        fixed: false,
        hideDelay: 50,
        showDelay: 300
      });
      
    if (screen.width < 1050) {
      new Asset.css('/css/screen_small.css');
    }
  
    
    }
    
    // smooth scroll
    var mySmoothScroll = new Fx.SmoothScroll({links: '#footer a, p a, #footnotes a'});
    
    // search input fields
    if($('search')) {
      $$('#search input')[0].addEvent('click', function(){
        if(this.value == 'Search Archives') this.value = '';
      });
      $$('#search input')[0].addEvent('blur', function(){
        if(this.value == '') this.value = 'Search Archives';
      });
    }
  },
  
  // homepage slidehow
  setSlideshow: function(){
    $$('#slideshow li')[0].clone().inject($('slideshow'), 'bottom');
    var mySlideShow = new slideShow($$('#slideshow li'));
    var myPerodical = mySlideShow.displayImage.periodical(5500, mySlideShow);
  },
  
  // very basic image Browser
  setImageBrowser: function(){
    imageLinks = $$('#imageNav a');
    imageLinks.addEvent('click', function(event){
      event.stop();
      var href = this.href.split('#')[1];
      var left = $(href).getPosition($('images')).x;
      var myFx = new Fx.Tween($('images'), {duration: 500, 'link': 'ignore'});
      myFx.start('margin-left', -left);
    });
  },

  setDirectory: function(){
    navLinks = $$('#nav a');
    directories = $$('#directory-list li.section');
    $('directory-list').fade('hide');
    directories.fade(0);
    $('directory-list').addEvent('mouseleave', function(){
      Site.fadeDirectory();
    });
    $('nav').getParent('.col.full').addEvent('mouseleave', function(){
      Site.fadeDirectory();
    });
    navLinks.addEvent('click', function(event){
      event.stop();
      $('directory-list').fade(1);
      directories.fade(0);
      var href = this.href.split('#')[1];
      $(href).fade(1);
    });
  },
  
  fadeDirectory: function(){
      $('directory-list').fade(0)
      $$('#directory-list li.section').fade(0);
  },
  
  setAboutTab: function() {
    $('about').addEvent('mouseenter', function(){
      $('about').tween('width', 40);
    });
    $('about').addEvent('mouseleave', function(){
      $('about').tween('width', 30);
    });
  },
  
  setVideo: function() {
    hi = $('hi-res').href;
    //low = $('low-res').href;
    width = $('image').width.toInt();
    height = $('image').height.toInt();
    var obj = new Swiff('/flash/videoPlayer.swf', {
        id: 'video-player',
        width: width,
        height: height,
        params: {
            wmode: 'opaque',
            bgcolor: '#000'
        },
        vars: {
            mov: hi,
            img: $('image').src
        }
    });
    $('video').setProperty('html', '');
    obj.inject($('video'));
  }
    
}

var slideShow = new Class({
  initialize: function(imageArray) {
    if ($type(imageArray)!='array') return;
    this.imageArray = imageArray;
    this.max = this.imageArray.length;
    this.active = 0;
    this.imageWidth = '630';
  },

  displayImage: function() { 
    
    // is directory hidden?
    if (!$('directory-list').getStyle('opacity')) {
      if (this.active == this.max - 1) {
        $('slideshow').setStyle('left', 0);
        this.active = 0;
      }

      this.active++;    
    
      var myFx = new Fx.Tween($('slideshow'), {duration: 500, 'link': 'chain'});
      myFx.start('left', -this.imageWidth*this.active);
    }
    
  }
});


window.addEvent('domready', Site.start);
