
jQuery(document).ready(function()
{
	/* Фильтр товаров */	
	
	if(defined('getCookie'))
	{ 
		if(parseInt(getCookie("catalog_filter_show")) )
		{
			jQuery("#catalog_filter_switcher").addClass("active");
			jQuery("#catalog_filter_switcher").next().show();
		}
	}	
	
	jQuery("#catalog_filter_switcher").click(function()
	{
		jQuery(this).toggleClass("active");
		if( jQuery(this).hasClass("active") )
		{
			jQuery(this).next().show();
			document.cookie = "catalog_filter_show=1; path=/";
		}
		else
		{
			jQuery(this).next().hide();
			document.cookie = "catalog_filter_show=0; path=/";
		}
		return false;
	});
	
	/* Вертикальное выравнивание дополнительных изображений товара */
	jQuery("#thumbnails img").each(function()
	{
		dh = (jQuery(this).parent().height() - jQuery(this).height());
		if( dh > 1 )
			jQuery(this).css('padding-top', dh/2)
	});
	
	/* Нестандартные input-radio */
	jQuery("input.custom_radio").each(function()
	{
		radio = jQuery(this);
		radio.parent().addClass("custom_radio_wrapper");		
		if(radio.attr("checked"))
			radio.wrap('<span class="custom_radio custom_radio_checked"></span>');
		else if(radio.attr("disabled"))
			radio.wrap('<span class="custom_radio custom_radio_disabled"></span>');
		else
			radio.wrap('<span class="custom_radio"></span>');
	});
	jQuery(".custom_radio_wrapper").mousedown(function()
	{
		radio = jQuery(this).find("input").eq(0);
		radio_name = radio.attr("name");
		radios_form = jQuery(this).parents('form');
		jQuery("input.custom_radio", radios_form).each(function()
		{
			if( jQuery(this).attr("name") == radio_name )
				jQuery(this).parent().removeClass("custom_radio_checked");
		});
		radio.parent().addClass("custom_radio_checked");
		radio.attr("checked", true);
	});
	
	/* Нестандартные input-checkbox */
	jQuery("input.custom_checkbox").each(function()
	{
		checkbox = jQuery(this);
		checkbox.parent().addClass("custom_checkbox_wrapper");
		if(checkbox.attr("checked"))
			checkbox.wrap('<span class="custom_checkbox custom_checkbox_checked"></span>');
		else if(checkbox.attr("disabled"))
			checkbox.wrap('<span class="custom_checkbox custom_checkbox_disabled"></span>');
		else
			checkbox.wrap('<span class="custom_checkbox"></span>');
	});	
	jQuery(".custom_checkbox_wrapper").click(function()
	{
		checkbox = jQuery(this).find("input").eq(0);
		if(checkbox.attr("checked"))
		{
			checkbox.parent().removeClass("custom_checkbox_checked");
			checkbox.attr("checked", false);
		}
		else
		{
			checkbox.parent().addClass("custom_checkbox_checked");
			checkbox.attr("checked", true);
		}
	});
});

/* Custom select в HTML нужно только присвоить класс custom_select тем селектам которые хотим изменить */
(function($){
	$.fn.custom_select = function()
	{
		$(document).mousedown(function(event)
		{
			if ($(event.target).parents('.custom_select').length === 0)
				$('.custom_select ul').hide();
		});
		
		return this.each(function(index)
		{
			custom_select_init(this, index);
		});
	};

	var custom_select_init = function(element, index)
	{
		var select_elem = $(element);
		var select_w = select_elem.outerWidth();
				
		select_elem.wrap('<div class="custom_select_wrapper"></div>');
		var select_wrapper = select_elem.parent();
		select_wrapper.width(select_w);
		index = (index) ? index : 0;
		select_wrapper.css('zIndex',100-index);
		
		select_elem.after('<div class="custom_select"><div><span class="text"></span><span class="opener"></span></div><ul></ul></div>');
		var select_custom = $(element).siblings('.custom_select').width(select_w);
		var select_text = jQuery('.text', select_custom);
		var select_opener = jQuery('.opener', select_custom);
		select_text.width(select_w - select_opener.width() - parseInt(select_text.css('padding-left')) - parseInt(select_text.css('padding-right')));
		
		ul = $('div', select_custom).siblings('ul');
		ul.width(select_w-2);
		
		if( jQuery.browser.msie && jQuery.browser.version < 7 )
		{
			select_elem.after('<iframe src="javascript:\'\';" marginwidth="0" marginheight="0" align="bottom" allowTransparency="true" scrolling="no" tabIndex="-1" frameborder="0"></iframe>');
			select_elem.next().css({height:select_elem.height()+4+'px', width:select_elem.width()+4+'px'}).animate({opacity: "0"}, "fast");

		}
		
		custom_select_update(element);
		
		$('div', select_custom).click(function()
		{
			ul = $(this).siblings('ul');
			if (ul.css('display')=='none')
				$('.custom_select ul').hide()
			ul.slideToggle(100);
			var offset = ($('a.selected', ul).offset().top - ul.offset().top);
			ul.animate({scrollTop: offset});
			return false;
		});
	};

	var custom_select_update = function(element)
	{
		var select_elem = $(element);
		var select_custom = select_elem.siblings('.custom_select');
		var $ul = select_custom.find('ul').find('li').remove().end().hide();
		$('option', select_elem).each(function(i){
			$ul.append('<li><a href="#" index="'+ i +'">'+ this.text +'</a></li>');
		});

		$ul.find('a').click(function()
		{
			$('a.selected', select_custom).removeClass('selected');
			$(this).addClass('selected');	
			if( select_elem[0].selectedIndex != $(this).attr('index') && select_elem[0].onchange )
			{
				select_elem[0].selectedIndex = $(this).attr('index');
				select_elem[0].onchange();
			}
			select_elem[0].selectedIndex = $(this).attr('index');
			$('span:eq(0)', select_custom).html($(this).html());
			$ul.hide();
			return false;
		});

		$('a:eq('+ select_elem[0].selectedIndex +')', $ul).click();
	};

	$(function()
	{
		$('select.custom_select').custom_select();
	});
})(jQuery);


