//-----------------------------------------------Twitter Block
function tmakeScrollable(twrapper, tscrollable){
	// Get jQuery elements
	var twrapper = jQuery(twrapper), tscrollable = jQuery(tscrollable);
	
	// Hide content until they are not loaded
	tscrollable.hide();
	var tloading = jQuery('<div class="loading">Loading...</div>').appendTo(twrapper);
	
	// Set function that will check if all images are loaded
	var tinterval = setInterval(function(){
		var timages = tscrollable.find('img');
		var tcompleted = 0;
		
		// Counts number of images that are succesfully loaded
		timages.each(function(){
			if (this.complete) tcompleted++;	
		});
		
		if (tcompleted == timages.length){
			clearInterval(tinterval);
			// Timeout added to fix problem with Chrome
			setTimeout(function(){
				
				tloading.hide();
				// Remove scrollbars	
				twrapper.css({overflow: 'hidden'});						
				
				tscrollable.slideDown('slow', function(){
					tenable();	
				});					
			}, 1000);	
		}
	}, 100);
	
	function tenable(){
		// height of area at the top at bottom, that don't respond to mousemove
		var tinactiveMargin = 200;					
		// Cache for performance
		var twrapperWidth = twrapper.width();
		var twrapperHeight = twrapper.height();
		// Using outer height to include padding too
		var tscrollableHeight = tscrollable.outerHeight() + 2*tinactiveMargin;
		// Do not cache wrapperOffset, because it can change when user resizes window
		// We could use onresize event, but it's just not worth doing that 
		// var wrapperOffset = wrapper.offset();
		
		// Create a invisible tooltip
		var ttooltip = jQuery('<div class="tsc_menu_tooltip"></div>')
			.css('opacity', 0)
			.appendTo(twrapper);
	
		// Save menu titles
		tscrollable.find('a').each(function(){				
			jQuery(this).data('ttooltipText', this.title);				
		});
		
		// Remove default tooltip
		tscrollable.find('a').removeAttr('title');		
		// Remove default tooltip in IE
		tscrollable.find('img').removeAttr('alt');	
		
		var tlastTarget;
		//When user move mouse over menu			
		twrapper.mousemove(function(te){
			// Save target
			tlastTarget = te.target;

			var twrapperOffset = twrapper.offset();
		
			var ttooltipLeft = te.pageX - twrapperOffset.left;
			// Do not let tooltip to move out of menu.
			// Because overflow is set to hidden, we will not be able too see it 
			ttooltipLeft = Math.min(ttooltipLeft, twrapperWidth - 75); //tooltip.outerWidth());
			
			var ttooltipTop = te.pageY - twrapperOffset.top + twrapper.scrollTop() - 40;
			// Move tooltip under the mouse when we are in the higher part of the menu
			if (te.pageY - twrapperOffset.top < twrapperHeight/2){
				ttooltipTop += 80;
			}				
			ttooltip.css({top: ttooltipTop, left: ttooltipLeft});				
			
			// Scroll menu
			var ttop = (te.pageY -  twrapperOffset.top) * (tscrollableHeight - twrapperHeight) / twrapperHeight - tinactiveMargin;
			if (ttop < 0){
				ttop = 0;
			}			
			twrapper.scrollTop(ttop);
		});
		
		// Setting interval helps solving perfomance problems in IE
		var tinterval = setInterval(function(){
			if (!tlastTarget) return;	
										
			var tcurrentText = ttooltip.text();
			
			if (tlastTarget.nodeName == 'IMG'){					
				// We've attached data to a link, not image
				var tnewText = jQuery(tlastTarget).parent().data('ttooltipText');

				// Show tooltip with the new text
				if (tcurrentText != tnewText) {
					ttooltip
						.stop(true)
						.css('opacity', 0)	
						.text(newText)
						.animate({opacity: 1}, 1000);
				}					
			}
		}, 200);
		
		// Hide tooltip when leaving menu
		twrapper.mouseleave(function(){
			tlastTarget = false;
			ttooltip.stop(true).css('opacity', 0).text('');
		});			
		
		/*
		//Usage of hover event resulted in performance problems
		scrollable.find('a').hover(function(){
			tooltip
				.stop()
				.css('opacity', 0)
				.text($(this).data('tooltipText'))
				.animate({opacity: 1}, 1000);
	
		}, function(){
			tooltip
				.stop()
				.animate({opacity: 0}, 300);
		});
		*/			
	}
}
	
jQuery(function(){	
	tmakeScrollable("div.scrollable", "div.sc_menu_1");
}
);
