var ContextMenu = {
	menuFrame: null,
	menuFX: null,
	isOver: false,
	timeoutID: null,
	cursorX: 0,
	cursorY: 0,
	
	start: function() {
		var contextElems = $$('.context');

		contextElems.each(function(cElem, j) {  
			cElem.addEvent('contextmenu', function() { return false; } );
		});
		
		if ( !$('context_menu_frame') ) {
			ContextMenu.menuFrame = new Element('div', {'id': 'context_menu_frame'});
			$$('body')[0].adopt(ContextMenu.menuFrame);
		} else {
			ContextMenu.menuFrame = $('context_menu_frame');
		}
		
		ContextMenu.menuFX = new Fx.Morph(ContextMenu.menuFrame, {'duration': 1000, 'wait': false, 'transition': Fx.Transitions.Quad.easeOut});
		
		ContextMenu.menuFrame.addEvent('mouseout', function() { ContextMenu.menuOut();	});
		ContextMenu.menuFrame.addEvent('mouseover', function() { ContextMenu.menuOver();	});
	},
	
	
	doContext: function(event, elem, options, params) {
		// Update cursor position
		ContextMenu.updateCursorPosition(event);
		ContextMenu.isOver = true;

		
		// Empty any previous cMenu
		ContextMenu.menuFrame.empty();
		var contextMenuList = new Element('ul', {'id': 'context_menu_list'});
		
		
		$each(options, function(option, idx) {
			if ( option.breakRow ) {
				var list_option = ContextMenu.addItemBreak(option, idx, params);
			} else {
				var list_option = ContextMenu.addItem(option, idx, params);
			}
			
			contextMenuList.adopt(list_option);
		});
		
		
		ContextMenu.menuFrame.adopt(contextMenuList);
		
		ContextMenu.menuFrame.setStyle('opacity', 0);
		ContextMenu.menuFrame.setStyle('top', (ContextMenu.cursorY + 5) + 'px');
		ContextMenu.menuFrame.setStyle('left', (ContextMenu.cursorX + 5) + 'px');
		ContextMenu.menuFrame.setStyle('display', 'block');
		
		ContextMenu.menuFX.start({'opacity': 1});
	},
	
	
	addItemBreak: function(option, idx, params) {
		var list_option = new Element('li', {	'class': 'context_menu_break'});
		list_option.adopt(new Element('div', {	'class': 'context_menu_break'		}));
		return list_option;
	},
	
	
	addItem: function(option, idx, params) {
		var list_option = new Element('li', {	'class': 'context_menu_item'		});
		var list_icon = new Element('div', {	'class': 'context_menu_icon'		});
		var list_label = new Element('div', {	'class': 'context_menu_label'		});
		
		
		// Add icon elements
		var icon_link = new Element('a', {		'href': option.url
											})
		
		// Add Icon
		if ( option.icon ) {
			icon_link.adopt(new Element('img', {	'src': option.icon	}));
		}
		
		list_icon.adopt(icon_link);
		
		
		// Add label elements			
		list_label.adopt(new Element('a', {		'href': option.url,
												'text': option.title
											}));
		
		
		// Add FX handlers
		list_icon.fx = new Fx.Morph(list_icon, {'duration': 1000, 'wait': false, 'transition': ((params.optionTransition) ? params.optionTransition : Fx.Transitions.Quad.easeOut)});
		list_label.fx = new Fx.Morph(list_label, {'duration': 1000, 'wait': false, 'transition': ((params.optionTransition) ? params.optionTransition : Fx.Transitions.Quad.easeOut)});
		
		
		// Add mouse event handlers to LI
		list_option.addEvent('mouseout', function() {
			children = this.getChildren();
			children.each(function(child, x) {
				if ( child.fx ) {
					child.fx.start('div.' + child.className + '.inactive');
				}
			});
		});
		
		list_option.addEvent('mouseover', function() {
			children = this.getChildren();
			children.each(function(child, x) {
				if ( child.fx ) {
					child.fx.start('div.' + child.className + '.active');
				}
			});
		});

		
		
		list_option.adopt(list_icon);
		list_option.adopt(list_label);
		
		return list_option;
	},
	
	
	menuOut: function() {
		ContextMenu.isOver = false;
		
		ContextMenu.timeoutID = window.setTimeout(ContextMenu.menuFade, 500);
	},
	
	
	menuOver: function() {
		ContextMenu.isOver = true;
		
		if ( ContextMenu.timeoutID ) {
			window.clearTimeout(ContextMenu.timeoutID);
		}
	},
	
	
	menuFade: function() {
		if ( !ContextMenu.isOver ) {
			ContextMenu.menuFX.start({'opacity': 0});
			ContextMenu.timeoutID = null;
		}
	},
	
	
	updateCursorPosition: function (e) { 
		e = e || window.event;
		
		try {
			if ( e.pageX ) {
				ContextMenu.cursorX = e.pageX;
				ContextMenu.cursorY = e.pageY;
			} else if ( e.clientX ) {
					ContextMenu.cursorX = e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
					ContextMenu.cursorY = e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
			}
		} catch (err) { }
	}
	
};


window.addEvent('load', ContextMenu.start);








//
// Supporting functions
//

// Checks for a right click event
function checkRightClick(e) {
	var ieMode = 0;

	if ( navigator.appVersion.toLowerCase().indexOf('msie') != -1 ) {
		ieMode = 1;
	}
   
	if( typeof( e.which ) == 'number' ) {
		//Netscape compatible
		e = e.which;
	} else if( typeof( e.button ) == 'number' ) {
		//DOM
		e = e.button;
	} else {
		//total failure, we have no way of obtaining the button
		return;
	}

	return ( (e == 3 && !ieMode) || (e == 2 && ieMode) );
}