﻿$(document).ready(function(){
	
		$(document).ready(function(){
    // initialize tooltip
    $('a.tool')
        .tooltip({
            offset: [20, 140],
            relative:true,
			events: {
                def: "click, ''",
                tooltip: "'','mouseout'"},
            onShow: function(){
               var tip = this.getTip();
               tip.show();
							 tip.closest("div.row").css({zIndex:4000});
							 tip.css({zIndex:4000});
            },
						onHide: function(){
							var tip = this.getTip();
							tip.closest("div.row").css({zIndex:500});
							tip.css({zIndex:500});
						}
        })
        .dynamic({
            top: { direction: 'up' } 
    });
        
    $('a.close').click(function() { 
        $(this).parents().find(".tool").tooltip().hide();        
    });
    
    $('a.tool').click(function() { 
        $(this).next().show();
    });
});
	
	// Needed for IE6 so the images display properly
	$(".series-content .item img").css("float","left");
	
	// Cycle slides on startpage
	$(".slides").cycle({
		timeout: 2000, // Time between slides
		speed: 1800, // Length of transition
		pause: true // Pause on hover
	});
			
	$("a.iframe").fancybox({
		'width'				: 660,
		'height'			: 560,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'   : 'no'

	});

	// All input fields assigned with the class 'default' will
	// get their default values hidden when clicked, and reset again
	// if the user leaves the field empty or hasnt changed anything
	$("input.default").each(function() {
		var defaultVal = $(this).val();	
		if (!($(this).val()))
			$(this).val(defaultVal);
		$(this).focus(function() {
			
			if($(this).val() == defaultVal)
				$(this).val("");
		});
		$(this).blur(function(){
			if ($(this).val() == "")
				$(this).val(defaultVal);
		});
	});
	
	if($("#performance-tool").length) {
		initiate_performance_tool();
	}
});


/**
 * initiate_performance_tool()
 * - function for the performance tool
 */
function initiate_performance_tool() {
	// Define variables
	if(typeof(init_num_chairs) === 'undefined') { init_num_chairs = 50; }
	if(typeof(init_num_hours) === 'undefined') { init_num_hours = 8; }
	if(typeof(init_cost_per_employee) === 'undefined') { init_cost_per_employee = 200; }
	if(typeof(init_percent_productivity) === 'undefined') { init_percent_productivity = 68; }
	if(typeof(init_cost_per_chair) === 'undefined') { init_cost_per_chair = 5500; }
	if(typeof(init_increase_productivity) === 'undefined') { init_increase_productivity = 3; }
	
	var num_chairs = init_num_chairs;
	var num_hours = init_num_hours;
	var cost_per_employee = init_cost_per_employee;
	var percent_productivity = init_percent_productivity;
	var cost_per_chair = init_cost_per_chair;
	var increase_productivity = init_increase_productivity;
	
	var multiplicator = 3;
	var break_even;
	var total_month_production;
	var total_new_month_production;
	var percent_new_productivity;
	var productivity_increase;
	var cost_total;
	var incomehour;
	
	// Start button click - Show calculation page
	$("#performance-tool-start a.start-button").click(function(event) {
		event.preventDefault ? event.preventDefault() : event.returnValue = false;
		$("#performance-tool-calc").show("slide", { direction: "right" }, 400);
	});
	
	// Show calc button click - Show calculation
	$("#performance-tool-calc a.show-calc-button").click(function(event) {
		event.preventDefault ? event.preventDefault() : event.returnValue = false;
		populate_calc_info();
		$("#performance-tool-calc-info").toggle("slide", { direction: "right" }, 400);
	});
	
	// Back button click - Hide current page
	$("#performance-tool a.back-button").click(function(event) {
		event.preventDefault ? event.preventDefault() : event.returnValue = false;
		$(this).closest(".performance-page").hide("slide", { direction: "right" }, 400);
	});
	
	// Reset button click - Reset calculation
	$("#performance-tool a.reset-button").click(function(event) {
		event.preventDefault ? event.preventDefault() : event.returnValue = false;
		$("input.num_chairs").val(init_num_chairs);
		$("#slider_num_hours").slider({value: init_num_hours});
		$("input.cost_per_employee").val(init_cost_per_employee);
		$("#slider_percent_productivity").slider({value: init_percent_productivity});
		$("input.cost_per_chair").val(init_cost_per_chair);
		$("#slider_increase_productivity").slider({value: init_increase_productivity});		
	});
	
	// Validation/change handle for textfields
	$("#performance-tool input.numeric").numeric({decimal: false, negative: false});
	$("#performance-tool input.numeric").keyup(function(event) {
		if($(this).val() == "") { $(this).val(0); }
		
		if($(this).hasClass("num_chairs")) {
			num_chairs = parseInt($(this).val());
		}
		else if($(this).hasClass("cost_per_employee")) {
			cost_per_employee = parseInt($(this).val());
		}
		else if($(this).hasClass("cost_per_chair")) {
			cost_per_chair = parseInt($(this).val());
		}
		perform_calculation();
  });
  $("#performance-tool input.numeric").keydown(function (event) {
      if ($(this).val() == 0) { $(this).val(""); }
  });

  $("form").bind("keypress", function (e) {
      if (e.keyCode == 13) {
          return false;
      }
  });
	
	// Initiate slider "Number of hours"
	$("#slider_num_hours").slider({
		max: 24,
		min: 0,
		step: 0.1,
		slide: function(event, ui) {
			num_hours = parseFloat(ui.value);
			$("span.num_hours").html(num_hours);
			perform_calculation();
		},
		change: function(event, ui) {
			num_hours = parseFloat(ui.value);
			$("span.num_hours").html(num_hours);
			perform_calculation();
		}
	});
	
	// Initiate slider "Productivity percent"
	$("#slider_percent_productivity").slider({
		max: 100,
		min: 0,
		step: 1,
		slide: function(event, ui) {
			percent_productivity = parseInt(ui.value);
			$("span.percent_productivity").html(percent_productivity);
			perform_calculation();
		},
		change: function(event, ui) {
			percent_productivity = parseInt(ui.value);
			$("span.percent_productivity").html(percent_productivity);
			perform_calculation();
		}
	});
	
	// Initiate slider "Increase productivity percent"
	$("#slider_increase_productivity").slider({
		max: 100,
		min: 0,
		step: 1,
		slide: function(event, ui) {
			increase_productivity = parseInt(ui.value);
			$("span.increase_productivity").html(increase_productivity);
			perform_calculation();
		},
		change: function(event, ui) {
			increase_productivity = parseInt(ui.value);
			$("span.increase_productivity").html(increase_productivity);
			perform_calculation();
		}
	});
	
	// Use reset button to set input-field default values.
	$("#performance-tool a.reset-button").trigger("click");
	
	
	/**
	 * perform_calculation()
 	 * - function that does the math.
 	 */
	function perform_calculation() {		
		var productivity_percent = percent_productivity * 0.01;
		var increased_percent	= increase_productivity * 0.01;
		var new_percent = productivity_percent * (increased_percent + 1);
			
		var stapleHeight = 200 * new_percent;
		$("#performance-tool-staple .inner").css("height", stapleHeight+"px");
		
		
		var costofchairs = num_chairs * cost_per_chair;
		incomehour = (cost_per_employee * multiplicator) - cost_per_employee;
			
		var incomeperday = ((num_chairs * num_hours) * incomehour) * productivity_percent;		
		var newincomeperday = ((num_chairs * num_hours) * incomehour) * new_percent;
			
		var increasedincomeday = newincomeperday - incomeperday;		
		var increaseincome_pct = ((newincomeperday / incomeperday) - 1) * 100;
												
		break_even = Math.round((costofchairs / increasedincomeday) * 100) / 100;
		if (break_even == Infinity) { break_even = "-"; }
		
		total_month_production = Math.round(incomeperday);
		total_new_month_production = Math.round(newincomeperday);
		percent_new_productivity = Math.round(new_percent * 100);
		
		$("p.break_even").html(break_even);
		$("p.percent_new_productivity").html(percent_new_productivity + "%");
		$("p.total_month_production .value").html(thousandsSep(total_month_production));
		$("p.total_new_month_production .value").html(thousandsSep(total_new_month_production));
		
		// Only update calc-info window if it's visible.
		if($("#performance-tool-calc-info").is(":visible")) {
			populate_calc_info();
		}
	}
	
	
	/**
	 * populate_calc_info()
 	 * - function that populates the calc-info window.
 	 */
	function populate_calc_info() {
		productivity_increase = total_new_month_production - total_month_production;
		cost_total = num_chairs * cost_per_chair;
		
		$("#performance-tool-calc-info .num_chairs").html(num_chairs);
		$("#performance-tool-calc-info .num_hours").html(num_hours);
		$("#performance-tool-calc-info .percent_productivity").html(percent_productivity);
		$("#performance-tool-calc-info .multiplicator").html(multiplicator);
		$("#performance-tool-calc-info .cost_per_employee").html(cost_per_employee);
		$("#performance-tool-calc-info .win_per_employee").html(thousandsSep(incomehour));
		$("#performance-tool-calc-info .total_month_production").html(thousandsSep(total_month_production));
		$("#performance-tool-calc-info .percent_productivity_increase").html(increase_productivity);
		$("#performance-tool-calc-info .percent_new_productivity").html(percent_new_productivity);
		$("#performance-tool-calc-info .total_new_month_production").html(thousandsSep(total_new_month_production));
		$("#performance-tool-calc-info .productivity_increase").html(thousandsSep(productivity_increase));
		$("#performance-tool-calc-info .cost_per_chair").html(cost_per_chair);
		$("#performance-tool-calc-info .cost_total").html(thousandsSep(cost_total));
		$("#performance-tool-calc-info .break_even").html(break_even);
	}
	
	
	/**
	 * thousandsSep()
	 * - function that seperates thousands.
	 *   Eg. 20000 becomes 20 000.
	 */
	function thousandsSep(number) {
		return Math.max(0, number).toFixed(0).replace(/(?=(?:\d{3})+$)(?!^)/g, ' ');
	}

}
