YUI().use('node', 'io-base', function(Y) { 

	//Subscribe our handlers to IO's global custom events:
	Y.on('io:success', handleSuccess);
	Y.on('io:start', handleStart);
	Y.on('io:complete', handleComplete);
	Y.on('io:failure', handleFailure);
	Y.on("click", handleCalendarClick, ".event_calendar_link");

	function handleStart(ioId, o){
		//alert('start');
		var ajax_loader_path = Y.one("#ajax_loader").get('innerHTML');
		ajax_loader_image = new Image(); 
		ajax_loader_image.src = ajax_loader_path;

		Y.one("#event_calendar_content").setStyle('opacity', '0.4');
		Y.one("#event_calendar_loader").append(ajax_loader_image);
	};
	
	function handleComplete(ioId, o){
		Y.one("#event_calendar_loader").setContent('&nbsp;');
	};

	function handleFailure(ioId, o){
		//alert('failure');
		Y.one("#event_calendar_loader").setContent('<b>Wystąpiły problemy techniczne.</b>');
	};
	
	function handleSuccess(ioId, o){
		//alert('success');
		Y.one("#event_calendar_content").setStyle('opacity', '0.9');		
		Y.one("#event_calendar_content").setContent(o.responseText);
	};
	
	function handleCalendarClick(e){
		
		var has_class = Y.all(".event_calendar_li").removeClass('active');
		this.get('parentNode').addClass('active');
		
		var year = this.get("id").substr(20, 4);
		var month = this.get("id").substr(25, 2);
		var day = this.get("id").substr(28, 2);

		var uri = Y.one("#ajax_url_calendar").get('innerHTML');
	
	    var cfg = {
	        method: "POST",
		    data: "action=get_content&year=" + year + "&month=" + month + "&day=" + day, 
		    timeout: 10000, // Abort the transaction, if it is still pending, after 3000ms.
		    headers: { 'X-Transaction': 'POST Event Calendar'}
		}; 
    
	    var request = Y.io(uri, cfg); 
	};

});


