// Homepage quickfact ticker
var TicketsCorpSteps = Class.create({
  venues : [],
  see_venues_button : null,
  capacity_input : null,
  show_all_button : null,
  
  initialize: function(){
    if (!$('tickets_corp_steps')) { return false; }
    if (!$('corp_venues')) { return false; }
    $('tickets_corp_steps').setStyle('visibility','hidden')
    
    // Set some shortcuts
    this.venues = $$('#corp_venues ul li.corp_venue')
    this.see_venues_button = $('tickets_corp_steps').down('li.step_one a.see_venues')
    this.capacity_input = $('tickets_corp_steps').down('li.step_one input.capacity')
    this.show_all_button = $('tickets_corp_steps').down('li.step_two a.show_all')
    
    this.see_venues_button.hide();
    
    // Click event for "see venues" button 
    this.see_venues_button.observe('click', function(e){
      e.stop()
      this.filter_venues()
    }.bind(this))
    
    // Keypress event for "capacity" text field
    this.capacity_input.observe('keypress', function(e){
      this.see_venues_button.show();
      
      if (e.keyCode == Event.KEY_RETURN) { 
        this.filter_venues()
      }
    }.bind(this))
    
    // Click event for "show all" button
    this.show_all_button.observe('click', function(e){
      e.stop()
      this.capacity_input.clear()
      this.filter_venues()
    }.bind(this))

    // Click event for "more info" button on each venue
    this.venues.each(function(li){
      
      // Set some shortcuts
      var button = li.down('a.corp_button')
      var details = li.down('.corp_venue_details')
      
      // All venue details default to hidden
      details.hide()
      
      // Hide/show details on click of button
      button.observe('click',function(e){    
        e.stop()
            
        if (button.hasClassName('more_info')){
          button.addClassName('less_info').removeClassName('more_info').setStyle({opacity:'.5'})
          new Effect.BlindDown(details, { duration: 0.3 })
          
        } else {
          button.addClassName('more_info').removeClassName('less_info').setStyle({opacity:'1'})
          new Effect.BlindUp(details, { duration: 0.3 })
        }
      }.bind(this))
    }.bind(this))

    // Hide list of venues
    //$('corp_venues').hide()
    $('tickets_corp_steps').setStyle('visibility','visible')
  },
  
  // Filter venues when capacity is entered
  filter_venues: function(){
    
    // Get the capacity value as an int
    var capacity = parseInt($F(this.capacity_input))
    
    $('corp_venues').show()
    
    // Loop through each venue
    this.venues.each(function(venue){
      if (capacity>0) {
        
        // Ensure venue capacity is between min and max
        if ((capacity >= parseInt(venue.readAttribute('min'))) && (capacity <= parseInt(venue.readAttribute('max')))){
          venue.show()
          venue.down('a.corp_button').show()
          
        // If not, hide this venue
        } else {
          venue.hide()
          venue.down('a.corp_button').hide()
        }
        
      // If no capacity is given, just show all venues
      } else {
        venue.show()
        venue.down('a.corp_button').show()
      }
    })
    
    // Ensure the container is visible
    if (!$('corp_venues').visible()) {
      new Effect.BlindDown('corp_venues', { duration: '0.3', afterFinish: function(){ 
        $('corp_venues').up('li').setStyle({paddingBottom:'0'})
        this.capacity_input.blur()
        
      }.bind(this)})
    }
    
    // Blur focus from input
    this.capacity_input.blur()
  }
  
});

// DOM onload
document.observe("dom:loaded", function() {
    new TicketsCorpSteps();
});


// Global Window Onload
Event.observe(window, 'load', function() {

});
