var langs = {
	current: 'ru',
	init: function(lang)
	{
		langs.current = lang;
	},
	
	key: function(param)
	{
		if (langs.current == 'en')
			return langs.en(param);
			
		switch(param)
		{
			case 'your_name':
				return 'Наверное вас как-то зовут';
			case 'invalid_email':
				return 'Неверный Email';
			case 'invalid_phone':
				return 'Телефон неверный';
			case 'empty_message':
				return "Что вы хотели написать?";
			case 'long_message':
				return "Слишком длинное сообщение";
			
		}
	},
	
	en: function(param)
	{
		switch(param)
		{
			case 'your_name':
				return 'Perhaps you once called';
			case 'invalid_email':
				return 'Invalid Email';
			case 'invalid_phone':
				return 'Invalid phone';
			case 'empty_message':
				return "What would you like to write?";
			case 'long_message':
				return "Message is too long";
				
		}
	}
};

var showed_last = false;

$(document).ready(function(){
	setTimeout(start, 300);

	$('.circle').click(function(){
		if ($(this).hasClass('active'))
			return;

		$(".projects .project-item .link .image").css({
			'border-top-radius': 0,
			'-webkit-border-top-radius':0,
			'-moz-border-top-radius':0,
			'border-right-radius': 0,
			'-webkit-border-right-radius':0,
			'-moz-border-right-radius':0,
			'border-bottom-radius': 0,
			'-webkit-border-bottom-radius':0,
			'-moz-border-bottom-radius':0,
			'border-left-radius': 0,
			'-webkit-border-left-radius':0,
			'-moz-border-left-radius':0,
			'border-radius': 0
		}).animate({
			'border-top-radius': 150,
			'-webkit-border-top-radius':150,
			'-moz-border-top-radius':150,
			'border-right-radius': 150,
			'-webkit-border-right-radius':150,
			'-moz-border-right-radius':150,
			'border-bottom-radius': 150,
			'-webkit-border-bottom-radius':150,
			'-moz-border-bottom-radius':150,
			'border-left-radius': 150,
			'-webkit-border-left-radius':150,
			'-moz-border-left-radius':150,
			'border-radius': 150}, 900
		);
		current_border_radius = 150;
		$('.square').removeClass("active");
		$(this).addClass("active");
	});

	$('.square').click(function(){
		if ($(this).hasClass('active'))
			return;

		if (is_anim_br == true)
			return;
		current_radius_timer = setInterval(change_border_radius, 1);


		$('.circle').removeClass("active");
		$(this).addClass("active");
	});
});

var current_border_radius = 0;
var current_radius_timer = 0;
var is_anim_br = false;
function change_border_radius()
{
	is_anim_br = true;
	if (current_border_radius <= 0)
	{
		is_anim_br = false;
		clearInterval(current_radius_timer);
	}
	current_border_radius -= 4.5;
	$(".projects .project-item .link .image").css({
			'border-top-radius': current_border_radius,
			'-webkit-border-top-radius':current_border_radius,
			'-moz-border-top-radius':current_border_radius,
			'border-right-radius': current_border_radius,
			'-webkit-border-right-radius':current_border_radius,
			'-moz-border-right-radius':current_border_radius,
			'border-bottom-radius': current_border_radius,
			'-webkit-border-bottom-radius':current_border_radius,
			'-moz-border-bottom-radius':current_border_radius,
			'border-left-radius': current_border_radius,
			'-webkit-border-left-radius':current_border_radius,
			'-moz-border-left-radius':current_border_radius,
			'border-radius': current_border_radius
		});
}

function start()
{
	load_logo();
}


function load_logo()
{
	$('.logo .logo-z').delay(300).animate({
		'opacity': 1
	}, 650);

	$('.logo .logo-g').delay(500).animate({
		'opacity': 1
	}, 1150);

	setTimeout(show_zga, 250);

	function show_zga()
	{
		$('.logo .logo-agency').animate({
			'opacity': 1
		}, 750);

		load_menu();
	}
}

var iter = 0;
var counter = 0;
function load_menu()
{
	var time = 500;
	$('.menu li').delay(500).each(function(index){
		iter++;
		setTimeout("show_menu_item('"+$(this).attr('id')+"')", time+=50);
	});

}

function show_menu_item(id)
{
	$('#'+id).animate({
		opacity: 1,
		width: 118
	}, 300);

	$('#'+id+' a').animate({
		opacity: 1
	}, 500);
	counter++;

	if (counter == iter)
		load_main();
}


function load_main()
{
	var hash = document.location.hash;

	if (hash == '')
		hash = "#projects";

	if (hash == "#projects")
	{
		var time = 500;
		$('.our-container').animate({
			opacity: 1
		}, 1000);
		$('.we').delay(500).animate({
			opacity: 1
		}, 1000);

		$('.main div').each(function(index){
			setTimeout("show_main_item('"+$(this).attr('id')+"')", time+=100);
		});
		$('.call').delay(1000).animate({opacity: 1}, 500);
	}

	$('.social li').each(function(index){
		$(this).animate({
			opacity: 1
		}, 800);
	});

	load_last_project();
}

var iter_main = 0;
var height = -48;
function show_main_item(id)
{
	$('.main #'+id).animate({
		opacity: 1,
		width: 400
	}, 600);
	iter_main++;
	if (iter_main == 3)
	{
		$('.main .current').delay(200).animate({
			opacity: 1,
		}, 600);
	}

}

function load_last_project()
{
	var i = 0;
	var time = 500;
	var time2 = 1000;

	$('footer').animate({opacity: 1}, 500);

	$('.projects .title').animate({opacity: 1}, 500, function(){
		$('.projects .project-item').each(function(index){			

			if (i % 2 == 0)
				setTimeout("show_project_item('"+$(this).attr('id')+"')", time+=200);
			else
				setTimeout("show_project_item('"+$(this).attr('id')+"')", time2+=50);

			i++;
		});
		
		setTimeout(function(){
			$(".projects .title .types").animate({opacity:1}, 200);
		}, i*100);
		
	});
	
}
var f = false;
function show_project_item(id)
{

	var hash = document.location.hash;


	$('.projects #'+id).animate({
		opacity: 1
	}, 600);

	if (f == false && (hash == '' || hash == '#projects'))
	{
		$('#item_projects').css({position:'relative', zIndex: 0}).animate({top: 30}, 800, function(){
			if ($('#item_about').hasClass('active') == false)
				$('#item_projects').addClass('active').animate({top: 0, zIndex: 1, position: 'none'}, 800);
			else
				$('#item_projects').animate({top: 0, zIndex: 1, position: 'none'}, 800);
		});
		f = true;
	}
}



function post_submit_form()
{
	var is_error = false;
	if (document.forms.post_form.name.value == "" || document.forms.post_form.name.value.length > 100)
	{
		$('.form #name').addClass('error');
		$('.error-tip', $('.form #name').parent()).remove();

		var tip = $('<div class="error-tip">'+langs.key('your_name')+'</div>').css({top: 77});
		$('.form #name').parent().append(tip);
		animate_tip(tip);
		is_error = true;
	}
	else
	{
		$('.form #name').removeClass('error');
		$('.error-tip', $('.form #name').parent()).animate({opacity: 0}, 600, function(){
			$(this).remove();
		});
	}

	 if (/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/.test($('.form #email').val()) == false)
	 {
		$('.form #email').addClass('error');
		$('.error-tip', $('.form #email').parent()).remove();
		var tipemail = $('<div class="error-tip">'+langs.key('invalid_email')+'</div>').css({top: 127});
		$('.form #email').parent().append(tipemail);
		animate_tip(tipemail);
		is_error = true;
	 }
	 else
	{
		$('.form #email').removeClass('error');
		$('.error-tip', $('.form #email').parent()).animate({opacity: 0}, 600, function(){
			$(this).remove();
		});
	}


	if ($('.form #phone').val() != "" && /(?:8|\+7)? ?\(?(\d{3})\)? ?(\d{3})[ -]?(\d{2})[ -]?(\d{2})/.test($('.form #phone').val()) == false)
	 {
		$('.form #phone').addClass('error');
		$('.error-tip', $('.form #phone').parent()).remove();
		var tipemail = $('<div class="error-tip">'+langs.key('invalid_phone')+'</div>').css({top: 175});
		$('.form #phone').parent().append(tipemail);
		animate_tip(tipemail);
		is_error = true;
	 }
	 else
	{
		$('.form #phone').removeClass('error');
		$('.error-tip', $('.form #phone').parent()).animate({opacity: 0}, 600, function(){
			$(this).remove();
		});
	}

	if (document.forms.post_form.message.value == "" || document.forms.post_form.message.value.length > 1000)
	{
		$('.form #message').addClass('error');
		$('.error-tip', $('.form #message').parent()).remove();

		var tipmessage = null;
		var message = langs.key('empty_message');
		if(document.forms.post_form.message.value.length > 1000)
			tipmessage = langs.key('long_message');

		var tipmessage = $('<div class="error-tip">'+message+'</div>').css({top: 243});
		$('.form #message').parent().append(tipmessage);

		animate_tip(tipmessage);
		is_error = true;
	}
	else
	{
		$('.form #message').removeClass('error');
		$('.error-tip', $('.form #message').parent()).animate({opacity: 0}, 600, function(){
			$(this).remove();
		});
	}

	if (is_error == true)
		return false;

	$.ajax({
		url: "/post_"+langs.current+".php",
		dataType: "json",
		type: "POST",
		data: {
			name: document.forms.post_form.name.value,
			email: document.forms.post_form.email.value,
			phone: document.forms.post_form.phone.value,
			message: document.forms.post_form.message.value			
		},
		success: function(data){
			if (data.status == 'ok')
			{
				$('.form .success').html(data.message);
				$('.form form').css({'display': 'none'});

				$('.form .success').css({'display': 'block'}).animate({opacity: 1}, 500);
			}
		}
	});

	return false;
}



function email_submit_form()
{
	var obj_email = $('#subscribe-email');
	if (/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/.test(obj_email.val()) == false)
	{
		show_subscribe_tip("<span style='color:red'>"+langs.key('invalid_email')+"</span>");
		return;
	}
	
	var post_email = obj_email.val();
	
	$.ajax({
		url: "/post_email_"+langs.current+".php",
		dataType: "json",
		type: "POST",
		data: {
			email: post_email
		},
		success: function(data){
			if (data.status == 'ok')
			{
				show_subscribe_tip(data.message);
			}
		}
	});

	return false;
}

function show_subscribe_tip(msg)
{
	$('.success-subscribe').html(msg);

	$('.subscribe .success-subscribe')
		.css({'display': 'block'})
		.animate({top: 29}, 500, hide_subscribe_tip);
}

function hide_subscribe_tip()
{
	setTimeout(function(){
		$('.subscribe .success-subscribe').animate({top: 2}, 500);
	}, 1000);
}


function animate_tip(obj)
{
	obj.animate({left: 310}, 100, function(){
		obj.animate({left: 312}, 100, function(){
			obj.animate({left: 308}, 100);
		});
	});
}


