var BannerRotator = Class.create();
BannerRotator.prototype = {
  initialize : function(container_id, image_class){
    this.container = $(container_id);
    if(this.container){
    this.banners = this.container.select(image_class);
    this.banners.invoke('hide');
    this.banners.first().show();
    this.position = 0;
    this.start_rotation();
    }
  },
  
  current: function(){
    return $(this.banners[this.position]);
  },
  
  rotate: function(){
    this.current().fade({ duration: 0.3});
    this.advance();
    this.current().appear({ duration: 0.5, queue: 'end' });
  },
  
  advance: function(){
    if(this.position < this.banners.length - 1){
      this.position = this.position + 1;
    }else{
      this.position = 0;
    }
  },
  
  start_rotation: function(){
    if(this.banners.length > 1){this.rotator = new PeriodicalExecuter(this.rotate.bind(this), 10)};
  }
  
};


var FAQs = Class.create();
FAQs.prototype = {
  initialize : function(selector){
    // Take CSS Selector
    $$(selector).each(function(wrapper) {
      // Hide the answer
      wrapper.select('.answer').invoke('toggle');
      // Grab the question
      wrapper.select('.question').each(function(question) {
        // Toggle the answer upon click
        question.observe('click', function(e){question.next().toggle()});
      });
    });
  }
};
var map_tools = Class.create();
map_tools.prototype = {
  initialize : function(selector){
      if($(selector)){
        $$('.close').each(function(ele){  
          Event.observe(ele, 'click', function(e){
            ele.up().fade({duration:.25});
            ele.up(1).setStyle({zIndex:1});
            e.stop();
          })
        })
        $$('.location_title').each(function(ele){
          
          ele.up().setStyle({zIndex:1});
          Event.observe(ele, 'click', function(e){
            $$('.location_details').each(function(i){if(i!=ele.next()){
              i.fade({duration:.2});
              i.up().setStyle({zIndex:1});
              }});
            if(ele.next().getHeight() > 540){
              ele.next().setStyle({
                overflowY:"scroll",
                height: "540px"
              })
            }
            var offset = ele.next().getHeight() / 2 * -1;
            ele.next().setStyle({top:offset+'px'});  
            ele.next().appear({duration:.2});
            ele.up().setStyle({zIndex:99});
            e.stop();
          })
        })
    }
  }  
};
var news_display = Class.create();
news_display.prototype = {
  initialize : function(selector){
    if($(selector)){
      var t = this;
      //console.log(window.location.hash);
      $$(".trigger").each(function(linktrigger){
        Event.observe(linktrigger, 'click', function(e){
          t.toggle_news_item(linktrigger);
        })
        if(linktrigger.name == window.location.hash){
          t.toggle_news_item(linktrigger);
        }
      })
    }
  },
  toggle_news_item : function(item){
    if(item.className.include("open")){
      $(item.rel).blindUp({duration:.25});
      item.removeClassName("open");
    }else{
      $(item.rel).blindDown({duration:.25});
      item.addClassName("open");
    }
  }
}

document.observe("dom:loaded", function() { 
  if (!location.href.match(/\/nterchange/g)) {
    var pTabs = new Tabs('#main .tab');
  }  
});

Event.observe(window, 'load', function(){
  var home_banners = new BannerRotator('banner_slideshow', '.bodyimage');
  var faq = new FAQs('.faq');
  var map = new map_tools('region');
  var news = new news_display('news-list');

  var context_menu=new Proto.Menu({
    selector: 'body', 
    className: 'menu desktop', 
    menuItems: [] 
  });
  
});