/* photo control */
RESTO.PhotoControl = {
  
  photo_box: null,
  
  init: function() {
    $$('.photo_box').each(function(element){
      var newPhotoBox = Object.create(RESTO.PhotoBox);
      newPhotoBox.element = element;
      newPhotoBox.init(this);
      RESTO.PhotoControl.photo_box = newPhotoBox;
    });
    
    // trace($$('.voteable_photo'))
    
    $$('.voteable_photo').each(function(element){
      var newPhotoOption = Object.create(RESTO.PhotoOption);
      newPhotoOption.element = element;
      // trace(element);
      newPhotoOption.init(this);
      /* RESTO.PhotoControl.photo_box = newPhotoBox; */
    });
    
  },
  
  votableImageClick: function(event) {
    event_element = Event.element(event);
    image_element = Element.select(event_element,'img');
    image_name = event_element.src;
    
    image_name = image_name.replace( /xtra_small/, 'large');
    main_image = $$('#main_photo .photo .photo_image img')[0];
    main_image.src = image_name;
  }
  
  
}

/* PhotoOption class */
/* use depends on crockford's prototypal inheritance */
RESTO.PhotoOption = {
    
    callbackObject: null,
    element: null,
    link_element: null,
    image_element: null,
    vote_up_element: null,
    vote_down_element: null,
    selected: null,
    position: null,
    
    init: function(callbackObject) {
      
      this.callbackObject = callbackObject;
      /* trace(this.element); */
      /* check if selected via classname */
      var selected = this.element.hasClassName('selected');
      if(selected == true) {
        this.selected = true;
      }
      
      
      /* set image_element */
      link_element = Element.select(this.element,'.photo_image a');
      this.link_element = link_element[0];
      trace(link_element);
      
      this.image_element = Element.select(this.element,'.photo_image a img');
      
      /* set vote_up_element */
      this.vote_up_element = Element.select(this.element,'.photo_option_vote_up');
      
      /* set vote_down_element */
      this.vote_down_element = Element.select(this.element,'.photo_option_vote_down');
      
      /* setup image click event */
      Event.observe( this.link_element, 'click', function(event){
        
        RESTO.PhotoControl.votableImageClick(event);
        
      });
      
      // function(event) {
        //RESTO.PhotoControl.votableImageClick(event);
        // trace('did');
      // });
      
      /* setup voting events */
      
      
    }
    
}

/* PhotoBox class */
/* use depends on crockford's prototypal inheritance */
RESTO.PhotoBox = {
  
  element: null,
  
  photo_images_element: null,
  next_button: null,
  previous_button: null,
  number_of_images: null,
  photos: null,
  position: 1,
  height: null,
  width: null,
  
  callbackObject: null,
  
  init: function(callbackObject) {
    
    this.callbackObject = callbackObject;
    var photo_images_element = this.element.select('.photo_images');
    this.photo_images_element = photo_images_element[0];
    var photo_images_list_element = this.element.select('.photo_images ul');
    this.photo_images_list_element = photo_images_list_element[0];
    this.photos = this.element.select('.photo_images ul li');
    var dimensions = Element.getDimensions(this.photo_images_element);
    this.height = dimensions.height;
    this.width = dimensions.width;
    
    var next_button = this.element.select('.photo_buttons .next_photo');
    this.next_button = next_button[0];
    var previous_button = this.element.select('.photo_buttons .previous_photo');
    this.previous_button = previous_button[0];
    
    this.setupEvents();
  
  },
  setupEvents: function() {
    this.next_button.observe('click', function(event) {
      RESTO.PhotoControl.photo_box.next();
    });
    this.previous_button.observe('click', function(event) {
      RESTO.PhotoControl.photo_box.previous();
    });
  },
  next: function() {
   // trace('next');
    el = this.photo_images_list_element;
    var left = el.getStyle('left');
    if( typeof(left) != 'string' ) {
      var left = '0px';
    }
    var current_x = left.split('px')[0];
    var new_x = 0;
    if(this.position < this.photos.length ) {
      new_x = current_x - this.width;
      this.position++;
    } else {
      new_x = 0;
      this.position = 1;
    }
    new Effect.Move(this.photo_images_list_element, { x: new_x, y: 0, mode: 'absolute' });
    
  },
  previous: function() {
    //trace('previous');
  }

};
