/** This file provides small implementation of strftime(), to be as
 * compatible as possible with PHP's strftime(), which is
 * platform-dependent.
 *
 * The original purpose for this function is Care2's Smarty variable
 * modifier plugin, local_date_format.  This modifier allows the display
 * of times and dates in the local timezone.
 *
 * Some formats are not supported, but they probably won't be missed.
 */

Date.prototype.strftime = function (format) {
    var result = "";
    var format = format.toString();
    var fmt_len = format.length;
    for (var i = 0; i < fmt_len; i++) {
        var chr = format.charAt(i);
        if (chr == "%") {
            i++;
            if (i < fmt_len) {
                result += this._strftime_chr(format.charAt(i));
            }
        } else {
            result += chr;
        }
    }
    return result;
};

Date.prototype._strftime_chr = function (chr) {
    var d = this;
    switch (chr) {
    case "a": return d.getDayAbbr();
    case "A": return d.getDayName();
    case "b": return d.getMonthAbbr();
    case "B": return d.getMonthName();
    case "c": return d.toLocaleString();
    case "C": var y=d.getFullYear(); return ((y-(y%100))/100).toString();
    case "d": return d.getDate().lpad(2, "0");
    case "D": return d.strftime("%m/%d/%y");
    case "e": return d.getDate().lpad(2, " ");
    //case "g": Not implemented, requires week number arithmetic
    //case "G": and isn't very useful
    case "h": return d._strftime_chr("b");
    case "H": return d.getHours().lpad(2, "0");
    case "I": return (((d.getHours()+11)%12)+1).lpad(2, "0");
    //case "j": Somewhat difficult to get right w/ Daylight savings and all
    case "l": return (((d.getHours()+11)%12)+1).lpad(2, " ");
    case "m": return (d.getMonth()+1).lpad(2, "0");
    case "M": return d.getMinutes().lpad(2, "0");
    case "n": return "\n";
    case "p": return (d.getHours()<12?"AM":"PM");
    case "r": return d.strftime("%I:%M:%S %p");
    case "R": return d.strftime("%H:%M");
    case "S": return d.getSeconds().lpad(2, "0");
    case "t": return "\t";
    case "T": return d.strftime("%H:%M:%S");
    case "u": var dow=d.getDay(); return (dow==0?7:dow).toString();
    //case "U": Another one with week arithmetic, seems less useful.
    //case "V": dito
    case "w": return d.getDay().toString();
    case "x": return d.toLocaleDateString();
    case "X": return d.toLocaleTimeString();
    case "y": return (d.getFullYear()%100).lpad(2, "0");
    case "Y": return d.getFullYear().toString();
    case "z": var o=-d.getTimezoneOffset(); 
        return ((o)>0?"+":"")+(((o)-(o%60))/60).toString()+":"+(o%60).lpad(2, "0");
    case "Z": return d._strftime_chr("z");
    case "%": return "%";
    default: return "%"+chr;
    }
}


/** Add a bit of new functionality to the usual Number, String, and Date classes.
 *  Hope it doesn't clash!  (:
 */

String.prototype.lpad = function (len, chr) {
    return (this.length < len) ? (chr+this).lpad(len, chr) : this;
};
Number.prototype.lpad = function (len, chr) {
    return this.toString().lpad(len, chr);
};
Date.prototype._days = {
    0:"Sunday", 1:"Monday", 2:"Tuesday", 3:"Wednesday", 
    4:"Thursday", 5:"Friday", 6:"Saturday", 7:"Sunday"
};
Date.prototype.getDayName = function () {
    return this._days[this.getDay()];
};
Date.prototype.getDayAbbr = function () {
    return this.getDayName().substring(0, 3);
};
Date.prototype._months = {
    1:"January", 2:"February", 3:"March", 4:"April", 5:"May", 6:"June", 7:"July",
    8:"August", 9:"September", 10:"October", 11:"November", 12:"December"
};
Date.prototype.getMonthName = function () {
    return this._months[this.getMonth()+1];
};
Date.prototype.getMonthAbbr = function () {
    return this.getMonthName().substring(0, 3);
};

