	var expanded = false;
	
	$(document).ready(function(){
		
		$('#pagination').show();
		
		$('#calendarList li').each(function(){			
			eventHover(this);			
		});
		
		$('.expand').toggle(
				function () {
					$('#calendarList li').each(function(){
						expanded = true;
						$('.expand').html('Colapsar Todo');
						eventExpand(this);
					});
				},
				function () {
					$('#calendarList li').each(function(){
						expanded = false;
						$('.expand').html('Expandir Todo');
						eventCollapse(this);
					});
				}
			);
		
		$('.searchButton').click(function(){
			
			loadCalendar(1);
			
		});
		
	});
	
	function loadCalendar(page){
		
		$('#calendarList .expand').hide();
		$('#calendarList .loading').show();
		$.ajax({
				type: "GET",
				url: "/eventsCalendarLoader.php?page=" + page + "&year=" + selectedYear + "&month=" + selectedMonth + "&day=" + selectedDay + "&" + $('#searchForm').serialize(),
				dataType: 'json',
				success: function(data){
					
					$('#calendarData').html(data.list);
					$('#pagination').html(data.pagination);
					
					$('#calendarList li').each(function(){			
						eventHover(this);							
					});
					
					$('.expand').toggle(
						function () {
							$('#calendarList li').each(function(){
								$('.expand').html('Colapsar Todo');
								expanded = true;
								eventExpand(this);
							});
						},
						function () {
							$('#calendarList li').each(function(){
								$('.expand').html('Expandir Todo');
								expanded = false;
								eventCollapse(this);
							});
						}
					);
					
					$('#calendarList .loading').hide();
					$('#calendarList .expand').show();				
					
				}
			});
				
	}		
	
	function eventClick(el){
		
		$(el)
			.toggle(
				function () {
					eventExpand(this);
				},
				function () {
					eventCollapse(this);
				}
			);
		
	}
	
	function eventHover(el){
				
		$(el)
			.hover(function () {
					eventExpand(this);
				}, 
				function () {
					if(!expanded){
						eventCollapse(this);
					}
				}
			);
		
	}
	
	function eventExpand(el){
		
		$(el)
			.find('.institution')
				.hide()
			.end()
			.find('.detail')
				.show()
			.end()
			.find('h3')
				.css('overflow','visible')
			.end()
			.find('.price p')
				.css('overflow','visible')
				.height('auto')
			.end();
			
	}
	
	function eventCollapse(el){
		
		$(el)
			.find('.detail')
				.hide()
			.end()
			.find('h3')
				.css('overflow','hidden')
			.end()
			.find('.price p')
				.css('overflow','hidden')
				.height('48px')
			.end()
			.find('.institution')
				.show()
			.end();
		
	}