$(document).ready(function(){

	$('div.photogallery a').lightbox();

	$("div.partners div").hover(function() {
	$(this).children().next("p").addClass("active");
	}, function() {
	$(this).children().next("p").removeClass("active");
	});

	$("#links_menu div a").attr("unselectable" , "on");

	var stage = 0;
	var counter = 0;
	
	$("#scroller img:not(.scimg" + current +")").fadeTo(0, 0.5);
	
	$("#attention p:not(:first)").hide();
	$("#attention div.head:first").addClass("active");
	$("#attention div").mouseover(function(){
	
		if ($(this).prev().css("display") == "none" && stage == 0)
		{
			stage = 1;
			$("#attention div").removeClass("active");
			$(this).addClass("active");
			
			var temp = this;
			
			$("#attention p").slideUp(333, function(){
				
				counter++;
				
				if (counter == attention)
				{
					$(temp).prev().slideDown(333, function(){
						stage = 0;
						counter = 0;
					});
				}
			});
		}
	});

});

function scroll_up()
{
	var divOffset = $('#main_borders').offset().top;
    var pOffset = $('#main_borders #form_p').offset().top;
	
    var pScroll = pOffset - divOffset;
	
	$('html').animate({scrollTop: pScroll + 'px'}, 1000 );
	
	return false;
}

function send_form_a(id)
{
	scroll_up();
	
	var get_string = "";
	
	$("#form_call input").each(function(i){
		if ($(this).val()!="" && $(this).val()!="Отправить") get_string+= $(this).attr("name") + "=" + encodeURIComponent($(this).val()) + "&";
	});
	
	$("#form_call textarea").each(function(i){
		if ($(this).val()!="") get_string+= $(this).attr("name") + "=" + encodeURIComponent($(this).val()) + "&";
	});

	get_string = get_string.slice(0, get_string.length - 1);
	
	if (get_string != "")
	{
		$.ajax({
			type: "GET",
			url: "inc/send_form_a.php",
			data: get_string,
			success: function(data){
				if (data == "")
				{
					$("#form_p").html('<span class="red">Ошибка отправки заявки. Проверьте правильность заполнения полей.</span>');		
				}
				else
				{
					$("#form_p").html(data);
					$("#form_call").remove();
				}
			}
		});
	}
}

function toggle_another(object)
{
	if ($(object).val() == "Другое") $("#another").attr("class", "on");
	else $("#another").attr("class", "off");
}

function send_form(id)
{
	scroll_up();
	
	var get_string = "";
	
	$("#form_call input:not(:checkbox)").each(function(i){
		if ($(this).val()!="" && $(this).val()!="Отправить") get_string+= $(this).attr("name") + "=" + $(this).val().replace(/\&/g, "%26") + "&";
	});
	
	$("#form_call textarea").each(function(i){
		if ($(this).val()!="") get_string+= $(this).attr("name") + "=" + $(this).val().replace(/\&/g, "%26") + "&";
	});
	
	$("#form_call select").each(function(i){
		if ($(this).val()!="") get_string+= $(this).attr("name") + "=" + $(this).val().replace(/\&/g, "%26") + "&";
	});
	
	$("#form_call input:checkbox").each(function(i){
		if ($(this).attr("checked")==true) get_string+= $(this).attr("name") + "=" + $(this).val().replace(/\&/g, "%26") + "&";
	});

	get_string = get_string.slice(0, get_string.length - 1);

	if (get_string != "")
	{
		$.ajax({
			type: "GET",
			url: "inc/send_form.php",
			data: get_string,
			success: function(data){
				if (data == "")
				{
					$("#form_p").html('<span class="red">Ошибка отправки заявки. Проверьте правильность заполнения полей.</span>');		
				}
				else
				{
					$("#form_p").html(data);
					$("#form_call").remove();
				}
			}
		});
	}
}

function recolor(mode, object)
{
	//alert(object);
	
	$("#menu .select").removeClass("select");
	
	if (mode == -1)
	{
		$(object).addClass("select");
		$(object).parent().addClass("select");
		$(object).parent().parent().prev().addClass("select");
		
		if($(object).parent().parent().parent().parent().prev().attr("id") == "lm2")
		{
			$(object).addClass("select");
			$(object).parent().addClass("select");
			$(object).parent().parent().prev().addClass("select");
			$(object).parent().parent().parent().addClass("select");
			$(object).parent().parent().parent().parent().prev().addClass("select");
		}
	}
	else
	{
		$("#lm" + mode).addClass("select");
	}
}

function ajax_it(id)
{
	$.ajax({
		type: "GET",
		url: "inc/ajax.php",
		data: "current=" + current + "&ajax=1" + "&id=" + id,
		success: function(data){
			data_array = data.split("#!#");
			$("#status span").html(data_array[0]);
			$("#title").html(data_array[1]);
			$("#body").html(data_array[2]);
		}
	});
}

function ajax_it_v2(id, entry)
{
	
	var add = "";
	
	if (entry) add = "&entry=" + entry;
	
	$.ajax({
		type: "GET",
		url: "inc/ajax.php",
		data: "current=" + current + "&ajax=1" + "&id=" + id + add,
		success: function(data){
			data_array = data.split("#!#");
			$("#status span").html(data_array[0]);
			$("#title").html(data_array[1]);
			$("#body").html(data_array[2]);
			
			$("#box").animate({marginLeft: 0}, 800);
			
			$("#clear").height((65 + $("#title").height() - 31));
			
			//Показываем заголовок
			$("#clear").animate({width: 1}, 800, function()
			{
				playing = 0;
				
				$('div.photogallery a').lightbox();
				
				$("div.partners div").hover(function() {
				$(this).children().next("p").addClass("active");
				}, function() {
				$(this).children().next("p").removeClass("active");
				});
			});
		}
	});
}

function move_it(scene, id, object, entry)
{
	if (playing == 0)
	{
		//alert(object);
		
		playing = 1;
		
		if (id > 0 || id == "news" || id == "analytics") recolor(-1, object);
		else recolor(scene, object);
		
		if (current != scene)
		{
			current = scene;
		
			var top_dest = 228*scene + 42;
			var bottom_dest = 228*scene;
			
			//Движение верхней части блока верхнего меню
			$("#top_menu_bg").animate({left: top_dest}, 4000);
			
			//Движение нижней части блока верхнего меню
			$("#bottom_menu_bg").animate({left: -228}, 2000, "", function()
			{
				$("#bottom_menu_bg").css("left", "684px");
				$("#bottom_menu_bg").animate({left: bottom_dest}, 2000);
			});
			
			$("#bottom_menu_bg_2").animate({left: -705}, 4000, "", function()
			{
				$("#bottom_menu_bg_2").css("left", "684px");
			});
			
			//Выцветание пункта верхнего меню
			$("#top_men" + scene).fadeOut(2800, function()
			{
				$(this).addClass("select");
				$(this).fadeIn(2000);
			});
			
			//Выцветание активного пункта верхнего меню
			$("#links_menu div.select").fadeOut(1200, function()
			{
				$(this).removeClass("select");
				$(this).fadeIn(2000);
			});
				
			//Затеняем картинки
			$("#scroller img").fadeTo(0, 0.5);
			
			//Двигаем картинки
			$("#scroller").animate({marginLeft: -2052}, 4000, "", function()
			{
				$("#scroller").css("marginLeft", "0");
				$("#scroller img.scimg" + scene).fadeTo(0, 1);
			});
			
			//Стираем заголовок
			$("#clear").animate({width: 663}, 800);
					
			//Сборка квадрата
			$("#box").animate({marginLeft: 705}, 800, "", function()
			{
				ajax_it(id, entry);
			
				//$("#square").css("background-position", "-21px");
				$("#square div").css("background", "none");
				
				$("#box").css("visibility", "hidden");
		
				$("#square").animate({left: 684, bottom: -38}, 800, "", function()
				{
					$("#square p").fadeOut(400, function()
					{
						if (current == 0) $("#square p").html('<a onclick="move_it(0, 2, &quot;#contacts&quot;); return false;" href="index.php?current=0&amp;id=2">Как нас<br /> найти?</a>');
						if (current == 1) $("#square p").html('<a onclick="move_it(0, 2, &quot;#contacts&quot;); return false;" href="index.php?current=0&amp;id=2">Остались<br /> вопросы?</a>');
						if (current == 2) $("#square p").html('<a onclick="move_it(2, &quot;i&quot;, &quot;#contacts&quot;); return false;" href="index.php?current=2&amp;id=i">Подать<br /> заявку!</a>');
						
						$("#square p").fadeIn(400, function()
						{
							$("#square").animate({left: 705, bottom: -59}, 800, "", function()
							{
								//$("#square").css("background-position", "left top");
								$("#square div").css("background", "#fff");
								$("#box").css("visibility", "visible");
								$("#box").animate({marginLeft: 0}, 800);
																
								$("#clear").height((65 + $("#title").height() - 31));
								
								//Показываем заголовок
								$("#clear").animate({width: 1}, 800, "", function()
								{
									//Конец всей анимации
									playing = 0;
									
									$('div.photogallery a').lightbox();
									
									$("div.partners div").hover(function() {
									$(this).children().next("p").addClass("active");
									}, function() {
									$(this).children().next("p").removeClass("active");
									});
								});
							});
						});
					});
				});
			});
		}
		else
		{
			//Стираем заголовок
			$("#clear").animate({width: 663}, 800);
			
			//Сборка квадрата
			$("#box").animate({marginLeft: 705}, 800, "", function()
			{
				ajax_it_v2(id, entry);
			});
		}
	}
}