var SmokyCalendar = new Class({
	Implements: [Options],
	options: {
		monthContainerSelector: 'div.month',
		monthDaysContainerSelector: 'ul.monthDaysContainer',
		loadingElementSelector: 'div.loading',
		previousMonthButtonSelector: 'div.prevMonth a',
		nextMonthButtonSelector: 'div.nextMonth a',
		dayPreviewContainer: 'div.dayPreview',
		dayPreviewContentContainer: 'div.dayPreviewContent',
		dayPreviewCloseBtn: 'div.dayPreviewCloseBtn',
		method: 'get',
		url: ''
	},
	
	monthNames: [	
			"Ιανουάριος",
			"Φεβρουάριος",
			"Μάρτιος",
			"Απρίλιος",
			"Μάιος",
			"Ιούνιος",
			"Ιούλιος",
			"Αύγουστος",
			"Σεπτέμβριος",
			"Οκτώβριος",
			"Νοέμβριος",
			"Δεκέμβριος"
	],
	
	initialize: function(container, options) {
		this.setOptions(options);
		this.setContainers(container);
		this.setStartingDate();
		this.setRequestObject();
		this.updateCalendar();
	},
	
	setContainers: function(container){ 
		var cal = document.id(container);
		this.monthContainer = cal.getElement(this.options.monthContainerSelector);
		this.monthDaysContainer = cal.getElement(this.options.monthDaysContainerSelector);
		this.loadingElement = cal.getElement(this.options.loadingElementSelector);
		this.dayPreview = cal.getElement(this.options.dayPreviewContainer);
		this.dayPreviewContent = cal.getElement(this.options.dayPreviewContentContainer);
		this.dayPreviewCloseBtn = cal.getElement(this.options.dayPreviewCloseBtn);
		this.previousButton = cal.getElement(this.options.previousMonthButtonSelector);
		this.nextButton = cal.getElement(this.options.nextMonthButtonSelector);
		this.previousButton.addEvent('click', this.previousMonthButtonClick.bind(this));
		this.nextButton.addEvent('click', this.nextMonthButtonClick.bind(this));	
		
		this.dayPreviewTween = new Fx.Tween(this.dayPreview,{
			'link': 'cancel',
			'duration': 500,
			'transition': 'expo:out'
		}).set('left',-this.dayPreview.getStyle('width').toInt());
		
		this.dayPreviewCloseBtn.addEvent('click', function(ev){
			ev.preventDefault();
			this.dayPreviewTween.start('left', -this.dayPreview.getStyle('width').toInt());
		}.bind(this));

		
	},
	
	setStartingDate: function() {
		var today = new Date();
		this.month = today.getMonth();
		this.year = today.getYear();
		if(this.year < 1900){
			this.year+=1900;//Y2K
		}
	},
	
	setRequestObject: function(){
 		this.request = new Request.JSON({
			url: this.options.url,
			method: this.options.method,
			onSuccess: function(data){
				this.renderMonth(data);
			}.bind(this),
			onFailure: function(xhr){
			}
		});
		
	},
	
	updateCalendar: function(){
		this.request.send({data:{m:this.month,y:this.year}});
		this.loadingElement.setStyle('display', 'block');
	},
	
	renderMonth: function(responseJSON){
		var i;
		var totalDaysInMonth = (new Date(this.year, this.month + 1, 0)).getDate();
		var precedingDays = this.calculateTotalPrecedingDays();
		this.monthContainer.set('text', this.monthNames[this.month] + ' ' + this.year);
		this.loadingElement.setStyle('display', 'none');
		
		this.monthDaysContainer.empty();
		for (i = 0; i < precedingDays; i++) {
			this.renderDay('');
		}
		for(i=1; i <= totalDaysInMonth; i++){
			this.renderDay(i, responseJSON[i]);
		}
		for (i = 0; i < 42 - totalDaysInMonth - precedingDays; i++) {
			this.renderDay('');
		}
	},
	
	renderDay: function(text, obj){
		var el, a;
		el = new Element('li',{
			'text': obj ? '' : text
		})
		if(obj){
			a = new Element('a',{
				'text': text,
				'href': obj['href'],
				'previewHref': obj['previewHref'],
				'events': {
					'click': this.showDayPreview.bind(this),
					//'mouseenter': this.showDayPreview.bind(this),
					//'mouseleave': this.hideDayPreview.bind(this)
				}
			});
			a.inject(el);
		}
		el.inject(this.monthDaysContainer);	
	},
	
	showDayPreview: function(ev){
		ev.preventDefault();
		this.loadingElement.setStyle('display', 'block');
		this.dayRequest = new Request({
			url: ev.target.get('previewHref'), 
			method: this.options.method,
			noCache: true,
			onSuccess: function(responseText){
				this.loadingElement.setStyle('display', 'none');
				this.dayPreviewContent.set('html', responseText);
				this.dayPreviewTween.start('left', 0);
			}.bind(this),
			onFailure: function(txt){
			}
		}).send();
	},
	
	hideDayPreview: function(ev){
		if(this.dayRequest){
			this.dayRequest.cancel();	
		}
		this.loadingElement.setStyle('display', 'none');
		this.dayPreview.setStyle('display', 'none');
	},
	
	nextMonthButtonClick: function(ev){
		ev.preventDefault();
		this.month++;
		if(this.month>11){
			this.month=0;
			this.year++;
		}
		this.updateCalendar();
	},
	
	previousMonthButtonClick: function(ev){
		ev.preventDefault();
		this.month--;
		if(this.month<0){
			this.month = 11;
			this.year--;
		}
		this.updateCalendar();		
	},
	
	calculateTotalPrecedingDays: function(){
		var total = (new Date(this.year,this.month,1)).getDay() - 1;
		if(total < 0) return 6;
		else if(total == 0) return 7;
		return total;
	}
	
});
