var Webadmin=window.Webadmin||{};

Webadmin.Calendar = function(id, options){
	if(!id){
		return;
	}
	this.element=document.getElementById(id);
	if(document.all){
		this.element.innerHTML = '<iframe src="/blank.html" scrolling="no" frameborder="0" style="filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);"></iframe><div></div>';		
		this.iframe = this.element.getElementsByTagName('iframe')[0];
		this.iframe.style.position='absolute';
		this.iframe.style.top=0;
		this.iframe.style.left=0;
		this.iframe.style.zIndex=0;
	}else{
		this.element.innerHTML='<div></div>';
	}
	this.div = this.element.getElementsByTagName('div')[0];
	this.options = {
		monthsLong:options.monthsLong||["January","February","March","April","May","June","July","August","September","October","November","December"],
		monthsShort:options.monthsShort||["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],
		daysHeader:options.daysHeader||["S","M","T","W","T","F","S"],
		title:options.title||null,
		startWeekday:options.startWeekday||true
	};	
	this.startWeekdayInt=(this.options.startWeekday)?1:0;
	this.daysinmonth= [0,31,28,31,30,31,30,31,31,30,31,30,31];
	this.sep = '-';
	this.selectEvent = new Webadmin.Event.custom.publisher();
	this.currentDate = new Date();
	this.selectedDate = new Date();
	this.render();
};

Webadmin.Calendar.prototype = {
	pad : function(int) {
		return int=(int<10)?"0"+int:int
	},
	monthEvent : function(e){
		e.stop();
		var direction = e.target.innerHTML;
		if(direction=='Previous'){
			this.currentDate.setMonth(this.previous_month);
			this.currentDate.setYear(this.previous_month_year);
		}else{			
			this.currentDate.setMonth(this.next_month);
			this.currentDate.setYear(this.next_month_year);
		}
		this.render();
	},
	dateEvent : function(e){
		e.stop();
		this.currentDate.setDate(e.target.innerHTML);
		this.selectedDate.setYear(this.getYear());
		this.selectedDate.setMonth(this.currentDate.getMonth());
		this.selectedDate.setDate(e.target.innerHTML);
		Webadmin.Dom.removeClass(this.selected, 'selected');
		Webadmin.Dom.addClass(e.target, 'selected');
		this.selected = e.target;
		this.selectEvent.fire(this.getDate());	
	},
	getDay : function(){
		return this.pad(parseInt(this.currentDate.getDate(), 10));
	},
	getMonth : function(){
		return this.pad(parseInt(this.currentDate.getMonth(), 10)+1);
	},
	getYear : function(){
		var year = this.currentDate.getYear();
		if(year < 2000){
			year = year + 1900;
		}
		return year;
	},
	getDate : function(){
		return [this.getYear(), this.getMonth(), this.getDay()].join(this.sep);
	},
	setDate : function(d){
		var date = d.split(this.sep);
		this.currentDate.setYear(date[0]);
		this.currentDate.setMonth(date[1]-1);
		this.currentDate.setDate(date[2]);
		this.selectedDate.setYear(date[0]);
		this.selectedDate.setMonth(date[1]-1);
		this.selectedDate.setDate(date[2]);		
		
	},
	show : function(){
		this.calendar.style.display='block';
		if(this.iframe){
			this.iframe.style.display='block';
		}
	},
	hide : function(e){
		if(e){
			e.stop();
		}
		this.calendar.style.display='none';
		if(this.iframe){
			this.iframe.style.display='none';
		}
	},
	render : function(){
		Webadmin.Event.listener.purge(this.div, true);
		var month = this.currentDate.getMonth();
		var year = this.currentDate.getYear();
		if(year < 2000){
			year = year + 1900;
		}
		var daysinmonth= [31,28,31,30,31,30,31,31,30,31,30,31];
		if(((year%4==0)&&(year%100!=0))||(year%400==0)){
			//Check to see if it's a leap year!
			daysinmonth[2] = 29;
		}	
		var currentDate = new Date(year, month, 1);		
		var display_year = year;
		var display_month = month;
		var display_date = 1;		
		var weekday = currentDate.getDay();		
		var offset = (weekday >= this.startWeekdayInt) ? weekday-this.startWeekdayInt : 7-this.startWeekdayInt+weekday ;
		if(offset>0){
			display_month--;
			if(display_month<0){
				display_month = 11;
				display_year--;
			}
			display_date = daysinmonth[display_month]-offset+1;
		}
		this.next_month = month+1;
		this.next_month_year = year;
		if(this.next_month>11){
			this.next_month=0;
			this.next_month_year++;
		}
		this.previous_month = month-1;
		this.previous_month_year = year;
		if(this.previous_month<0){
			this.previous_month=11;
			this.previous_month_year--;
		}		
		var div = Webadmin.Dom.create('<div class="calendar"><table><thead></thead><tbody></tbody></table></div>',[]);
		var thead = div.getElementsByTagName('thead')[0];
		var tbody = div.getElementsByTagName('tbody')[0];
		if(this.options.title){
			div.insertBefore(Webadmin.Dom.create('<div class="calendar-header">{0} <a href="#" id="{1}_calendar_close">Close</a></div>', [this.options.title, this.element.id]), div.firstChild);
		}		
		//thead.innerHTML='<tr><th colspan="7"><div>'+this.options.monthsLong[month]+' '+year+' <a href="#" id="'+this.element.id+'_calendar_previous" class="calendar-previous">Previous</a> <a href="#" id="'+this.element.id+'_calendar_next" class="calendar-next">Next</a></div></th></tr>';
		var tr = document.createElement('tr');
		var th = document.createElement('th');
		th.colSpan=7;
		th.innerHTML='<div>'+this.options.monthsLong[month]+' '+year+' <a href="#" id="'+this.element.id+'_calendar_previous" class="calendar-previous">Previous</a> <a href="#" id="'+this.element.id+'_calendar_next" class="calendar-next">Next</a></div>';
		tr.appendChild(th);
		thead.appendChild(tr);
		var prev = thead.getElementsByTagName('a')[0];
		var next = thead.getElementsByTagName('a')[1];
		Webadmin.Event.listener.add(prev, 'click', this.monthEvent, this);
		Webadmin.Event.listener.add(next, 'click', this.monthEvent, this);
		var row = document.createElement('tr');
		if(this.options.startWeekday===true){
			for(var i=1; i<this.options.daysHeader.length; i++){
				row.appendChild(this.createCell('th', this.options.daysHeader[i]));
			}
			row.appendChild(this.createCell('th', this.options.daysHeader[0]));
		}else{
			this.options.daysHeader.forEach(function(day){
				row.appendChild(this.createCell('th', day));
			}, this);
			
		}
		thead.appendChild(row);	
		for (var r=1; r<=6; r++) {
			var row = document.createElement('tr');
			for (var c=1; c<=7; c++) {						
				if(month!=display_month){
					var cell = this.createCell('td', display_date);
					cell.className='calendar-disabled calendar-cell c'+r+'-'+c;
				}else{
					var cell = this.createCell('td');
					cell.className='calendar-cell c'+r+'-'+c;
					var link = document.createElement('a');
					link.href='#';
					link.innerHTML=display_date;
					cell.appendChild(link);
					if(display_date==this.selectedDate.getDate()&&this.currentDate.getMonth()==this.selectedDate.getMonth()&&this.currentDate.getYear()==this.selectedDate.getYear()){
						link.className = 'selected';
						this.selected = link;
					}
					Webadmin.Event.listener.add(link, 'click', this.dateEvent, this);
				}
				row.appendChild(cell);
				display_date++;
				if (display_date > daysinmonth[display_month]) {
					display_date=1;
					display_month++;
				}
				if (display_month > 11) {
					display_month=0;
					display_year++;
				}
			}
			tbody.appendChild(row);			
		}	
		this.div.innerHTML = '';
		this.div.appendChild(div);
		this.calendar = div;
		if(this.iframe){
			this.iframe.style.width = div.offsetWidth+'px';
			this.iframe.style.height = div.offsetHeight+'px';
		}
		Webadmin.Event.listener.add(document.getElementById(this.element.id+'_calendar_close'), 'click', this.hide, this);
	},
	createCell : function(element, html){
		var cell = document.createElement(element);
		if(html){
			cell.innerHTML=html;
		}
		return cell;
	}
};

