
/* classes/projects_carousel.js */

 ProjectsCarousel = Class.create();
ProjectsCarousel.prototype = new CategoriesCarousel();
Object.extend(ProjectsCarousel.prototype, {
  notify: function() {
    var project = this.dom.li_items[this.first_current_item - 1];
    project.classNames().each(function(class_name) {
      if (class_name.match(/_project/)) {
        project = class_name.replace('_project', '');
        throw $break;
      }
    });
    this.observer.react(project);
  },
  react: function(id) {
    var category_index = this.category(id);
    this.shiftFocusTo(category_index);
  },
  category: function(id) {
    var category_name = id.replace("_category", '');
    this.dom.li_items.each(function(item, idx) {
      if (item.id == category_name) {
        index = idx;
        throw $break;
      }
    });
    return index;
  },
  shiftFocusTo: function(index) {
    direction = false;
    if (this.first_current_item < index) {
      direction = true;
    }
    var previous_state = this.first_current_item;
    this.first_current_item = index + 1;
    if ((index + this.config.slide_by) > this.total_items) {
      this.last_current_item = this.total_items;
    } else {
      this.last_current_item = index + this.config.window_size;
    }
    var scroll = new Scroll(previous_state, this.first_current_item, this.last_current_item, this.total_items, this.config, this.dom, this.callbacks);
    scroll.slide(previous_state);
  },
  toggleButtons: function() {
    this.togglePrevious('hide');
    this.toggleNext('hide');
  },
  hoverButtons:function() {
    this.hovering_buttons = false;
    if(this.dom.li_items.length > this.config.window_size) {
      this.hovering_buttons = true;
      this.dom.next_button.addClassName('hide');
      this.dom.previous_button.addClassName('hide');
      this.dom.window.observe('mouseover', function(ev) {
        this.toggleButtons('hide');
      }.bindAsEventListener(this));
  
      this.dom.window.observe('mouseout', function(ev) {
        this.dom.next_button.addClassName('hide');
        this.dom.previous_button.addClassName('hide');
      }.bindAsEventListener(this));
    }
  },
  attachListeners: function() {
    if (!this.attached) {
      this.attached = true;
      if (this.dom.next_button) {
        this.dom.next_button.observe('click', function(ev) {
          Event.stop(ev);
          this.next();
          //TODO: refactor, duplication below
          this.toggleButtons();
          this.notify();
        }.bindAsEventListener(this));
      }
      if (this.dom.previous_button) {
        this.dom.previous_button.observe('click', function(ev) {
          Event.stop(ev);
          this.previous();
          //TODO: refactor, duplication above
          this.toggleButtons();
          this.notify();
        }.bindAsEventListener(this));
      }
    }
  },
  customize: function() {
  }
});
