$(document).ready(function(){
	$('#contact-category > li').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	}).bind('click', contactExpand).filter('.open').trigger('click');			 
});


function contactExpand(){
	$li = $(this);
	$element=$('.jcontent', this);
	if($li.siblings().filter('.open').length){
		$li.siblings().filter('.open').find('.close').trigger('click');
	}
	$li.addClass('open');
	$element.slideDown();
	$(this).unbind('click');
	
	var index = $("#contact-category > li").index(this);
	$('#scroll-content').scrollTo('> .wrapper > li:eq('+index+')', 800);
	
	var linkClose = function(){
		var callback = function(){ $(this.parentNode).bind('click', contactExpand); }
		this.element.slideUp(callback);
		$(this).fadeOut();
		$(this.parentNode).removeClass('open');
		
		var siblingsOpen = $(this).parents('li').siblings().filter('.hover');
		if(!siblingsOpen.length){
			$('#scroll-content').scrollTo($('#offerte-e-servizi'), 800);
		}
		return false;
	}
	
	if(!this.linkClose){
		$close = $("<a class='expand' href='#' title='Chiudi'>Chiudi</a>");
		$li.prepend($close);
		$close[0].element = $element;
		$close.bind('click', linkClose);
		this.linkClose=true;
	}
	$('.expand', this).fadeIn();
}