
var locale =
{
    SI:
    {
        code: "SI",
        days_short: [ "ne", "po", "to", "sr", "če", "pe", "so" ],
        days_long: [ "nedelja", "ponedeljek", "torek", "sreda", "četrtek", "petek", "sobota" ],
        months_short: [ "jan", "feb", "mar", "apr", "maj", "jun", "jul", "avg", "sep", "okt", "nov", "dec" ],
        months_long: [ "januar", "februar", "marec", "april", "maj", "junij", "julij", "avgust", "september", "oktober", "november", "december" ],
        date_format: "j.n.Y",
        time_format: "G:i"
    },

    EN:
    {
        code: "EN",
        days_short: [ "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" ],
        days_long: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ],
        months_short: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ],
        months_long: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ],
        date_format: "n/j/Y",
        time_format: "g:i A"
    },

    load: function (code)
    {
        if(this[code]) { this.current = this[code]; }
        else { this.current = this.EN; }
    },

    current : this.EN
};

Date.prototype.format = function(format_string)
{
    var result = "";
    if(!format_string || "datetime" == format_string)
    {
        format_string = locale.current.date_format + " " + locale.current.time_format;
    }
    else if("date" == format_string)
    {
        format_string = locale.current.date_format;
    }
    else if("time" == format_string)
    {
        format_string = locale.current.time_format;
    }

    LZ = function(x) { return (x < 0 || x > 9 ? "" : "0") + x };

    // Prepare date components.
    var y = this.getFullYear().toString();
    var M = this.getMonth()+1;
    var d = this.getDate();
    var E = this.getDay();
    var H = this.getHours();
    var m = this.getMinutes();
    var s = this.getSeconds();

    // Format date components.
    var value = new Object();
    value['Y'] = y.toString();
    value['y'] = y.substring(2);
    value['n'] = M;
    value['m'] = LZ(M);
    value['F'] = locale.current.months_long[M-1];
    value['M'] = locale.current.months_short[M-1];
    value['j'] = d;
    value['d'] = LZ(d);
    value['D'] = locale.current.days_short[E];
    value['l'] = locale.current.days_long[E];
    value['G'] = H;
    value['H'] = LZ(H);
    if (H==0) {value['g']=12;}
    else if (H>12){value['g']=H-12;}
    else {value['g']=H;}
    value['h']=LZ(value['g']);
    if (H > 11) {value['a']='pm'; value['A'] = 'PM';}
    else { value['a']='am'; value['A'] = 'AM';}
    value['i']=LZ(m);
    value['s']=LZ(s);

    // Construct the result string.
    var i_format = 0;
    while (i_format < format_string.length)
    {
        var c = format_string.charAt(i_format);
        var token = "";
        while ((format_string.charAt(i_format)==c) && (i_format < format_string.length))
        {
            token += format_string.charAt(i_format++);
        }

        if (value[token] != null) { result += value[token]; }
        else { result=result + token; }
    }

    return result;
};
