// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var order_section_height = new Array();
var tb_pathToImage = "images/loading.gif";

function roundNumber(num, dec) {
  var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
  return result;
}

var class_templates = new Array();
var ACTIVE = 0;
var DISABLE = 1;
function initializeTabClasses() {
  var class_template = new Array();
  class_template.push("description tab_button");
  class_template.push("description_disabled tab_button");
  class_templates.push(class_template);

  class_template = new Array();
  class_template.push("how_to_use tab_button");
  class_template.push("how_to_use_disabled tab_button");
  class_templates.push(class_template);

  class_template = new Array();
  class_template.push("terms tab_button");
  class_template.push("terms_disabled tab_button");
  class_templates.push(class_template);
}

$(document).ready(function() {

  if ($['datepicker']) {
    $.datepicker.setDefaults({
      'dateFormat': 'yy-mm-dd'
    });
  }

  if ( $('#order_form').length ) {
    $('#order_form').submit(function() {
      $(this).find('.submit input').val('Placing Order. Please wait...');
      $(this).submit();
    });
  }

  if ( $("#time_left").length ) {
    $("#time_left").html("<span id='precise_time_left'></span>");
    date_now = new Date();
    $("#precise_time_left").counter({sec: parseInt($('#seconds_left').html()), message: "... it's gone!"});
  }

  initializeTabClasses();

  $(".tab_button").each(function(index, tab) {
    $(tab).click(function() {
      // Hide all tabs.
      $(".tab").each(function(iterator, another){
        var element = $(".tab_button")[iterator];
        if (iterator != index) {
          $(another).hide();
          $(element).attr("class", class_templates[iterator][DISABLE]);
        } else {
          $(another).show();
          $(element).attr("class", class_templates[iterator][ACTIVE]);
        }
      });
    });
  });

  $('.city_link').click(function() {
      if ($('#all_cities').attr("style") == "display: none;") {
        $('#all_cities').attr("style", "display:block;")
        $('#city_link_container').attr("class", "active_link_container");
      } else {
        $('#all_cities').attr("style", "display:none;")
        $('#city_link_container').attr("class", "");
      }
    return false;
  });

  function update_price() {
    n = parseInt( $('#order_quantity').val() );
    price = parseFloat( $('#offer_price').val() );
    $('#order_total').html(roundNumber(n*price,2).toFixed(2));
  }

  update_price();

  $("#order_quantity").change(function() {
    update_price();
  });
});


function removeErrorMessages() {
  $('#errorExplanation li').each(function(index, object) {
    if (object.innerHTML == '') {
      object.setAttribute('style', 'display: none');
    }
  });
}
