 /*
 * Copyright (c) 2009 Gergely Hernesz
 * This is licensed under GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * For a copy of the GNU General Public License, see <http://www.gnu.org/licenses/>.
 *
*/ 
 
 (function($){
	$.fn.linkMenu = function(options){
	
		var defaults = {
			padding: 5,
			bgColor: '#ffffff',
			borderColor: '#333333',
            menuWidth: '120px'
		};
		
		var options = $.extend(defaults, options);

		var href        = '';
		var currentText = '';
        var linkText    = '';
        var menuText    = '';
        var element     = this;

		return this.each(function(){
			
				$(this).hover(
					function(over){
						href            = $(this).attr('href');
						currentText     = $(this).text();
						var w           = $(this).width() + 3;
                        var menuLinks   = '';
                        var link        = '';
                        
						$(this).removeAttr('href');
                        
                        for ( var text in options.links ) {
                            link        = $.extend({ target:'_self', alt:'', title:'' }, options.links[text]);
                            menuLinks   = menuLinks + '<p> &nbsp; <a href="'+ link.href +'" target="'+ link.target +'" title="'+ link.title +'">'+ text +'</a> </p>';
                        }

                        linkText  = '<div id="link-text" style="position:absolute; display:block; border-right:none; z-index:10; background:' + options.bgColor + '; border:' + options.borderColor + ' 1px solid; border-right:none;">' + currentText + ':</div>';
						menuText  = '<span style="width:'+options.menuWidth+'; position:absolute; top:-' + (options.padding+1) + 'px; left:' + (w+options.padding*2) + 'px; padding:' + options.padding + 'px; background:' + options.bgColor + '; border:' + options.borderColor + ' 1px solid; z-index:9;">' + menuLinks + '</span>';

                        $(this).css('position','relative');
						$(this).html(currentText + linkText + menuText);
						
						$('#link-text').css({
							top: '-' + (options.padding+1) + 'px',
							left: '-' + (options.padding+1) + 'px',
							padding: options.padding,
							paddingRight: options.padding+1,
							width: (w+options.padding)
						});
					},
								   
					function(out){
						$(this).html(currentText).attr('href',href);
					}
				);
			
		});
	
	};
	
})(jQuery);
