Drupal.behaviors.accommodationAttach = function() {
   var options = {
        //target:        'div.availability_search_results',   // target element(s) to be updated with server response
        beforeSubmit:  Drupal.accommodation_submit,  // pre-submit callback
        success:      function (data, status) { // post-submit callback
                        $('div.availability_search_results').html(data['content']);
                        accommodation_list_update(data.price);
                      },
        // other available options:
        //url:       url         // override for form's 'action' attribute
        //type:      type        // 'get' or 'post', override for form's 'method' attribute
        dataType:  'json'        // 'xml', 'script', or 'json' (expected server response type)
        //clearForm: true        // clear all form fields after successful submit
        //resetForm: true        // reset the form after successful submit

        // $.ajax options can be used here too, for example:
        // timeout:   3000
    };

    // bind form using 'ajaxForm'
    $('#accommodation-unit-block').ajaxForm(options);

  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);
}

/**
 * Handler for the form redirection submission.
 */
Drupal.accommodation_submit = 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: '25px',
    paddingTop: '10px',
	paddingBottom:'20px'
  });
  $('div.availability_search_results')
    .html('')
    .append(el)
    .fadeIn('slow');
  $.scrollTo( $('div.availability_search_results'), 800, {axis:'y'});
}

accommodation_list_update = function(fields) {
  if(typeof fields !== 'undefined') {
    for(var i=0, len=fields.length; i<len; ++i) {
      var id_price = '#block-unit-' + fields[i].nid + ' .unit_teaser_price';
      var id_buttons = '#block-unit-' + fields[i].nid + ' .unit_teaser_buttons';
      if ($(id_price).length) {
        var content = fields[i].price;
        $(id_price).html(fields[i].price);
      }
      if ($(id_buttons).length) {
        var content = fields[i].buttons;
        $(id_buttons).html(fields[i].buttons);
      }
    }
    Drupal.attachBehaviors();
  }

}

