/**
  * SUSI 
  *
  * Автор: sum (Сирашев Денис) 2011
  *
  */

function addCart(id) {
	if(!parseInt($('#num-'+ id).val()) > 0) return false;
	
	$.get('/engine/ajax/susi.php?action=add_cart', {id: id, num: $('#num-'+ id).val()}, function(response, status, xhr) {
		if( $('#request_content').text() == 'Стол заказов' ) {
			$('#request_content').css('width', 210).html( response.good );
		}
		else {
			$( response.good ).appendTo('#request_content');
			$('#request_content').css('width', $('#request_content').width() + 210 );
		}
		
		$('#cost').html( response.cost );
		
		$('#discount').html( response.discount );
		
		if(response.cost) $('#summ').html( response.cost - response.discount );
		
		maxLeft();
	});
	
	return false;
}

function deleteGood(id) {
	$.get('/engine/ajax/susi.php?action=delete_good', {id: id}, function(response) {
		var discount = 0.00;
		
		$('#good-'+ id).remove();
		
		$('#cost').html( response.cost );
		
		if(response.allow_discount) discount = Math.round( response.cost * 0.1, 2 );
		
		$('#discount').html( discount );
		$('#request_content').css('width', $('#request_content').width() - 210 );
		
		if(response.cost >= 0) $('#summ').html( response.cost - discount );
		
		maxLeft();
	});
	
	return false;
}

function maxLeft()
{
	var rc = $('#request_content'),
		max_left = 918 - rc.width();
	
	if(max_left >= 0) max_left = 0;
	
	rc.css('marginLeft', max_left);
}

$().ready(function() {
	$('#left_arrow')
		.css('cursor', 'pointer')
		.click(function() {
			var rc = $('#request_content'),
				left = parseInt( rc.css('margin-left').replace(/px/i, '') );
			
			if( left < 0 ) rc.stop(true, true).animate({marginLeft: ((left + 210) <= 0) ? left + 210 : 0}, 700);
		});
	
	$('#right_arrow')
		.css('cursor', 'pointer')
		.click(function() {
			var rc = $('#request_content'),
				left = parseInt( rc.css('margin-left').replace(/px/i, '') ),
				max_left = rc.width() - 918;
			
			if( left > (918 - rc.width() ) ) {
				//rc.css('margin-left', left - 50 );
				rc.stop(true, true).animate({marginLeft: (Math.abs(left-210) < max_left) ? left - 210 : -max_left}, 700);
			}
		});
	
	maxLeft();
});
