var language_changeable = true;

function start_change_language() {
	language_changeable = false;
	
	$('.step_choose, .step_thermos, .step_bag').fadeOut(400);
	
	$('#step_loading').delay(400).fadeIn(400);
	
	
	if($('.current_step').attr('id') != undefined){
		var temp = 'end_' + $('.current_step').attr('id').substring(5);
		
		if(eval('window.' + temp)){
			eval(temp + '()');
		}
	}
	
	$('.step_no:visible').addClass('step_no_visible').fadeOut(400);
	
	
	setTimeout('change_language()', 800);
}

function change_language() {
	$.ajax(
		{
			'type': 'post',
			'url': 'ajax_get_texts.php',
			'data': {'lang': $('.lang_current').attr('id').substring(5)},
			'dataType': 'json',
			'success': function(data) {
				for(var i in data){
					$('.' + i).html(data[i]);
				}
				
				end_change_language();
			},
			'error': function(request, error) {
				alert(error);
			}
		}
	);
}

function end_change_language() {
	$('#step_loading').fadeOut(400);
	
	var temp_bag = false;
	var temp_thermos = false;
	
	for(var i in products){
		if(products[i] == 'bag'){
			temp_bag = true;
		}else if(products[i] == 'thermos'){
			temp_thermos = true;
		}
	}
	
	if(temp_bag && temp_thermos){
		if($('.current_step').length == 0){
			$('#step_choose_product').addClass('current_step');
		}
		
		$('.step_choose').delay(400).fadeIn(400);
	}else if(temp_bag){
		if($('.current_step').length == 0){
			$('#step_bag_choose_bag').addClass('current_step');
		}
	}else if(temp_thermos){
		if($('.current_step').length == 0){
			$('#step_thermos_choose_thermos').addClass('current_step');
		}
	}
	
	$('.step_choose, .step_bag, .step_thermos').height(40);
	
	if(product == 'bag' && temp_bag){
		$('.step_bag').delay(400).fadeIn(400);
	}else if(product == 'thermos' && temp_thermos){
		$('.step_thermos').delay(400).fadeIn(400);
	}
	
	$('.step_no_visible').removeClass('step_no_visible').delay(400).fadeIn(400);
	
	
	var temp = 'start_before_' + $('.current_step').attr('id').substring(5);
	
	if(eval('window.' + temp)){
		setTimeout(temp + '()', 400);
	}
	
	
	var temp = 'start_after_' + $('.current_step').attr('id').substring(5);
	
	if(eval('window.' + temp)){
		setTimeout(temp + '()', 800);
	}
	
	
	setTimeout('open_next("' + $('.current_step').attr('id') + '")', 800);
	setTimeout('language_changeable = true', 800);
	setTimeout('recount_bags()', 800);
	setTimeout('recount_thermoses()', 800);
}

$('document').ready(
	function() {
		start_change_language();
		
		$('.lang_img:not(.lang_current) IMG').animate({'opacity': 0.3}, 400);
		
		$('.lang_img').click(
			function() {
				if(language_changeable && $(this).attr('id') != $('.lang_current').attr('id')){
					$('.lang_img').removeClass('lang_current');
					$(this).addClass('lang_current');
					
					$('.lang_img:not(.lang_current) IMG').animate({'opacity': 0.3}, 400);
					$('IMG', $(this)).animate({'opacity': 1.0}, 400);
					
					start_change_language();
				}
			}
		);
	}
);

