var CtCalendar = Class.create(CtComponent_Abstract, {

	element: null,
	locker: null,
	id: null,
	buttons: [],

	initialize: function(htmlElement) {
		this.element = $(htmlElement);
		this.id = this.element.identify();
		this.locker = new CtComponent_BlockLocker(this.element);
		this.reassembly();

		CtPage.addComponent(this);
	},


	reassembly: function() {
		this.buttons = this.element.select('a.ctCalendar-button');
		for (var i = 0; i < this.buttons.length; i++) {
			this.buttons[i].observe('click', this.clickHandler.bindAsEventListener(this, this.buttons[i]));
		}
	},


	refresh: function(response) {
		if (response && response.content) {
			this.element.update(response.content);
			this.reassembly();
		}

		this.locker.hide();
	},


	clickHandler: function(event, button) {
		Event.stop(event);
		this.locker.show();

		var href = button.readAttribute('href');
		var url = href.substr(0, href.indexOf('?'));

		var params = href.toQueryParams();

		params.ajax = 1;
		if (button.hasClassName('ctCalendar-navigation')) {
			params.navigation = 1;
		}

		var request = CtPage.getRequest(this.id, 'iframe');
		request.url = url + '?' + Hash.toQueryString(params);
		request.send();
	},

//	ajaxSuccessHandler: function(transport, json) {
//		if (json && json.ok) {
//			this.element.update(transport.responseText);
//			this.refresh();
//		}
//	},
//
//	ajaxFailureHandler: function(transport, json) {
//		console.log(transport, json);
//	},


	x: null

});


var CtCalendar_Popup = Class.create(CtCalendar, {

	isDayHover: false,
	isPopupHover: false,
	timer: null,

	reassembly: function() {
		this.buttons = this.element.select('a.ctCalendar-button');
		var body = $$('body')[0];
		for (var i = 0; i < this.buttons.length; i++) {
			this.buttons[i].observe('click', this.clickHandler.bindAsEventListener(this, this.buttons[i]));
			if (this.buttons[i].hasClassName('ctCalendar-day-notempty')) {
				var rel = eval(this.buttons[i].readAttribute('rel'));
				var div = new Element('div', {'id': 'calendar-info-' + this.buttons[i].identify(), 'class': 'ctCalendar-day-popup', 'style': 'display: none'});
				div.appendChild(new Element('div', {'class': 'ctCalendar-day-popup-corner'}));
				var divTop = new Element('div', {'class': 'ctCalendar-day-popup-top'});
				divTop.appendChild(new Element('div', {'class': 'ctCalendar-day-popup-top-left'}));
				divTop.appendChild(new Element('div', {'class': 'ctCalendar-day-popup-top-right'}));
				div.appendChild(divTop);
				var divContent = new Element('div', {'class': 'ctCalendar-day-popup-content'});
				for (var j = 0; j < rel.length; j++) {
					divContent.appendChild(new Element('a', {'href': rel[j].url, 'style': 'display: block;'}).update(rel[j].name));
				}
				div.appendChild(divContent);
				var divBottom = new Element('div', {'class': 'ctCalendar-day-popup-bottom'});
				divBottom.appendChild(new Element('div', {'class': 'ctCalendar-day-popup-bottom-left'}));
				divBottom.appendChild(new Element('div', {'class': 'ctCalendar-day-popup-bottom-right'}));
				div.appendChild(divBottom);
				body.appendChild(div);
				this.buttons[i].writeAttribute('rel', false);
				this.buttons[i].observe('mouseover', this.overHandler.bindAsEventListener(this, this.buttons[i]));
				this.buttons[i].observe('mouseout', this.outHandler.bindAsEventListener(this, this.buttons[i]));
				Event.observe(div, 'mouseover', this.popupOverHandler.bindAsEventListener(this, div));
				Event.observe(div, 'mouseout', this.popupOutHandler.bindAsEventListener(this, div));
			}
		}
	},

	clickHandler: function(event, button) {
		Event.stop(event);
		this.locker.show();

		var href = button.readAttribute('href');
		var url = href.substr(0, href.indexOf('?'));

		var params = href.toQueryParams();

		if (button.hasClassName('ctCalendar-navigation')) {
			params.navigation = 1;
			params.ajax = 1;
			var request = CtPage.getRequest(this.id, 'iframe');
			request.url = url + '?' + Hash.toQueryString(params);
			request.send();
		} else {
			location.href = href;
		}
	},

	overHandler: function(event, button) {
		this.isDayHover = true;
		if (this.timer != null) {
			clearTimeout(this.timer);
		}
		this.timer = setTimeout(this.showPopup.bind(this, button), 500);
	},

	outHandler: function(event, button) {
		this.isDayHover = false;
		if (this.timer != null) {
			clearTimeout(this.timer);
		}
		this.timer = setTimeout(this.hidePopup.bind(this, button.identify()), 500);
	},

	popupOverHandler: function(event, popup) {
		this.isPopupHover = true;
		if (this.timer != null) {
			clearTimeout(this.timer);
		}
	},

	popupOutHandler: function(event, popup) {
		this.isPopupHover = false;
		if (this.timer != null) {
			clearTimeout(this.timer);
		}
		this.timer = setTimeout(this.hidePopup.bind(this, popup.identify().replace('calendar-info-', '')), 500);
	},

	showPopup: function(button) {
		var currentIdent = button.identify();
		var ident;
		var popup;
		for (var i = 0; i < this.buttons.length; i++) {
			ident = this.buttons[i].identify();
			popup = $('calendar-info-' + ident);
			if (currentIdent == ident) {
				if (Prototype.Browser.IE) {
					popup.down('.ctCalendar-day-popup-top').setStyle({
						'width': popup.getWidth() - 30 + 'px'
					});
					popup.down('.ctCalendar-day-popup-bottom').setStyle({
						'width': popup.getWidth() - 30 + 'px'
					});
				}
				popup.setStyle({
					'display': 'block',
					'top': button.positionedOffset().top + 20 + 'px',
					'left': button.positionedOffset().left - popup.getWidth() + 32 + 'px'
				});
				popup.setOpacity(0.8);
			} else if (popup) {
				popup.setStyle({
					'display': 'none'
				});
			}
		}
	},

	hidePopup: function(ident) {
		if (!this.isDayHover && !this.isPopupHover) {
			$('calendar-info-' + ident).setStyle({
				'display': 'none'
			});
		}
	}
});

var CtCalendar_Manager = new (Class.create({

	initialize: function() {
		Event.observe(document, 'dom:loaded', function() {this.refresh()}.bindAsEventListener(this));
	},

	refresh: function() {
		var elements = $$('.ctCalendar');
		for (var i = 0; i < elements.length; i++) {
			new CtCalendar(elements[i]);
		}

		elements = $$('.ctCalendar-popup');
		for (i = 0; i < elements.length; i++) {
			new CtCalendar_Popup(elements[i]);
		}
	}

}));
CtPage.registerScript("CtCalendar");
