
function calendar(id, publishDate) {
	
	this.cssFile = 'admin/calendar.css';
	this.setCss = function(cssFile) { if(!cssFile) cssFile=this.cssFile; includeCSS(PATH_STYLES+this.cssFile); };
	this.setCss();
	
	this.textSelectedDate = 'дата публикации';
	
	this.selectPastDates = false;
	this.setPastDatesMode = function(mode) { if(mode) this.selectPastDates=mode; };
	
	this.yearsNavigate = false;
	this.setYearsNavigate = function(mode) { if(mode) this.yearsNavigate=true; }
	
	this.monthsNavigate = true;
	
	this.defDate = false;
	
	this.id_input = id;
    this.id_popup = 'popup_'+id;
	this.id_icon = 'icon_'+id;
	this.id_dest = 'dest_'+id;
	
    this.infocell = null;
    
    this.table = document.createElement('table');
   
    this.currDay = null;
	
	this.isSetStartDate = false;
	
	this.showWeekDay = false;
	
	this.isCreate = false;
	
	this.actionClick = '';
	this.actionOver = '';
	this.actionOut = '';
	this.actionClose = '';
	
	this.idIcon = '';
	
	this.actionArgs = new Array();
	this.hiddenSelects = new Array();
    
    setClass(this.table, 'table_calendar');
	this.table.cellspacing = 0 ;
	this.table.cellpadding = 0 ;

    if (publishDate == false) {
		this.publishDate = new Date();
    } else {
		this.isSetStartDate = true;
		publishDate.setMonth(publishDate.getMonth()-1);
		this.publishDate = publishDate;
    }
	
	
	this.className = 'dateInput';
	
	this.setSelectedDateText = function(txt) { this.textSelectedDate = txt };
	
	this.setClassName = function(className) { 
		if(className) { 
			this.className=className; 
			if(getElement(this.id_input)) { 
				setClass(getElement(this.id_input),this.className); 
			} 
		}
	};
	
	// input
	var input=getElement(this.id_input);
	this.setClassName(this.className);
	
	// create popup:
	var popup=document.createElement('div');
	popup.id=this.id_popup;
	input.appendChild(popup);
	setClass(popup,'popup');
	
	var icon=document.createElement('div');
	icon.id=this.id_icon;
	input.appendChild(icon);
	// create dest:
	var dest=document.createElement('span');
	dest.id=this.id_dest;
	input.appendChild(dest);
	setClass(dest,'dest');
	// create hidden input:
	var hInput=document.createElement('input'); 
	hInput.type='hidden';
	hInput.id='h_'+this.id_input;
	hInput.name=this.id_input;
	
	this.insertVisibleDate = function(y,m,d) {
		if(!Agent.isIE) y+=1900;
		m+=1;
		if(d<10) d='0'+d;
		if(m<10) m='0'+m;
		getElement(this.id_dest).innerHTML = d+'.'+m+'.'+y;
	};
	
	this.insertMessage = function(msg) { getElement(this.id_dest).innerHTML = msg };
	
	this.showWeekDay = function(m) { this.showWeekDay = m };
	
	if(this.defDate || this.isSetStartDate) {
		hInput.value = this.publishDate.getFullYear()+'-'+(this.publishDate.getMonth()+1).leadZero(2)+'-'+this.publishDate.getDate().leadZero(2);
		this.insertVisibleDate(this.publishDate.getYear(),this.publishDate.getMonth(),this.publishDate.getDate());
	} else {
		hInput.value = null;
		this.insertMessage('выберите дату');
	}
	
	input.appendChild(hInput);
	
	this.inputDisabled = function() {
		hInput.diabled = true;
	};
	
	this.show = function() { getElement(this.id_popup).style.visibility='visible'; };
	
	this.hide = function() { getElement(this.id_popup).style.visibility='hidden'; };
	
	this.iconOpen = function() {
		var icon=getElement(this.id_icon);
		setClass(icon,"cal_icon cal_open");
		var self=this;
		icon.onclick = function() { 
			self.hide(); 
			self.iconClose(); 
			if(Agent.isIE6) {
				self.showSelects();
			}
		};
	};
	
	this.iconClose = function() {
		var icon=getElement(this.id_icon);
		setClass(icon,"cal_icon cal_close");
		var self=this;
		icon.onclick = function() { 
			if(!self.isCreate) self.create(); 
			if(Agent.isIE6) {
				self.hideSelects();
			}
			self.show(); 
			self.iconOpen(); 
		};
	};
	
	this.iconClose();
	
	this.preCreate = function() {
        var row = this.table.insertRow(-1);
        var leftCell = row.insertCell(-1);
		leftCell.setAttribute('colSpan', 2);
		
		if(this.yearsNavigate) {
			var a_prev_year = document.createElement('a');
			a_prev_year.innerHTML = "&lt;&lt;";
			a_prev_year.title = "предыдущий год";
			setClass(a_prev_year,'selMonth');
			(function(self) {
					addEvent(a_prev_year, 'click', function () { 
					self.changeYear(self.getYear() - 1);
				});
			})(this);
			leftCell.appendChild(a_prev_year);
		}
		
		if(this.monthsNavigate) {
			var a_prev_month = document.createElement('a');
			a_prev_month.innerHTML = "&lt;";
			a_prev_month.title = "предыдущий месяц";
			setClass(a_prev_month,'selMonth');
			
			(function(self) {
				addEvent(a_prev_month, 'click', function () { 
					self.changeMonth(self.publishDate.getMonth() - 1);
				});
			})(this);
			
			leftCell.appendChild(a_prev_month);
		}
		
        this.infocell = row.insertCell(-1);
        this.infocell.setAttribute('colSpan', 5);
		setClass(this.infocell,'monthName');
		
        var rightCell = row.insertCell(-1);
        rightCell.setAttribute('colSpan', 2);
		
        if(this.monthsNavigate) {
			var a_next_month = document.createElement('a');
			a_next_month.innerHTML = ">";
			a_next_month.title = "следующий месяц";
			setClass(a_next_month,'selMonth');
			
			(function(self) {
					addEvent(a_next_month, 'click', function () { 
					self.changeMonth(self.publishDate.getMonth() + 1);
				});
			})(this);
			rightCell.appendChild(a_next_month);
		}
		
		if(this.yearsNavigate) {
			var a_next_year = document.createElement('a');
			a_next_year.innerHTML = ">>";
			a_next_year.title = "следующий год";
			setClass(a_next_year,'selMonth');
			(function(self) {
					addEvent(a_next_year, 'click', function () { 
					self.changeYear(self.getYear() + 1);
				});
			})(this);
			rightCell.appendChild(a_next_year);
		}
		
    };
	
	this.getYear = function () { return parseInt(this.publishDate.getYear())+1900 };
	
	this.addHiddenSelect = function (id) { this.hiddenSelects.push(id) };
	
	this.hideSelects = function () {
		for(var s=0; s<this.hiddenSelects.length;s++) {
			var sel=getElement(this.hiddenSelects[s]);
			if(sel) sel.style.visibility='hidden';
		}
	};
	
	this.showSelects = function () {
		for(var s=0; s<this.hiddenSelects.length;s++) {
			var sel=getElement(this.hiddenSelects[s]);
			if(sel) sel.style.visibility='visible';
		}
	};
    
    this.create = function() {
        while(this.table.rows.length) {
            this.table.deleteRow(0);
        }

        this.preCreate();
        
        var date = this.publishDate.clone(), row = this.table.insertRow(-1), day = 1;
        
        date.setDate(1);
        
        var  firstNumDay = date.getDay();
		
        if (firstNumDay == 0) {
            firstNumDay = 7;
        }
		
        while (day != firstNumDay) {
			if(day==1) {
				var cell = row.insertCell(-1);  
				setClass(cell, 'rasp');
			}
			var cell = row.insertCell(-1);
			setClass(cell, 'empty');
			day++;
        }

        while (date.getMonth() == this.publishDate.getMonth()) {
			
			if(day==1) {
				var cell = row.insertCell(-1);
				cell.innerHTML = '&nbsp;';
				setClass(cell, 'rasp');
			}
			
	        var cell = row.insertCell(-1);
	       	
			// check selectPastDates value:
			
			var cy=new Date().getYear();
			var dy=date.getYear();
			var cm=new Date().getMonth();
			var dm=date.getMonth();
			var cd=new Date().getDate();
			var dd=date.getDate();
				
			if(!this.selectPastDates && ((cy>dy) || (cy==dy && cm>dm) || (cy==dy && cm==dm && cd>dd) )) {
				var span_node=document.createElement('span');
				span_node.innerHTML=date.getDate();
				setClass(span_node, 'ua_day_cell');
				cell.appendChild(span_node);
				
			} else {
				
				var a_node=document.createElement('a');
				a_node.innerHTML=date.getDate();
				
				setClass(a_node, 'day_cell');
				
				
				if((date.getDate() == this.publishDate.getDate()) && (new Date().getYear() == date.getYear()) && (new Date().getMonth() == date.getMonth())) 
				{
					a_node.title = this.textSelectedDate;
					setClass(cell, 'publishDate');
				}
				
				if ( (new Date().getDate() == date.getDate()) && (new Date().getMonth() == date.getMonth()) && (new Date().getYear() == date.getYear()) )
				{
					a_node.title = 'сегодня';
					setClass(cell, 'currentDate');
				}
				
				cell.appendChild(a_node);
				
				if(this.actionOver!='') {
					addEvent(a_node,'mouseover',function(self){
							return function() { eval(self.actionOver+'()'); }
						}(this)
					);
				}
				if(this.actionOut!='') {
					addEvent(a_node,'mouseout',function(self){
							return function() { eval(self.actionOut+'()'); }
						}(this)
					);
				}
				if(this.actionClick!='') {
					addEvent(a_node, 'click', function(self,y,m,d,args) {
							return function() { 
								self.currDay = d;
								eval(self.actionClick+'('+y+', '+m+', '+d+args+')');
							}
						}(this,parseInt(date.getYear()),parseInt(date.getMonth()),parseInt(date.getDate()),this.getArgs())
					);
				} else {
					addEvent(a_node, 'click', function(self,y,m,d) {
							return function() { 
								self.currDay = d;
								self.publishDate.setDate(d);
								self.insertVisibleDate(y,m,d);
								self.iconClose();
								self.hide();
								
								hInput.value=self.publishDate.getFullYear()+'-'+(self.publishDate.getMonth()+1).leadZero(2)+'-'+self.publishDate.getDate().leadZero(2);
								self.showSelects();
							}
						}(this,parseInt(date.getYear()),parseInt(date.getMonth()),parseInt(date.getDate()))
					);
				}
				
			}
			
	        date.setDate(date.getDate() + 1);
			
	        if (day == 7) {
				var cell = row.insertCell(-1);
				setClass(cell,'rasp');
				
		        var row = this.table.insertRow(-1);
	    	    day = 0;
    	    }
			
	        day++;
        }

        while (day < 8) {
	        var cell = row.insertCell(-1);
	        cell.innerHTML = '&nbsp;';
	        day++;
        }
        getElement(this.id_popup).appendChild(this.table);
        this.beforeCreate();
		this.isCreate=true;
    };
    
    this.changeMonth = function(i) {
        this.publishDate.setMonth(i);
        this.create();
    };
    
    this.changeYear = function(i) {
        this.publishDate.setYear(i);
        this.create();
    };
    
    this.setDate = function(date) {
        this.publishDate = date.clone();
        this.create();
    };
    
    this.beforeCreate = function() {
        this.infocell.innerHTML = this.publishDate.getFullMonth() + ', ' + this.publishDate.getFullYear();
    };
			
	this.setActionClick = function(m) {
		if(m) this.actionClick=m;
	};
	
	this.setActionOver = function(m) {
		if(m) this.actionOver=m;
	};
	
	this.setActionOut = function(m) {
		if(m) this.actionOut=m;
	};
	
	this.close = function() {
		var dest=getElement(this.id_div+'_table');
		if(dest) dest.removeNode(true);
		getElement(this.id_div).style.visibility='hidden';
		if(this.idIcon!='') {
			$icon=getElement(this.idIcon);
			setClass($icon,'cal_icon cal_open');
			var self=this;
			$icon.onclick = function() { self.create(); }; 
		}
		this.isCreate=false;
	};
	
	this.addArg = function(a) {
		this.actionArgs.push(a);
	};
	
	this.getArgs = function() {
		var st='';
		if(!this.actionArgs.length) return st;
		for(i=0;i<this.actionArgs.length;i++) {
			st += ', \''+this.actionArgs[i]+'\'';
		}
		return st;
	};
	
	this.setIdIcon = function(id) {
		this.idIcon = id;
	};
}
