//minimal calendar, work in progress  --Dan Simpson

var Calendar = new Class({

	days		: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
	months		: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	blocked		: [],
	items		: null,
	
	date		: null,
	
	//remote calls
	remote		: false,
	remoteConfig: null,
	
	gridEl		: null,
	nextEl		: null,
	prevEl		: null,
	dispEl		: null,
	
	tips		: null,
	

	initialize: function(opts) {
		$extend(this, opts);
		
		this.prevEl.addEvent('click', this.prevMonth.bind(this));
		this.nextEl.addEvent('click', this.nextMonth.bind(this));
		
		this.load();
	},

	load: function() {
		if(this.remote && this.remoteConfig) {
			this.showLoading();
			new Request.JSON({
				url: this.remoteConfig.url,
				onComplete: this.loadComplete.bind(this)
			}).get(this.getParams());
		} else {
			this.refresh();
		}
	},
	
	showLoading: function() {
		this.gridEl.adopt(new Element('div', {
			'class'	: 'loading-dialog'
		}));
	},
	
	getParams: function() {
		return  {
			year : this.date.getFullYear(),
			month: this.date.getMonth()+1
		};
	},
	
	loadComplete: function(items) {
		if(this.translateFn) {
			this.translateFn(items);
		}
		this.items = items;
		this.refresh();
	},
	
	refresh: function() {
		this.clear();
		

		this.getDays().each(function(d) {
			var ele = new Element('div', {
				'rel'	: d.day,
				'class'	: d.cls
			});
			ele.adopt(new Element('span', {
				'html': d.day
			}));
			
			if(d.items && d.items.length > 0) {
				
				var text = "";
				d.items.each(function(item) {
					text += "<h4>" + item.event.title + " - " + item.event.showtime + "</h4><p>" + item.event.content + "</p>";
				}, this);
				ele.store('tip:text', text);
				
				ele.addEvents({
					'click': this.clicked.bind(this)
				});
			}
			
			ele.inject(this.gridEl);
			
			//alert(this.gridEl.getChildren().length);
		}, this);
		
		this.gridEl.adopt(new Element('div', {
			'class': 'clear'
		}));
		
		this.dispEl.set('html', this.months[this.date.getMonth()] + ' ' + this.date.getFullYear());
		
		this.tips = new Tips('.event-item');
	},

	
	getDays: function() {

		var d 		= this.date;
		var days 	= [];
		var dim 	= this.daysInMonth(d.getFullYear(),d.getMonth());
		var dilm 	= this.daysInMonth(d.getFullYear(),d.getMonth()-1);
		var first 	= this.dayOfWeek(d.getFullYear(),d.getMonth(),1);
		var last 	= this.dayOfWeek(d.getFullYear(),d.getMonth(),dim);

		for(var i = dilm - first;i < dilm;i++) {
			days.push({day:i,cls:'day inactive'});
		}

		for(var i = 1;i <= dim;i++) {
			var items = this.getItemsFor(i);
			var region = this.getRegionFor(i);

			if(items.length > 0) {
				days.push({
					day		: "<a href=\"/" + region + "/events\">" +i + "</a>",
					cls		: 'day active event-item',
					items	: items
				});
			} else {
				days.push({
					day:i,
					cls:'day active'
				});
			}
		}

		for(var i = 0;i < 6 - last;i++) {
			days.push({day:i+1,cls:'day inactive'});
		}

		days[first+this.date.getDate()-1].cls += ' today';
		
		return days;
	},
	
	getItemsFor: function(day) {
		return this.items.filter(function(item) {
			return item.day && item.day == day;
		});
	},
	
	getRegionFor: function() {
	 return location.pathname.substr(1).split('/', 2)[0] || '/';
	},

	daysInMonth: function(y,m) {
		return 32 - new Date(y,m,32).getDate();
	},

	dayOfWeek: function(y,m,d) {
		return new Date(y,m,d).getDay();
	},

	prevMonth: function(e) {
		this.date.setMonth((this.date.getMonth()-1));
		this.load();
		return false;
	},

	nextMonth: function(e) {
		this.date.setMonth(this.date.getMonth()+1);
		this.load();
		return false;
	},

	clicked: function(e) {
	},

	clear: function() {
		this.gridEl.empty();
	}
});