// 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();
  });

  $(".product_category_thumb").hover(
    function() {
      this.src = this.src.replace("_on","_off");
    },
    function() {
      this.src = this.src.replace("_off","_on");
    }
  );

  $(".product").hover(
    function() {
      $(this).css('background-color','#fff');
    },
    function() {
      $(this).css('background-color','#f0f0f1');
    }
  );

  $('#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_assets').sortable({items:'.asset', cursor:'move', containment:'parent', axis:'y', update: function() {
    $.post('/admin/products/order_assets', '_method=post&=product' + $('#product').val() + '&' + $(this).sortable('serialize'));
  }});

  $('#variations tbody').disableSelection().sortable({items:'.variation', cursor:'move', containment:'parent', axis:'y', update: function() {
    $.post('/admin/products/order_variations', '_method=post&product=' + $('#product').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");
  });
  */

  $('.options select').bind("change", function(event) {
    var variation = $(this).val();
    var url = "/products/update_variation/?variation_id=" + variation;

    $.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'
    });
  });

  $('#pickup_information').hide();
  $('#order_customer_pickup').click(function(){
    if(this.checked) {
      $('#shipping_information').hide();
      $('#event_details').hide();
      $('#pickup_information').show('blind', { direction: 'vertical' }, 1000);
    } else {
      $('#shipping_information').show('blind', { direction: 'vertical' }, 1000);
      $('#event_details').show('blind', { direction: 'vertical' }, 1000);
      $('#pickup_information').hide();
    }
  });

  $('#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();
      }
    }
  });
   */
});
