
jQuery().ready(function(){
		$('.sentmail').dialog({
			autoOpen:true,
			width:400,
			modal:true,
			resizable:false,
			position: ['center',100],
			open: function() {$(".ui-dialog-title").css("color", "green");$(".ui-dialog-title").css("font-size", "1em");}
		});

		$('.senterror').dialog({
			autoOpen:true,
			width:400,
			modal:true,
			resizable:false,
			position: ['center',100],
			open: function() {$(".ui-dialog-title").css("color", "red");$(".ui-dialog-title").css("font-size", "1em");}
		});

		

		jQuery.fn.onImagesLoaded = function(_cb) { 
			  return this.each(function() {
			 
			    var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this),
			        _cont = this,
			            i = 0,
			    _done=function() {
			      if( typeof _cb === 'function' ) _cb(_cont);
			    };
			 
			    if( $imgs.length ) {
			      $imgs.each(function() {
			        var _img = this,
			        _checki=function(e) {
			          if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') )
			          {
			            if( ++i===$imgs.length ) _done();
			          }
			          else if( _img.readyState === undefined ) // dont for IE
			          {
			            $(_img).attr('src',$(_img).attr('src')); // re-fire load event
			          }
			        }; // _checki \\
			 
			        $(_img).bind('load readystatechange', function(e){_checki(e);});
			        _checki({type:'readystatechange'}); // bind to 'load' event...
			      });
			    } else _done();
			  });
			};
			jQuery(function($){ // on page ready
				 
				  $('div#block_news_home').onImagesLoaded(function(_this){
				    $(_this).fadeIn(100);
				    $('#photos').galleryView({
						panel_width: 400,
						panel_height: 267,
						frame_width: 63,
						frame_height: 43,
						filmstrip_size: 5,
						background_color: 'transparent',
					    border: 'none',
					    pause_on_hover: true,
					    transition_speed: 1200,
					    overlay_height: 77,
					    overlay_text_color:'#D8D8AC',
					    nav_theme: 'light'
					});
				  });
				 
				 
				 
				}); // end on ready
				
	});


$(document).ready(function() {

	
	
	
/*
$("#tokenize").tokenInput("/token_comici.php", {

    hintText: "inserisci il tuo comico preferito"
});
$("#tokenize2").tokenInput("/token_recensioni.php", {

    hintText: "inserisci la tua commedia preferita",
    noResultsText: "Nessun risultato",
    searchingText: "Cerco..."
});*/
	/*var ricerca = $(this).next();
	var input = $(this).next().children().children();
	//ricerca.show();
	input.val("Cerca un comico").focus().select();
	$("body").click( function(event) {
	    if (!$(event.target).is("#tokenize2 *")) {
		ricerca.hide();
		$("#tokenize").parent().nextAll().remove();
		$(this).parent().nextAll().remove();
		$("body").unbind();
	    }
	});*/
	
$("#tokenize").keyup( function(event) {
	
	if ($(this).val().length>0) {
		
	    var selected;
	    if (event.which==40) {
		selected = $(".selected", $(this).parent().parent());
		if (selected.length==0)
		    selected = $(this).parent();
		if (selected.next().length>0) {
		    selected.removeClass("selected");
		    selected.next().addClass("selected");
		}
	    }
	    else if (event.which==38) {
		selected = $(".selected", $(this).parent().parent());
		if (selected.length==0)
		    selected = $(this).parent();
		if (selected.prev().length>0 && !selected.prev().children().is("input")) {
		    selected.removeClass("selected");
		    selected.prev().addClass("selected");
		}
	    }
	    else if (event.which==27) {
		$("body").click();
	    }
	    else if (event.which==13) {
		location.href = $(".selected", $(this).parent().parent()).children().attr("href");
	    }
	    else {
		var query = $(this).val().split(" ");
		var word1 = query[0];
		var word2 = query[1];
		
		$.post("/token_comici2.php", 
		    {word1: word1, word2: word2},
		    function (json) {
			var result = json.result;
			
			$("#tokenize").parent().nextAll().remove();
			if (result) {
				alert('res');
			    for (var i=0; i<result.length; i++) {
				var reg = new RegExp("("+word1+")|("+word2+")", "ig");
				var name = result[i].name.replace(reg, "<span class='resChar'>$1$2</span>");
				$("#tokenize").parent().after("<li><a href='?page=docente&id="+result[i].id+"'>"+name+"</a></li>");
			    }
			}
			/*$("#menuDocenti li").nextAll().hover( function() {
			    $("#menuDocenti li").nextAll().removeClass("selected");
			    $(this).addClass("selected");
			});*/
		    },
		    "json");
	    }
	}
	else
	    $(this).parent().nextAll().remove();
    });


});
