function sliderVal(left, right, leftId, rightId, postfix) {
	$(leftId).get(0).value = left + postfix
	$(rightId).get(0).value = right + postfix
	}


$(document).ready(function() {
    $(".jNice").jNice();

	var defaultSearch = 'Найти на сайте';
	$('.b-search-footer .text').focus(function() {
		if ($(this).val() == defaultSearch)
			$(this).val('');
	}).blur(function() {
		if ($(this).val() == '')
			$(this).val(defaultSearch);
	});
	$('.b-search-footer').submit(function() {
		$(this).find('.text').focus();
	});

    $(".currency A").live("click", function() {
        $(".l-meta").attr({ "class": "l-meta " + $(this).attr("class").substring(2) });
        $(".currency STRONG").each(function(i, item) {
            $(item).replaceWith(
                $("<a />").html($(item).html()).attr({ "href": "#", "class": $(item).attr("class") })
            );
        });
        $(this).replaceWith(
            $("<strong />").html($(this).html()).attr({ "class": $(this).attr("class") })
        );

        setCookie("currency", $(this).attr("class").substring(2), 365);
        return false;
    });

	if ($('#slider-price').length) {
		$('#slider-price').trackbar({
			onMove : function() {
				sliderVal(this.leftValue, this.rightValue, "#slider-price-l", "#slider-price-r", "");
			},
			width : 178, // px
			leftLimit : 0, // unit of value
			leftValue : 100, // unit of value
			rightLimit : 3000, // unit of value
			rightValue : 800, // unit of value
			clearLimits : true,
			clearValues : true
		});
	}


	// Bike Search dialog
	var $catSelect = $('#search-filter-category');
	var $venSelect = $('#search-filter-vendor');
	var optionsByType = new Array();

	$venSelect.change(function() {
		var vendor = $venSelect.val();
		if (optionsByType[ vendor ])
			return $catSelect.html( optionsByType[ vendor ] );

		$.ajax({
			url: '/ajax/vendor-tags/' + vendor,
			type: 'GET',
			dataType: 'json',
			success: function(data) {
				$catSelect.empty();
				for (i = 0; i < data.length; i++) {
					var opt = $("<option />").attr('value', data[i].id).html(data[i].name);
					$catSelect.append(opt);
				}
				optionsByType[ vendor ] = $catSelect.html();
			}
		});
	});


	// Items compare
	function handleCompareCheckboxes() {
		$('.compare :checkbox').removeAttr('name');
		$('.compare :checkbox').click(function() {
			var $this = $(this);
			var $p = $(this).parent('.compare')
			var val = $(this).val();

			$.ajax({
				url: '/ajax/toggle-compare/' + val,
				type: 'GET',
				dataType: 'json',
				beforeSend: function() {
					$p.addClass('ajax');
				},
				success: function(data) {
					var found = false;
					for (var i = 0; !found && i < data.length; i++)
						if (data[i] == val)
							found = true;

					if (found)
						$this.attr('checked', 'checked');
					else
						$this.removeAttr('checked');

					$p.removeClass('ajax');
				}
			});
		});
	}
});

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + (expiredays ? "" : ";expires=" + exdate.toUTCString()) + ";path=/";
}
