var artbox_basket = new ArtboxBasket();

/*order basket form js logic*/

var recountPrice = function(container,new_value){
  var new_price = +$(container).find('.price span').html() * new_value;
  $(container).find('.cost span').html(new_price.toFixed(2));
  var total_price = 0;
  $('#ordersFrom').find('#cost span').each(function(){
    total_price += +$(this).html();
  });

  var total = $('#total span').html(total_price.toFixed(2));
};

$(document).on('click', '#ordersFrom .count_choise .plus', function(e) {
  e.preventDefault();
  var container = $(this).parents('.order_item');
  var input = $(container).find('.prod_count');
  var value = $(input).val();
  var new_value = parseInt(value) + 1;
  $(input).val(new_value);
  $(input).trigger('change');
  recountPrice(container,new_value);

});

$(document).on('click', '#ordersFrom .count_choise .minus', function(e) {
  e.preventDefault();
  var container = $(this).parents('.order_item');
  var input = $(container).find('.prod_count');
  var value = $(input).val();
  if(value > 1 ){
    var new_value = parseInt(value) - 1;
    $(input).val(new_value);
    $(input).trigger('change');
    recountPrice(container,new_value);
  }

});

$(document).on('change', '#ordersFrom .prod_count', function(e) {
  e.preventDefault();
  var container = $(this).parents('.order_item');
  var new_value = $(this).val();
  recountPrice(container,new_value);
  var product_variant_id = container.data('id');
  var value = $(this).val();
  if(value < 0) {
    value = 0;
  }
  artbox_basket.set(product_variant_id, value);
});



$(document).on('click', '#ordersFrom .cart_remove', function(e) {
  e.preventDefault();
  var container = $(this).parents('.order_item');

  var id = $(container).data('id');
  container.remove();
  artbox_basket.set(id, 0);
});


/*end order basket form js logic*/

$(document).on('click', '.buy_button', function(e) {
  e.preventDefault();
  var product_variant_id = $(this).data('id');
  var count = 1;
  artbox_basket.add(product_variant_id, count);
});
$(document).on('change', '#buyForm .prod_count', function(e) {
  e.preventDefault();
  var product_variant_id = $(this).parents('tr').data('id');
  var value = $(this).val();
  if(value < 0) {
    value = 0;
  }
  artbox_basket.set(product_variant_id, value);
});

$(document).on('click', '#buyForm .count_choise .minus', function(e) {
  e.preventDefault();
  var container = $(this).parents('tr');
  var input = $(container).find('.prod_count');
  var value = $(input).val();
  var new_value = parseInt(value) - 1;
  $(input).val(new_value);
  $(input).trigger('change');
});


$(document).on('click', '#buyForm .count_choise .plus', function(e) {
  e.preventDefault();
  var container = $(this).parents('tr');
  var input = $(container).find('.prod_count');
  var value = $(input).val();
  var new_value = parseInt(value) + 1;
  $(input).val(new_value);
  $(input).trigger('change');
});

$(document).on('click', '#buyForm .cart_remove', function() {
  var container = $(this).parents('tr');
  var id = $(container).data('id');
  artbox_basket.set(id, 0);
});

/* Category filter open submenu */
$(document).on('click', '.block_title', function(e) {
  var active = $(this).parent().hasClass('opened');
  if(active) {
    $(this).parent().removeClass('opened').addClass('closed');
    $(this).parent().find('.chechboxes').hide();
  } else {
    $(this).parent().removeClass('closed').addClass('opened');
    $(this).parent().find('.chechboxes').show();
  }
});
/* End Category filter open submenu */
/* Cart resize on scroll */
$('.img_part').height($('.info').height());
$(document).on('scroll', window, function(e)
{
  currentScroll = $(window).scrollTop();
  if (currentScroll > 0) {
    $('#top-cart-content').addClass('small-cart');
  } else {
    $('#top-cart-content').removeClass('small-cart');
  }
});
/* End cart resize on scroll */
/* Catalog product scroll watcher */
$(document).on('scroll', window, function(e) {
    if(($('.detail_main_tabs').length)>0) {
        checkFixed(e);
        checkSelector(e);
    }

});
function checkFixed(e) {
  var img_part = $('.img_part');
  var position = $(img_part).find('.position');
  var position_height = $(position).height();
  var info = $('.info');
  var info_position = $(info).position();
  var info_height = $(info).height();
  var info_top = info_position.top;
  var info_bottom = info_top + info_height;
  var currentScroll = $(window).scrollTop();
  if(info_bottom - currentScroll > 0 && info_bottom - currentScroll < position_height) {
    $(position).removeClass('fixed').css({
      position: 'absolute',
      bottom: 0,
      top: 'auto',
    });
  } else if(currentScroll > info_top && currentScroll < info_bottom) {
    $(position).addClass('fixed').css({
      position: 'fixed',
      top: '100px',
      bottom: 'auto'
    });
  } else {
    $(position).removeClass('fixed').css({
      position: 'relative',
      top: '50px',
      bottom: 'auto'
    });
  }
}
function checkSelector(e) {
  var tab_content = $('#characteristics');
  var tab_content_top = $(tab_content).position().top - 100;
  var comments_block = $('#reviews');
  var comments_block_top = $(comments_block).position().top - 100;
  var collection = $('#collection');
  var collection_top = $(collection).position().top - 100;
  var currentScroll = $(window).scrollTop();
  var detail_main_tabs = $('.detail_main_tabs');
  var active;
  if(currentScroll > tab_content_top && currentScroll < collection_top) {
    active = 'characteristics';
  } else if(currentScroll > comments_block_top) {
    active = 'reviews';
  } else if(currentScroll > collection_top && currentScroll < comments_block_top) {
    active = 'collection';
  }
  $(detail_main_tabs).find('li.selected').removeClass('selected');
  $(detail_main_tabs).find('[data-target='+active+']').addClass('selected');
}
$(window).scroll(function(e) {
  // checkFixed(e);
  // checkSelector(e);
});
/* End catalog product scroll watcher */
/* Animated links */
$(document).on('click', '.detail_main_tabs a', function(e) {
  e.preventDefault();
  var hash = $(this).attr('href').replace(/^.*?(#|$)/,'');
  var target = $('#'+hash);
  var target_top = $(target).position().top - 50;
  var body = $('html, body');
  body.stop().animate({scrollTop:target_top}, 500, 'swing', function() {});
});
/* End animated links */
/* Ajax form submit */
$(document).on('submit', '#feedback-form', function(e) {
  e.preventDefault();
  $.post('/ajax/feedback', $(this).serialize(), function(data) {
    $('#myCallback').find('.modal-body').html(data.result);
  });
});
$(document).on('submit', '#quickbuy-form', function(e) {
  e.preventDefault();
  var container = $(this).parents('.fast_order_form');
  $.post('/order/quick', $(this).serialize(), function(data) {
    if(!data.error) {
      $(container).html(data.result);
    } else {
      $(container).html(data.error);
    }
  })
});
$(document).on('submit', '#quickbuy-modal', function(e)
{
  e.preventDefault();
  var form = $('#buyForm');
  $.post('/order/quick-basket', $(this).serialize(), function(data)
  {
    if(!data.error) {
      $(form).find('.modal-body').html(data.result);
    } else {
      $(form).find('.modal-body').html(data.error);
    }
  }).fail(function(jqXHR, textStatus, errorThrown) {
    $(form).find('.modal-body').html(JSON.parse(jqXHR.responseText).message);
  });
  $(form).find('.modal-title').remove();
  $(form).find('.modal-footer').remove();
});
/* End ajax form submit */