//$(document).unbind('keydown');
var container=false;
$(document).bind('keydown', function(e) {
    if (e.keyCode == 27) {
	hideDialog();
    }
});    

$().ready(function() {
//alert(1);
container = $('.cdlg-window').parent();
 });    

function addCartItem(item) {
	var windowWidth	= $(window).width();
	if(windowWidth > 640) {
	showDialog();
	$('#basket_count').html('данные обновляются');
	$('#basket_price').html('данные обновляются');
	}
	$.getJSON("/cart/index.php", { add_item: item, return_json: 1 }, function(json){
		$('#basket_count').html(json.count+' шт.' );
		$('#basket_price').html(json.value+' р.');
		$('#info_basket_count').html(json.count );
		$('#info_basket_price').html(json.value);
		if(json.count==0)
			$('#cart_link').html('Ваша корзина пуста');
		else
			$('#cart_link').html('<a href="/cart/index.php" class="news"><b>Оформить заказ</b></a>');
});
}

function removeCartItem(item) {
	$.getJSON("/cart/index.php", { remove_item: item, return_json: 1 }, function(json){

		$('#tdTotal').html(json.value + ' руб.');

		$('#info_basket_count').html(json.count );
		$('#info_basket_price').html(json.value);
		if(json.count==0)
			$('#cart_link').html('Ваша корзина пуста');
		else
			$('#cart_link').html('<a href="/cart/index.php" class="news"><b>Оформить заказ</b></a>');
		$('tr#tr'+item).hide();
	return false;
});
return false;
}

function updateCartItem(item) {
	$.getJSON("/cart/index.php", { change_item : item, value: $('#change_'+item).val(), return_json: 1 }, function(json){

		$('#tdTotal').html(json.value + ' руб.');

		$('#info_basket_count').html(json.count );
		$('#info_basket_price').html(json.value);
		if(json.count==0)
			$('#cart_link').html('Ваша корзина пуста');
		else
			$('#cart_link').html('<a href="/cart/index.php" class="news"><b>Оформить заказ</b></a>');

});
return false;
}

function updateDost(item) {
	$.getJSON("/cart/index.php", { dost: item, value: $('#change_'+item).val(), return_json: 1 }, function(json){
       		$('#tdTotal').html(json.value + ' руб.');
		$('#info_basket_price').html(json.value);

});
return false;
}

function refreshCoords()
{
	var windowWidth	= $(window).width(),
		windowHeight= $(window).height(),
		cdlgWidth	= $('.cdlg-window', container).width(),
		cdlgHeight	= $('.cdlg-window', container).height(),
		windowLeft	= $(window).scrollLeft(),
		windowTop	= $(window).scrollTop(),
		widthCenter	= windowWidth / 2 - cdlgWidth / 2, 
		heightCenter= windowHeight / 2 - cdlgHeight / 2;
		
	$('.cdlg-window', container).css({
		'left'	: widthCenter + windowLeft,
		'top'	: heightCenter + windowTop
	});
	
	$('.cdlg-border', container).css({
		'width'	: cdlgWidth + 10,
		'height': cdlgHeight + 10,
		'left'	: widthCenter + windowLeft - 5,
		'top'	: heightCenter + windowTop - 5,
		'opacity': 0.7
	});
}

function showDialog()
{
	$(container).show(); 	
	refreshCoords();
}

function hideDialog()
{
	$(container).hide(); 	
}

$('.cdlg-window').hide(); 	

