/**
 * http://vunite.com
 * by @karmadude and @jordantbro
 */

/**
 * This file defines the Stopwatch class.
 * Note that it knows nothing about instances and how those instances are used.
 * Stopwatch Source from: http://webpages.cs.luc.edu/~laufer/424/StopwatchJQuery/
 * Stopwatch Developer: Konstantin Läufer
 */
var Stopwatch;
if (!Stopwatch) 
    Stopwatch = {};

/**
 * Constructs a new Stopwatch instance.
 * @param {Object} displayTime the strategy for displaying the time
 */
function Stopwatch(displayTime){
    this.runtime = 0; // milliseconds
    this.timer = null; // nonnull iff runnig
    this.displayTime = displayTime; // not showing runtime anywhere
}

/**
 * The increment in milliseconds.
 * (This is a class variable shared by all Stopwatch instances.)
 */
Stopwatch.INCREMENT = 100

/**
 * Displays the time using the appropriate display strategy.
 */
Stopwatch.prototype.doDisplay = function(){
    if (!this.laptime) 
        this.displayTime(this.runtime);
    else 
        this.displayTime(this.laptime);
}

/**
 * Handles an incoming start/stop event.
 */
Stopwatch.prototype.startStop = function(){
    if (!this.timer) {
        var instance = this;
        this.timer = window.setInterval(function(){
            instance.runtime += Stopwatch.INCREMENT;
            instance.doDisplay();
        }, Stopwatch.INCREMENT);
    }
    else {
        window.clearInterval(this.timer);
        this.timer = null;
        this.doDisplay();
    }
}

/**
 * Handles an incoming reset/lap event.
 */
Stopwatch.prototype.resetLap = function(){    
    if (!this.laptime) {
        if (this.timer) {
            this.laptime = this.runtime;
        }
        else {
            this.runtime = 0;
        }
    }
    else {
        delete this.laptime;
    }
    this.doDisplay();
}

// A Stopwatch instance that displays its time nicely formatted.
var s = new Stopwatch(function(runtime) {
  // format time as hh:mm:ss
  var hours = Math.floor(runtime / 3600000);
  var minutes = Math.floor(runtime % 3600000 / 60000);
  var seconds = Math.floor((runtime % 3600000) % 60000 / 1000 );
  var decimals = Math.floor(((runtime % 3600000) % 60000) % 1000 / 100);
  var displayText = (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds + "." + decimals;
  $("#time .value").html(displayText);
  
  var hourlywage = parseFloat($("#hourlywage").val());
  var employees = parseFloat($("#employees").val());
  var unitcost = hourlywage/3600000.0;
  var cost = unitcost * employees * runtime;
  cost = cost.toFixed(2);
  
  if( isNaN(cost) ) cost = 0;
  
  $("#money .value").html("$" + cost);
  $("#donatemessage span").html("$" + cost);
});


// Code to create instances and wire everything together.
$(document).ready(function(){  
  $("#hourlywage").focus().select();
  $("#startstop").bind("click", function(){ 
    s.startStop(); 
    var l = $("#startstop").val();
    if( l == 'Start' )
    {
        $("#startstop").removeClass('green');
        $("#startstop").addClass('stop');
        $("#startstop").val('Stop');
    }
    else
    {
        $("#startstop").removeClass('stop');
        $("#startstop").addClass('green');
        $("#startstop").val('Start');
    }
  });
  
  $("#reset").bind("click", function(){ 
    if( $("#startstop").val() == 'Stop' )
    {
        s.startStop();        
    }
    $("#startstop").removeClass('stop');
    $("#startstop").addClass('green');
    $("#startstop").val('Start');
    s.resetLap(); 
  });
  s.doDisplay();
});


