///
/// $Id: smile-counter.js,v 1.2 2011-11-10 13:09:33-07 chadwick Exp chadwick $
///
/// Copyright (c) 2011. All rights reserved.
///
/// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
/// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
/// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
/// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
/// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
/// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
/// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
///


var SmileCounter = {

    counters : [],
    timerId : 0,
    start_date : "11/01/2011",
    start_count : 9000127,
    frequency   : 6.8, /* seconds per smile */
    scrollTime  : 600, /* milliseconds to scroll a digit */
    defaultOptions : {
        digitsNumber:   3,
	direction:      Counter.ScrollDirection.Upwards,
	characterSet:   Counter.DefaultCharacterSets.numericUp,
	charsImageUrl:  "/images/counter_numeric.png",
	markerImageUrl: "/images/counter_marker.png"
    }

};

SmileCounter.C_HUNDREDS  = 0;
SmileCounter.C_THOUSANDS = 1;
SmileCounter.C_MILLIONS  = 2;

SmileCounter.calcSmiles = function() {

    var startDate   = new Date( this.start_date );
    var today       = new Date();
    var elapsedSecs = Math.round((today.getTime() - startDate.getTime()) / 1000);
    var newSmiles   = Math.round( elapsedSecs / this.frequency );

    return( this.start_count + newSmiles );

};

SmileCounter.updateSmiles = function() {

    var startDate   = new Date( this.start_date );
    var today       = new Date();
    var elapsedSecs = Math.round((today.getTime() - startDate.getTime()) / 1000);
    var newSmiles   = Math.round( elapsedSecs / this.frequency );
    var totalSmiles = this.start_count + newSmiles;

    var millions = parseInt( totalSmiles / 1000000 );
    var thousands = parseInt( ( totalSmiles - (millions * 1000000)) / 1000 );
    var hundreds = totalSmiles - (millions * 1000000) - (thousands * 1000);

    this.counters[SmileCounter.C_HUNDREDS].setValue( hundreds, SmileCounter.scrollTime );
    this.counters[SmileCounter.C_THOUSANDS].setValue( thousands, SmileCounter.scrollTime );
    this.counters[SmileCounter.C_MILLIONS].setValue( millions, SmileCounter.scrollTime );

};

SmileCounter.tick = function() {

    this.updateSmiles();

};


SmileCounter.init = function() {

    var options = null;

    options = SmileCounter.defaultOptions;
    this.counters[SmileCounter.C_HUNDREDS] = new Counter("smile_counter_h", options );

    options = SmileCounter.defaultOptions;
    this.counters[SmileCounter.C_THOUSANDS] = new Counter("smile_counter_t", options );

    options = SmileCounter.defaultOptions;
    options.digitsNumber = 2;
    this.counters[SmileCounter.C_MILLIONS] = new Counter("smile_counter_m", options );

    this.updateSmiles();
    this.timerId = window.setInterval( "SmileCounter.tick()", (this.frequency * 1000) );

};

SmileCounter.display = function() {

    var obj = document.getElementById( 'smile_counter' );
    if ( !obj ) {
        alert( "SmileCounter: Counter element not found in document." );
        return false;
     }

    var html = 
        "<div id='smile_counter_m'></div><img src='/images/counter_comma.png'/>" +
        "<div id='smile_counter_t'></div><img src='/images/counter_comma.png'/>" +
        "<div id='smile_counter_h'></div>";

    obj.innerHTML = html;
    this.init();
}

///
/// EOF $RCSfile: smile-counter.js,v $
///

