﻿/**
 * @author Sergey Chikuyonok
 * @copyright 2006 Art. Lebedev Studio | http://www.artlebedev.ru
 */

/*
format strings:
%d - Day of the month, 2 digits with leading zeros (01 to 31)
%j - Day of the month without leading zeros (1 to 31)
%F - A full textual representation of a month, such as January or March (January through December)
%f - A full textual representation of a month, such as January or March in genetive (January through December)
%m - Numeric representation of a month, with leading zeros (01 through 12)
%M - A short textual representation of a month, three letters (Jan through Dec)
%n - Numeric representation of a month, without leading zeros 1 through 12 
%U - Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
%Y - A full numeric representation of a year, 4 digits. Examples: 1999 or 2003 
%y - A two digit representation of a year. Examples: 99 or 03 
*/

Date.aFormatParams=['d', 'F', 'f', 'j', 'm', 'M', 'n', 'U', 'Y', 'y'];
Date.aMonths=['январь', 'февраль', 'март', 'апрель', 'май', 'июнь', 'июль', 'август', 'сентябрь', 'октябрь', 'ноябрь', 'декабрь'];
Date.aMonthsGenetive=['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря'];
Date.aMonthsShort=['янв', 'фев', 'мар', 'апр', 'май', 'июн', 'июл', 'авг', 'сен', 'окт', 'ноя', 'дек'];

Date.prototype.format=function(sFormatString){
	var _result='', _char, _temp;
	for(var i=0; i<sFormatString.length; i++){
		if(sFormatString.charAt(i) == '%' && (i+1) != sFormatString.length){
			_char=sFormatString.charAt(i+1);
			i++;
			switch(_char){
				case 'd':
					_temp=this.getDate();
					_result+=((_temp < 10) ? '0' : '')+_temp;
					break;
				case 'j':
					_result+=this.getDate();
					break;
				case 'F':
					_result+=this.constructor.aMonths[this.getMonth()];
					break;
				case 'f':
					_result+=this.constructor.aMonthsGenetive[this.getMonth()];
					break;
				case 'm':
					_temp=this.getMonth();
					_result+=((_temp < 9) ? '0' : '')+(_temp+1);
					break;
				case 'M':
					_result+=this.constructor.aMonthsShort[this.getMonth()];
					break;
				case 'n':
					_result+=this.getMonth();
					break;
				case 'U':
					_result+=Math.round(this.getTime()/1000);
					break;
				case 'Y':
					_result+=this.getFullYear();
					break;
				case 'y':
					_temp=(this.getYear()%100);
					_result+=((_temp < 10) ? '0' : '')+_temp;
					break;
				case '%':
					_result+='%';
					break;
			}
		}
		else{
			_result+=sFormatString.charAt(i);
		}
	}
	
	return _result;
}

/**
 * Returns type of fragment 
 * @param {String} sFormatString Format string (1 letter) to check
 * @return {String} Type of fragment (day, month, year) or null 
 */
Date.GetFragmentType=function(sFormatString){
	switch(sFormatString){
		case 'd':
		case 'j':
			return 'day';
			break;
		case 'F':
		case 'f':
		case 'm':
		case 'M':
		case 'n':
			return 'month';
			break;
		case 'y':
		case 'Y':
			return 'year';
			break;
	}
	return null;
}