var ToggleMenu = Class.create({
	initialize: function(elmt) {
		this.parent = $(elmt);
		this.button = $(this.parent.childElements()[0]);
		this.child = $(this.parent.childElements()[1]);
    	this.isVisible = false;
		this.buttonPressed = false;

		this.observer = this.docUp.bindAsEventListener(this);

		this.button.observe('click', this.toggleState.bind(this));
		this.button.observe('mouseover', this.Over.bind(this));

		Event.observe(document, 'click', this.observer);
	},
	
	Over: function () {
		this.button.style.cursor = 'pointer';
	},
	
	docUp: function () {
		if (this.buttonPressed == false && this.isVisible == true) {
			this.isVisible = false;
			this.child.hide();
		}
		
		this.buttonPressed = false;
	},
	
	Up: function (event) {
		if (this.isVisible && this.buttonPressed) {
			this.isVisible = false;
			this.child.hide();
		}
		
		this.buttonPressed != this.button.Pressed;
	},
	
	Down: function (event) {
		if (this.buttonPressed) {
			this.isVisible = true;
			this.child.show();
		}

		this.buttonPressed != this.button.Pressed;
	},
	
	toggleState: function (event) {
		this.buttonPressed = true;
				
  		if (this.isVisible == false) {
			this.Down();
		} else {
			this.Up();
		}
 	}
});

document.observe('dom:loaded', function() {
	var menuObj = new ToggleMenu('filters');
});
