//
// DateTimeStamp
//
FVWebComponent.DateTimeStamp = function(id, config) {
    this._timer = null;
    this._updateDateTimeFunc = null;
    this._format =  "h:nn TT";;
    this._updatePeriod = 1000;
    this._now = new Date();
    this._updateDateTimeHandler = null;
    this.init(id, config);    
}

FVWebComponent.DateTimeStamp.prototype = new FVWebComponent.ComponentBase;

FVWebComponent.DateTimeStamp.prototype.init = function (id, config) {
    FVWebComponent.ComponentBase.prototype.init.call(this, id);
    
    if (config["format"] != null) {
        this._format = config["format"];
    }
    
    if (config["updateDuration"] != null) {
        this._updateDuration = config["updateDuration"] * 1000;
    }
    
    var me = this;
    
    this._updateDateTimeHandler = function () {
        me.updateDateTime();
    }
    
    this.updateDateTime();
}

FVWebComponent.DateTimeStamp.prototype.stop = function () {
    if (this._timer != null) {
        clearInterval(this._timer);
        this._timer = null;
    }
}

FVWebComponent.DateTimeStamp.prototype.start = function () {
    if (this._timer == null) {        
        this._timer = setInterval(this._updateDateTimeHandler, this._updatePeriod);
    }
}

FVWebComponent.DateTimeStamp.prototype.MonthNames = new Array(
                                                                'January',
                                                                'February',
                                                                'March',
                                                                'April',
                                                                'May',
                                                                'June',
                                                                'July',
                                                                'August',
                                                                'September',
                                                                'October',
                                                                'November',
                                                                'December'
                                                            );

FVWebComponent.DateTimeStamp.prototype.DayNames = new Array(
                                                                'Sunday',
                                                                'Monday',
                                                                'Tuesday',
                                                                'Wednesday',
                                                                'Thursday',
                                                                'Friday',
                                                                'Saturday'
                                                            );                                                            
                                                            
FVWebComponent.DateTimeStamp.prototype.updateDateTime = function () {
    this._now = new Date();
    
 
    
    var indexCurr = 0;
    var formatStr = this._format;
    var formatLength = formatStr.length;
    
    var charCurr;
    
    var tokenChar = '';
    var tokenCurr = '';
    var tokenPattern = /[ymdhnsT]/;
    
    var output = '';
    
    for (indexCurr = 0; indexCurr < formatLength; indexCurr++) {
        charCurr = formatStr.substr(indexCurr, 1);
        if (charCurr.search(tokenPattern) != -1) {
            if (tokenChar == '') {
                // Begin token
                tokenChar = charCurr;
                tokenCurr = charCurr;
            } else if (charCurr == tokenChar) {
                // Token continues
                tokenCurr += charCurr;
            } else {
                // New token encountered
                output += this.formatDateTimeInfo(tokenCurr);
                // Begin new token
                tokenChar = charCurr;
                tokenCurr = charCurr;
            }
        } else {
            if (tokenCurr != '') {
                // End of token
                output += this.formatDateTimeInfo(tokenCurr);
                // Clear token
                tokenChar = '';
                tokenCurr = '';
            }
            output += charCurr;
        }
    }
    
    if (tokenCurr != '') {
        // Token left over.
        output += this.formatDateTimeInfo(tokenCurr);
    }
        
    this.container.innerHTML = output;
}

FVWebComponent.DateTimeStamp.prototype.formatDateTimeInfo = function (token) {
    var result;
    var padZero = false;
    
    switch (token) {
        case 'yyyy':
            result = this._now.getFullYear();
            break;
        case 'yy':
            result = this._now.getFullYear() % 100;
            padZero = true;
            break;
        case 'mmmm':
            result = this.MonthNames[this._now.getMonth()];
            break;
        case 'mmm':
            result = this.MonthNames[this._now.getMonth()].substr(0, 3);
            break;
        case 'mm':  
            result = (this._now.getMonth() + 1);
            padZero = true;
            break;
        case 'm':
            result = (this._now.getMonth() + 1);
            break;
        case 'dddd':
            result = this.DayNames[this._now.getDay()];
            break;
        case 'ddd':
            result = this.DayNames[this._now.getDay()].substr(0, 3);
            break;
        case 'dd':
            result = this._now.getDate();
            padZero = true;
            break;
        case 'd':
            result = this._now.getDate();
            break;
        case 'hh':
            result = ((h = this._now.getHours() % 12) ? h : 12);
            padZero = true;
            break;
        case 'h':
            result = ((h = this._now.getHours() % 12) ? h : 12);
            break;
        case 'nn':                    
            result =  this._now.getMinutes();
            padZero = true;
            break;
        case 'ss':                    
            result = this._now.getSeconds();
            padZero = true;
            break;
        case 'TT':        
            result = this._now.getHours() < 12 ? 'AM' : 'PM';   
            break;
        default:
            result = token;
            break;          
    }
   
    if (padZero) {
        if (result < 10) {
            result = '0' + result;
        }
    }
    
    return result;
        
}