/**
 *	trida pro praci formatem casu, aby se podle toho dalo parsovat a menit datum a cas
 *	@version 0.1
 */
_timeFormat=function(format)
{
	this.parse(format);
}
with({s:_timeFormat,o:_timeFormat.prototype})
{
	s.ISNT=0;
	s.CAN=1;
	s.IS=2;

	s._text=function(f){this.format(f);}
	with({p:s,s:s._text,o:s._text.prototype})
	{
		o.dtPar=-1;
		o.v='';
		o.format=function(f){this.v=f.toString();}
		o.get=function(dt){return this.v;}
		o.set=function(dt, v){}

		s.isFormat=function(f){return f.indexOf('%')<0?p.IS:p.ISNT;}
		o.is=function(v)
		{
			return v.length>this.v.length && v!=this.v.slice(0,v.length)
				?p.ISNT
				:(v.length<this.v.length?p.CAN:p.IS);
		}
	}


	s._percent=function(t){this.format(t);}
	with({p:s,s:s._percent,o:s._percent.prototype})
	{
		o.dtPar=-1;
		o.f='%%';
		o.v='%';
		o.format=function(f){}
		o.get=function(dt){return this.v;}
		o.set=function(dt, v){}

		s.isFormat=function(f){return f=='%'?p.CAN:(f==o.f?p.IS:p.ISNT);}
		o.is=function(v)
		{
			return v.length>this.v.length && v!=this.v.slice(0,v.length)
				?p.ISNT
				:(v.length<this.v.length?p.CAN:p.IS);
		}
	}


	s._fullyear=function(f){this.format(f);}
	with({p:s,s:s._fullyear,o:s._fullyear.prototype})
	{
		o.dtPar=0;
		o.f='%Y';
		o.format=function(f){}
		o.get=function(dt){return dt.getFullYear();}
		o.set=function(dt, v){return dt.setFullYear(v);}

		s.isFormat=function(f){return f=='%'?p.CAN:(f==o.f?p.IS:p.ISNT);}
		o.is=function(v){return /^(([1-9][0-9]*)|0)$/.match(v)?p.IS:p.ISNT;}
	}


	s._month=function(f){this.format(f);}
	with({p:s,s:s._month,o:s._month.prototype})
	{
		o.dtPar=1;
		o.f='%m';
		o.format=function(f){}
		o.get=function(dt, fromZero){return sprintf('%02d',dt.getMonth()+(fromZero?0:1));}
		o.set=function(dt, v, fromZero){return dt.setMonth(Number(v)-(fromZero?0:1));}

		s.isFormat=function(f){return f=='%'?p.CAN:(f==o.f?p.IS:p.ISNT);}
		o.is=function(v){return /^[0-9]+$/.match(v)?p.IS:p.ISNT;}
	}


	s._day=function(f){this.format(f);}
	with({p:s,s:s._day,o:s._day.prototype})
	{
		o.dtPar=2;
		o.f='%d';
		o.format=function(f){}
		o.get=function(dt, fromZero){return sprintf('%02d',dt.getDate()-(fromZero?1:0));}
		o.set=function(dt, v, fromZero){return dt.setDate(Number(v)+(fromZero?1:0));}

		s.isFormat=function(f){return f=='%'?p.CAN:(f==o.f?p.IS:p.ISNT);}
		o.is=function(v){return /^[0-9]+$/.match(v)?p.IS:p.ISNT;}
	}


	s._hour=function(f){this.format(f);}
	with({p:s,s:s._hour,o:s._hour.prototype})
	{
		o.dtPar=3;
		o.f='%H';
		o.format=function(f){}
		o.get=function(dt){return sprintf('%02d',dt.getHours());}
		o.set=function(dt, v){return dt.setHours(v);}

		s.isFormat=function(f){return f=='%'?p.CAN:(f==o.f?p.IS:p.ISNT);}
		o.is=function(v){return /^[0-9]+$/.match(v)?p.IS:p.ISNT;}
	}


	s._minute=function(f){this.format(f);}
	with({p:s,s:s._minute,o:s._minute.prototype})
	{
		o.dtPar=4;
		o.f='%M';
		o.format=function(f){}
		o.get=function(dt){return sprintf('%02d',dt.getMinutes());}
		o.set=function(dt, v){return dt.setMinutes(v);}

		s.isFormat=function(f){return f=='%'?p.CAN:(f==o.f?p.IS:p.ISNT);}
		o.is=function(v){return /^[0-9]+$/.match(v)?p.IS:p.ISNT;}
	}


	s._second=function(f){this.format(f);}
	with({p:s,s:s._second,o:s._second.prototype})
	{
		o.dtPar=5;
		o.f='%s';
		o.format=function(f){}
		o.get=function(dt){return sprintf('%02d',dt.getSeconds());}
		o.set=function(dt, v){return dt.setSeconds(v);}

		s.isFormat=function(f){return f=='%'?p.CAN:(f==o.f?p.IS:p.ISNT);}
		o.is=function(v){return /^[0-9]+$/.match(v)?p.IS:p.ISNT;}
	}

	s.formats=[
		s._fullyear,
		s._month,
		s._day,
		s._hour,
		s._minute,
		s._second,
		s._percent,
		s._text
	];

	o.parts=[];

	o.parse=function(format)
	{
		this.parts=[];
		var step=[];
		step.length=s.formats.length;
		step.fill(s.IS);

		for(var from=0, to=1;to<=format.length;to++)
		{
			var prev=step.slice();
			for(var k=0;k<s.formats.length;k++)
			{
				if(step[k])step[k]=s.formats[k].isFormat(format.slice(from,to));
			}

			var ntSm=!step.some(function(v){return v;});
			if(ntSm || to==format.length)
			{
				if(ntSm) // pokud nejsme pouze na konci ale dosli jsme k spatnemu znaku, vratime se o krok zpet
				{
					to--;
					step=prev;
				}

				if(to)for(var i=0;i<s.formats.length;i++)
				{
					if(step[i]==s.IS)
					{
						this.parts.push(new s.formats[i](format.slice(from,to)));
						break;
					}
				}
				from=to;
				step.fill(s.IS);
			}
		}
		//alert(this.parts);
	}

	o.dt2str=function(dt)
	{
		var str='';
		for(var i=0;i<this.parts.length;i++)
			str+=this.parts[i].get(dt);
		return str;
	}

	o.str2dt=function(str)
	{
		var dt=[];
		dt.length=6;
		dt.fill(0);
		var step=s.IS;

		for(var from=0, to=1, k=0;to<=str.length && k<this.parts.length;to++)
		{
			var prev=step;
			if(step)step=this.parts[k].is(str.slice(from,to));

			//alert('type: '+this.parts[k].f+'\nstep: '+step+'\nstr: '+str.slice(from,to));

			if(to==str.length || !step)
			{
				if(!step)to--;
				if(to && prev==s.IS && this.parts[k].dtPar>-1)
					dt[this.parts[k].dtPar]=str.slice(from,to);

				from=to;
				step=s.IS;
				k++;
			}
		}
		return new Date(dt[0],dt[1]-1,dt[2],dt[3],dt[4],dt[5]);
	}
}

