  var delta_top = 0;
  var delta_left = 0; 

  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 inc_count(i)
  {
    var count = get_count();
    set_count(count+i);
  }

  function autoCenter(layer){
	  var emptyHeight = Math.max(($(window).height() - $(layer).height())/2, 15);
	  $(layer).css("top", $(window).scrollTop() + emptyHeight + "px");
	  return false;
  }
 
  function reprocess()
  {
    
    var count = 0;
    var sum = 0;
    $('.product_cart').each(function(){
    var max_c = parseInt($(this).find('p').eq(-2).text().replace('Доступно для заказа: ', ''));
    
    var price = $(this).find('big').find('b').html().replace(" руб.", "");
    var c = parseInt($(this).find('.count input').val());
    if(max_c < c) {
     c = max_c;
     $(this).find('.count input').val(c);
     }
    var s = parseFloat(price) * c;
    s = roundNumber(s, 2);
    if(isNaN(s) || s == 0) {
    $('.summ', this).find('b').html('по запросу');
    }
    
    else {
    $('.summ', this).find('b').html(s+' .руб'); 
    }
    
    
    
    
      
      sum += s;
      count += c;
    });
    
    sum = roundNumber(sum, 2);
    
    set_count(count);
    
    if(isNaN(sum)) {
    $('.all_price').text('Сумму заказа уточнит менеджер');
    
    }
    else
    {
    $('.all_price').text('Итог: '+sum+' .руб')
    }
    if (parseFloat(sum) == 0) location.href='/catalog';
    /*$('.order tr:has(.count):odd').addClass('dark');
    $('.order tr:has(.count):even').removeClass('dark');*/
  }
  
  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() {

  
  $('.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);
        inc_count(1);
      }
    );
  });

  $('.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();
      }
    );
  });
  
  $('.delete_file').click(function(e){
    e.preventDefault();    
    var id = this.id.substr(12);    
    $.post(
      this.href,
      {
        id:    id              
      },
      function(data){
			  //alert("Data Loaded: " + data);
			  $('#file-'+id).innerHTML; 
			  $('#file-'+id).show();
        $('#file-uploaded-'+id).hide();
			}
    );
  });
  
  $('.add_description_link').click(function(e){     
   
    //alert($('#description_block_' + this.id.substr(12)).css('display'));
    if ($('#description_block_' + this.id.substr(12)).css('display') == 'none') {
      $('#description_block_' + this.id.substr(12)).show();     
    } else {
       $('#description_block_' + this.id.substr(12)).hide(); 
    }
  });
  

  $('.del_from_list').click(function(e){
    e.preventDefault();
    var container = $(this).parent();
    $(this).nextAll('.loader').show();
    $(this).hide();
    $.get(
      this.href,
      function(response) {
        container.html(response);
        inc_count(-1);         
      }
    );
  });
  
  
  $('.product_name, .product').click(function(e){
   
  // if($(window).height() - $(document).scrollTop() > parseInt($('#photo_layer').height()))
   var top = 100 + parseInt($(document).scrollTop());
  //  else  var top =  parseInt($(document).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').show();
        $('#photo_layer').css('left',left + 'px');
        $('#photo_layer').css('top', top + 'px');
      //autoCenter($('#photo_layer')); 


		$(document).trigger('product_loaded');
      }
    );
    
    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-description').change(function(e){
    var url = $('#basket-form').attr('action');
    $.post(
      url,
      {
        id:    this.id.substr(12),
        desc:  this.value        
      }
    );
    reprocess();
  });

  var file_id = '';
  $('.basket-file').change(function(e){
  
   
    var url = $('#basket-form').attr('action');
    file_id = this.id.substr(5);
    $('#file-'+file_id).innerHTML;     
    $.ajaxFileUpload({
      url: url,
      secureuri: false,
      fileElementId: this.id,
      dataType: 'html',
      addData: {id: file_id},
      success: function (data, status) {
        //alert(data); 
        $('#file-'+file_id).hide();
        $('#file-uploaded-'+file_id).show();
   
      },
      error: function (data, status, e) {
        //alert('error ' + data);
      }
    });
  });

  $('#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();
  });

  /*$('#order-link').click(function(e){
    e.preventDefault();
    $('#basket_form_layer').show();
  });*/

  /*$('#photo_show .close-img').click(function(){
    $('#photo_show').hide();
  });*/

  $('.price_list').click(function(e){
   
   var top =  parseInt($(window).height()/2) - parseInt($('#price_layer').height()/2) + parseInt(document.documentElement.scrollTop);
   var left =  parseInt($(window).width()/2) - parseInt($('#price_layer').width()/2);
   
    e.preventDefault();
    var container = $('div#price_layer .layer_content');
    $.get(
      this.href,
      function(response) {
        container.html(response);
        //alert(delta_left);
        $('#price_layer').css('left',left + 'px');
        $('#price_layer').css('top', top + 'px');
        $('#price_layer').show();
		$(document).trigger('product_loaded');
      }
    );
    
    return false;
  });

});
