$('document').ready(
	function() {
		$('.button_next').css('opacity', '0.3');
		$('.step_header_back').hide();
	}
);

function show_next(skip_till) {
	var current = $('.current_step');
	
	var temp = 'end_' + current.attr('id').substring(5);
	
	if(eval('window.' + temp)){
		eval(temp + '()');
	}
	
	
	var next = current.next('.step');
	
	var again = false;
	
	if(skip_till != undefined && next.attr('id') != skip_till){
		again = true;
	}
	
	while(next.css('display') == 'none' || next.hasClass('step_no') || again){
		next = next.next('.step');
		
		
		var again = false;
		
		if(skip_till != undefined && next.attr('id') != skip_till){
			again = true;
		}
	}
	
	var temp = 'start_before_' + next.attr('id').substring(5);
	
	if(eval('window.' + temp)){
		eval(temp + '()');
	}
	
	var temp = 'start_after_' + next.attr('id').substring(5);
	
	if(eval('window.' + temp)){
		setTimeout(temp + '()', 400);
	}
	
	
	setTimeout('animate_steps("' + current.attr('id') + '", "' + next.attr('id') + '")', 50);
}

function animate_steps(current, next) {
	close_current(current);
	open_next(next);
}

function close_current(current) {
	var current = $('#' + current);
	
	current.animate({'height': 40}, 400);
	
	$('.step_header_back', current).fadeIn(400);
	
	current.removeClass('current_step');
}

function open_next(next) {
	var next = $('#' + next);
	
	var new_height = 0;
	
	if($('#' + next.attr('id') + '_loading').css('display') != 'block'){
		new_height += parseInt($('.step_header', next).height());
		new_height += parseInt($('.step_header', next).css('margin-top'));
		new_height += parseInt($('.step_header', next).css('margin-bottom'));
		
		new_height += parseInt($('.step_content', next).height());
		new_height += parseInt($('.step_content', next).css('margin-top'));
		new_height += parseInt($('.step_content', next).css('margin-bottom'));
	}else{
		new_height = 40;
	}
	
	next.animate({'height': new_height}, 400);
	
	next.addClass('current_step');
}

function reset_steps(step) {
	$('.current_step').removeClass('current_step');
	
	$('#' + step).addClass('current_step');
	
	var first = step;
	var temp_step = step;
	
	while(temp_step != null){
		if(temp_step != first){
			$('#' + temp_step).animate({'height': 40}, 400);
		}
		
		$('.step_header_back', $('#' + temp_step)).fadeOut(400);
		
		var temp = 'reset_' + temp_step.substring(5);
		
		if(eval('window.' + temp)){
			eval(temp + '()');
		}
		
		temp = $('#' + temp_step).next('.step');
		
		while(temp.css('display') == 'none' || temp.hasClass('step_no')){
			temp = temp.next('.step');
		}
		
		temp_step = temp.attr('id');
	}
	
	
	var temp = 'start_before_' + $('#' + step).attr('id').substring(5);
	
	if(eval('window.' + temp)){
		eval(temp + '()');
	}
	
	var temp = 'start_after_' + $('#' + step).attr('id').substring(5);
	
	if(eval('window.' + temp)){
		setTimeout(temp + '()', 400);
	}
	
	
	var new_height = 0;
	
	new_height += parseInt($('.step_header', $('#' + step)).height());
	new_height += parseInt($('.step_header', $('#' + step)).css('margin-top'));
	new_height += parseInt($('.step_header', $('#' + step)).css('margin-bottom'));
	
	new_height += parseInt($('.step_content', $('#' + step)).height());
	new_height += parseInt($('.step_content', $('#' + step)).css('margin-top'));
	new_height += parseInt($('.step_content', $('#' + step)).css('margin-bottom'));
	
	$('#' + step).animate({'height': new_height}, 400);
}

function recount_bags() {
	var count = 1;
	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){
		$('.step_header_image IMG', $('#step_choose_product')).attr('src', 'img/steps/step' + count + '.png');
		
		count++;
	}
	
	if((temp_thermos && temp_bag && product == 'bag') || (!temp_thermos && product == 'bag')){
		$('.step_bag').each(
			function() {
				$('.step_header_image IMG', $(this)).attr('src', 'img/steps/step' + count + '.png');
				
				count++;
			}
		);
	}
}

function recount_thermoses() {
	var count = 1;
	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){
		$('.step_header_image IMG', $('#step_choose_product')).attr('src', 'img/steps/step' + count + '.png');
		
		count++;
	}
	
	if((temp_thermos && temp_bag && product == 'thermos') || (!temp_bag && product == 'thermos')){
		$('.step_thermos').each(
			function() {
				$('.step_header_image IMG', $(this)).attr('src', 'img/steps/step' + count + '.png');
				
				count++;
			}
		);
	}
}

