// JavaScript Document
$(document).ready(function() {
	
	//rollover menu
	$("#menu li").hover(function () {
	  $(this).addClass("sel");
	}, function () {
	  $(this).removeClass("sel");
	});
	
	//rollover lingua
	$(".lang_label").hover(function () {
	  this.src = this.src.replace('_off','_on');
	}, function () {
	  this.src = this.src.replace('_on','_off');
	});

    //overlay links
	$("a[rel='#overlay']").overlay({  
        expose: '#F9FDCD', 
        effect: 'apple',  
        onBeforeLoad: function() {  
            // grab wrapper element inside content 
            var wrap = this.getContent().find(".contentWrap");  
            // load the page specified in the trigger 
            wrap.load(this.getTrigger().attr("href")); 
        }  
    }); 
	
	 //overlay_small links
	$("a[rel='#overlay_small']").overlay({  
        expose: '#F9FDCD', 
        effect: 'apple',  
        onBeforeLoad: function() {  
            // grab wrapper element inside content 
            var wrap = this.getContent().find(".contentWrap_small");  
            // load the page specified in the trigger 
            wrap.load(this.getTrigger().attr("href")); 
        }  
    });
	
	//Glossary functions
	$("#search_ita").click(function (){
			if( jQuery.trim($("input[name=italiano]").val()) != ''){						 
				$.post("/extra/watson/search.php", { val: $("input[name=italiano]").val() , lang: "ita", req : Math.floor(Math.random()*1000) },
				   function(data){
					 $("#note").hide(); 
					 $("#glossary_result").html(data);
					 $("input[name=italiano]").val('');
					 $("input[name=inglese]").val('');
					 $("a[rel='#result_view']").overlay({
							effect: 'drop',
							onBeforeLoad: function (){
								$.post("/extra/watson/view_item.php", { search_item: this.getTrigger().context.id , req : Math.floor(Math.random()*1000) },
								   function(data){									
									 $("#result_view_content").html(data);									 
								   });	
							}
						});
					 });	
			} 
    });
	
	$("#search_eng").click(function (){
			if( jQuery.trim($("input[name=inglese]").val()) != ''){						 
				$.post("/extra/watson/search.php", { val: $("input[name=inglese]").val() , lang: "eng", req : Math.floor(Math.random()*1000) },
				   function(data){
					 $("#note").hide(); 
					 $("#glossary_result").html(data);
					 $("input[name=italiano]").val('');
					 $("input[name=inglese]").val('');					 
					 $("a[rel='#result_view']").overlay({
							effect: 'drop',
							onBeforeLoad: function (){
								$.post("/extra/watson/view_item.php", { search_item: this.getTrigger().context.id , req : Math.floor(Math.random()*1000) },
								   function(data){									
									 $("#result_view_content").html(data);									 
								   });	
							}
						});
				   });
			}
    });
	
	// create custom animation algorithm for jQuery called "drop" 
	$.easing.drop = function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	};
	
	// create custom overlay effect for jQuery Overlay
	$.tools.overlay.addEffect("drop",  
		
		// loading animation
		function(done) { 
			var animateProps = {
				top: '+=55', 
				opacity: 1, 
				width: '+=20'
			};
			this.getOverlay().animate(animateProps, "medium", 'drop', done).show();
		}, 
		
		// closing animation
		function(done) {
			var animateProps = {
				top: '-=55', 
				opacity: 0, 
				width: '-=20'
			};
			this.getOverlay().animate(animateProps, "fast", 'drop', function()  {
				$(this).hide();
				done.call();		
			});
		}
	);


});
