/* Tags list */
var tagsList = {
selected: [],
open: function(el) {
	$(el).closest('.section').addClass('with-minors').find("dl:hidden").fadeIn(700);
},
select: function(tag) {
	this.selected = this.selected.concat(tag);
	this.apply();
},
unselect: function(tag) {
	this.selected = this.selected.except(tag);
	this.apply();
},
apply: function() {
	var tags = this.selected.join("+");
	var url = window.location.href;
	window.location = url.replace(/places(\/?[\+\w]*\/?)/, 'places/' + tags + (tags ? "/" : ""));
}
};

/* Places list */
init.placesOrder = function(id) {
	var placesOrder = new stationsButton('#places-order');
	var hash = window.location.hash.substring(1);
	if (!hash && id) {
		placesOrder.select(id);
	}
	var state = $('#get-places-state');
	var getAjax = ajax('place/get', {
		control: state,
		error: true,
		process: function(s) {
			$('#places-list').html(s);			
		}
	});
	state.bind('send', function() {
		$(this).html('Загрузка мест…').show();
	}).bind('error success', function() {
		$(this).hide().html('');
	});
	placesOrder.onchange = function() {
		var po = placesOrder.selected;
		var data = {
			uname: tagsList.selected
		};
		if (po) {
			data.lname = po.lname;
			data.sname = po.sname;
		}
		getAjax.send(data);
		window._gaq && _gaq.push(['_trackEvent', 'Каталог', 'Сортировка мест']);		
	};
	if (hash) {
		$('#places-list').html('');
		placesOrder.select(hash);
		placesOrder.onchange();
	}
	$('#places-order').css('visibility', 'inherit');
};

function showNets() {
	var context = $("#nets");
	var all = $("dl", context);
	var active = all.filter(":visible");
	var limiter = 0;
	while (limiter++ < 3 && active.length < all.length && active.length < 3) {
		all.filter(":hidden").eq(0).removeClass("minor");
		active = all.filter(":visible");
	}
	var amount = all.length - active.length;
	$(".more span.link", context).toggle(amount > 0).html("Ещё " + amount.inflect("сеть", "сети", "сетей"));
}
