/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Name: jContext
 * Version: 1.0 (April 28, 2008)
 * Requires: jQuery 1.2+
 */
(function($) {
	$.fn.showMenu = function(options) {
		var opts = $.extend({}, $.fn.showMenu.defaults, options);
		showMenu_menus[showMenu_menus.length]=opts.query;
		
		$(this).bind("click",function(e){
			//Ocultar los demas
			for(var i=0; i<showMenu_menus.length; i++)
				if(showMenu_menus[i]!=opts.query) 
					$(showMenu_menus[i]).hide();
			
			$(opts.query).css({
                top:e.pageY+"px",
                left:e.pageX-$(opts.query).width()+"px",
                position:"absolute",
                opacity: opts.opacity,
                zIndex: opts.zindex
            }).toggle();
			return false;
		});
		$(document).bind("click",function(e){
			var hasParent=false;
            var parents=$(e.target).parents('*');
            for(var i=0; i<parents.length;i++){
                if( $(parents[i]).is(opts.query) ) hasParent=true;
            }
			
          /*  $(e.target).parents('*').each(function () {
				if( $(this).is(opts.query) ) hasParent=true; 
			})*/
            
            
			if(!hasParent) $(opts.query).hide();
		});
	};
	
	$.fn.showMenu.defaults = {
		zindex: 2000,
		query: document,
		opacity: 1.0
	};
	var showMenu_menus= new Array();
})(jQuery);
