$(document).ready( function(){

/*** TOOLTIP */
//	$('a.tTip').tinyTips('light', 'title');
//	$('a.soltip').tinyTips('light', '<img src="images/preview_cat/sol.jpg" />');
//	$('a.oletip').tinyTips('light', '<img src="images/preview_cat/ole.jpg" />');
/* FINE */
	
	
	$('#txt_ricerca').placeHolder();
	
/*** PER RITORNARE IN CIMA ALLA PAGINA */	
	$('#top').click( function(){
		
		$(window).scrollTop(0);		
	});	
/* FINE */

	
/*** PER LA RICERCA DEI PRODOTTI */
	
	// ricerca classica, senza div popup
	if( $('div#ricerca_no_popup').length == 1 ){

/*		$('#btn_avvia_ricerca').click( function(){
			document.location = "ricerca/" + $('#txt_ricerca').val();	
		})*/
		
		$('#txt_ricerca').keyup( function( e ){
			
			// quando il tasto INVIO viene premuto
			c = e.which ? e.which : e.keyCode; 	
			
			if( c == 13 ) {
				//$(window.location).attr('href', "ricerca/" + $(this).val());
				$(window.location).attr('href', "http://www.difast.it/ricerca/" + $(this).val());
			}
			
		})		
	}
	
	
	// ricerca in real-time con div popup
	if( $('div#ricerca_popup').length == 1 ) {	

			$('#txt_ricerca').keyup( function( e ){
		
			var inputString = $(this).val();
			
			if( inputString.length >= 3 ){
				var btnSearchClose = $("#btn_search_close");
				var btnSearchLoading = $("#btn_search_loading");
				var searchPopup = $('#search_popup');			
				
				
				// quando il tasto INVIO viene premuto
				c = e.which ? e.which : e.keyCode; 	
				if( c == 13 ) {
	
					var linkEl = $( '.live_search .live_search_key_down a:first' );
					// se è stato selezionato un elemento con i tasti DOWN o UP con
					// la pressione dell'invio, mostro la pagina dell'emento selezionato
					if( linkEl.length > 0 ){
						document.location = linkEl.attr ( 'href' );
					// Nessun elemento selezionato con DOWN o UP, mostro la pagina di tutti i risultati
					} else {
						document.location = "ricerca/" + inputString;					
					}
						
				 // quando i tasti DOWN, UP, LEFT o RIGHT vengono premuti
				}else if( c >= 37 && c <= 40 ) {
					
					var elems = $('.live_search .live_search_key_down');
					// se si è premuto il tasto DOWN
					if( c == 40 ) {	
	
						if( elems.length == 0 ) 
							$('.live_search .live_ssingle_res:first').addClass('live_search_key_down');
						else{								
							$('.live_search .live_search_key_down:last').next().addClass('live_search_key_down');
							$('.live_search .live_search_key_down:last').prev().removeClass('live_search_key_down');
						}
					
					// se si è premuto il tasto UP
					} else if ( c == 38 ) {
						
						if( elems.length == 0 ) 
							$('.live_search .live_ssingle_res:last').addClass('live_search_key_down');
						else{		
							$('.live_search .live_search_key_down:last').prev().addClass('live_search_key_down');		
							$('.live_search .live_search_key_down:first').next().removeClass('live_search_key_down');
							
						}
						
					}
					
	
				// in tutti gli altri casi, faccio partite una richiesta ajax per i risultati
				} else {
					btnSearchClose.hide();	
					btnSearchLoading.show();
					
					$.ajax({
						url : "ricerca_ajax_popup.php",
						data: 'str=' + inputString,
						type: 'GET',
						dataType: 'html',
						success : function (msg, stato) {
							searchPopup.show().html(msg);
							btnSearchLoading.hide();
							
							btnSearchClose.show().click(function(){
								btnSearchClose.hide();
								searchPopup.hide();
								
							});
						},
					});
				
				}
			} else {
				$('#search_popup').hide();
			}
			
		})
	
	
		
	}
		
		

/*** FINE */
	
	
});



 var notification = function( tipo, messaggio ){
	 
	var messaggio = $('<textarea/>').html(messaggio).val();
	
	jQuery.noticeAdd({
                        text: messaggio,
                        stay: true,
						type: tipo,
    });
	 
 }
