	$(document).ready(function() {
		
		$('.search-form .input').focus(function() {
			if ($(this).val() == "Поиск по сайту") {
				$(this).addClass('active').val("");
			}
		}).blur(function() {
			if ($(this).val() == "") {
				$(this).removeClass('active').val("Поиск по сайту");
			}
		});

		if ($('.price-blocks .price-block').length > 0)
		{
			//alert('hello');
			var cont = $($('.price-blocks')[0]);
			var blocks = $('.price-blocks .price-block');
			var blockMinW = 230;
			var blockMargin = 20;
				
			function processPriceBlocks()
			{
				var contW = cont.width();
				var num = Math.floor(contW/blockMinW);
				var newBlockW = Math.floor((contW - blockMargin * (num - 1)) / num);
				newBlockW = newBlockW - 2;
				$(blocks).each(function(ind, elem){
					$(elem).css({'width': newBlockW + 'px', 'margin-right': blockMargin});
				});
			}
			
			processPriceBlocks();
			$(window).load(processPriceBlocks);
			$(window).resize(processPriceBlocks);
		} 
		
		if ($('.catalog-item').length > 0)
		{
			$('.fancy').fancybox();
		}
		
		if ($('.portfolio').length > 0)
		{
			var current_tick = $($('.portfolio-tick.active')[0]);
			var current_inn = $($('.portfolio-inn.active')[0]);
			var current_tick_id = current_tick.attr('id');
			$('.portfolio-tick').click(function(){
				if (current_tick_id != $(this).attr('id'))
				{
					$(this).addClass('active');
					current_tick.removeClass('active');
					current_tick = $(this);
					current_tick_id = current_tick.attr('id');
					current_inn.removeClass('active');
					current_inn = $($('#' + current_tick_id + '_inn')[0]);
					current_inn.addClass('active');
				}
				return false;
			});
		}
		
		if ($('.catalog-item').length > 0)
		{
			var curr_cati_tab = $($('.catalog-item-tab.active')[0]);
			var curr_cati_inn = $($('.catalog-item-inn.active')[0]);
			var curr_cati_id = curr_cati_tab.attr('id');
			$('.catalog-item-tab').click(function(){
				if (curr_cati_id != $(this).attr('id'))
				{
					$(this).addClass('active');
					curr_cati_tab.removeClass('active');
					curr_cati_tab = $(this);
					curr_cati_id = curr_cati_tab.attr('id');
					curr_cati_inn.removeClass('active');
					curr_cati_inn = $($('#' + curr_cati_id + '_inn')[0]);
					curr_cati_inn.addClass('active');
				}
				return false;
			});
		}
		
		if ($('#index-image').length > 0)
		{
			var index_links = $('.index-image-link');
			var index_images = $('.index-image-img');
			var curr_ind = 0;
			var interval = 0;
			
			function changeIndexImage(target_ind)
			{
				new_link = $(index_links[target_ind]);
				new_img = $(index_images[target_ind]);
				new_link.addClass('active');
				$(index_links[curr_ind]).removeClass('active');
				new_img.addClass('active');
				$(index_images[curr_ind]).removeClass('active');
				curr_ind = target_ind;
			}
			function nextIndexImage()
			{
				var new_ind = curr_ind + 1;
				if (new_ind >= index_images.length)
				{
					new_ind = 0;
				}
				changeIndexImage(new_ind);
				clearInterval(interval);
				interval = setInterval(nextIndexImage, 4000);
			}
			
			index_links.each(function(ind, el){
				$(el).attr('ind', ind);
			});
			$('.index-image-link').click(function(){
				if (curr_ind != parseInt($(this).attr('ind')))
				{
					changeIndexImage(parseInt($(this).attr('ind')));
					clearInterval(interval);
					interval = setInterval(nextIndexImage, 4000);
				}
				return false;
			});
			
			interval = setInterval(nextIndexImage, 4000);
		}
		
		function refreshLogo()
		{
			$('#logo img').attr('src', '/common/images/a_logo_2.gif');
		}
		
		setInterval(refreshLogo, 3000);

		$('.h-menu-items .menu-item').mouseover(function(){
			$(this).find('.submenu').show();
		}).mouseleave(function(){
			$(this).find('.submenu').hide();
		});

		
		/* $('.call-order-show').show().click(function(){
			alert('Консультант');
		});
		
		var overlay = $('#register-overlay');
		var register = $('#register');

		function onWindowResize(){
			var winH = $(this).height();
			var regH = register.height();
			var newTop = Math.ceil((winH - regH)/2);
			newTop = $(this).scrollTop() + Math.max(newTop, 10);
			register.css({top: newTop + 'px'});
		}
		function showRegForm(){
			//overlay.show();
			overlay.animate({opacity: 'toggle'}, 500);
			overlay.click(hideRegForm);
			onWindowResize();
			$(window).resize(onWindowResize);
			register.animate({opacity: 'toggle'}, 500);
			//register.show();
			return false;
		}
		function hideRegForm(){
			overlay.animate({opacity: 'toggle'}, 500);
			//overlay.hide();
			register.animate({opacity: 'toggle'}, 500);
			//register.hide();
			overlay.unbind('click');
			$(window).unbind('resize');
			return false;
		}
		$('#register-show').click(showRegForm);
		$('#register-close').click(hideRegForm); */
	});
	$(window).load(function(){
		var catalog_list_items = $('.catalog-list-item');
		
		if (catalog_list_items.length > 0){
			var cont_w = $('.catalog-list').width();
			var el_w = $('.catalog-list-item:first').width();
			var max_in_row = Math.floor(cont_w/el_w);
			var margin_size = (cont_w - (el_w+2)*max_in_row)/(max_in_row-1);
			var max_height = 0;
			$(catalog_list_items).each(function(ind, el){
				max_height = Math.max(max_height, $(el).height());
				if ((ind+1)%max_in_row != 0){
					$(el).width(el_w).css('margin-right',margin_size+'px');
				} else {
					$(el).width(el_w).css('margin-right',0)
				}
			});
			$(catalog_list_items).height(max_height);
		}

		var price_block_items = $('.price-block');
		
		if (price_block_items.length > 0){
			var cont_w = $('.price-blocks-list').width();
			var el_w = $('.price-block:first').width();
			var max_in_row =  Math.floor(cont_w/el_w);
			var margin_size = (cont_w - el_w * max_in_row)/(max_in_row-1);
			$(price_block_items).each(function(ind, el){
				if ((ind+1)%max_in_row != 0){
					$(el).width(el_w).css('margin-right',margin_size+'px');
				} else {
					$(el).width(el_w).css('margin-right',0)
				}
			});
		}
	});

	$(window).resize(function(){
		var catalog_list_items = $('.catalog-list-item');
		
		if (catalog_list_items.length > 0){
			var cont_w = $('.catalog-list').width();
			var el_w = $('.catalog-list-item:first').width();
			var max_in_row = Math.floor(cont_w/el_w);
			var margin_size = (cont_w - (el_w+2) * max_in_row)/(max_in_row-1);
			$(catalog_list_items).each(function(ind, el){
				if ((ind+1)%max_in_row != 0){
					$(el).width(el_w).css('margin-right',margin_size+'px');
				} else {
					$(el).width(el_w).css('margin-right',0)
				}
			});
		}

		var price_block_items = $('.price-block');
		
		if (price_block_items.length > 0){
			var cont_w = $('.price-blocks-list').width();
			var el_w = $('.price-block:first').width();
			var max_in_row = Math.floor(cont_w/el_w);
			var margin_size = (cont_w - el_w * max_in_row)/(max_in_row-1);
			$(price_block_items).each(function(ind, el){
				if ((ind+1)%max_in_row != 0){
					$(el).width(el_w).css('margin-right',margin_size+'px');
				} else {
					$(el).width(el_w).css('margin-right',0)
				}
			});
		}

		
	});

