$(document).ready(function(){

 // Wizard JS
 // initialize the wizard state
 
hideSteps();

load_state(1);

// loads new state based on button clicked
$('.move_tab').click(function(){

	var current_state = $('#wizard').attr('class');
	
	//we only want the number, converted to an int
	current_state = parseInt(current_state.replace(/(step_)/, ""));
	
	//figure out if 'next' or 'previous' was clicked and load appropriate state
	if (validateStep(current_state)) {
		if ($(this).attr('id') == 'next') {
			load_state(++current_state);
		}
		else if ($(this).attr('id') == 'previous') {
			load_state(--current_state);
		}
	}
	
	return false;
});

$('#s1').click(function (){
		
	var current_state = $('#wizard').attr('class');
	
	//we only want the number, converted to an int
	current_state = parseInt(current_state.replace(/(step_)/, ""));
	
	if(current_state == 4) {
		load_state(1);
	} else {
		return false;
	}
});

$('#s2').click(function (){
		
	var current_state = $('#wizard').attr('class');
	
	//we only want the number, converted to an int
	current_state = parseInt(current_state.replace(/(step_)/, ""));
	
	if(current_state == 4) {
		load_state(2);
	} else {
		return false;
	}
});

$('#s3').click(function (){
		
	var current_state = $('#wizard').attr('class');
	
	//we only want the number, converted to an int
	current_state = parseInt(current_state.replace(/(step_)/, ""));
	
	if(current_state == 4) {
		load_state(3);
	} 
	
	return false;

});

function hideSteps() {
	$("div[id^='step_']").hide();
}

// this is used to do any validation or processing required on each step
function validateStep(current_state) {
	
	var currentStepObj = $('#step_' + current_state);
	var currentStepName = currentStepObj.attr("id");
	
	var returnVal = true;
	
	if (currentStepName == 'step_2') {
		var salePrice = $('#saleprice').val();
		$('#kitchen_cost').html(' $'+salePrice);
	}
	if (currentStepName == 'step_3') {
		calculateResults();
	}

	return returnVal;
}

function calculateResults() {
	
	var floorStaff = parseInt($('#floorstaff').val());
	var officeStaff = parseInt($('#officestaff').val());
	var salePrice = parseFloat($('#saleprice').val());
	var quotingTime = parseFloat($('#quotingtime').val());
	var cutTime = parseFloat($('#cuttime').val());
	var drillTime = parseFloat($('#drilltime').val());
	var assemblyTime = parseFloat($('#assemblytime').val());
	
	var floorTime = floorStaff * 1816;
	
	var overHeads = ((floorStaff * 30000) + (officeStaff * 50000)) * 4;
	
	var factoryHourlyCost = 0;
	
	if (floorTime > 0) {
		factoryHourlyCost = overHeads / floorTime; 	
	}
	
	// current costs
	var quoteCost = quotingTime * factoryHourlyCost;
	var cutCost = cutTime * factoryHourlyCost;
	var drillCost = drillTime * factoryHourlyCost;
	var assemblyCost = assemblyTime * factoryHourlyCost;
	var materials = salePrice * .325;
	
	var totalTime = quotingTime + cutTime + drillTime + assemblyTime;
	var total = quoteCost + cutCost + drillCost + assemblyCost + materials;
	
	// hpp costs
	var hppQuoteCost = .75 * factoryHourlyCost;
	var hppAssemblyCost = assemblyCost * .9;
	var hppMaterials = salePrice * .45;
	
	var hppTotalTime = .75 + (assemblyTime * .9);
	var hppTotal = hppQuoteCost + hppAssemblyCost + hppMaterials;
	
	if(totalTime == 0) {
		totalTime = 1;
	}
	
	var costSaving = parseInt(((total - hppTotal) / total) * 100);
	var timeSaving = parseInt(((totalTime - hppTotalTime) / totalTime) * 100);
	
	if(costSaving > 0) {
		$('#profit').html('<strong>'+costSaving+'%</strong> increase in <span class="underline">Profit</span>');		
	} else {
		$('#profit').html("We couldnt save you this time, however see labour hours below.");
	}

	$('#labour').html('<strong>'+timeSaving+'%</strong> saving in <span class="underline">labour hours</span>');
	
}

function load_state(current_state){
	
	var elementTotal = 0;
	$('#wizNav').children().each(function (i) {
		elementTotal++;
	});
 
	//load the content for this state into the wizard content div and fade in
	hideSteps();
	$('#step_' + current_state).show();
	
	//set the wizard class to current state for next iteration
	$('#wizard').attr('class','step_'+ current_state);
 
	var iterator = 1;

	// loop through the list items and set classes for css coloring
	
 	$('#wizNav li').each(function(){
		
		var step = $(this)
		
		if (iterator == current_state){ 
			step.attr('class','current'); 
		} else if (current_state - iterator == 1){ 
			step.attr('class','lastDone'); 
		} else if (current_state - iterator > 1){ 
			step.attr('class','done'); 
		} else { 
			step.attr('class',''); 
		}
			
		// special case for step 5 because it doesn't have bacground image
		if (iterator == elementTotal){ 
			step.addClass('wizNavNoBg');
		}
		iterator++;
 	});
	
	//depending on the state, enable the correct buttons
	switch(current_state){
		case 1:
	  		$('#next').show();
			$('#previous').hide();
			$('#goback').show();
			$('#submit').hide();
			break;
		case elementTotal:
			$('#previous').hide();
			$('#next').hide();
			break;
		default:
			$('#previous').show();
			$('#next').show();
			$('#goback').hide();
			$('#submit').hide();
	}
}
});
