 /**
 * Context Menu
 * @author Scott Murphy
 * 
 * Copyright (c) 2008 Pictage Inc.
 * ALL RIGHTS RESERVED
 */
// http://forum.mootools.net/viewtopic.php?pid=43284
if ( Browser.Engine.trident )
	document.ondragstart = function() { return false; }; //IE Only (disabled image dragging)

var ContextMenu = new Class({
    initialize: function(options) {
		this.options = options;
		this.popup = new Element('div', {
			styles : { 'display': 'none' },
			'class': 'contextMenuPane',
			events : {
				'mouseleave': this.hide.bind(this)
			}
		});
		this.popup.addEvent('mouseexit', this.hide );
		this.container = new Element('div').inject(this.popup);
 
 		this.contextMenu = new Element('ul').inject( this.container );
 
 		for ( var i = 0; i < this.options.menu.length; i++ ) {
			var item = new Element('li').inject( this.contextMenu );
			var a = new Element('a', { 'text': this.options.menu[i].text, 'events': {
				'click': this.options.menu[i].action.bind(this) 
			}}).inject( item );
			a.addEvent('click', this.hide.bind(this));
 		}
		this.popup.inject(document.body);
		if ( $defined(this.options.selector) ) {
			$$(this.options.selector).each( this.addContext.bind(this) );			
		}
	},
	addContext: function( el ) {
		el.addEvent('contextmenu', function( event ) {
			event.stop();
			this.show(event, el);
		}.bind(this));		
	},
	show: function(event, source) {
		this.source = source;
		this.popup.set('styles', {
			'left': event.page.x + 'px',
			'top': event.page.y + 'px',
			'display': 'block'				
		});
	},
	hide: function() {
		this.popup.setStyle('display','none');
	},
	close: function() {
		this.cover.destroy();
		this.popup.destroy();
	}
});	