jQuery(document).ready(function()
{
	window.glassPrefix = '_l_';

		jQuery('#samples-doors img').click(function()
		{
			jQuery('#samples-doors a').removeClass('active');
			jQuery(this).parent().addClass('active');

			if (jQuery('#colorGlass')[0])
			{
				if(jQuery(this).attr('rel')=='b')
				{
					jQuery('#colorGlass').css('display', 'block');
				}
				else
				{
					if (jQuery('#colorGlass').css('display') != 'none')
					{
						jQuery('.glass').each(function()
						{
							jQuery(this)[0].checked = false;
						});

						jQuery('.glass')[0].checked = true;

						jQuery('#colorGlass').hide();
						if (window.glassPrefix == '_b_')
						{
							changeGlass('_l_');
						}
						window.glassPrefix = '_l_';
					}
					jQuery('#colorGlass').css('display', 'none');
				}
			}

			var prefix = jQuery(this).attr('alt'); 
			var bigImg = jQuery('#big-photo-doors img').attr('src').replace(/\d+\./, prefix + '.');
			jQuery('#big-photo-doors img').attr({'src':bigImg});

			var colorTitle = jQuery(this).parent().attr('title');
			jQuery('#description-doors h3').html(colorTitle);

			var colorDesc = jQuery('#color-doors #c'+ prefix).html();
			jQuery('#description-doors p').html(colorDesc);

			smallList = jQuery('#list-doors img');
			jQuery.each(smallList, function()
			{
				source = jQuery(this).attr('src').replace(/\d+\./, prefix + '.');
				jQuery(this).attr({'src':source});
				
				var b = jQuery(this).next('b')
				var imgTitle = jQuery(this).next('b').html().replace(/\d+\./, prefix + '.');
				b.html(imgTitle);
				b.parent().attr({'title': 'Модель ' + imgTitle});
				
				var bigImgTitle = jQuery('#big-photo-doors h3').html().replace(/\d+\./, prefix + '.');
				
				jQuery('#big-photo-doors h3').html(bigImgTitle);
			});

			return false;
		});

		jQuery('#list-doors a img').click(function()
		{
			jQuery('#list-doors a').removeClass('active');
			jQuery(this).parent().addClass('active');
			var bigImg = jQuery(this).attr('src').replace(/small/g, 'big');
			jQuery('#big-photo-doors img').attr({'src':bigImg});
			
			var bigImgTitle = jQuery(this).parent().attr('title');
			
			jQuery('#big-photo-doors h3').html(bigImgTitle);
			
			return false;
		});

		jQuery('.glass').click(function()
		{
			var prefix = '_l_';
			if (jQuery(this).val() == 'black')
			{
				prefix = '_b_';
			}
			changeGlass(prefix);

			return true;
		});
		/**/
		window.scrollWidth = 170;
		window.typesScroll = 0;
		var types = jQuery('#slider-doors ul');
		var total = types.children('li').size();
		if (total > 1)
		{
			jQuery('#samples-doors').append('<span id="types_back">&lt;&lt;</span>');
			jQuery('#samples-doors').append('<span id="types_next">&gt;&gt;</span>');
			jQuery('#types_back').click(function(){
				if (window.typesScroll > 0)
				{
					window.typesScroll--;
					var step = -1*(window.typesScroll*window.scrollWidth);
					types.css('marginTop', step+'px');
				}
			});
			jQuery('#types_next').click(function(){
				if (window.typesScroll < total-1)
				{
					window.typesScroll++;
					var step = -1*(window.typesScroll*window.scrollWidth);
					types.css('marginTop', step+'px');
				}
			});
		}
		/**/
});

function changeGlass(prefix)
{
	var expr = new RegExp(window.glassPrefix);
	window.glassPrefix = prefix;
	smallList = jQuery('#list-doors img');
	jQuery.each(smallList, function()
	{
		source = jQuery(this).attr('src').replace(expr, prefix);
		jQuery(this).attr({'src':source});
		
		var b = jQuery(this).next('b')
		var imgTitle = jQuery(this).next('b').html().replace(expr, prefix);
		b.html(imgTitle);
		b.parent().attr({'title': 'Модель ' + imgTitle});
		
		var bigImgTitle = jQuery('#big-photo-doors h3').html().replace(expr, prefix);
		
		jQuery('#big-photo-doors h3').html(bigImgTitle);
	});
	var bigImg = jQuery('#big-photo-doors img').attr('src').replace(expr, prefix);
	jQuery('#big-photo-doors img').attr({'src':bigImg});
}
