var App = {
  accordionClick:function(self, klass) {
    var effects = [];
    var parent = $(self).up('.'+klass+'.off');
    $$('.'+klass+'.on').each(function(item){
        effects.push(new Effect.SlideUp(item.down('.'+klass+'_body'), {sync:true,
          beforeStart:function(){ item.removeClassName("on"); },
          afterFinish:function(){ item.addClassName("off");   }
        }));
    });
    if (parent) {
      effects.push(new Effect.SlideDown(parent.down('.'+klass+'_body'), {sync:true,
        beforeStart:function(){ parent.removeClassName("off"); },
        afterFinish:function(){ parent.addClassName("on"); }
      }));
    }
    new Effect.Parallel(effects, {});
    return false;
  }
};


var Wall = Class.create({
  initialize: function(wall, pool, item, noAutoPlay) {
    this.wall = $(wall);
    this.pool = $(pool);
    this.item_selector = item;
    this.items = this.pool.select(this.item_selector);
    this.windows = this.wall.select('.window');
    this.last_group_index = 0;
    this.item_groups = [];
    this.busy = 0;
    this.sliding = true;
    this.keep_sliding = true;
    this.timer = null;
    this.autoPlay = !(!!(noAutoPlay));
    
    var last_group = [];
    var i, j = 0;
    this.item_groups.push(last_group);
    for (i = 0; i < this.items.size(); i++) {
      last_group.push(this.items[i]);
      j++;
      if ((j >= this.windows.size()) && ((i + 1) < this.items.size())) {
        last_group = [];
        this.item_groups.push(last_group);
        j = 0;
      }
    }
    this.populate();
    
    if (this.autoPlay)
      this.autoSlide.delay(3, this);
  },
  
  autoSlide:function(self) {
    if (self.item_groups.size() <= 1) return false;
    self.sliding = true;
    self.keep_sliding = true;
    self.slideRight();
  },
  
  afterSlide:function() {
    if (!this.autoPlay) return null;
    if (this.keep_sliding) { // auto
      window.clearTimeout(this.timer);
      this.sliding = true;
      this.timer = this.autoSlide.delay(3, this);
    } else { // user
      window.clearTimeout(this.timer);
      this.sliding = false;
      this.timer = this.autoSlide.delay(3, this);
    }
    this.keep_sliding = false;
  },
  
  populate: function() {
    
    var self = this;
    var group = this.item_groups[this.last_group_index];
    var item_index = 0;
    var item = null;
    
    this.windows.each(function(win){
      item = group[item_index];
      if (item) {
        if (win.old_item) win.old_item.remove();
        win.old_item = win.item;
        win.insert(item);
        win.item = win.select(self.item_selector).last();
        win.item.absolutize();
        win.item.setStyle("left:0px;height:"+win.getHeight()+"px;width:"+win.getWidth()+"px");
      } else {
        if (win.old_item) win.old_item.remove();
        win.old_item = win.item;
        win.item = null;
      }
      
      item_index++;
    });
    
  },
  
  slideTo: function(index) {
    if (index == this.last_group_index) return false;
    if (index  > this.last_group_index) {
      this.last_group_index = index - 1;
      this.slideRight();
    }
    if (index  < this.last_group_index) {
      this.last_group_index = index + 1;
      this.slideLeft();
    }
  },
  
  slideRight: function() {
    if (this.busy > 0) return false;
    this.busy++;
    if (this.last_group_index < 0) this.last_group_index = 0;
    this.last_group_index++;
    if (this.last_group_index >= this.item_groups.size()) this.last_group_index = 0;
    
    var self = this;
    var windows = [];
    var group = this.item_groups[this.last_group_index];
    var item_index = 0;
    var item = null;
    
    this.windows.each(function(win){
      item = group[item_index];
      if (item) {
        if (win.old_item) win.old_item.remove();
        win.old_item = win.item;
        win.insert(item);
        win.item = win.select(self.item_selector).last();
        win.item.absolutize();
        win.item.setStyle("left:"+win.getWidth()+"px;height:"+win.getHeight()+"px;width:"+win.getWidth()+"px");
      } else {
        if (win.old_item) win.old_item.remove();
        win.old_item = win.item;
        win.item = null;
      }
      
      win.move_by = win.getWidth();
      windows.push(win);
      
      item_index++;
    });
    
    Effect.multiple(windows, function(win, opts){
      var effects = [];
      var options = { sync: true, x: (0 - win.move_by), y: 0, mode: 'relative' };
      
      if (win.item)     effects.push(new Effect.Move(win.item,     options));
      if (win.old_item) effects.push(new Effect.Move(win.old_item, options));
      
      new Effect.Parallel(effects, opts);
    },{speed:0.25,
      beforeStart:function(){self.busy++;},
      afterFinish:function(){self.busy--;if (self.busy == 1) {self.busy--;}}});
    
    this.afterSlide();
    return false;
  },
  
  slideLeft: function() {
    if (this.busy > 0) return false;
    this.busy++;
    if (this.last_group_index < 0) this.last_group_index = 0;
    this.last_group_index--;
    if (this.last_group_index < 0) this.last_group_index = this.item_groups.size()-1;
    
    var self = this;
    var windows = [];
    var group = this.item_groups[this.last_group_index];
    var item_index = 0;
    var item = null;
    
    this.windows.each(function(win){
      item = group[item_index];
      if (item) {
        if (win.old_item) win.old_item.remove();
        win.old_item = win.item;
        win.insert(item);
        win.item = win.select(self.item_selector).last();
        win.item.absolutize();
        win.item.setStyle("left:-"+win.getWidth()+"px;height:"+win.getHeight()+"px;width:"+win.getWidth()+"px");
      } else {
        if (win.old_item) win.old_item.remove();
        win.old_item = win.item;
        win.item = null;
      }
      
      win.move_by = win.getWidth();
      windows.push(win);
      
      item_index++;
    });
    
    Effect.multiple(windows, function(win, opts){
      var effects = [];
      var options = { sync: true, x: (win.move_by), y: 0, mode: 'relative' };
      
      if (win.item)     effects.push(new Effect.Move(win.item,     options));
      if (win.old_item) effects.push(new Effect.Move(win.old_item, options));
      
      new Effect.Parallel(effects, opts);
    },{speed:0.25,
      beforeStart:function(){self.busy++;},
      afterFinish:function(){self.busy--;if (self.busy == 1) {self.busy--;}}});
    
    this.afterSlide();
    return false;
  }
});



Shadowbox.loadLanguage('en', '/javascripts/lang');
Shadowbox.loadPlayer(['img', 'html', 'flv'], '/javascripts/player');
Shadowbox.loadSkin('classic', '/javascripts/skin');

Event.observe(window, 'load', function() {
  Shadowbox.init({ flvPlayer: '/swfs/flvplayer2.swf'});
});

function newsletterLoading(){
  var img = new Element('img', { 'class': 'loading', 'src': '/images/ajax-loader.gif', 'alt': '' });
  $$("#left_newsletter .button_send").each(function(e){
    e.insert( { top: img } );
  });
  Effect.BlindUp('newsletter_action');
}

function newsletterComplete(){
  $$(".button_send img.loading").each(function(e){
    e.remove();
  })
}
