var Site = {

  
  // initialize page and all its many wonderful functions
  start: function(){
    Site.setNav();
    Site.ticker();
    if ($$('#image img')[1]) {
      Site.prepSlideshow();
      window.addEvent('load', Site.setSlideshow());
    }
    Site.popExternal();
    if ($('archives')) {
      Site.doDropDownSelect('archives');
    }
    // new SmoothScroll(); waiting for mootools bug fix
  },
  
  setNav: function(){
    var tabs = $$('#nav li > a'); // grab relevant headers
    tabs.each(function(tab) {
      var myFx = new Fx.Tween(tab.getParent(), 'margin-top', {duration: 1000, link: 'cancel', transition: Fx.Transitions.Elastic.easeOut});
      tab.addEvent('mouseenter', function(){
        myFx.start(-15);
      });
      tab.addEvent('mouseleave', function(){
        myFx.start(0);
      });
    });
  },
  
  ticker: function(){
    $('ticker').empty();
    $('ticker').grab(new Swiff('/flash/ticker.swf', {width: 964, height: 30,
        params: {
          loop: 'true',
          wMode: 'window'
        }}));
  },
  
  setSlideshow: function(){
    var mySlideShow = new simpleSlide($$('#image li'));
    mySlideShow.displayImage.periodical(5000,mySlideShow);
  },

  prepSlideshow: function(){
    var imgs = $$('#image img');
    for (var i=0; i < imgs.length; i++) {
      li = imgs[i].getParent();
      li.setStyle('z-index', 100 - i);
      alt = imgs[i].alt;
      if ($$('.about, .home')[1] && alt) {
        var caption = new Element('span', {'class': 'caption', 'html': alt});
        caption.inject($$('#image li')[i]);
      }
    };
  },
  
  doDropDownSelect: function(id) {
    var dropDownSelect = $$('#'+id+' select')[0];
      dropDownSelect.onchange=function() {
        if (this.value.indexOf('http://') == 0) window.location.replace(this.value);
    }
    
  },
  
  popExternal: function() {
    var inLink = "edgemont-village";
    var outLink;
    if (document.getElementsByTagName('a')) {
      for (var i = 0; (outLink = document.getElementsByTagName('a')[i]); i++) {
        if (outLink.href.indexOf(inLink) == -1) {
          outLink.setAttribute('target', '_blank');
        }
      }
    } 
  }
}

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

  displayImage: function() { 
    // Set next image or the first
    if (this.active < this.max - 1)  {
      this.last = this.active;
      this.active++;
    }
    else {
      this.active = 0;
      this.last = this.max - 1;
    }

    // don't fade in IE
    if(window.ie) {
      var dur = 1;
    }
    
    else {
      var dur = 1000;
    }
     
    // Hide image
    this.images[this.last].effect('opacity', {
      duration: dur,
      'link': 'chain'}).start(1,0);
    
    // Show image
    this.images[this.active].effect('opacity', {
      duration: dur,
      'link': 'chain'}).start(0,1);
    
  }
});


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