/**
*	Oggetto globale con variabili ad uso globale
*/
G_NOSEL = false;
G_JQT = {
	'lang':					'it',
	'mouse_left':			null,
	'mouse_top':			null,
	'selttip_timeout':		null,
	'selttip_timeout_secs':	null,
	'min_chars':			3,
	'max_words':			7,
	'site':					'pat_generale',			/* ------------ GSA :: SITE ------------ */
	'client':				'pat_it',				/* ------------ GSA :: CLIENT------------ */
	'popupwin':				'#jqt_popupwin',
	'text_block_selector':	'p',//'.blocco_testo',//'.contenitore',//
	'searchText':			'#q:eq(0)',
	'searchButton':			'.bot_vai_img',
	'siteURL': '',
	'urlBase': '',
	'tunnelScript': '',
	'urlSearch': '',
	'urlselected':			'online',
	'urlconfig':			{
								'sviluppo':	{
									'siteURL':				'http://sviluppo2.od.loc/pat_portale/',
									'urlBase':				'http://sviluppo2.od.loc/ricerca_pat/',
									'tunnelScript':			'http://sviluppo2.od.loc/pat_portale/tunnel.php',
									'urlSearch':			'http://sviluppo2.od.loc/ricerca_pat/?q={term}'
								},
								'anteprime':	{
									'siteURL':				'http://anteprime.provincia.tn.it/pat_portale/',
									'urlBase':				'http://anteprime.provincia.tn.it/ricerca_new/',
									'tunnelScript':			'http://anteprime.provincia.tn.it/pat_portale/tunnel.php',
									'urlSearch':			'http://anteprime.provincia.tn.it/ricerca_new/?q={term}'
								},
								'online':	{
									'siteURL':				'http://www.provincia.tn.it/',
									'urlBase':				'http://search.provincia.tn.it/',
									'tunnelScript':			'http://www.provincia.tn.it/tunnel.php',
									'urlSearch':			'http://search.provincia.tn.it/?q={term}'
								}
							},
	'labels':	{
					'it':	{
								'risultati':		'Risultati',
								'di_circa':			'di circa',
								'per':				'per',
								'ordina_per':		'Ordina per',
								'ordina_data':		'data',
								'ordina_rilevanza':	'rilevanza',
								'chiudi_finestra':	'Chiudi la finestra',
								'visualizza_tutto':	'Visualizza tutti i risultati',
								'click_info':		'Clicca per cercare informazioni su',
								'errore_ajax':		'Condizione di errore comunicazione AJAX',
								'errore':			'Errore',
								'mime_txt':			'Documento di testo',
								'mime_rtf':			'Documento Rich Text',
								'mime_pdf':			'Documento PDF',
								'mime_ps':			'Documento PostScript',
								'mime_ppt':			'Presentazione di Powerpoint',
								'mime_xls':			'Foglio di Excel',
								'mime_doc':			'Documento di Word',
								'mime_zip':			'Archivio Compresso',
								'mime_mp4':			'File Video',
								'mime_mp3':			'File Audio',
								'mime_html':		'Pagina HTML',
								'min_caratteri':	'Si devono inserire almeno {min_chars} caratteri',
								'nessun_risultato':	'Nessun risultato',
								'puoi_provare_anche': 'puoi provare anche',
								'in_evidenza':		'in evidenza'
							},
					'en':	{
								'risultati':		'Results',
								'di_circa':			'of about',
								'per':				'for',
								'ordina_per':		'Ordina by',
								'ordina_data':		'date',
								'ordina_rilevanza':	'relevance',
								'chiudi_finestra':	'Close window',
								'visualizza_tutto':	'See all results',
								'click_info':		'Click to get informations on',
								'errore_ajax':		'AJAX communication error',
								'errore':			'Error',
								'mime_txt':			'Text document',
								'mime_rtf':			'Rich Text document',
								'mime_pdf':			'PDF document',
								'mime_ps':			'PostScript document',
								'mime_ppt':			'Powerpoint presentation',
								'mime_xls':			'Excel sheet',
								'mime_doc':			'Word document',
								'mime_zip':			'Compressed archive',
								'mime_mp4':			'Video file',
								'mime_mp3':			'Audio file',
								'mime_html':		'HTML page',
								'min_caratteri':	'You can search at least {min_chars} characters',
								'nessun_risultato':	'No results',
								'puoi_provare_anche': 'you can try with',
								'in_evidenza':		'highlight'
							}
				}
}


jQuery.noConflict();
jQuery(document).ready(function(){
	/*
	// ricava la lingua da usare
	var classes = ''+jQuery('.jq_lang:eq(0)').get(0).className;
	var splt = classes.split('jq_lang jq_lang_');
	if (splt.length > 1) {
		G_JQT.lang = splt[1];
	}
	*/
	
	/* URL INIT */
	if ( (''+location.href).indexOf('sviluppo2.') >= 0 ) {
		G_JQT['urlselected'] = 'sviluppo';
	}
	else if ( (''+location.href).indexOf('anteprime.') >= 0 ) {
		G_JQT['urlselected'] = 'anteprime';
	}
	G_JQT['siteURL'] = G_JQT['urlconfig'][G_JQT['urlselected']]['siteURL'];
	G_JQT['urlBase'] = G_JQT['urlconfig'][G_JQT['urlselected']]['urlBase'];
	G_JQT['tunnelScript'] = G_JQT['urlconfig'][G_JQT['urlselected']]['tunnelScript'];
	G_JQT['urlSearch'] = G_JQT['urlconfig'][G_JQT['urlselected']]['urlSearch'];
	
	/* LABEL INIT */
	jQuery('.jq_textip').each(function(){
		var text = '';
		var splt1 = (''+jQuery(this).html()).split('{{');
		for(var i1=0; i1<splt1.length; i1++) {
			if (splt1[i1].indexOf('}}') < 0) {
				text += splt1[i1];
			}
			else {
				var splt2 = splt1[i1].split('}}');
				for(var i2=0; i2<splt2.length; i2++) {
					if (i2 % 2 == 0) {
						text += G_JQT['labels'][G_JQT['lang']][splt2[i2]];
					}
					else {
						text += splt2[i2];
					}
				}
			}
		}
		jQuery(this).html(text);
	});
	
	/* layout fixes */
	if (jQuery.browser.mozilla) {
		jQuery('.jqt_showall').css({
			'margin-top':	0
		});
	}
	if (jQuery.browser.msie) {
		if (jQuery.browser.version == '6.0' || jQuery.browser.version == '7.0') {
			jQuery('.jquicksearch_pad').css({
				'padding-left':		'0px'
			});
			jQuery('.jqt_subtitle_text_quicksearch').css({
				'padding-left':		'26px'
			});
			jQuery('.jqt_showall').css({
				'margin-top':	'-1px'
			});
		}
		if (jQuery.browser.version == '8.0') {
			jQuery('.jqt_showall').css({
				'margin-top':	'-1px'
			});
		}
	}
	pngFix();
	
	/* synonyms */
	jQuery('a.synlink').live('click',function(){
		var qry = '';
		if (jQuery.browser.msie) {
			if (jQuery.browser.version == '6.0' || jQuery.browser.version == '7.0') {
				qry = (''+jQuery(this).attr('href')).split('#')[1];
			}
		}
		if (qry == '') {
			qry = (''+jQuery(this).attr('href')).replace('#','');
		}
		jQuery('#q').val(qry);
		jQuery('#jquicksearch')
			.data('link',G_JQT['urlSearch'].replace('{term}', encodeURI(qry)))
			.data('selection', qry);	// salvo il testo cercato
		ajaxRetrieveData(qry, 'date:D:L:d1', '#jquicksearch_results');
		return false;
	});
});

/**
*	Funzione che restituisce la selection corrente
*/
function getSelected(){
	var w=window,d=document,gS='getSelection';
	return(''+(w[gS]?w[gS]():d[gS]?d[gS]():d.selection.createRange().text)).replace(/(^\s+|\s+$)/g,'');
}
/* funzione di detect dei mime sui risultati */
function mimeSelect(mime) {
	var mimeIco = '';
	var mimeImgAlt = '';
	if (mime == 'text/plain') {
		mimeIco = 'ico_txt.png';
		mimeImgAlt = G_JQT.labels[G_JQT.lang].mime_txt;
	}
	else if (mime == 'application/rtf') {
		mimeIco = 'ico_word.png';
		mimeImgAlt = G_JQT.labels[G_JQT.lang].mime_rtf;
	}
	else if (mime == 'application/pdf') {
		mimeIco = 'ico_pdf.png';
		mimeImgAlt = G_JQT.labels[G_JQT.lang].mime_pdf;
	}
	else if (mime == 'application/postscript') {
		mimeIco = 'ico_ps.png';
		mimeImgAlt = G_JQT.labels[G_JQT.lang].mime_ps;
	}
	else if (mime == 'application/vnd.ms-powerpoint') {
		mimeIco = 'ico_ppt.png';
		mimeImgAlt = G_JQT.labels[G_JQT.lang].mime_ppt;
	}
	else if (mime == 'application/vnd.ms-excel') {
		mimeIco = 'ico_excel.png';
		mimeImgAlt = G_JQT.labels[G_JQT.lang].mime_xls;
	}
	else if (mime == 'application/msword') {
		mimeIco = 'ico_word.png';
		mimeImgAlt = G_JQT.labels[G_JQT.lang].mime_doc;
	}
	else if (mime == 'application/vnd.ms-powerpoint') {
		mimeIco = 'ico_ppt.png';
		mimeImgAlt = G_JQT.labels[G_JQT.lang].mime_ppt;
	}
	else if (mime == 'application/zip') {
		mimeIco = 'ico_zip.png';
		mimeImgAlt = G_JQT.labels[G_JQT.lang].mime_zip;
	}
	else if (mime == 'video/mpeg') {
		mimeIco = 'ico_video_MP4.png';
		mimeImgAlt = G_JQT.labels[G_JQT.lang].mime_mp4;
	}
	else if (mime == 'audio/mpeg') {
		mimeIco = 'ico_mp3.png';
		mimeImgAlt = G_JQT.labels[G_JQT.lang].mime_mp3;
	}
	else {
		mimeIco = 'ico_html.png';
		mimeImgAlt = G_JQT.labels[G_JQT.lang].mime_html;
	}
	return {
		"mimeIco":		mimeIco,
		"mimeImgAlt":	mimeImgAlt
	};
}
/* GLOBALE */
G_dataURI = '';
/* Funzione per chiamata AJAX dei risultati e render */
function ajaxRetrieveData(selection, sort, elemSel) {
	jQuery('.bt-wrapper').remove(); // rimuovo eventuali tooltip
	var dropDownMode = true;
	if (elemSel != '#jquicksearch_results') {
		dropDownMode = false;
	}

	var extra = '';
	/*if (jQuery('.tipo_utente_comune_comune').length > 0) {
		extra = '&requiredfields=servizio_comune:'+jQuery('.tipo_utente_comune_comune span:eq(0)').text();//.replace('(','').replace(')','')
	}*/
	//alert(encodeURI("q="+selection+'&lr=lang_'+G_JQT.lang+'&site='+G_JQT['site']+"&output=xml_no_dtd&sort="+sort+extra));
	var dataURI = encodeURI("q="+selection+'&lr=lang_'+G_JQT.lang+'&site='+G_JQT['site']+"&client="+G_JQT['client']+"&output=xml_no_dtd&sort="+sort+extra);
	G_dataURI = dataURI;
	var elementoContenuti = elemSel;
	//alert(tunnelScript+'___'+dataURI);
	jQuery.ajax({
		"type":		"POST",
		"url":		G_JQT['tunnelScript'], /* chiamata fatta al tunnel PHP in modo da evitare problemi di crossdomain */
		"data":		dataURI,
		"dataType":	"xml",//"html",
		"success":	function(xml){ //alert(xml);//alert(jQuery(xml).text());
						if (jQuery('RES', xml).length > 0) {
							jQuery('.jqt_vis_all').show();
							var contenuti = '';
							
							jQuery('.jqt_synonyms').text('');
							jQuery('.jq_textip').removeClass('jq_with_related');
							if (jQuery('Synonyms', xml).length > 0) {
								jQuery('.jq_textip').addClass('jq_with_related');
								jQuery('Synonyms>OneSynonym', xml).each(function(){
									///alert(jQuery(this).text());
									var url = '#'+jQuery(this).text();
									if (jQuery('.jqt_synonyms').text() != '') {
										jQuery('.jqt_synonyms').append(', ');
									}
									jQuery('.jqt_synonyms').append('<a class="synlink" href="'+url+'">'+jQuery(this).text()+'</a>');
								});
							}

							if (jQuery('GM', xml).length > 0) {
								jQuery('GM', xml).each(function(){
									var desc = jQuery(this).find('GD').text();
									var link = jQuery(this).find('GL').text();
									//alert(desc+"\n"+link);
									contenuti += '<div style="margin:2px 5px;padding:3px;background:#F2F2F2;font-size:0.7em;">';
									 contenuti += '<div style="float:right;text-align:right;color:#B6B6C6;">';
									  contenuti += G_JQT['labels'][G_JQT['lang']]['in_evidenza'];
									 contenuti += '</div>';
									 contenuti += '<div style="float:left;text-align:left;">';
									  contenuti += '<div style="color:#7A92A2;"><a href="'+link+'">'+desc+'</a></div>';
									  contenuti += '<div style="color:#8A0B16;">'+link+'</div>';
									 contenuti += '</div>';
									 contenuti += '<div class="clear">&nbsp;</div>';
									contenuti += '</div>';
								});
							}

							if ( ! dropDownMode ) {
								jQuery('#jqt_startnum').html(jQuery("RES", xml).attr('SN'));
								jQuery('#jqt_endnum').html(Math.min(jQuery("RES", xml).attr('EN')-0,jQuery("M", xml).text()-0));
								jQuery('#jqt_results').html(jQuery("M", xml).text());
								jQuery('#jqt_question').html(jQuery("Q", xml).text());
							}
							var url,title,subtitle;
							jQuery('RES>R', xml).each(function(){ /* SCORRO gli elementi R dell'XML ricevuto dalla GSA */
								url = jQuery(this).find('U').text();
								title = jQuery(this).find('T').text();
								subtitle = jQuery(this).find('S').text();
								var mime = jQuery.trim(jQuery(this).attr('MIME'));
								var mimeObj = mimeSelect(mime);
								var mimeIco = mimeObj['mimeIco'];
								var mimeImgAlt = mimeObj['mimeImgAlt'];
								var mimeImg = '';
								if (mimeIco != '') {
									mimeImg = '<img title="'+mimeImgAlt+'" alt="'+mimeImgAlt+'" class="jqt_result_image" src="'+G_JQT['siteURL']+'js/textip/images/mimes/'+mimeIco+'" width="16" height="16" />';
								}
								subtitle = subtitle.split('<br>').join('').split('- ').join('');
								contenuti += '<div class="jqt_result_block"><div class="jqt_result_block_int">'+mimeImg+'<div class="jqt_result_title"><a class="jqt_result_title_link" href="'+url+'">'+title+'</a></div><div class="jqt_result_body">'+subtitle+'</div></div></div>';
							});
							jQuery(elementoContenuti).html(contenuti);

							setTimeout(function(){
								if ( ! dropDownMode ) {
									jQuery(G_JQT.popupwin).show();
									jQuery('#jqt_contents,#jquicksearch_results').scrollTop(0); /* reinizializzo la barra di scroll */
									jQuery('.jqt_close').one('click', function(){
										jQuery(G_JQT.popupwin).hide();
									});
									jQuery('.jqt_showall').one('click', function(){
										location.href = G_JQT['urlBase']+'?'+G_dataURI;
									});
								}
								else {
									jQuery('#jquicksearch_results').css({
										'background': 'white'
									});
									jQuery('.jqt_footer_dropdown').fadeIn();
									jQuery('#jquicksearch_results').scrollTop(0); /* reinizializzo la barra di scroll */
									jQuery('.jqt_close').one('click', jqtCloseDropDown);
									jQuery('.jqt_showall').one('click', function(){
										location.href = G_JQT['urlBase']+'?'+G_dataURI;
									});
									jQuery(G_JQT['searchText']).one('focus', function(){
										jqtCloseDropDown();
									});
									jQuery('#site').one('change', function(){
										jqtCloseDropDown();
									});
								}
							},500);
						}
						else { /* NO RESULTS */
							jQuery('.jqt_vis_all').hide();
							jQuery(elementoContenuti).html('<div class="jqt_noresults">'+G_JQT.labels[G_JQT.lang].nessun_risultato+'</div>');
							jQuery('#jqt_startnum').html('0');
							jQuery('#jqt_endnum').html('0');
							jQuery('#jqt_results').html('0');
							jQuery('#jqt_question').html(selection);
							setTimeout(function(){
								if ( ! dropDownMode ) {
									jQuery(G_JQT.popupwin).show();
									jQuery('#jqt_contents,#jquicksearch_results').scrollTop(0); /* reinizializzo la barra di scroll */
									jQuery('.jqt_close').one('click', function(){
										jQuery(G_JQT.popupwin).hide();
									});
									jQuery('.jqt_showall').one('click', function(){
										location.href = G_JQT['urlBase']+'?'+G_dataURI;
									});
								}
								else {
									jQuery('#jquicksearch_results').css({
										'background': 'white'
									});
									jQuery('.jqt_footer_dropdown').fadeIn();
									jQuery('#jquicksearch_results').scrollTop(0); /* reinizializzo la barra di scroll */
									jQuery('.jqt_close').one('click', jqtCloseDropDown);
									jQuery('.jqt_showall').one('click', function(){
										location.href = G_JQT['urlBase']+'?'+G_dataURI;
									});
									jQuery(G_JQT['searchText']).one('focus', function(){
										jqtCloseDropDown();
									});
									jQuery('#site').one('change', function(){
										jqtCloseDropDown();
									});
								}
							},500);
						}
						if ( ! dropDownMode ) {
							clearTimeout(G_JQT.selttip_timeout);
							jQuery('#jqt_tipcall').hide();
						}
					},
		"error":	function(xhr, status, err){
						alert(G_JQT.labels[G_JQT.lang].errore_ajax+": "+status+"\n\n"+G_JQT.labels[G_JQT.lang].errore+": "+err);
					}
	});
}

function jqtCloseDropDown() {
	jQuery('#jquicksearch_shadow_bottom').fadeOut(0);
	jQuery('#jquicksearch_content').animate(
		{
			'top': '-362px'
		},{
			'duration':	1000,
			'complete':	function(){
							jQuery('#jquicksearch').css({
								'height': '0px'
							});
							jQuery('#jquicksearch_results').css({
								'background': 'transparent'
							});
							jQuery('#jquicksearch_results').html('');
							
							//jQuery('#jquicksearch').remove(); // #############
						}
		}
	);
}
/**
*	Recupero AJAX dei risultati corrispondenti alla ricerca "selezionata"
*/
function ajaxRetrieveResults(sort) {
	var where = '#jqt_contents,#jquicksearch_results';
	jqtCloseDropDown();
	var selection = jQuery(G_JQT.popupwin).data('selection'); /* dentro #jqt_popupwin, nella variabile data con nome "selection" viene salvata la stringa da cercare */
	jQuery(where).html('<img class="jqt_loader_image" src="'+G_JQT['siteURL']+'js/textip/images/jqt_loader.gif" />');
	jQuery('.jqt_order_selected').removeClass('jqt_order_selected');
	if (sort=='date:D:L:d1') {
		jQuery('.jqt_order_relevance').addClass('jqt_order_selected');
	}
	else {
		jQuery('.jqt_order_date').addClass('jqt_order_selected');
	}
	ajaxRetrieveData(selection, sort, where); /* CHIAMATA AJAX */
	imposta_click_ordinamento();
}
function imposta_click_ordinamento() {
	jQuery('.jqt_order_date,.jqt_order_relevance').each(function(){
		jQuery(this)
			.css({
				'cursor':	'pointer'
			})
			.one('click', function(){
				if ( ! jQuery(this).hasClass('jqt_order_selected') ) {
					tipcallTimeout();
					jQuery('.jqt_order_selected').removeClass('jqt_order_selected');
					jQuery(this).addClass('jqt_order_selected');
					if (jQuery(this).hasClass('jqt_order_date')) {
						ajaxRetrieveResults('date:D:S:d1');		/* CHIAMATA AJAX - sulle opzioni nella popup */
					}
					else {
						ajaxRetrieveResults('date:D:L:d1');		/* CHIAMATA AJAX - sulle opzioni nella popup */
					}
				}
			});
	});
}
/**
*	Recupero AJAX dei risultati corrispondenti alla ricerca in tendina
*/
function ajaxRetrieveResults_quicksearch(sort) {
	var where = '#jquicksearch_results';
	var selection = jQuery('#jquicksearch').data('selection'); /* dentro #jqt_popupwin, nella variabile data con nome "selection" viene salvata la stringa da cercare */
	jQuery(where).html('<img class="jqt_loader_image_quicksearch" src="'+G_JQT['siteURL']+'js/textip/images/jqt_loader.gif" />');
	jQuery('.jqt_order_selected').removeClass('jqt_order_selected');
	if (sort=='date:D:L:d1') {
		jQuery('.jqt_order_relevance').addClass('jqt_order_selected');
	}
	else {
		jQuery('.jqt_order_date').addClass('jqt_order_selected');
	}
	ajaxRetrieveData(selection, sort, where); /* CHIAMATA AJAX */
	imposta_click_ordinamento_quicksearch();
}

/**
 *
 */
function imposta_click_ordinamento_quicksearch() {
	jQuery('.jqt_order_date,.jqt_order_relevance').each(function(){
		jQuery(this)
			.css({
				'cursor':	'pointer'
			})
			.one('click', function(){
				if ( ! jQuery(this).hasClass('jqt_order_selected') ) {
					tipcallTimeout();
					jQuery('.jqt_order_selected').removeClass('jqt_order_selected');
					jQuery(this).addClass('jqt_order_selected');
					if (jQuery(this).hasClass('jqt_order_date')) {
						ajaxRetrieveResults_quicksearch('date:D:S:d1');		/* CHIAMATA AJAX - sulle opzioni nella popup */
					}
					else {
						ajaxRetrieveResults_quicksearch('date:D:L:d1');		/* CHIAMATA AJAX - sulle opzioni nella popup */
					}
				}
			});
	});
}

/* FIX PNG 32 per IE6 */
function pngFix() {
	if (jQuery.browser.msie && jQuery.browser.version == '6.0') { /* RICHIEDE DD_belatedPNG */
		DD_belatedPNG.fix('.png_fix');
	}
}

/**
*	Inizializzazione globali di timeout e funzione di timeout per il tooltip
*/
G_JQT.selttip_timeout = null;
G_JQT.selttip_timeout_secs = 10;
function tipcallTimeout(){
	clearTimeout(G_JQT.selttip_timeout);
	G_JQT.selttip_timeout = setTimeout(function(){
/////// ######		jQuery('#jqt_tipcall').hide();
	}, G_JQT.selttip_timeout_secs * 1000);
}

/**
*	Inizializzazioni jQuery e globali usate
*/
jQuery.noConflict();
jQuery(document).ready(function() {
	//if (jQuery(G_JQT.popupwin).length <= 0) {
		var classTip = '_'+G_JQT.lang;
		jQuery('#jqt_tipcall').addClass('jqt_tipcall'+classTip);
		pngFix();
	//}
	if (jQuery(G_JQT['text_block_selector']).length > 0) {
		jQuery(G_JQT.popupwin).draggable({ /* DRAGGABILE */
			'containment':		'document',
			'handle':			'.jqt_handle',
			'cursor':			'move'
		});
	}
	jQuery(G_JQT['text_block_selector']).mousedown(function(e) { /* INIZIO SELEZIONE */
		/*for(var i in e)if(!confirm(i+"\n"+e[i]))break;*/
		var nodoSelezionato = (''+e.target.nodeName).toLowerCase();
		if (nodoSelezionato=='input' || nodoSelezionato=='textarea') { /* evito la selezione in campi form testuali [selezione con trascinamento] */
			G_NOSEL = true;
		}
		G_JQT.mouse_top  = e.pageY -0;
		G_JQT.mouse_left = e.pageX -0;
	});
	jQuery(G_JQT['text_block_selector']).mouseup(function(e) { /* FINE SELEZIONE */
		var nodoSelezionato = (''+e.target.nodeName).toLowerCase();
		if (nodoSelezionato=='input' || nodoSelezionato=='textarea') { /* evito la selezione in campi form testuali [doppioclick di selezione] */
			G_NOSEL = true;
		}
		if (G_NOSEL) { /* evito la selezione in campi form testuali */
			G_NOSEL = false;
			return false;
		}
		clearTimeout(G_JQT.selttip_timeout);
		/*var selObject = getSelObj();
		for(var i in selObject) if(!confirm(i+"\n"+selObject[i]))break;*/
		var selection = getSelected();
		//alert(selection);
		if (selection) { /* SE HO UNA SELEZIONE */ /*alert('testo: '+selection);return;*/
			selection = jQuery.trim(''+selection).replace(/"/g,"'");
			if (selection=='' || selection.split(' ').length > G_JQT.max_words || (''+selection).length < G_JQT.min_chars) {				/* NUMERO MASSIMO DI PAROLE e NUMERO MINIMO DI CARATTERI */
				jQuery(G_JQT.popupwin).hide();
				jQuery('#jqt_tipcall').hide();
				return;
			}
			var G_selectionImage = jQuery('#jqt_tipcall');
			G_selectionImage.attr('title',G_JQT.labels[G_JQT.lang].click_info+': '+selection);
			var mouseTop  = e.pageY -0;
			var mouseLeft = e.pageX -0;
			if (mouseLeft < G_JQT.mouse_left) {
				mouseLeft = G_JQT.mouse_left;
				mouseTop  = G_JQT.mouse_top;
			}
			jQuery(G_JQT.popupwin).hide();
			var winTop = mouseTop  + 20;
			var winLeft = mouseLeft - 200;
			var winWid = jQuery(window).width();
			if (winLeft + 400 > winWid) {
				winLeft = winWid - 400;
			}
			if (winLeft < 10) {
				winLeft = 10;
			}
			jQuery(G_JQT.popupwin).css({
				"top":  winTop,
				"left": winLeft
			});
			var offTop = mouseTop  - 50;
			var offLeft = mouseLeft - 0;	// - 13;
			if (offLeft + 80 > winWid) {
				offLeft = winWid - 80;
			}
			jQuery(G_JQT.popupwin)
				.data('link',G_JQT['urlSearch'].replace('{term}',encodeURI(selection)))
				.data('selection',selection);	// salvo il testo cercato
			G_selectionImage
				.css({
					"top": 	offTop,
					"left": offLeft
				})
			   .one('click', function(e){
				   ajaxRetrieveResults('date:D:L:d1');  /* CHIAMATA AJAX - sulla popup */
				   return false;
			   })
			   .show();
			tipcallTimeout();
			imposta_click_ordinamento();
		}
		else {
			jQuery(G_JQT.popupwin).hide();
			jQuery('#jqt_tipcall').hide();
		}
	});
	
	jQuery('object').mousedown(function(e) { /* evito la select sui tag object */
		return false;
	});

	if (jQuery('.jquicksearch_enabled').length > 0) {
		//jQuery(G_JQT['searchButton']).click(function(){ /* CLICK sul bottone della FORM in PAGINA */
		jQuery('#gs').submit(function(){ /* CLICK sul bottone della FORM in PAGINA */
			jQuery(G_JQT.popupwin).hide();
			
			/*if (jQuery('#jquicksearch').length <= 0) {*/
				/*if (jQuery.browser.msie) {
					if (jQuery.browser.version == '6.0' || jQuery.browser.version == '7.0') {
						jQuery('.jquicksearch_pad').css({
							'padding-left':		'0px'
						});
						jQuery('.jqt_subtitle_text_quicksearch').css({
							'padding-left':		'26px'
						});
					}
				}
	
				pngFix();*/
			/*}*/
			
			var ricercata = jQuery.trim(jQuery(G_JQT['searchText']).val());//alert('['+ricercata+']');
			if (ricercata != '' && ricercata.length >= G_JQT.min_chars) {
				//if (jQuery('#jquicksearch:hidden').length > 0) {
				if (jQuery('#jquicksearch').height() == 0) {
					/*jQuery('#jquicksearch').show();*/
					var jqt_left = 15;
					var jqt_top = 30;
					if (jQuery.browser.msie) {
						if (jQuery.browser.version == '6.0') {
							jqt_top = 28;
						}
						else if (jQuery.browser.version == '7.0') {
							jqt_top = 28;
						}
						else if (jQuery.browser.version == '8.0') {
							jqt_top = 26;
						}
						else {
							jqt_top = 28;
						}
					}
					else if (jQuery.browser.safari || jQuery.browser.webkit) { /* .safari è deprecato dalla 1.4+ rimane webkit */
						jqt_top = 30;
					}
					var jqq = jQuery(G_JQT['searchText']);
					jQuery('#jquicksearch').css({
						'height': '362px',
						'left':	Math.round(jqq.offset().left) -jqt_left,
						'top': Math.round(jqq.offset().top) +jqt_top
					});
					jQuery('#jquicksearch')
						.data('link',G_JQT['urlSearch'].replace('{term}', encodeURI(ricercata)))
						.data('selection', ricercata);	// salvo il testo cercato
					ajaxRetrieveData(ricercata, 'date:D:L:d1', '#jquicksearch_results'); /* CHIAMATA AJAX */
					setTimeout(function(){
						jQuery('#jquicksearch_content').animate(
							{
								'top': '0px'
							},{
								'duration':	1500,
								'complete':	function(){
												jQuery('#jquicksearch_shadow_bottom').show();
												
												imposta_click_ordinamento_quicksearch();
												
											}
							}
						);
					}, 500);
					return false;
				}
				else { // se è già aperta la finestra, ma ho solo cambiato i termini di ricerca...
					jQuery('#jquicksearch')
						.data('link',G_JQT['urlSearch'].replace('{term}', encodeURI(ricercata)))
						.data('selection', ricercata);	// salvo il testo cercato
					ajaxRetrieveData(ricercata, 'date:D:L:d1', '#jquicksearch_results'); /* CHIAMATA AJAX */
				}
				return false;//return true;
			}
			else if (ricercata.length < G_JQT.min_chars) { /* alert per ricerche con numero di caratteri inferiore al minimo */
				alert(G_JQT.labels[G_JQT.lang].min_caratteri.replace('{min_chars}',G_JQT.min_chars));
			}
			return false; // non avvio ricerche senza parole da cercare
		});
	}
});
