jQuery.fn.hookMenu = function(settings) {

	var defaults = {
		mode: 'auto'
	};

	settings = jQuery.extend({},defaults, settings);

	var hook_menu = [];

	var win = jQuery(window);

	this.each(function(i) {

		var $el = jQuery(this);

		hook_menu[i] = $el.next('ul.hook_menu');

		if(hook_menu[i].length) {

			hook_menu[i].css({
				'position': 'absolute',
				'display': 'none'
			});

			$el.bind('click', function(e) {

				var width = hook_menu[i].outerWidth(true);
				var height = hook_menu[i].outerHeight(true);

				  //xPos = ((e.pageX - win.scrollLeft()) + mWidth < win.width()) ? e.pageX : e.pageX - mWidth,
				  //yPos = ((e.pageY - win.scrollTop()) + mHeight < win.height()) ? e.pageY : e.pageY - mHeight;

				  var x = e.pageX;
				  var y = e.pageY;

				if ((window.innerWidth && (x > window.innerWidth/2+window.pageXOffset)) || (document.body.clientWidth && (x > document.body.clientWidth/2+document.body.scrollLeft))){
					x = x - width;
				}

				if ((window.innerHeight && (y > window.innerHeight/2+window.pageYOffset)) || (document.body.clientHeight && (y > document.body.clientHeight/2+document.body.scrollTop))) {
					y = y - height;
				}

				if (settings.mode == 'lightbox') {
					hook_menu[i].css({
						left: x,
						bottom: 20,
						width: width,
						height: height,
						zIndex: 1000
					});
				} else if (settings.mode == 'panel') {
					hook_menu[i].css({
						right: 0,
						'bottom': 20,
						'width': 150,
						'height': 'auto'
					});
				} else {
					hook_menu[i].css({
						left: 0,
						top: 0
					});
				}
				hook_menu[i].toggle();
			});

			hook_menu[i]
				.bind('click', function(e) {
					hook_menu[i].fadeOut('fast');
				});
				//.find('a').bind('mouseout', function(e) {
				//	hook_menu[i].fadeOut('fast');
				//});

		}
	});
	return this;
};
