  var delta_top = 0;
  var delta_left = 0;
  var tariff_list = {};

  function get_count()
  {
    return parseInt($('#basket_cart_count').text());
  }

  function set_count(count)
  {
    if (parseInt(count) > 0) {
      $('span.cart').show();
      $('#basket_cart_link').show();
      $('#basket_cart_count').show();
      $('#basket_cart_span').hide();
    } else {
      $('#basket_cart_link').hide();
      $('#basket_cart_count').hide();
      $('#basket_cart_span').hide();
    }
    $('#basket_cart_count').text(count);
  }

  function set_price(price)
  {
    if (parseInt(price) < 0) {
      price = 0;
    }
    $('#all_price').html(price);
  }
  function get_price()
  {
    return parseInt($('#all_price').html());
  }
  function del_price(price)
  {
    var count = parseInt(get_price());
    set_price(count-parseInt(price));
  }
  function inc_price(price)
  {
    var count = parseInt(get_price());
    set_price(count+parseInt(price));
  }


  function reprocess()
  {
    var count = 0;
    var sum = 0;
    $('.count2').each(function(){
      var c = parseInt($(this).val());
      var price = parseInt($(this).next('input').val());

      /*
      alert(c);
        alert(price);
      */
      var s = parseInt(price) * parseInt(c);
      $(this).next('input').next('p').find('strong.price').html(s);

      /*$(this).nextAll('.price').text(s);*/

      sum += s;
      count += c;
    });

    sum = roundNumber(sum, 2);

    /*set_count(count);*/
    $('#all_price').html(sum);
    /*if (parseFloat(sum) == 0) location.href='/';*/
  }

  function roundNumber(number,x) {
    // rounds number to x decimal, default to 2
    x = (!x ? 2 : x);
    return Math.round(number*Math.pow(10,x))/Math.pow(10,x);
  }

  function addToBasket(service_id) {
    var _url = url_add_service + '?service_id=' + service_id;
    if (hook_list[service_id]) {
      _url = _url + '&tariff_id=' + hook_list[service_id];
      inc_price(tariff_list[hook_list[service_id]]);
    } else {
      inc_price(services[service_id][5]);
    }
    $.get(
      _url,
      function(response) {
      }
    );
  }
  function delToBasket(service_id) {
    for(var i=0;i<services[service_id][3];i++) {
      if (hook_list[service_id]) {
        del_price(tariff_list[hook_list[service_id]]);
      } else {
        del_price(services[service_id][5]);
      }
    }
    $.get(
      url_delete_service + '?service_id=' + service_id,
      function(response) {
        canAddService(service_id, false);
      }
    );
  }
  function editToBasket(service_id, count, action) {
    var _url = url_edit_service + '?service_id=' + service_id + '&count=' + count;
    if (action == 'del') {
      if (hook_list[service_id]) {
        del_price(tariff_list[hook_list[service_id]]);
      } else {
        del_price(services[service_id][5]);
      }
    } else {
      if (hook_list[service_id]) {
        inc_price(tariff_list[hook_list[service_id]]);
        _url = _url + '&tariff_id=' + hook_list[service_id];
      } else {
        inc_price(services[service_id][5]);
      }
    }
    $.get(
      _url,
      function(response) {
        canAddService(service_id, false);
      }
    );
  }

$(function() {


  $('.basket-count').each(function() {
      $(this).numeric();
  });

  $('.add_to_cart').click(function(e){
    e.preventDefault();
    var container = $(this).parent();
    container.html('Загрузка...');
    $(this).nextAll('.loader').show();
    $.get(
      this.href,
      function(response) {
        container.html(response);
      }
    );
  });

  $('.del_to_cart').click(function(e){
    e.preventDefault();
    var row = $(this).parent().parent();
    $(this).nextAll('.loader').show();
    $(this).hide();
    $.get(
      this.href,
      function(response) {
        row.next('tr').remove();
        row.remove();
        reprocess();
      }
    );
  });


  $('.product_name, .product').click(function(e){

    var top =  parseInt($(window).height()/2) - parseInt($('.photo_layer').height()/2) + parseInt(document.documentElement.scrollTop);
    var left =  parseInt($(window).width()/2) - parseInt($('.photo_layer').width()/2);

    e.preventDefault();
    var container = $('div#photo_layer .layer_content');
    $.get(
      this.href,
      function(response) {
        container.html(response);
        //alert(delta_left);
        $('#photo_layer').css('left',left + 'px');
        $('#photo_layer').css('top', top + 'px');
        $('#photo_layer').show();
      }
    );

    return false;
  });

  $('.basket-count').change(function(e){
    var url = $('#basket-form').attr('action');
    $.post(
      url,
      {
        id:    this.id.substr(8),
        count: this.value
      }
    );
    reprocess();
  });

  $('.book-count').change(function(e){
    var url = $('#basket-form').attr('action') + 'boot';
    $.post(
      url,
      {
        id:    this.id.substr(8),
        count: this.value
      }
    );
    reprocess();
  });


  $('#basket-form').submit(function(e){
    e.preventDefault();

    var top =  parseInt($(window).height()/2) - parseInt($('#basket_form_layer').height()/2) + parseInt(document.documentElement.scrollTop);
    $('#basket_form_layer').css('top', top + 'px');
    $('#basket_form_layer').show();
  });

});
