/**************************************************************

	Script		: Menu
	Version		: 1.0
	Authors		: Leonard Martin
	Desc		: Creates a drop-down menu from a link

**************************************************************/

//start menu class
var Menu = new Class({

//implements
	Implements: [Options],
	
	//options
	options:{
		speed: 200,
		links:[]
	},

	//initialization
	initialize: function(trigger,options)
	{
		//set options
		this.setOptions(options);
		
		trigger.addEvent(
			'click',
			function(e){
				e.stop();
				this.toggle();
			}.bind(this)// we want 'this' to be our Menu object
		)
		
		this.trigger = trigger;
		this.isOpen = false;
		
	},
	
	toggle: function() {
		this.isOpen?this.close():this.open();
	},
	
	open: function() {
		if(!this.menu) {
			this.build();
		}
		this.isOpen = true;
		
		this.fade.start({
			opacity: 1
		});
	},
	
	close: function() {
		this.isOpen = false;
		this.fade.start({
			opacity: 0
		});
	},
	
	build: function() {
		
		var pos = this.trigger.getPosition();
		
		var container = new Element('div', {
				'class': 'menudrop',
				'styles': {
					position: 'absolute',
					listStyle: 'none',
					display: 'block',
					top:pos.y+'px',
					left:pos.x+'px',
					zIndex: 100,
					opacity:0
				}
			}),
			inner = new Element('div', {
				'class': 'menudrop-inner'
			}),
			list = new Element('ul'),
			doc = $(document.body).addEvent('click',
				function(e){
					this.close();
				}.bind(this)
			)
			
		this.options.links.each(
			function(link) {
				var li = new Element('li',{
					'html':link
				}).addEvent('click',
					function(e){
						e.stopPropagation();
					}
				);
				list.grab(li);
			}
		);
		
		this.menu = container.grab(inner.grab(list)).inject(doc);
		this.fade = new Fx.Morph(this.menu,{duration:this.options.speed});
		
	}
	
});