$(document).ready(function(){

	/*
	*** Фильтр ***
	*/
	
	// Сворачивание-разворачивание списка значений фильтра
	$("div.filter-param p.filter-current").click(function(){
		$(this).siblings("ul").slideToggle(250);
	});
	$("div.filter-param span.name").click(function(){
		$(this).parents("div.filter-param").find("ul").slideToggle(250);
	});
	$("div.filter-param span.filter-arrow").click(function(){
		$(this).parents("div.filter-param").find("ul").slideToggle(250);
	});

	// Установка конкретного значения фильтра	
	$("div.filter-param ul li span").click(function(){
	
		var href = $(this).find("a").attr("href");
		if (typeof href != 'undefined')
		{
			window.location.href = href;
		}
		else
		{
			$(this).parents("ul").siblings("p.filter-current").html($(this).html());
			
			var filterName = $(this).parents("ul").attr("name");
			if (filterName == 'PROPERTY_STYLE')
				$(this).parents("ul").siblings("input").val($(this).attr('id'));
			else
				$(this).parents("ul").siblings("input").val($(this).html());
			
			$(this).parents("div.filter-param").find("span.disable-filter").show();
			$(this).parents("ul").slideToggle(250);
		}
	});
	
	// Снятие конкретного значение фильтра
	$("div.filter-param span.disable-filter").click(function(){
		$(this).parents("div.filter-param").find("input").val("");
		$(this).parents("div.filter-param").find("p.filter-current").html("");
		$(this).hide();
	});
	
	// Очистка всех значений фильтра
	$("button#filter-clear").click(function(e){
		$(this).parents("form").find("p.filter-current").html("");
		$(this).parents("form").find("input.filter-input").val("");
		$(this).parents("form").find("span.disable-filter").hide();
		$(this).parents("form").find("ul").slideUp(250);
		e.preventDefault();
	});
	
	$("button#filter-search").click(function(e){
		$(this).parents('form').submit();
		e.preventDefault();
	});


	/*
	*** Сравнение ***
	*/
	
	// Сворачивание/разворачивание плашки
	$("nav#main li#compare").click(function(e){
		$("div#comparison").slideToggle(250);
		$(this).toggleClass("selected");
		e.preventDefault();
	});
	
	// Удаление выбранных элементов из сравнения
	$("div#comparison span#delete").click(function(){
		$("div#comparison input:checked").parents("li").fadeOut(500, function() {
			$.post("/catalog/ajax.comparison.php?action=delete", { id: $(this).attr("id") });
			$(this).remove();
			if ($("div#comparison ul.items li").length == 0)
			{
				$("div#comparison div.controls").hide();
				$("div#comparison").slideUp(500);
				$("nav#main li#compare").removeClass('selected');
				$("div#comparison p#no-compared-items").show();
			}
		});
	});
	
	// Удаление всех элементов из сравнения
	$("div#comparison span#delete-all").click(function(){
		$("div#comparison ul.items li").fadeOut(500, function() {
			$.post("/catalog/ajax.comparison.php?action=delete", { id: $(this).attr("id") });
			$(this).remove();
			$("div#comparison div.controls").hide();
			$("div#comparison").slideUp(500);
			$("nav#main li#compare").removeClass('selected');
			$("div#comparison p#no-compared-items").show();
		});
	});
	
	// Добавление к сравнению со страницы покрытия
	$("div.right a.comparison.add").click(function(e){
		$.post("/catalog/ajax.comparison.php?action=add", { id: $(this).attr("id") }, function(data){
			$("div#comparison ul.items").append(data);
			$("div.right a.comparison.add").html("Добавлено к сравнению");
			$("div.right a.comparison.add").removeClass("add");
			$("div#comparison p#no-compared-items").hide();
			$("div#comparison div.controls").show();
		});
		$(this).unbind('click');
	});
	
	// Добавление к сравнению со страницы списка покрытий
	$("ul#catalog-list li span.comparison.add").click(function(e) {
		$.post("/catalog/ajax.comparison.php?action=add", { id: $(this).attr("id") }, function(data) {
			$("div#comparison ul.items").append(data);
		});
		$(this).removeClass("add");
		$(this).unbind('click');
		$(this).html("добавлено к сравнению");
		$("div#comparison p#no-compared-items").hide();
		$("div#comparison div.controls").show();
	});
	
	
	/*
	*** Слайд-шоу на главной странице ***
	*/
	function pagerClick()
	{
		//console.log('test');
		$('ul#slideshow').cycle('pause');
	}
	
	$('ul#slideshow')
	.cycle({ 
    fx:     'scrollDown',
    speed:  500, 
    timeout: 5000, 
    pager:  '#slideshow-pager',
    onPagerEvent: pagerClick
	});
	
	
	/*
	*** Кнопка "Выбрать город" ***
	*/
	$("a#select-a-city-button").click(function(e){
		$("div#list-of-cities").slideToggle(250);
		e.preventDefault();
	});
	$("div#list-of-cities a.close").click(function(e){
		$("div#list-of-cities").slideToggle(250);
		e.preventDefault();
	});


	/*
	*** Салоны ***
	*/
	// Кнопка «Подробнее»
	$("ul.store-news a.read-more").click(function(e){
		$(this).parent().siblings("p.article-detail-text").slideToggle(250);
		e.preventDefault();
	});
	
	
	//  Карусель на страница элемента
	jQuery('div#similar-items ul').jcarousel({
		scroll: 1
	});
	
	// Градиент
	/*$(".dark-gradient").hover(
		function(e) { $(this).removeClass("dark-gradient").addClass("metallic-gradient"); },
		function(e) { $(this).removeClass("metallic-gradient").addClass("dark-gradient"); }
	);*/
	
	
	// Галерея
	$('ul#galleries li.gallery').each(function(){
		$(this).find('a').lightBox();
	});	

});
