/*
 * SCROLLBAR FUNCTIONALITY
 */
var downTimeout = null;
var upTimeout = null;
var trackingTimeout = null;
var step_size = 3;
var interval = 15; // in ms
var add_content_height = 0;

$j(document).ready(function(){
	
	// Remove scrollbar if it is "optional" and holder_height is greater or equal to scrollable_content
	if($j("#scroll_line").hasClass('optional_scrollbar'))
	{
		holder_height = $j("#scrollable_content_wrapper").height();
		height = $j("#scrollable_content").height() + add_content_height;
		if(height <= holder_height)
		{
			$j("#scroll_line").addClass('none');
		}
	}
	
	//content = $j("#scrollable_content");
	//holder_height = $j("#scrollable_content_wrapper").height();
	//scrollbar_height = $j("#scroll_handle_holder").height() - $j("#scroll_handle").height();
	
	$j("#scroll_down").mousedown(function() {
		move_down();
		stop_up();
		stop_tracking();
	});
	$j("#scroll_up").mousedown(function() {
		move_up();
		stop_down();
		stop_tracking();
	});
	
	$j("#scroll_down").mouseup(function() {
		stop_down();
	});
	$j("#scroll_up").mouseup(function() {
		stop_up();
	});
	
	$j("#scroll_handle").mousedown(function() {
		$j("#scroll_handle").draggable('enable');
		start_tracking();
		stop_up();
		stop_down();
	});
	$j("#scroll_handle").mouseup(function() {
		$j("#scroll_handle").draggable('disable');
		stop_tracking();
	});
	
	$j("#scroll_handle").draggable({
	    axis: "y",
		containment: "parent"
	});
	$j("#scroll_handle").draggable('disable');

});

function move_down()
{
	holder_height = $j("#scrollable_content_wrapper").height();
	height = $j("#scrollable_content").height() + add_content_height;
	current = $j("#scrollable_content").css("top");
	current_length = current.length;
	current_int = parseFloat(current.substring(0, current_length-2));
	next_int = parseFloat(current_int - step_size);
	next = next_int+"px";
	if(current_int > -height+holder_height)
	{
		$j("#scrollable_content").css("top", next);
		downTimeout = window.setTimeout("move_down()", interval);
		fix_handle_position();
	}
	else
	{
		clearTimeout(downTimeout);
	}
}

function move_up()
{
	current = $j("#scrollable_content").css("top");
	current_length = current.length;
	current_int = parseFloat(current.substring(0, current_length-2));
	next_int = parseFloat(current_int + step_size);
	next = next_int+"px";
	if(current_int < 0)
	{
		$j("#scrollable_content").css("top", next);
		upTimeout = window.setTimeout("move_up()", interval);
		fix_handle_position();
	}
	else
	{
		clearTimeout(upTimeout);
	}
}

function start_tracking()
{
	holder_height = $j("#scrollable_content_wrapper").height();
	scrollbar_height = $j("#scroll_handle_holder").height() - $j("#scroll_handle").height();
	height = $j("#scrollable_content").height() + add_content_height;
	if (height > holder_height)
	{
		factor = scrollbar_height / (height - holder_height);
		handle_start = parseFloat($j("#scroll_handle").css("top"));
		$j("#scrollable_content").css("top", -(Math.abs(parseFloat(handle_start)) / factor));
		
		trackingTimeout = window.setTimeout("start_tracking()", interval);
	}
}

function stop_down()
{
	window.clearTimeout(downTimeout);
}

function stop_up()
{
	window.clearTimeout(upTimeout);
}

function stop_tracking()
{
	window.clearTimeout(trackingTimeout);
}

function fix_handle_position()
{
	holder_height = $j("#scrollable_content_wrapper").height();
	scrollbar_height = $j("#scroll_handle_holder").height() - $j("#scroll_handle").height();
	
	height = $j("#scrollable_content").height() + add_content_height;
	factor = scrollbar_height / (height - holder_height);
	current = $j("#scrollable_content").css("top");
	
	// Scrolling down
	if(Math.abs(parseFloat(current)) * factor < scrollbar_height)
	{
		$j("#scroll_handle").css("top", Math.abs(parseFloat(current)) * factor);
	}
	else
	{
		$j("#scroll_handle").css("top", scrollbar_height);
	}
	
	// Scrolling up
	if(parseFloat(current.substring(0, current_length-2)) > 0)
	{
		$j("#scroll_handle").css("top", 0);
	}
}

/*
 * NEWS SCROLLER FUNCTIONALITY
 */
var counter = 0;
var scroll_size = 1;
var total_height = 0;
var building_height = 0;
var story_margin = 10;
var interval = 75; // milliseconds

$j(document).ready(function(){
	if($('news'))
	{
		counter = 0;
		
		// Build stories on top of each other
		$$('#news li').each(function(item){
			counter++;
			$j("#story_"+counter).css("position", "absolute");
			$j("#story_"+counter).css("top", building_height);
			building_height += $j("#story_"+counter).height()+story_margin;
		});
		
		var myInterval = window.setInterval(function() {
			
			counter = 0;
			
			$$('#news li').each(function(item){
				
				counter++;
				
				current = $j("#story_"+counter).css("top");
				current_length = current.length;
				current_int = parseFloat(current.substring(0, current_length-2));
				
				current_bottom = current_int+$j("#story_"+counter).height()+story_margin 
				if(current_bottom < 0)
				{
					new_top = current_int+building_height+"px";
					$j("#story_"+counter).css('top', new_top);
					current_int = current_int+building_height;
				}
						
				next_int = parseFloat(current_int - scroll_size);
				$j("#story_"+counter).css("top", next_int);
			});
			
		}, interval);

	}
});
