jQuery( function($) {
  $.fn.set_active_navigation = function() {
    return this.each( function() {
      if (location.pathname.match('^' + $(this).attr('href') + '$')) {
        $(this).addClass('active');
      }
    });    
  }

  $.fn.observe_contact_form = function() {
    if ($(this).length > 0) {
      $('#enquiry').change( function() {
        location.hash = $(this).val().replace(' ', '-');
      });

      $(this).submit( function() {
        if ($("#enquiry").val() != "Accommodation Bookings") {
          $(".accomodation-field").remove();
        }

        $('input, select, textarea').fadeTo('fast', 0.5);
        $('#submit').replaceWith("<span>Sending Enquiry ...</span>");
      });

      $(window).bind('hashchange', function() {
        var title = location.hash.split('#')[1].replace('-', ' ');

        $('#enquiry').val(title);
        $('#body h1').html(title);
                
        if (title == "Accommodation Bookings") {
          $(".accomodation-field").show();
        } else {
          $(".accomodation-field").hide();
        }
      });

      if (location.hash && location.hash != "#contact-form") {
        $(window).trigger('hashchange');
      } 
    }    
  }
  
  $.fn.observe_order_form = function() {
    var form = $(this);
    
    if($(this).length > 0) {
      form.find('.wine').each( function() {
        var row = $(this);

        row.find(':checkbox').change( function() {
          $(this).attr('checked') ? enable_row(row) : disable_row(row);
        });

        row.find('select').change( function() {
          row_total(row);
          set_total();
        });

        disable_row(row);
      });

      form.submit(function() {
        $('input, select, textarea').fadeTo('fast', 0.5);
        $('#submit').replaceWith("<span>Sending Order ...</span>");
      });      
    }
          
    function enable_row(row) {
      row.find('select').attr('disabled', false).end().find('.hide').show();
    }

    function disable_row(row) {
      row.find('select').val('0').trigger('change').attr('disabled', true).end().find('.hide').hide();
    }

    function row_total(row) {
      var total = cell_total($(row).find('.per-bottle')) + cell_total($(row).find('.half-dozen')) + cell_total($(row).find('.dozen'));
      $(row).find('.sub-total').html(total == 0 ? '' : '$' + total);
    }

    function cell_total(cell) {
      var qty   = $(cell).find('select').val();
      var price = $(cell).find('.price').html();

      return qty ? qty * price : 0;
    }

    function set_total() {
      var total = 0;

      form.find('.sub-total').each(function() {
        price = $(this).html().split('$')[1];

        if (price) {
          total += Number(price);
        }
      });

      $('#total').val(total);
      $('.sum-total').first().html(total == 0 ? '' : '$' + total);
    }
  }


  // Set the active nav item
  $('#navigation a').set_active_navigation();

  // Get the divider looking right
  if ($('#sidebar').height() > $('#body').height()) {
    $('#body').css('border', 'none');
    $('#sidebar').css('border-left', '1px dashed #ccc');
  };

  // Observe the forms
  $('#contact-form').observe_contact_form();
  $('#order-form').observe_order_form();
});
