window.addEvent('domready', function(e) {
	$$('#circonscription a.item').each(function(el, index) {
			if (window.console) console.log('Found A ' +  el.id);
			el.store('opened', false);
			var ul = el.getParent().getFirst('ul');
			el.store('height', ul.getSize().y);
			ul.setStyles({display: 'none', height: '0px', opacity: 0});
			el.set('morph', {duration: 'short', transition: Fx.Transitions.Quad.easeIn});
			el.addEvent('mousedown', mouseDownSideBarItem.bind(el));
	});
});	

var mouseDownSideBarItem = function(e) {
	if (window.console) console.log('mouseDownSideBarItem ' +  this.id);
	if (this.retrieve('opened')) {
		closeSideBarItem.bind(this).run();
	}
	else {
		closeAllsideBarItem();
		openSideBarItem.bind(this).run();
	}
}

var openSideBarItem = function() {
	if (window.console) console.log('openSideBarItem ' +  this.id);
	this.store('opened', true);
	var ul = this.getParent().getFirst('ul');
	
	ul.setStyles({ display: 'block'});
	ul.morph({height: this.retrieve('height'), opacity: 1});
}

var closeSideBarItem = function() {
	if (window.console) console.log('closeSideBarItem ' +  this.id);
	this.store('opened', false);
	var ul = this.getParent().getFirst('ul');
/*	ul.setStyle.delay(700, ul, ['display', 'none']);*/
	ul.morph({height: 0, opacity: 0});
}

var closeAllsideBarItem = function() {
	$$('#circonscription a.item').each(function(el, index) {
		 if (el.retrieve('opened')) {
		   closeSideBarItem.bind(el).run();				 
		 }
	});
}