/*Вертикальное выравнивание тескта button для firefox*/
function buttons_fix_ff()
{
	buttons = document.getElementsByTagName('button');
	for(i=0; i<buttons.length; i++)
		buttons[i].style.paddingBottom = '4px';
}
if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1)
{
	window.addEventListener("load", buttons_fix_ff, false);
}

jQuery(document).ready(function()
{
	
	$('del.ruble').empty().append('<span class="nd">&ndash;</span><span class="ru">p</span>');
	
	/*Равные колонки*/
	dh = jQuery(".left_column").height() - jQuery(".main_column").height();
	if(dh > 0)
		jQuery(".main_column_inner").css("padding-bottom", dh + 30);
	
	
	/*Галерея*/
	jQuery("#thumbs li a").click(function()
	{
		jQuery("#thumbs li a").removeClass("active");
		jQuery(this).addClass("active");
		jQuery("#main_photo").attr("src",jQuery(this).attr("rel"));
		return false;
	});
	
	jQuery(document).mousedown(function(event)
	{
		if( $(event.target).parents("#enter").length === 0 )
			$("#enter").hide();
		if( $(event.target).parents("#cabinet").length === 0 )
			$("#cabinet").hide();
	});
	
	/*Форма входа*/
	jQuery("#enter_opener").click(function()
	{
		jQuery("#enter").slideToggle('fast');
		return false;
	});
	
	/*Личный кабинет*/
	jQuery("#cabinet_opener").click(function()
	{
		jQuery("#cabinet").slideToggle('fast');
		return false;
	});
	
	/*Нестандартные 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");
		jQuery("input.custom_radio").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);
		}
	});
});

/*Нестандартный 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" scrolling="no" tabIndex="-1" frameborder="0"></iframe>');
			select_elem.next().css({height:select_elem.height()+4+'px', width:select_elem.width()+4+'px'});
		}
		
		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);
