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

/*
function insert_fields(link, method, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp(”new_” + method, “g”)
  $(link).before(content.replace(regexp, new_id));
}

function remove_fields(link) {
  var hidden_field = $(link).prev(”input[type=hidden]“);
  if (hidden_field) {
    hidden_field.val(’1′);
  }
  $(link).closest(”.fields”).hide();
}
*/

$(document).ready( function () {
  $("#newVariation").click( function() {
    var new_id = new Date().getTime();
    $("#variations").append( variation.replace(/NEW_RECORD/g, new_id) );
  });

  $("#tabs").tabs();

  $("#toggle-markdown").click(function() {
    $("#markdown-help").toggle('blind slow');
  });

  $("#show-markdown").click(function() {
    $("#markdown-help").toggle();
  });

  $('#products').sortable({items:'.category_product', cursor:'move', containment:'parent', axis:'y', update: function() {
    $.post('/admin/product_categories/order_products', '_method=post&product_category_id=' + $('#product_category_id').val() + '&' + $(this).sortable('serialize'));
  }});

  $('#product_categories').sortable({items:'.product_category', cursor:'move', containment:'parent', axis:'y', update: function() {
    $.post('/admin/product_categories/order', '_method=post&product_category_id=' + $('#product_category_id').val() + '&' + $(this).sortable('serialize'));
  }});

  // $(".product-variations").hide();

  $(".product-details").toggle(function(){
    $(this).addClass("active");
  }, function () {
    $(this).removeClass("active");
  });

  $(".product-details").click(function(){
    $(this).next(".product-variations").slideToggle("slow");
    $(this).removeClass("active");
  });

  // I think I need a separate onchange function for each option type

  $('.options select').bind("change", function(event) {

    var id = this.id;
    var product_id = $('#product_id').val();
    var variation_id = $('#product_variation_id').val();
    var option_values = [];

    $('.options select').each(function() {
      option_values += "option_value_ids[]=" + $(this).val() + "&";
    });

    var url = "/products/" + product_id + "/update_variation/?variation_id=" + variation_id;

    $.ajax({
      url: url,
      type: 'GET',
      dataType: 'script'
    });
  });

  $('.update').live("click", function(event) {
    var variation = $(this).siblings("input.variation").val();
    var quantity = $(this).siblings("input.quantity").val();
    var url = "/store/update_quantity/?variation_id=" + variation + "&quantity=" + quantity;

    $.ajax({
      url: url,
      type: 'GET',
      dataType: 'script'
    });
  });

  $('.remove').live("click", function(event) {
    var variation = $(this).parent().siblings('.item_variation').children("input.variation").val();
    var url = "/store/remove_from_cart/?variation_id=" + variation;

    $.ajax({
      url: url,
      type: 'GET',
      dataType: 'script'
    });
  });

  $('#same_as_billing').click(function(){
    if(this.checked) {
      $('#order_shipping_first_name').val($('#order_billing_first_name').val());
      $('#order_shipping_last_name').val($('#order_billing_last_name').val());
      $('#order_shipping_company_name').val($('#order_billing_company_name').val());
      $('#order_shipping_address_one').val($('#order_billing_address_one').val());
      $('#order_shipping_address_two').val($('#order_billing_address_two').val());
      $('#order_shipping_city').val($('#order_billing_city').val());
      $('#order_shipping_region_code').val($('#order_billing_region_code').val());
      $('#order_shipping_postal_code').val($('#order_billing_postal_code').val());
      $('#order_shipping_phone_number').val($('#order_billing_phone_number').val());
    } else {
      $('#order_shipping_first_name').val('');
      $('#order_shipping_last_name').val('');
      $('#order_shipping_address_one').val('');
      $('#order_shipping_address_two').val('');
      $('#order_shipping_city').val('');
      $('#order_shipping_region_code').val('');
      $('#order_shipping_postal_code').val('');
      $('#order_shipping_phone_number').val('');
    }
  });

  if ($('#order_shipping_city_select').val() != 'Other') {
    $('#order_shipping_city_select_box').hide();
  }

  $('#order_shipping_city_select').bind("change", function(event) {
    if (this.value == 'Other') {
      $('#order_shipping_city_select_box').show();
      $('#order_shipping_city').val('');
    } else {
      $('#order_shipping_city_select_box').hide();
      $('#order_shipping_city').val(this.value);
    }
  });

  $('.formatted_date').datepicker();
  $('.formatted_date_with_year').datepicker({ changeYear: true, yearRange:"-110:+0" });

  /*
  $('#order_billing_city').autocomplete("/javascripts/cities.js", {
    selectFirst: false,
    minChars: 1,
    max: 12,
    autoFill: false,
    matchContains: false,
    extraParams: {
      city: function() {
	$('#order_billing__city').val();
      }
    }
  });
   */

  /*
  $('#order_shipping_city').autocomplete("/javascripts/cities.js", {
    selectFirst: false,
    minChars: 1,
    max: 12,
    autoFill: false,
    matchContains: false,
    extraParams: {
      city: function() {
	$('#order_shipping_city').val();
      }
    }
  });
   */

  /*
  $('#address_city').autocomplete("/javascripts/cities.js", {
    selectFirst: false,
    minChars: 1,
    max: 12,
    autoFill: false,
    matchContains: false,
    extraParams: {
      city: function() {
	$('#address_city').val();
      }
    }
  });
   */

  $("#slider").easySlider({
    continuous: true,
    prevText: '',
    nextText: ''
  });
});

