(function($){
    $.fn.extend({ 
    	langMenu: function(options) {

            var defaults = {
                menuId: "",
                buttonId : "",
                timeout : -1
            }
                 
            var options =  $.extend(defaults, options);
 
            return this.each(function() {
                
				this.opened = false;

				this.mouseLeave = function() {
					var owner = this;
					options.timeout = setTimeout(function() { 
						owner.close(); 
					}, 250 );
				}
				
				this.open = function() {
					clearTimeout(options.timeout);
					if( opened == false ) {
						var buttonElement = $("#"+options.buttonId);
						var menuElement = $("#"+options.menuId);
						menuElement.css("display","");
						menuElement.css("left", buttonElement.offset().left - menuElement.width()/2 + buttonElement.width()/2 );
						menuElement.css("top", buttonElement.offset().top + buttonElement.height() + 12 );
					}
					opened = true;
				}
				
				this.close = function() {
					var menuElement = $("#"+options.menuId);
					menuElement.css("display","none");
					opened = false;
				}
				
				this.close();
				
				var buttonElement = $("#"+options.buttonId);
				buttonElement.click( function() {this.open();} );
				buttonElement.mouseenter(function() {if( opened == true ) {this.open();}});
				buttonElement.mouseleave( function() {this.mouseLeave();} );
				
				var menuElement = $("#"+options.menuId);
				var owner = this;
				menuElement.mouseenter(function() {owner.open();});
				menuElement.mouseleave(function() {owner.mouseLeave();});	
				
            });
        }
    });
})(jQuery);
