function replaceAll(txt, replace, with_this) {
  return txt.replace(new RegExp(replace, 'g'),with_this);
}

jQuery.fn.meganau = function(options){
	
	var options = jQuery.extend({
	    target: '_blank', // куда открывать
	  },options);
	  
	jQuery('body').append('<div id="meganau_popup"></div>');
	jQuery('#meganau_popup')
	.css({
		'position' : 'absolute',
		'z-index' : '1001',
		'border' : '1px solid #1155a6',
		'padding' : '4px 6px 4px 6px',
		'background' : 'url(/meganau/nau_centre.jpg) repeat-x #85a9d9',
		'color' : '#0e3668',
		'font-size' : '14px'

	})
	.hide();
	jQuery('#meganau_popup a').css({'color' : '#0e3668 !important', 'text-decoration' : 'none !important'});
	
	jQuery('body').append('<div id="meganau_instr" class=""></div>');
	jQuery('#meganau_instr')
	.css({
		'position' : 'fixed',
		'z-index' : '1000',
		'border' : '0',
		'background' : 'url(/meganau/podskazka-ru.gif) no-repeat #1a4886',
		'bottom' : '0',
		'right' : '0',
		'width' : '109px',
		'height' : '29px',
		'color' : '#ffffff',
		'font-size' : '12px',
		'padding' : '3px 100px 3px 5px'
		
	}).bind('click',function(){
		jQuery('#meganau_instr').hide();
	});

	return this.each(function(){
		

		jQuery(this).mouseup(function(e){

			if (jQuery('#meganau_popup').is(':hidden')) {

				var selected_text = (

					(window.getSelection && window.getSelection()) ||

					(document.getSelection && document.getSelection()) ||

					(document.selection && document.selection.createRange && document.selection.createRange().text)

				);
				
				//var encoded = encodeURIComponent(selected_text.toString());
				//alert('http://zakon.nau.ua/search/?word=' + encodeURIComponent(selected_text.toString()));
				
				if (selected_text.toString().length > 3){
				
					jQuery.get('/nau_convert_word.php',
						{word : selected_text.toString()},
						function(responce){
							//alert(responce);
							jQuery('#meganau_popup').html('<a target="_blank" href="http://zakon.nau.ua/search/?word=' + responce + '">Искать фразу <b>&laquo;' + selected_text.toString() + '&raquo;</b></a> в <a target="_blank" href="http://zakon.nau.ua/search/?word=' + responce + '"><img align="center" valign="middle" src="/meganau/nau-logotip.png" alt="МЕГА-НАУ"></a>');							
							
							jQuery('#meganau_popup')
							.css({
								'left' : parseInt(e.pageX) + 7 + 'px',
								'top' : parseInt(e.pageY) + 7 + 'px'
							})
							.show();
						});

				}

			}

			else { jQuery('#meganau_popup').hide(); }

		});
		
		
		
		
	
	});

}
