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() {
    var obj = new Swiff('/flash/flower.swf', {
      width: '100%',
      height: '100%',
        container: 'flower',
        params: {
            wmode: 'opaque',
            bgcolor: '#fff'
        },
    });
  },
  
  setNav: function() {
    var displayNum = 0;
    
    if ($('loginError')) {
      displayNum = 2;
    }
    var myAccordion = new Fx.Accordion($$('.togglers'), $$('.elements'), {
        display: displayNum,
        alwaysHide: false
    });
    $$('.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();
    Site.setPopups();
  },
  
  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 = '';
        this.type = 'password';
      }
    });
    $$('#password')[0].addEvent('blur', function(){
      if(this.value == '') {
        this.value = 'Password';
        this.type = 'type';
      }
    });
  },
  
  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);
    });
  },
  
  goFull: function(URL){ // wonky Adbeast Code
     if (window.screen) {
        var agt = navigator.userAgent.toLowerCase();
        var isMac = (agt.indexOf("mac")!=-1);
        var isIE = (agt.indexOf("msie")!=-1);
        var newWidth = screen.availWidth;
        newWidth = newWidth - 10;
        var newHeight = screen.availHeight;

        if ( isMac && isIE )
  	      newHeight = newHeight - 10;
        else 
  	      newHeight = newHeight - 30;

        var qtFSsize = 'width=' + newWidth + ',height=' + newHeight;
        var qtFSparam = '&w=' + newWidth + '&h=' + newHeight;

        if ( navigator.appName == "Netscape" ) 
  	      var qtFSpos = 'screenX=0,screenY=0,';
        else 
  	      var qtFSpos = 'left=0,top=0,';

        qtFS = window.open(URL,'adbeast_screening','scrollbars=no,resizable=yes,' + qtFSpos + qtFSsize);
     }
  },

  setPopups: function(scrLink) {
    $$('a.reel').addEvent('click', function(e){
      e.stop();
      Site.goFull(this.href);
    })
  }

  
}

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);