function refreshBasketStats() {
	$.getJSON("/basket/getBasketStats", {}, function(data) {
		$("#basketAmount").html(data.amount);
		$("#basketSumm").html(data.summ);
		if (data.creditAmount!='0')
		{
			if ($(".itemCount").size()==1)
				$('<p class="itemCount">Товаров в кредите <strong id="creditAmount">'+data.creditAmount+'</strong></p>').insertBefore('.cartPrice');
			else
				$('#creditAmount').html(data.creditAmount);
		}
		else
		{
			if ($('.creditI').size()!=0)
			{
				$('.creditI').remove();
				$('.empty').last().remove();
			}
			if ($(".itemCount").size()==2)
				$(".itemCount").last().remove();
		}
		if (data.amount==0 && data.creditAmount==0)
		{
			var code = $('#basket_img a').html();
			$('#basket_img a').remove();
			$('#basket_img').append(code);
		}
		else
		{
			if ($('#basket_img a').size()==0)
			{
				var code = $('#basket_img').html();
				$('#basket_img').html('<a href="/basket/order">'+code+'</a>');
			}
		}
		if (data.amount==0)
		{
			if ($(".semiopacity").size()==1)
			{
				if ($('.middle table :first tr:first').attr('class')!='creditI')
					$('.middle table :first tr:lt(3)').remove();
			}
		}
		if (parseInt(data.summ) > 0)
			$(".cart span a").addClass("full")
		else
			$(".cart span").addClass("empty")
		if ($("#basketTableSumm").size() > 0)
			$("#basketTableSumm").html(data.summ+data.delivery);
		if ($("#di").size() > 0)
			$("#di").html(data.summ+data.delivery);
		if ($("#transfer").size()>0)
			getTransferSumm();
		if ((data.amount=='0' && data.creditAmount=='0') && $(".basket_text").size() > 0)
			$(".basket_text").html("Ваша корзина пуста");
		if ((data.amount=='0' && data.creditAmount=='0') && $(".semiopacity").css('display')!='none')
			$(".semiopacity").toggle();
		if ($("#credit_amount").size() > 0)
		{
			$("#credit_amount").val(data.creditSumm);
			recalcCredit();
		}
	});
}

function getTransferSumm()
{
	if ($("#transfer").size()>0)
	{
	$.get("/basket/transfersumm", {}, function(data)
			{
				if (data.length > 0)
					$("#transfer td").html(data);
			});
	}
}

function addToCart(itemId, credit, type) {
	if (!itemId)
		return false;
	
		var getvars = {itemId : itemId};
	if (credit)
		getvars.credit = 1;
	if (type)
		getvars.type = type;
	$.get("/basket/addtocart", getvars, function(data) {
		$(".semiopacity").toggle();
		$(".minicart").css("top", $(window).scrollTop());
		$(".minicart .middle").html(data);
		$(".minicart .count input").keyfilter(/[\d\-]/);
		$(".semiopacity").height($(document).height()).width($(window).width());

		if (credit)
			initCalc();
		if (window.location.href.match('order'))
			{
			if (typeof(dojo)!='undefined')
				{
				if ($("#basket").html()!="Ваша корзина пуста")
				{
					var newStore = new dojo.data.ItemFileWriteStore({url:"/basket/getItems"});
		    		if (newStore)
		    		{
		    			grid.setStore(newStore);
		    		}
				}
				else
					window.location.reload();
				}
			else
				window.location.reload();
			}
		else
		{
			refreshBasketStats();
		}
	});
}


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')
		var id = id.substr(7);
	
	if (id != 'price')
	{
		for ( var i in arr) {
			if (arr[i] == 'filter' + id + '_from') {
				arr[parseInt(i) + 1] = $('#amount_from_' + id).val()!='' ? $('#amount_from_' + id).val() : 0;
				var found = true;
			}
			if (arr[i] == 'filter' + id + '_to') {
				arr[parseInt(i) + 1] = $('#amount_to_' + id).val()!='' ? $('#amount_to_' + id).val() : 0;
				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');
			var val = $('#amount_from_' + id).val()!='' ? $('#amount_from_' + id).val() : 0;
			arr.push(val);
			arr.push('filter' + id + '_to');
			var val = $('#amount_to_' + id).val()!='' ? $('#amount_to_' + id).val() : 0;
			arr.push(val);
		}
		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('/').replace("#",'');
}

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();
					if ($('#comp_'+itemId).size() > 0)
						$('#comp_'+itemId).attr('class','b-add-compare-btn');
			   });
}
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');
	        });

	    }
function rotateBanner()
{
	var el = $("#banner");
	if ($(el).size() > 0)
	{
		var rawId = $(el).find('a').attr('id');
		var id = rawId.substring(2,rawId.length);
		$.getJSON('/banners/getbanner',{id:id},function(data){
			var aEl = $(el).find('a');
			
			$(aEl).attr('title',data.name);
			$(aEl).attr('href',data.href);
			$(aEl).attr('id','id'+data.id);
			$(el).find('img').attr('src','/public/uploads/'+data.filename);
		});
		setTimeout('rotateBanner()', 4500)
	}
}
