﻿// SCOTT.CSC.Tools.SavingsCalculator.Application
/// <reference path="../jquery-1.2.6-vsdoc.js"/>

Type.registerNamespace('SCOTT.CSC.Tools.CalorieCounter');

SCOTT.CSC.Tools.CalorieCounter.Application = function(panelId) 
{   
    var __app = this;
    this.ClientID = panelId;
    this.Selector = '#' + panelId;
    this.Calories = 0;
    
    // inheritance (of sorts)
    SCOTT.CSC.Extend(SCOTT.CSC.Tools.CalorieCounter.Application, SCOTT.CSC.Core);
    
    this.Initialize();
    this.Start();
};

SCOTT.CSC.Tools.CalorieCounter.Application.prototype =
{
    __type : 'SCOTT.CSC.Tools.CalorieCounter.Application',
    Start : function()
    {
        var __app = this;
        var context = $(this.Selector);
        
        // Reset the result to a new state
        $('#CalorieCounter-Calories').text('0');
         
        // Preset the default values for all fields
        $('#CalorieCounter-Duration').val("0");
        $('#CalorieCounter-Weight').val("0");
        $('#CalorieCounter-Activity').val("0.0");
        
        // Handle the blur event to set the value for fields to defaults if the user clears them
        $('.FormItem input[type=text]').blur(function() {
            if (this.value.length == 0) {
                this.value = "0";
            }
        });
    },
    AttachEvents : function()
    {
        var __app = this;
        var context = $(this.Selector);
        
        $('#CalorieCounter-Calculate').click(function()
        {
            __app.Calculate();
            __app.DisplayResults();
        });
        
        // Restrict input to integer values
        $('#CalorieCounter-Duration, #CalorieCounter-Weight').restrict("integer", { tooltip: "Please enter an integer value." });
    },
    Calculate : function()
    {
        var __app = this;
        var context = $(this.Selector);
        
        var min = parseFloat($('#CalorieCounter-Duration').val());
        var lbs = parseFloat($('#CalorieCounter-Weight').val());
        var act = parseFloat($('#CalorieCounter-Activity').val());
        
        __app.Calories = Math.round(Math.round((min / 60) * (lbs / 2.2)) * act); 
    },
    DisplayResults : function()
    {
        var __app = this;
        var context = $(this.Selector);
       
        $('#CalorieCounter-Calories').fadeOut(1, function() {
            $(this).text(__app.Calories).fadeIn(1000);
        });
    }
};
