        /*

Calendar

*/
Proto.UI.cal = Class.create({
       	date:new Date(),
       	process:false,
       	typing:false,
       	initialize: function() 
        {
                      
        	this.options = Object.extend({
        	
                renderTo:document.body,
                classname:"ProtoCalendar",
                field:false,
                format:"yyyy-mm-dd hh:nn",
                startDay:1,
                monthes:["январь","февраль","март","апрель","май","июнь","июль","август","сентябрь","октябрь","ноябрь","декабрь"],
                shortDays:["пн","вт","ср","чт","пт","сб","вс"],
                holidays:[0,6],//вс,сб
                legalHolidays:["01.01","01.02","01.03","01.04","01.05","01.07","02.23","03.08","05.01","05.09","06.12","11.04"],//оф. праздничные дни
                addHollydayIfIsLegalHollyday:true,//переносить ли выходной если совпадает с праздником
                workDayAdded:["10.02.27","10.11.13"],//фикс не поддающихся логике добавленных выходных
                weekendAdded:["10.02.22","10.11.05"],//фикс не поддающихся логике добавленных рабочих дней
                todayOnBlank:true,
                changeAble:true,
                onChange:function(){},
                onDblClick:function(){},
                now:false
                
                }, arguments[0] || { });
                
                      		if(!this.options.changeAble)
      		{
      		  			new PeriodicalExecuter(function(pe) { 
				        var date = new Date();
				        var da = date.getDate();
				        if(this.date.getDate() != da){
				       
	                			this.date.setDate(da);
	                			this.bkpDate.setDate(da);
	                			//document.title+=":"+this.date.getDate();
	                			this.titleContent.update("");
	                			this.daysContent.update("");
	                			this.renderMonth(1);
                          		this.renderMonth(0);
                          		this.renderMonth(-1);
                    			this.fixscroll();
                            }
                            
			 }.bind(this), 1);  
      		}
         	  
         	      
                
                if(this.options.field)
                {
                
                this.options.field.observe("keyup",function(){
                    if(this.typingtime)
                    {
                        window.clearTimeout(this.typingtime);
                    }
                    this.typingtime = this.stopTyping.bind(this).delay(1);
                }.bind(this));
                
	                if(this.options.field.value!="")
	                {
	                	this.date = this.parseString(this.options.field.value);
	                	this.options.field.value=this.date.myformat(this.options.format);
	                }
	                else
	                {
	                   if(this.options.now)
	                   {
	                   this.date =new Date();
	                   this.options.field.value=this.date.myformat(this.options.format);
	                   }
	                }
	                
				} 
				this.bkpDate = new Date(this.date.getFullYear(), this.date.getMonth(), this.date.getDate(),this.date.getHours(),this.date.getMinutes());
                this.render();
                
                
         },
         stopTyping:function(){
           if(this.options.field.value!="")
	                {
	                	this.date = this.parseString(this.options.field.value);
	                }
	                
	                
	                			this.date = this.parseString(this.options.field.value);
	                			
	                			this.titleContent.update("");
	                			this.daysContent.update("");
	                			this.renderMonth(1);
                          		this.renderMonth(0);
                          		this.renderMonth(-1);
                    			this.fixscroll();
						
         },
         parseString:function(i)
         {
				return(parseDateString(i));
				
         },
         weelhandler:function(e)
        {
           Event.stop(e);
            if(this.process){return;}
            var raw = e.detail ? e.detail : e.wheelDelta;
            
            if(Prototype.Browser.Gecko)
            {
            axis=((e.axis!=1)?"v":"h");
            }
            else
            {
            axis=((e.wheelDeltaY!=0)?"v":"h");
            }
            var sotn=((axis=="h")?this.hsotn:this.vsotn);
            if(Prototype.Browser.WebKit)
            {
            if(raw <0){
            this.nextMonth();
            }else{
            this.prewMonth();
            }
            
            }else{
            if(raw <0){
            this.prewMonth();
            }else{
            this.nextMonth();
            }
               
            }
 
        },
        setd:function(dd,e){
        $$("."+this.options.classname+"_dayselected").each(function(d){d.removeClassName(this.options.classname+"_dayselected");}.bind(this));
        Event.element(e).addClassName(this.options.classname+"_dayselected");
        this.date.setDate(dd);
        this.date.setHours(0);
        this.date.setMinutes(0);
        
        this.date.setDate(dd);
        if(this.options.field)
        {
        this.options.field.value=this.date.myformat(this.options.format);
        }
        this.options.onChange(this.date);
        this.bkpDate = new Date(this.date.getFullYear(), this.date.getMonth(), this.date.getDate(),this.date.getHours(),this.date.getMinutes());
                       	
        },
         render:function()
         {
         	this.container = new Element("div",{"class":this.options.classname+"_container"});
         	this.options.renderTo.insert(this.container);
         	this.topBox = new Element("div",{"class":this.options.classname+"_top"});
         	this.container.insert(this.topBox);
         	
         	this.arrowLeft = new Element("div",{"class":this.options.classname+"_arrowleft"});
         	this.topBox.insert(this.arrowLeft);
         	this.arrowLeft.observe("click",this.prewMonth.bind(this));
         	
         	this.title = new Element("div",{"class":this.options.classname+"_title"});
         	this.topBox.insert(this.title);
         	this.titleContent = new Element("div",{"class":this.options.classname+"_titlecontent"});
         	this.title.insert(this.titleContent);
         	this.arrowRight = new Element("div",{"class":this.options.classname+"_arrowright"});
         	this.topBox.insert(this.arrowRight);
         	this.arrowRight.observe("click",this.nextMonth.bind(this));
         	this.daysTitle = new Element("div",{"class":this.options.classname+"_daystitle"});
         	this.container.insert(this.daysTitle);
         	this.options.shortDays.each(function(d){
         		var t = new Element("span");
         		this.daysTitle.insert(t);
         		t.update(d);
         		}.bind(this));
         	
         	this.days = new Element("div",{"class":this.options.classname+"_days"});
         	this.container.insert(this.days);
         	this.daysContent = new Element("div",{"class":this.options.classname+"_dayscontent"});
         	this.days.insert(this.daysContent);
         	if(this.options.changeAble){
         	this.container.observe(((!Prototype.Browser.Gecko)?"mousewheel":"DOMMouseScroll"), this.weelhandler.bind(this)); 
         	 }
         	this.renderMonth(1);
      		this.renderMonth(0);
      		this.renderMonth(-1);
			this.fixscroll();    		

      		
         },
         goTo:function(){
            //this.date=d;
            this.titleContent.update("");
            this.daysContent.update("");
            this.renderMonth(1);
      		this.renderMonth(0);
      		this.renderMonth(-1);
			this.fixscroll(); 
            if(this.options.field)
            {
            this.options.field.value=this.date.myformat(this.options.format);
            }
			
         },
         renderMonth:function(ofs,toBegin)
         {
         
         
         	var d = new Date();
         	
         	d.setYear(Number(this.date.getYear()));
         	d.setDate(1);
         	d.setMonth(this.date.getMonth()+ofs);
         	
         	var t1 = new Element("div",{"class":this.options.classname+"_block",'style':((toBegin)?"display: none":"")});
         	if(!toBegin){
         	this.titleContent.insert(t1);
         	}
         	else
         	{
         	this.titleContent.insertBefore(t1,this.titleContent.firstDescendant());
         	
         	}
         	var y = d.getFullYear();
         	if(y<1000){y+=1900;}
         	
         	//t1.update(this.options.monthes[Number(d.getMonth())]+" "+y);
         	
         	var monSelect = new Element("select",{style:'width:75px;font-size:11px;'});
         	t1.insert(monSelect);
         	for(var i=0; i<this.options.monthes.length ; i++)
         	{
         	  opt = new Element("option",{value:i}).update(this.options.monthes[i]);
         	  if(d.getMonth()==i){opt.selected="selected"}
         	  monSelect.insert(opt);
         	}
         	
         	eval('monSelect.observe("change",function(){ var mid="'+monSelect.identify()+'"; this.date.setMonth(_(mid).value);this.goTo();}.bind(this));');
         	
         	var ySelect = new Element("select",{style:'width:60px;font-size:11px;'});
         	t1.insert(ySelect);
         	for(var i=y-10; i<y+10 ; i++)
         	{
         	  opt = new Element("option",{value:i}).update(i);
         	  if(y==i){opt.selected="selected"}
         	  ySelect.insert(opt);
         	}
         	
         	eval('ySelect.observe("change",function(){ var midz="'+ySelect.identify()+'"; this.date.setFullYear(_(midz).value);this.goTo();}.bind(this));');
         	
         	mon1 = new Element("div",{"class":this.options.classname+"_block",'style':((toBegin)?"display: none":"")});
         	
         	if(!toBegin){
         	this.daysContent.insert(mon1);
         	}
         	else
         	{
         	this.daysContent.insertBefore(mon1,this.daysContent.firstDescendant());
         	}
         	if(Number(d.getDay())<6){
	         	for(var i=0; i<=Number(d.getDay()) ; i++)
	         	{
	         		var dy = new Element("div",{"class":this.options.classname+"_day"});
	         		mon1.insert(dy);
	         	}
         	}
         	
         	var curm = Number(d.getMonth());
         	var holly= new Array();
         	for(var i=1; curm == Number(d.getMonth()); i++)
         	{
         		d.setDate(i);
         		var d2 = new Date(d.getFullYear()+1900,d.getMonth(),d.getDate());
         		
         		if(curm != Number(d.getMonth())){ break; }
         		///alert(this.bkpDate);
         		var dy = new Element("div",{"class":this.options.classname+"_day"+((d.getDate()+" "+d.getMonth() == this.bkpDate.getDate()+" "+this.bkpDate.getMonth())?" "+this.options.classname+"_dayselected":"")});
         		mon1.insert(dy);
         		
         		if( this.options.holidays.indexOf(d2.getDay())!=-1){
         		
         		dy.addClassName(this.options.classname+"_holiday");
         		
         		}
        		
         		if( this.options.legalHolidays.indexOf(Number(d2.getMonth()+1).toPaddedString(2)+"."+Number(d2.getDate()).toPaddedString(2))!=-1)
         		{
             		
             		if(!dy.hasClassName(this.options.classname+"_holiday"))
             		{
             		 dy.addClassName(this.options.classname+"_holiday");
             		}
             		else
             		{
             		 if(this.options.addHollydayIfIsLegalHollyday){holly.push(1);}
             		}
         		
         		}
          		if( holly.length > 0 )
         		{
             		
             		if(!dy.hasClassName(this.options.classname+"_holiday"))
             		{
             		 dy.addClassName(this.options.classname+"_holiday");
             		 holly.shift();
             		}
             		
         		
         		}
         		//weekendAdded
         		var tmp = String( d2.getYear() ).substring(1)+"."+Number(d2.getMonth()+1).toPaddedString(2)+"."+Number(d2.getDate()).toPaddedString(2);
         		
         		
         		if( this.options.weekendAdded.indexOf(tmp)!=-1)
         		{
             		
             		if(!dy.hasClassName(this.options.classname+"_holiday"))
             		{
             		 dy.addClassName(this.options.classname+"_holiday");
             		}         		
         		}        		         		
         		if( this.options.workDayAdded.indexOf(tmp)!=-1)
         		{
             		
             		if(dy.hasClassName(this.options.classname+"_holiday"))
             		{
             		 dy.removeClassName(this.options.classname+"_holiday");
             		}         		
         		}          		         		

         		
         		if(this.options.changeAble){
         		 eval('dy.observe("click",function(e){this.setd('+i+',e);}.bind(this));');
         		 eval('dy.observe("dblclick",function(e){this.setd('+i+',e);Event.stop(e);this.options.onDblClick();}.bind(this));');
         		 //onDblClick
         		 }//FIX ME
         		else
         		{
         		dy.style.cursor="default";
         		}
         		dy.update(i);
         	}
         	this.fixscroll();	
         },
         fixscroll:function()
         {
         	this.days.scrollLeft = this.days.getWidth() || 170;
      		this.title.scrollLeft = this.title.getWidth() || 140;         
         },
         nextMonth:function()
         {
			if(this.process){return;}
			this.process=true;
         	this.renderMonth(2,true);
         	this.date.setMonth(Number(this.date.getMonth())+1);
         			
         	new Effect.Grow(this.titleContent.firstDescendant(),{duration:0.5,afterFinish:function(){
         			this.titleContent.childElements()[3].remove();
         		}.bind(this)});
         		new Effect.Grow(this.daysContent.firstDescendant(),{duration:0.5,afterFinish:function(){
         			this.daysContent.childElements()[3].remove();
         			this.process=false;
         		}.bind(this)});       	
         },
         prewMonth:function()
         {
         	if(this.process){return;}
			this.process=true;
         	
         	new Effect.Shrink(this.titleContent.firstDescendant(),{duration:0.5,afterFinish:function(){
         			this.titleContent.firstDescendant().remove();
         		}.bind(this)});
         	new Effect.Shrink(this.daysContent.firstDescendant(),{duration:0.5,afterFinish:function(){
         			this.daysContent.firstDescendant().remove();
         			this.renderMonth(-2,false);
         			this.date.setMonth(Number(this.date.getMonth())-1);
         			this.process=false;
         		}.bind(this)});
         }
        })