(function($){

	var elements = new Array();
	var elements_button = new Array();
	var elements_count = 0;

	$.fn.popupmenu = function(options) {

		var defaults = {
			addStyle: false,
			speed: "",
			autooff: true
		};
		var options = $.extend(defaults, options);

		return this.each(function() {

			var button = $(this);
			var target = $('#' + button.attr('id') + '_target');

			// Register the element target
			elements[elements_count] = target;
			elements_button[elements_count] = button;
			elements_count++;

			button.hover(function(e)
			{
				if(options.autooff){
					$.each( elements, function(i, n){
						n.hide();
					});
					$.each( elements_button, function(i, n){
						n.removeClass(options.addStyle);
					});
				}
				
				if(options.addStyle != false){
					button.addClass(options.addStyle);
				}
				target.show(options.speed);
			},
			function(e)
			{
				if(options.addStyle != false){
					button.removeClass(options.addStyle);
				}
				target.hide(options.speed);
			});
		});
	};
})(jQuery);
