/* star control */
RESTO.StarControl = {
  
  major_tom: null,
  stars: [],
  starPos: [ 0, 0 ],
  whytheluckystarbox: null,
  star_rating_form: null,
  
  init: function() {
    
    $$('.star_rating').each(function(element){
      newStar = Object.create(RESTO.Star);
      newStar.element = element;
      if(element.hasClassName('rerateable')) {
        newStar.starbox_options.rerate = true;
      }
      // check if needs change graphic for tan background
      if(element.hasClassName('tan_bg')) { newStar.starbox_options.overlay = 'default_tan.png'; }
       // check if starbox should be locked
      if(element.hasClassName('locked')) { newStar.starbox_options.locked = true; } else { newStar.starbox_options.locked = false; }
      // run that beautiful bean footage
      newStar.init(this);
    });
    
    $$('.star_rating_with_description').each(function(element){
      newStar = Object.create(RESTO.Star);
      newStar.element = element;
      newStar.rating_description_element = element.select('.rating_description');
      if(element.hasClassName('locked')) { newStar.starbox_options.locked = true; } else { newStar.starbox_options.locked = false; }
      if(element.hasClassName('big_stars')) {
        newStar.starbox_options.overlay = 'big.png';
      }
      newStar.init(this);
      RESTO.StarControl.whytheluckystarbox = newStar;
    });
    
    RESTO.StarControl.star_rating_form = $('star_rating_form');
    
  },
  
  form_rate_event: function(element,memo) {
    // form_rate_event  for when a starbox that is a part of a form gets rated "clicked"
    var rated_value = memo.rated;
    var star_rating_form = element.up().up();
    // set the value of the form element
    var stars_hidden_input = star_rating_form.select('#stars')[0];
    stars_hidden_input.value = rated_value;
  },
  
  // fires when one of the stars has been rated
  rateEvent: function(element,memo) {
    
    // get rated value
    var rated_value = memo.rated;
    trace(element);
    // setup a new starbox inside of the whytheluckystar form ... if this is one of those starboxes which should have it.
    var star_rating_forms = element.up().up().select('.whytheluckystar .star_rating_form');    
    if(star_rating_forms.length > 0) {
      RESTO.StarControl.setup_why_the_lucky_form( star_rating_forms[0], element, memo );
    }
    
    // rewrite hidden field on add review form... if this starbox is inside of that form.
    var hidden_star_inputs = element.up().up().select('.hidden_review_stars_input');
    trace(hidden_star_inputs)
    if( hidden_star_inputs.length > 0 ) {
      hidden_star_input = hidden_star_inputs[0];
      hidden_star_input.value = memo.rated;
    }
  },
  
  setup_why_the_lucky_form: function( star_rating_form, element, memo ) {
    // setup new starbox inside of little "whytheluckystar" form
    star_rating_form.select('.star_rating_fresh').each( function(starbox_element) {
      
      // setup starbox options
      var starbox_options = {
        onRate: RESTO.StarControl.form_rate_event,
        rerate: true,
        overlay: 'default.png'
      }
      
      // setup new starbox
      var new_starbox = new Starbox( starbox_element, memo.rated, starbox_options );
      
      // on change event for changing text description
      starbox_element.observe('starbox:changed', function(event) {
        // this event triggers when hovering stars
        var rating_description_el = Event.element(event).up().up().select(".rating_description");
        // hide all ratings
        var child_els = rating_description_el[0].childElements();
        child_els.each( function(element) {
          element.hide();
        });
        // show the target rating text
        var target = Event.element(event).up().up().select(".rating_description .rating_" + event.memo.rating );
        target[0].show();
      });
      
      // set the value of the form element
      var stars_hidden_input = star_rating_form.select('#stars')[0];
      stars_hidden_input.value = memo.rated;
      
      // finally... make the form appear
      starbox_element.up().up().appear( { duration: 0.5 } );
      
    });
    
  }
  
  
}
/* star class */
/* use depends on crockford's prototypal inheritance */
RESTO.Star = {
  
  element: null,
  key: null,
  value: null,
  starbox: null,
  rating_description_element: null,
  starbox_options: {
    onRate: RESTO.StarControl.rateEvent, 
    rerate: true, 
    overlay: 'default.png',
    locked: false
  },
  
  init: function(callbackObject) {
    
    this.callbackObject = callbackObject;
    var children = this.element.childElements();
    this.key = children[0];
    this.value = children[1].innerHTML.strip();
    
    // create the starbox
    starbox = new Starbox(this.element, this.value, this.starbox_options );
    this.starbox = starbox;

    // change event
    this.element.observe('starbox:changed', function(event) {
      
      // this event triggers when hovering stars
      var rating_description_el = Event.element(event).up().up().select(".rating_description");
      
      // hide all ratings
      var child_els = rating_description_el[0].childElements();
      child_els.each( function(element) {
        element.hide();
      });
      
      // show the target rating text
      var target = Event.element(event).up().up().select(".rating_description .rating_" + event.memo.rating );
      target[0].show();
      
    });
    // end change event
      
  }
};
