﻿/// <reference path="../../Scripts/jquery-1.2.6-vsdoc.js" />

$(document).ready(function() {
    //NOTE: sides should by default be positioned all the way at the top of each side.
    //Template will have some minimum height set.
    //#1)Detect the form height
    var height = $('form').innerHeight();
    //#2 Set left and right side height to form height
    $('.Wing').height(height);
    //#3 Shift left and right sides into position
    $('.Wing').css("margin-top", "-" + height + "px");
    //#4 Show the Wings
    $('.Wing').show();

    //Set RSS Link in RightNav
    var rssLinkVal = $('.rssTemplateLink').attr('href');
    $('#mapAreaRSS').attr('href', rssLinkVal);
});



//Object definition for BudgetCountDown
//NOTE: The client's current time is used for simplicity.
function BudgetCountDown(tickerId) {
    var self = this; //serves as a handle to self members from within the object. "this" alone is used to define members only not reference them.
    this.budgetStartTime = 1246424400000; // July 1, 2009 12:00 am These numbers are calculated like: var a = new Date(2010,5,30) a.getTime(); milliseconds since 1971
    this.budgetEndTime = 1277960399000; // June 30, 2009 11:59:59am
    this.budgetIncrement = 0.19756786521968100; // $197.56786521968100 / s
    this.ticker = document.getElementById(tickerId);
    this.lastYearsBudget = 6133154558; //used until incase msSinceStart is negative, as when making changes just before the next year.

    this.shouldCountDown = function() {
        var currentTimeMS = (new Date()).getTime();
        var msSRemaining = (self.budgetEndTime - currentTimeMS);
        return msSRemaining > 0;
    }

    this.updateCountDown = function() {
        var shouldCountDown = self.shouldCountDown();

        if (!self.ticker) { return; }
        var currentTime = ((new Date()).getTime());
        var msSinceStart = currentTime - self.budgetStartTime;

        //If budgetEndTime has already been reached just show the final budget amount
        if (shouldCountDown == false) {
            msSinceStart = self.budgetEndTime - self.budgetStartTime;
        }

        var amount = self.lastYearsBudget;
        if (msSinceStart > 0) {
            amount = msSinceStart * self.budgetIncrement;
        }

        //Calculate and format amount
        currentDollar = self.CommaFormatted(self.CurrencyFormatted(amount));

        //Set to element
        self.ticker.innerHTML = '$' + currentDollar + '*';

        if (shouldCountDown) {
            setTimeout(self.updateCountDown, 50);
        }
    }

    //Currency formatting scripts courtesy of:
    //William Bontrager Programmer/Publisher
    //mailto:possibilities@willmaster.com

    this.CurrencyFormatted = function(amount) {
        var i = parseFloat(amount);
        if (isNaN(i)) { i = 0.00; }
        var minus = '';
        if (i < 0) { minus = '-'; }
        i = Math.abs(i);
        i = parseInt((i + .005) * 100);
        i = i / 100;
        s = new String(i);
        if (s.indexOf('.') < 0) { s += '.00'; }
        if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
        s = minus + s;
        return s;
    };
    // end of function CurrencyFormatted()


    this.CommaFormatted = function(amount) {
        var delimiter = ","; // replace comma if desired
        var a = amount.toString().split('.', 2)
        var d = a[1];
        var i = parseInt(a[0]);
        if (isNaN(i)) { return ''; }
        var minus = '';
        if (i < 0) { minus = '-'; }
        i = Math.abs(i);
        var n = new String(i);
        var a = [];
        while (n.length > 3) {
            var nn = n.substr(n.length - 3);
            a.unshift(nn);
            n = n.substr(0, n.length - 3);
        }
        if (n.length > 0) { a.unshift(n); }
        n = a.join(delimiter);
        if (d.length < 1) { amount = n; }
        else { amount = n + '.' + d; }
        amount = minus + amount;
        return amount;
    };
}