window.addEvent('domready',function(){
  
  $$('input[type=submit]').setStyle('display','none');
  
  $('toggle').fade('hide');
    
  var formSlide = new Fx.Tween.Toggle($('donate').getElement('.content'),{
    property: 'width',
    toggleIn: ['228px','920px'],
    toggleOut: ['920px','228px']
  }).setOut();
 
 var galleryFade = new Fx.Tween.Toggle('slide_show',{
   property: 'opacity',
   toggleIn: [1,0],
   toggleOut: [0,1]
 }).setOut();
 
 var showDonationDisplay = function(event){
   if(event) event.stop();
   if(!formSlide.toggled && !galleryFade.toggled) {
     formSlide.toggleIn();
     galleryFade.toggleIn();
     $('toggle').fade('in');
     $('expand').fade('out');
     ticker.pause();
   }
 }
 
 var hideDonationDisplay = function(){
   formSlide.toggleOut();
   galleryFade.toggleOut();
   $('toggle').fade('out');
   ticker.resume();
   $('expand').fade('in');
 }
 
 $('toggle').addEvent('click',function(event){
   event.stop()
   hideDonationDisplay();
 });
 
 var checkboxes = $('checkboxes').getElements('input[type=checkbox]');
 
 var setCost = function(){
   var total = 0;
   checkboxes.each(function(checkbox){
     if(checkbox.get('checked')) total = total + checkbox.value.toInt();
   });
   
   var other = $('other_amount').value.toInt()*100;
   if(other > 0) total = total + other;
   
   total = total/100;
   total = (total.toFloat() > 2400) ? 2400 : total;
   $('donation_amount').getElement('span').set('text',total.toDollarString());
   $('cost1copy').value = total;
 }
 
 checkboxes.each(function(checkbox){
   checkbox.addEvent('click',function(){
     showDonationDisplay();
     setCost();
   });
   checkbox.getNext().addEvent('click',function(){
     showDonationDisplay();
     setCost();
   });
 });
 
 $('textfields').getElements('input[type=text]').each(function(input){
   new Placeholder(input,{ attr: 'title' });
 });
  
 $('other_amount').addEvents({
   'keyup': setCost,
   'focus': showDonationDisplay
  });
  
  var slide_show_icons = $$('#slide_show_icons > div');
  ticker = new Ticker('#slides > img',{
    delay: 15000,
    onBeforeShow: function(index){
      slide_show_icons[index].tween('background-color','#99bad1');
    },
    onShow: function(index){
      slide_show_icons[index].tween('background-color','#fff');
    }
  });
  
  $$('#slide_show_icons>div').each(function(icon,index){
    icon.addEvents({
      'mouseenter': function(){
        ticker.show(index);
        ticker.pause();
      },
      'mouseleave': function(){
        ticker.resume();
      }
    });
  });
  
  $('next').addEvent('click',ticker.next.bind(ticker));
  $('previous').addEvent('click',ticker.previous.bind(ticker));
  
  $('expand').addEvent('click',showDonationDisplay);
  
  $('donate_overlay').addEvent('click',showDonationDisplay);
  
  
  $('donate_form').addEvent('submit',function(event){
    event.preventDefault();
  });
  
  var form_elements = $('donate_form').getElements('#textfields input', '#textfields select');
  
  $('submit').addEvent('click',function(event){
    event.stop();
    var errors = [];
    form_elements.each(function(input){
      if(input.value == '' || input.value == input.get('title')) errors.include(input);
    });
    if(errors.length > 0){
      errors.each(function(input){
        input.addClass('error');
      });
      alert('Please fill in all fields');
    }
    else if($('fec_reg').get('checked') != true){
      alert('Please check the box to certify you are using a personal credit card');
    }
    else {
      
      var data = {
        donation: $('cost1copy').value,
        state: $('state').value
      };
      form_elements.each(function(el){
        data[el.get('name')] = el.value;
      });
      new Request.JSON({
        url: 'process_form_values.php',
        data: data,
        method: 'post',
        onComplete: function(response){
          document.donate_form.submit();
        }
      }).send();
    }
  });
 
});
