$(document).ready(function(){

	$('.menu_table').find('.unit').mouseenter(function(){
	$(this).addClass('active');
	
	if ($(this).prev().hasClass('divider_unactive'))	
	$(this).prev().addClass('divider_unactive_active').removeClass('divider_unactive');	

	if ($(this).prev().hasClass('divider_empty'))	
	$(this).prev().addClass('divider_empty_active').removeClass('divider_empty');	
	
	if ($(this).next().hasClass('divider_unactive'))
	$(this).next().addClass('divider_active_unactive').removeClass('divider_unactive');
	
	if ($(this).next().hasClass('divider_left_right'))
	$(this).next().addClass('divider_left_right_active').removeClass('divider_left_right');
	
	}).mouseleave(function(){
	$(this).removeClass('active');
	
	if ($(this).prev().hasClass('divider_empty_active'))	
	$(this).prev().addClass('divider_empty').removeClass('divider_empty_active');	

	if ($(this).prev().hasClass('divider_empty_active'))	
	$(this).prev().addClass('divider_empty').removeClass('divider_empty_active');	

	if ($(this).next().hasClass('divider_active_unactive'))
	$(this).next().addClass('divider_unactive').removeClass('divider_active_unactive');

	if ($(this).prev().hasClass('divider_unactive_active'))
	$(this).prev().addClass('divider_unactive').removeClass('divider_unactive_active');
	
	if ($(this).next().hasClass('divider_left_right_active'))
	$(this).next().addClass('divider_left_right').removeClass('divider_left_right_active');
	}).click(function(){
	document.location.href = $(this).find('a').attr('href');
	});
	

$('.big_banner').find('.images').find('img').each(function(){
$("<img>").attr("src", $(this).attr('src'));
});
	
function go_rotate(current_img){
change_img = setInterval(function(){
$('.page_'+(current_img+1)).removeClass('active').addClass('unactive');
current_img = current_img + 1;
if (current_img == 5)
current_img = 0;
$('.big_banner').find('.images').find('img:eq('+(4-current_img)+')').fadeIn(1000);
$('.big_banner').find('.images').find('img:eq('+(4-current_img)+')').siblings().fadeOut(1000);
$('.page_'+(current_img+1)).addClass('active').removeClass('unactive');;
clearInterval(change_img);
go_rotate(current_img);
}, 2000);
};

go_rotate(0);
go_again = 0;

$('.big_banner_paginator').find('div').click(function(){
	clearInterval(change_img);
	if (go_again)
	clearInterval(go_again);
	$(this).removeClass('unactive').addClass('active');
	$(this).siblings().removeClass('active').addClass('unactive');
	if ($(this).hasClass('page_1'))
	this_id = 4;
	if ($(this).hasClass('page_2'))
	this_id = 3;
	if ($(this).hasClass('page_3'))
	this_id = 2;
	if ($(this).hasClass('page_4'))
	this_id = 1;
	if ($(this).hasClass('page_5'))
	this_id = 0;
	$('.big_banner').find('.images').find('img:eq('+this_id+')').fadeIn(1000);
	$('.big_banner').find('.images').find('img:eq('+this_id+')').siblings().fadeOut(1000);
	
	go_again = setInterval(function(){
	clearInterval(go_again);
	go_rotate(4-this_id);
	}, 2000);
});


$('.mainpagesub').mouseenter(function(){
	clearInterval(change_img);
	if (go_again)
	clearInterval(go_again);
	$('.big_banner_paginator').find('div').removeClass('active').addClass('unactive');
	
	$.get("/assets/ajax/show_image.php?type=onhoversubmainpage&href="+$(this).attr('href'), function(data){
	
	if ($('.big_banner').find('.images').find('.hover_sub_image').length == 0)
	hover_sub_image = $('<img>').addClass('hover_sub_image').attr('src', data).appendTo($('.big_banner').find('.images'));
	else
	hover_sub_image.attr('src', data);
	});

	go_again = setInterval(function(){
	clearInterval(go_again);
	hover_sub_image.fadeOut(2000, function(){$(this).remove();});
	go_rotate(0);
	}, 8000);	
}).mouseleave(function(){
	//hover_sub_image.remove();
});
	

function rotate_images_production(subdivisionid, number)
{
	if (timer_is_off != 1)
	{
		$.get("/assets/ajax/show_image.php?subdivisionid="+subdivisionid+"&num="+number, function(data){
			if (data != $('.production_bottom').find('.images').find('img').attr('src'))
			{
			$('.production_bottom').find('.images').find('img').fadeOut(900, function(){$(this).remove();});
					newimg = $('<img style="display:none">').attr('src', data).appendTo($('.production_bottom').find('.images'));
					newimg.fadeIn(1000, function(){
					setTimeout(function(){
					rotate_images_production(subdivisionid, number+1);
					}, 1000);
					});
			};
		});
	} else
	{
		setTimeout(function(){
		rotate_images_production(subdivisionid, number);
		}, 1000);	
	};
};

timer_is_off = 0;
rotate_images_production($('[name="subdivisionid"]').val(), 1);

$('.production_menu_unit').mouseover(function(){
timer_is_off = 1;
$.get("/assets/ajax/show_image.php?type=onhoversub&href="+$(this).attr('href'), function(data){
on_hover_image = $('<img>').attr('src', data).appendTo($('.production_bottom').find('.images'));
});
}).mouseleave(function(){
timer_is_off = 0;
});

$('.production_active_menu_unit').mouseover(function(){
timer_is_off = 1;
$.get("/assets/ajax/show_image.php?type=onhoverelement&id="+$(this).attr('message_id'), function(data){
on_hover_element_image = $('<img>').attr('src', data).appendTo($('.production_bottom').find('.images'));
});
}).mouseleave(function(){
timer_is_off = 0;
});


$('.call_us').click(function(){
	if ($('.call_us_form').length==0)
	{
		newformcallus = $("<div class='call_us_form'><input id='phone' value='Телефон'><input id='name' value='Ваше имя'><input type='button' value='Отправить' id='send_call_us'></div>").appendTo($(this).parent());
		newformcallus.fadeIn(600);
		
		$('#phone').focus(function(){
			if ($(this).val() == 'Телефон')
			{
				$(this).val('');
			};
		}).blur(function(){
			if ($(this).val() == '')
			{
				$(this).val('Телефон');
			};			
		});

		$('#name').focus(function(){
			if ($(this).val() == 'Ваше имя')
			{
				$(this).val('');
			};
		}).blur(function(){
			if ($(this).val() == '')
			{
				$(this).val('Ваше имя');
			};			
		});
		
		$('#send_call_us').click(function(){
			$.get('/assets/ajax/send_call_us.php?phone='+$('#phone').val()+'&name='+$('#name').val(), function(){
				$('.call_us_form').fadeOut(600, function(){$(this).remove();});
			});
		});
		
	} else
	{
		$('.call_us_form').fadeOut(600, function(){$(this).remove();});
	};
})
	

$('.price_ask').click(function(){
	if ($('.newprice_ask_form').length==0)
	{
	newprice_ask_form = $("<div class='price_ask_form'><table><tr><td colspan='2'>Интересует цена на: "+$('.production_unit_content').find('.title').text()+"</td></tr><tr><td>Город:</td><td><input id='city' ></td></tr><tr><td>Наименование организации:</td><td><input id='organization' ></td></tr><tr><td>Как с Вами связаться:</td><td><textarea id='contacts'></textarea></td></tr><tr><td colspan='2'><input type='button' value='Отправить' id='send_price_ask'></td></tr></table></div>").appendTo($(this).parent());
	newprice_ask_form.fadeIn(600);
	
		$('#send_price_ask').click(function(){
			$.get('/assets/ajax/send_price_ask.php?product='+$('.production_unit_content').find('.title').text()+'&city='+$('#city').val()+'&organization='+$('#organization').val()+'&contacts='+$('#contacts').val(), function(){
				$('.price_ask_form').fadeOut(600, function(){$(this).remove();});
			});
		});
	
	} else
	{
		$('.newprice_ask_form').fadeOut(600, function(){$(this).remove();});
	};
	return false;
});


	
});















