var submitted = 0;
Drupal.accommodationAttach = function() {
  $('#accommodation-unit-block')
    .append('<input type="hidden" name="accommodation" value="1" />')
    .each(function () {
      var elt = new Drupal.accommodation(this);
    });
  var header_bg = $('#header').css('background-color');
  var header_text = $('#header').css('color');
  $('#accommodation_legend').css('background-color', header_bg);
  $('#accommodation_legend a').css('color', header_text);
  $('.unit_availability_legend').css('background-color', header_bg);
  $('.unit_availability_legend a').css('color', header_text);
  // disable submit button after clicked
  $('.form-submit').click( function(){
	if(this.id == 'edit-submit' || this.id == 'edit-submit-2') {
      if(submitted == 0) {
	    submitted = 1;
        return true;
      } else {
	    return false;
      }
    }
  });
}

Drupal.accommodation = function(form) {
  Drupal.redirectFormSubmit($(form).attr('action'), form, this);
}

/**
 * Handler for the form redirection submission.
 */
Drupal.accommodation.prototype.onsubmit = function () {
  // Insert progressbar.
  // Insert progressbar and stretch to take the same space.
  this.progress = new Drupal.progressBar('ajaxeditprogress');
  this.progress.setProgress(-1, 'Fetching results');
  var el = this.progress.element;
  $(el).css({
    width: '250px',
    height: '15px',
    paddingTop: '10px',
	paddingBottom:'20px'
  });
  $('div.availability_search_results')
    .html('')
    .append(el)
    .fadeIn('slow');
  Drupal.scrollTo(el);
}

/**
 * Handler for the form redirection completion.
 */
Drupal.accommodation.prototype.oncomplete = function (data) {
  //$(this.progress.element).remove();
  //this.progress = null;
  $('div.availability_search_results')
    .hide()
    .html(data)
    .end()
    .fadeIn('slow');
  Drupal.searchcriteria();
}

/**
 * Handler for the form redirection error.
 */
Drupal.accommodation.prototype.onerror = function (error) {
  alert(error);
  $(this.progress.element).fadeOut('slow', function() {
      $(this).remove();
    });
  this.progress = null;
}

if (Drupal.jsEnabled) {
  $(document).ready(Drupal.accommodationAttach);
}