var expanded = false;

$(document).ready(function(){
	$('#calendar-container li').not('#calendar-container li:last').each(function(){			
		eventHover(this);			
	});
	
	$('#calendar-container li:last').hover(function(){			
		eventExpand(this);
	});
	
	$('.expand').toggle(function () {
			$('#calendar-container li').each(function(){
				expanded = true;
				$('.expand').html('<img alt="Colapsar Todo" src="/img/layout/facebook/collapse.jpg">');
				eventExpand(this);
			});
		},
		function () {
			$('#calendar-container li').each(function(){
				expanded = false;
				$('.expand').html('<img alt="Expandir todo" src="/img/layout/facebook/expand.jpg">');
				eventCollapse(this);
			});
		}
	);	
	
	$("#calendar-container ul li").each(function(){
		var initialHeight = $(this).children(".info").height();
		$(this).children(".hour").css("height",initialHeight);
		$(this).children(".price").css("height",initialHeight);
	});
});

function eventHover(el){
	$(el)
		.hover(function () {
				eventExpand(this);
				eventCollapse($('#calendar-container li:last'));
			}, 
			function () {
				if(!expanded){
					eventCollapse(this);
				}
			}
		);
}

function eventExpand(el){
	$(el)
		.find('.resume .institution')
			.hide()
		.end()
		.find('.detail')
			.show()
		.end()
		.find('.price')
			.css('overflow','auto')
		.end()
		.children('.price')
			.height($(el).children('.info').height())
		.end()
		.children('.hour')
			.height($(el).children('.info').height())
		.end();
}

function eventCollapse(el){
	$(el)
		.find('.detail')
			.hide()
		.end()
		.find('.resume .institution')
			.show()
		.end()
		.children('.price')
			.height($(el).children('.info').height())
		.end()
		.find('.price')
			.css('overflow','hidden')
		.end()
		.children('.hour')
			.height($(el).children('.info').height())
		.end();
}
