var FormHandler = Class.create();
FormHandler.prototype = {
  'handle_submit': function (event) { 
    Event.stop(event);

    var errors_container = $('error-top-box');
    errors_container.innerHTML = '';
    var url = '/!site.share_video';
    var qs = Form.serialize('share-video-form');
    new Ajax.Request(url + '?' + qs, {
        'onComplete': function (response) {
          var data = response.responseText;

          if (data == 'success') {
            Element.hide('share-video-form');
            Element.removeClassName('submit-success', 'hidden');
            Element.addClass('error-top-box', 'hidden');
          }
          else if (data == 'failure') {
            $('error-top-box').innerHTML = ' * please check that you have correctly filled in the fields';
            Element.removeClassName('error-top-box', 'hidden');
          }
        },
        'onFailure': function () {      
          $('error-top-box').innerHTML = ' * There was an error submitting the form.';
            Element.removeClassName('error-top-box', 'hidden');
        }
        
      }
    );
  },
  'update': function ($video_id, $video_title) { /*
      
      takes a video id and title and resets itself
      
      
    */
    Form.reset('share-video-form');
    $('video-id-input').value = $video_id;
    $('video-title-input').value = $video_title;
    $('video-name').innerHTML = $video_title;
    Element.show('share-video-form');
    if (!Element.hasClassName('submit-success', 'hidden')) {
      Element.addClassName('submit-success', 'hidden');
    }
  },
  'handle_reset': function (event) {
    Event.stop(event);
    this.reset();
  },
  'reset': function () {
    Element.show('share-video-form');
    Element.addClassName('submit-success', 'hidden');
    
    $video_id = $('video-id-input').value;
    $video_title = $('video-title-input').value;
    $video_title = $('video-name').innerHTML;
    $sender_name = $('video-user-name-input').value;

    Form.reset('share-video-form');    
    
    $('video-id-input').value = $video_id;
    $('video-title-input').value = $video_title;
    $('video-name').innerHTML = $video_title;
    $('user_name').value = $sender_name;
    $('video-message-input').value = '';
    $('video-email-input').value = '';
  },
  'open': function () {
    Element.show('border-and-form-container');
  },
  'close': function () {
    Element.hide('border-and-form-container');
  },
  'handle_close': function (event) {
    Event.stop(event);
    this.close();
  },
  'initialize': function () {
    Element.removeClassName('border-and-form-container', 'hidden');
    this.close();
    Event.observe('share-video-form', 'submit', this.handle_submit.bindAsEventListener(this));
    Event.observe('close-window-link-1', 'click', this.handle_close.bindAsEventListener(this));
    Event.observe('close-window-link-2', 'click', this.handle_close.bindAsEventListener(this));
    Event.observe('share-again-link', 'click', this.handle_reset.bindAsEventListener(this));
  }
};