var Dropdowns = {
	initialized : false,
	menus : null,
	clicked : null,
	selected : null,
	tweening : false,
	
	_click : null,
	
	initialize : function(selector) {
		if (!this.initialized) {
			this.menus = $$(selector);
			this._toggle = this.toggle.bindAsEventListener(this);
			this._stopEvent = this.stopEvent.bindAsEventListener(this);
			this._setState = this.setState.bindAsEventListener(this);
			this.createEvents();
			this.initialized = true;
		}
	},
	
	createEvents : function() {
		for (var i=0, c=this.menus.length; i<c; i++) {
			var ul = this.menus[i];
			ul.up('div').show();
			if (!window.SimpleCMSMenu) {
				var li = $(ul.parentNode);
				var newA = $(document.createElement('a'));
				newA.innerHTML = '&nbsp;';
				newA.addClassName('arrow');
				var a = li.down('a');
				newA.setStyle({right: a.getWidth() - 15 + 'px'});
				li.insertBefore(newA, li.firstChild);
				var count = ul.childElements().length;
				var div = $(document.createElement('div'));
				ul.parentNode.appendChild(div);
				div.appendChild(ul);
				div.setStyle({
					height: ul.getHeight() + 10 + 'px'
				});			
				ul = div;
				li.ul = ul;
				ul.li = li;
				ul.hide();
				if (li.hasClassName('selected')) {
					Effect.toggle(ul, 'slide', {duration: .6, afterFinish: this._setState});
				};
				newA.observe('mousedown', this._toggle);
				newA.observe('click', this._stopEvent);
				newA.observe('mouseup', this._stopEvent);
			}
		}
	},
	
	toggle : function(event) {
		if (!this.tweening) {
			var li = Event.findElement(event, 'li');
			Event.stop(event);
			this.tweening = true;
			this.clicked = li;
			var ul = li.ul;
			Effect.toggle(ul, 'slide', {duration: .6, afterFinish: this._setState})
			li.toggleClassName('selected');
		}
	},
	
	setState : function() {
		this.tweening = false;
	},
	
	stopEvent : function(event) {
		Event.stop(event);	
	}
}
Event.observe(window, 'load', function() {
	Dropdowns.initialize('.column_left ul ul');
});