var Site = {

  start: function() {
    if ($('nav')) {
      Site.setNav();
    }
    if ($('slideshow')){
      $$('#recentWork a')[0].addClass('current');
    }
    if ($('splash')) {
      Site.setSplash();
    }
    if ($('flower')) {
      Site.setFlower();
    }
  },
  
  setSplash: function() {
    var obj = new Swiff('/flash/brown-splash.swf', {
        width: '100%',
        height: '100%',
        container: 'splash'
    });
  },
  
  setFlower: function() {
    $('flower').setStyle('background', '#fff');
    var obj = new Swiff('/flash/flower.swf', {
        width: '100%',
        height: '100%',
        background: '#fff',
        container: 'flower'
    });
  },
  
  setNav: function() {
    var myAccordion = new Fx.Accordion($$('.togglers'), $$('.elements'), {
        display: 0,
        alwaysHide: false
    });
    function showNewContent() { 
      $('content').fade('in');
    }
    function hideContent() {
      $('content').fade('hide');
    }
    $('content').set('load',{
      onRequest: function(){hideContent()},
      onSuccess: function(){showNewContent()}
    });
    $$('.recentWork .togglers').addEvent('click', function(){
      if ($('slideshow')) {
      }
      else {
        $$('#recentWork a').setStyle('margin-left', 0);
        $$('#recentWork a').setStyle('opacity', 1);
        $$('#recentWork a')[0].morph('#nav li li a.current');
        if (typeof(myPerodical) !== 'undefined') {
          myPerodical = $clear(myPerodical);
        }
        var mySlideShow = new slideShow($$('#recentWork a'));
        myPerodical = mySlideShow.displayImage.periodical(3000, mySlideShow);
        slideShow.num = 0;
        $('content').load('/pages/recent-work.php');
      }
    });
    
    $$('.directors .togglers').addEvent('click', function(){
      first = $$('.directors a')[0];
      page = first.href.split('/');
      page = page[page.length-1];
      $('content').load('/pages/director.php?page='+page);
      first.morph('#nav li li a.current');
      first.addClass('current');
    });

    $$('#nav li li a, #nav li .about, #nav li .contact').addEvent('click', function(e){
      page = this.href.split('/');
      page = page[page.length-1];
      if (this.className == 'contact' || this.className == 'about') {
        e.stop();
        myAccordion.display(99);
        $('content').load('/pages/'+page+'.php');
      }
      else if (this.getParent('ol').id !== 'recentWork') {
        e.stop();
        current = $$('.directors a.current')[0];
        current.removeClass('current');
        current.morph('#nav li li a');
        this.addClass('current');
        $('content').load('/pages/director.php?page='+page);
      }
    });
    links = $$('#nav li li a');
    links.addEvent('mouseenter', function(e){
      this.set('morph', {duration: '300', link: 'chain'});
      this.morph('#nav li li a.current');
    });
    links.addEvent('mouseleave', function(e){
      if (this.className !== 'current' || this.getParent('ol').id == 'recentWork') {
        this.set('morph', {duration: '300', link: 'chain'});
        this.morph('#nav li li a');
      }
    });
    
    Site.setForm();
  },
  
  setForm: function() {
    $$('#username')[0].addEvent('click', function(){
      if(this.value == 'Username') this.value = '';
    });
    $$('#username')[0].addEvent('blur', function(){
      if(this.value == '') this.value = 'Username';
    });
    $$('#password')[0].addEvent('click', function(){
      if(this.value == 'Password') this.value = '';
    });
    $$('#password')[0].addEvent('blur', function(){
      if(this.value == '') this.value = 'Password';
    });
  },
  
  setSlideShow: function() {
    var mySlideShow = new slideShow($$('#nav ol:first-child a'));
    myPerodical = mySlideShow.displayImage.periodical(3000, mySlideShow);
    $('recentWork').addEvent('mouseleave', function(){
      myPerodical = mySlideShow.displayImage.periodical(3000, mySlideShow);
    });
    Site.stopSlideShow();
  },
  
  stopSlideShow: function() {
    $('recentWork').addEvent('mouseenter', function(){
      myPerodical = $clear(myPerodical);
    });
  }

  
}

var slideShow = new Class({
  initialize: function(listArray) {
    if ($type(listArray)!='array') return;
    this.listArray = listArray;
    this.max = this.listArray.length - 1;
  },
  displayImage: function() {
    if ($type(this.num)!='number') this.num = 0;
    if (this.num == this.max) {
      current =  $$('#recentWork a')[this.max];
      next =  $$('#recentWork a')[0];
      this.num++;
    }
    else if (this.num > this.max) {
      current =  $$('#recentWork a')[0];
      next =  $$('#recentWork a')[1];
      $('slideshow').setStyle('margin-top', 0);
      this.num = 1;
    }
    else {
      current =  $$('#recentWork a')[this.num];
      next =  $$('#recentWork a')[this.num+1];
      this.num++;
    }
    current.set('morph', {duration: 500, link: 'chain'});
    next.set('morph', {duration: 500, link: 'chain'});
    current.morph('#nav li li a');
    next.morph('#nav li li a.current');
    
    var myFx = new Fx.Tween($('slideshow'), {duration: 500, 'link': 'chain'});
    myFx.start('margin-top', -315*this.num);
    }
});

var imageLoad = {
  start: function() {
    if ($('slideshow')){
      (function(){Site.setSlideShow()}).delay(500);
      $$('#slideshow li')[0].clone().inject($('slideshow'), 'bottom');
    }
  }
}


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