$(document).ready(function() {
  $('.round_corner').corner();
  $('.round_corner_title').corner("top cc:#fff").corner("bottom");
  $('.round_corner_navigation').corner("top");
  $('.round_corner_navigation_bottom').corner("bottom");
  $('.round_corner_with_border').corner("round 8px").parent().corner("round 10px");
  
  $(".datepicker").datepicker({ firstDay: 1, dateFormat: "dd/mm/yy", dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'], monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'] });
  
  $("div.scrollable").scrollable({
    size: 3,
    clickable: false
  }).circular();
});

function activate_add_to_cart() {
  $("a.add_to_cart").each(function(i) {
    var id = $(this).attr('id');
    var type = $(this).attr('type');
    $(this).colorbox({
      transition:"none",
      inline:true,
      href:"#show_add_to_cart_"+type+"_"+id,
      width: "600px"
    });
  });
}

function activate_show_item_description (){
 $("div #small_item_img").each(function(i) {
	var item = $(this).attr('rel');

   $('#img-'+item).qtip({
	  content: $("#desc-item-"+item).html(), // Give it some content, in this case a simple string
      position: {
         corner: {
            tooltip: 'topLeft',
			target: 'bottomMiddle'
         }
      },
      style: {
         name: 'light',
         padding: '7px 13px',
         width: {
            max: 500,
            min: 400
         },
         tip: true
      }
   });
  });
}

function active_customer_use_shipping_adress() {
  $("#customer_use_shipping_address").click(function(){
    on_change_use_shipping_address();
  });
  
  on_change_use_shipping_address();
}

function on_change_use_shipping_address() {
  if($("#customer_use_shipping_address").is(":checked")){
    $("#show_shipping_address").show();
  } else{
    $("#show_shipping_address").hide();
  }
}

function ajax_submit_for_new_order_session(form_id) {
    var new_post_form = $('#new_order_session');
    $.ajax({
      type : 'POST',
      url : new_post_form.attr('action'),
      data : new_post_form.serialize(),
      dataType: "script"
    });
    return false;
}

function update_our_order(locale, reg) {
  $('#our_order').html("<div>Mise à jour de la commande en cours. Veuillez patienter...</div>")
  
  $.ajax({
    type: "GET",
    url: "/"+locale+"/"+reg+"/order_sessions/ajax_update_our_order",
    dataType: "script"
  });
}

function change_tab(tab_string) {
  $('.tab_content').hide();
  $('#tab_'+tab_string).show();
}

function activate_show_hide_interval() {
  $('.new_order_session').each(function(){
    $(this).find("#order_session_number").change(function(){
      show_hide_interval();
    });
  });
  show_hide_interval();
}

function show_hide_interval() {
  $('.new_order_session').each(function(){
    if($(this).find('#order_session_number').val() == 1) {
      $(this).find('#order_session_interval_choice_item_1').hide();
      $(this).find("label[for='order_session_interval_choice_item_1']").hide();
    } else {
      $(this).find('#order_session_interval_choice_item_1').show();
      $(this).find("label[for='order_session_interval_choice_item_1']").show();
    }
  });
}

function hide_new_customer_text() {
  $('#new_customer_text').hide();
}

function show_customer_new_description(id) {
  if($('#'+id).is(":hidden")){
    $('#'+id).show();
  } else if($("#customer_use_shipping_address").is(":showed")){
    $('#'+id).hide();
  }
}

function show_hide_items_in_preference_category(id) {
  if($("#preference_category_"+id).is(":visible")){
    $("#preference_category_"+id).hide();
  } else if($("#preference_category_"+id).is(":hidden")){
    $("#preference_category_"+id).show();
  }
}

function set_preference(locale, reg, item_id, state) {
  $('#preference_item_links_'+item_id).html("<div><img src='/images/loader.gif' /> Mise à jour...</div>");
  
  data_url = "item_id="+item_id+"&state="+state;
  
  $.ajax({
    type: "POST",
    url: "/"+locale+"/"+reg+"/customers/current/preferences/set_preference",
    data: data_url,
    dataType: "script"
  });
}

function set_preference_on_category(locale, reg, category_id, state) {
  $('#preference_category_'+category_id+' > .preference_category_item').each(function(){
    item_id = $(this).attr('item_id');
    set_preference(locale, reg, item_id, state);
  });
}

function activate_check_interval(locale, reg, order_id, default_interval) {
  check_interval(locale, reg, order_id, default_interval);
  $('#order_orderable_id').change(function(){
    check_interval(locale, reg, order_id, default_interval);
  });
}

function check_interval(locale, reg, order_id, default_interval) {
  var orderable_type = $('#order_orderable_type').val();
  var orderable_id = $('#order_orderable_id').val();
  if(orderable_type == 'CartType') {
    data_url = "cart_type_id="+orderable_id+"&order_id="+order_id+"&default_interval="+default_interval
    $.ajax({
      type: "GET",
      url: "/"+locale+"/"+reg+"/customers/current/orders/check_interval",
      data: data_url,
      dataType: "script"
    });
  }
}

