function refreshBasketStats() {
	$.getJSON("/basket/getBasketStats", {}, function(data) {
		$("#basketAmount").html(data.amount);
		$("#basketSumm").html(data.summ);
		$('.b-total-summ span.total-price').html(data.summ);
	});
}

function getTransferSumm()
{
	if ($("#transfer").size()>0)
	{
	$.get("/basket/transfersumm", {}, function(data)
			{
				if (data.length > 0)
					$("#transfer td").html(data);
			});
	}
}

function addToCart(itemId, credit) {
	if (!itemId)
		return false;
	if (!credit)
		var getvars = {itemId : itemId};
	else
		var getvars = {itemId : itemId, credit: 1};
	$.get("/basket/addtocart", getvars, function(data) {
		$(".b-shadow").toggle();
		$(".b-minicart").css("top", $(window).scrollTop()+100);
		$('.b-minicart').show();
		$(".b-minicart").html(data);
		$(".input-count").keyfilter(/[\d\-]/);
		$(".b-shadow").height($(document).height()).width($(window).width());
		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();
			   });
}
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)
	}
}

$(document).ready(function(){
	//* Выбор валюты *//
	$('.cart-currency').hover(function(){
		$('.b-currency-list').toggle();
		});
		
		
	//* Выбор региона *//	
	$('.select-region').hover(function(){
		$('.b-city-box').toggle();
		});
		

	//* Выбор бренда *//
	$('.b-brand').click(function(e){
		e.stopPropagation();
	    $('.b-brand-list').show();
	});
	$('.b-layout').click(function(){
	    $('.b-brand-list').hide();
	});
	
	    	
	//* Выбор категории поиска *//
	$('.b-search-cat').live('click',function(e){
		$('.b-search-list').show();
	});
	$('.b-layout').click(function(){
	    $('.b-search-list').hide();
	});

	
	//* Раскрывающееся меню слева *//
	$('.b-left-col .navigation > li > a').live('click',function(e)
			{
				e.preventDefault();
				$('ul.navigation>li.active').toggleClass('active')
				$(this).parent().toggleClass('active');
				$(this).parent().children('ul').each(function(index){
					$(this).toggleClass('active');
				});
				
			}
		);
	if (window.location.href.match('search.+catid'))
	{
		var catId = parseInt(window.location.href.substr(window.location.href.indexOf('catid')+6,window.location.href.length));
		$("#menu-"+catId).parent().addClass('active');
		$("#menu-"+catId).parent().parent().parent().addClass('active');
	}
	if ($('.navigation li.active').size()==0)
	{
		$('.navigation').children('li').first().addClass('active');
	}
	
	
	//* Позиционирование сео-текста на главной *//
	var	b_seo_text = $('.b-seo-text').height();
		
		$('.b-right-col').css({'padding-bottom':b_seo_text})
    
	//* Minicart *//
	var shadow = $('.b-shadow');
	    doc_w = $(document).width();
		doc_h = $(document).height();
		shadow.css({'width':doc_w, 'height':doc_h});
		
    var minicart = $('.b-minicart');
	    minicart.css('left', (doc_w/2 - minicart.width()/2));
				
		$('.b-minicart-close, .b-link-next').live('click', function(){
		    minicart.hide();
			shadow.hide();
		});
		
	//* Авторизация *//
	var popup = $('.b-popup');
		$('.open-auth').live('click', function(){
			popup.css("top", $(window).scrollTop()+150);
			popup.css({'left':(doc_w/2 - popup.width()/2)})
			shadow.show();
			popup.show();
			
		});
		
		$('.b-popup-close').live('click', function(){
		    shadow.hide();
			popup.hide();
		});
	
	
	
    	
});
