function setCookie (name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

$(document).ready(function(){
	$('#sort__button_price').click(function(){ 
		setCookie('product_sort','price',null,'/');
		setCookie('product_brand',$('#sort_brand').val(),null,'/');
		window.location.reload(); 
	});
	$('#sort__button_name').click(function(){ 
		setCookie('product_sort','name',null,'/');
		setCookie('product_brand',$('#sort_brand').val(),null,'/');
		window.location.reload(); 
	});
	$('#sort_brand').change(function(){
		if ($('#sort__button_price').hasClass('sort_button_active'))
			setCookie('product_sort','price',null,'/');
		else
			setCookie('product_sort','name',null,'/');
		setCookie('product_brand',$('#sort_brand').val(),null,'/');
		window.location.reload(); 
	});
	
	if (!getCookie('product_brand')) {
		$('option:first' ,'#sort_brand').attr('selected','selected');
	}
	else {
		$('#sort_brand').val(getCookie('product_brand'));
		setCookie('product_brand','0',new Date(0),'/');
	}

	if (getCookie('product_sort'))
		setCookie('product_sort','0',new Date(0),'/');
	else {
		$('#sort__button_price').removeClass('sort_button_active');
		$('#sort__button_name').addClass('sort_button_active');
	}
});


$(document).ready(function(){
	$('.count_plus').click(function(){
		var el = $('.count_input',$(this).parent());
		var val = el.val();
		el.val(val*1+1);
	});

	$('.count_minus').click(function(){
		var el = $('.count_input',$(this).parent());
		var val = el.val()*1-1;
		if (val < 1) val = 1;
		el.val(val);
	});
});
