function refreshBasketStats() {
	$.getJSON("/basket/getBasketStats", {}, function(data) {
		jQuery("#basketAmount").html(data.amount);
		jQuery("#basketSumm").html(data.summ);
		if (jQuery("#basketTableSumm"))
			jQuery("#basketTableSumm").html(data.summ);
	});
}

function appendFilter(filter) {
	window.location = filter.options[filter.selectedIndex].value;
}

function selectBrand(brand) {
	if (brand.selectedIndex == 0) {
		window.location = "/catalog/";
	} else {
		window.location = "/catalog/brand/"
				+ brand.options[brand.selectedIndex].value;
	}
	return true;
}

function filter(id) {
	var arr = window.location.toString().split('/');
	if (id != 'price')
	{
		for ( var i in arr) {
			if (arr[i] == 'filter' + id + '_from') {
				arr[parseInt(i) + 1] = $('#amount_' + id).val().split(' - ')[0];
				var found = true;
			}
			if (arr[i] == 'filter' + id + '_to') {
				arr[parseInt(i) + 1] = $('#amount_' + id).val().split(' - ')[1];
				var found = true;
			}
		}
	}
	else
	{
		for ( var i in arr) {
			if (arr[i] == 'price_from') {
				if ($('#price_from').val()!='')
					arr[parseInt(i) + 1] = $('#price_from').val();
				else
					arr.splice(i,2);
				var found = true;
			}
			if (arr[i] == 'price_to') {
				if ($('#price_to').val()!='')
					arr[parseInt(i) + 1] = $('#price_to').val();
				else
					arr.splice(i,2);
				var found = true;
			}
		}
	}
	if (!found) {
		if (id != 'price')
		{
			arr.push('filter' + id + '_from');
			arr.push($('#amount_' + id).val().split(' - ')[0]);
			arr.push('filter' + id + '_to');
			arr.push($('#amount_' + id).val().split(' - ')[1]);
		}
		else
		{
			if ($('#price_from').val()!='')
			{
				arr.push('price_from');
				arr.push($('#price_from').val());
			}
			if ($('#price_to').val())
			{
				arr.push('price_to');
				arr.push($('#price_to').val());
			}
		}
	}
	window.location = arr.join('/');
}

function setLang(lang) {
	$.cookie('lang', lang);
	location.reload();
}

function addToCart(itemId) {
	$.getJSON("/basket/add", {
		itemId : itemId
	}, function(data) {
		refreshBasketStats();
		showMessage('Товар успешно добавлен в корзину');
	});
}

function StopDrag() {
	filter(window.id);
}
function showMessage(text, time) {
	if (!time)
		var time = 5600;
	if (text)
		$("#messageText").html(text);
	$('#messages').slideDown(function() {
		setTimeout(function() {
			jQuery('#messages').slideUp();
		}, time);
	//console.log(time);
	});
}
function getCompareItems()
{
	$.get("/catalog/getcompareitem", { },
			   function(data){
					if (data.length > 1)  
						{
						  $("#compareItems").show();
						  $("#compareItems").html(data);
						}
					else
						 $("#compareItems").hide();
			   });
}


function addToCompare(itemId)
{
	$.get("/catalog/addcompareitem", {id:itemId},
			   function(data){
					getCompareItems();
					showMessage('Товар успешно добавлен в список сравнения');
			   });
}

function delCompareitem(itemId, cat)
{
	$.get("/catalog/delcompareitem", {id:itemId, cat:cat},
			   function(data){
					getCompareItems();
			   });
}
function FilerOnEnter(id)
	    {
	       var  testTextBox = $(id);
	        var code =null;
	        testTextBox.keypress(function(e)
	        {
	            code= (e.keyCode ? e.keyCode : e.which);
	            if (code == 13)
		            filter('price');
	        });

	    }
