/*
Fancy Drop Downs - by Russell James Smith (AWA) April 2007.

This code is dependant on the JQuery Lib.

This code could do with an optimization pass to make it more efficient
*/

var superScroller_step = 16; /* basic step for the scroll in px, good if it's set to line-height */

/* recursive function that scrolls the area "up" while mouse is held down */
function superScroller_scroll_up(menu_id) {
	var current_top = 0;
	var new_top = 0;

	current_top = parseInt($("#" + menu_id + " div.inner div.scroller").css("top"));				
	$("span.current_top").html("*" + current_top + "* [" + $("#" + menu_id + " div.inner div.scroller").css("top") + "]");				
			
	new_top = current_top + superScroller_step;			
	if (new_top >= 0) { // if near or at the top
		new_top = 0; 
		$("#" + menu_id + " div.inner div.scroller").css("top",new_top);
		$("#" + menu_id + " div.inner div.scrollerControls span.up").addClass("disabled");
	}	else if ( $("#" + menu_id).attr("class").search(/goingUp/i) >= 1 ) { // if going up
		$("#" + menu_id + " div.inner div.scroller").animate({top: new_top}, 10, "linear", function(){ superScroller_scroll_up(menu_id); });
	} else {
		$("#" + menu_id + " div.inner div.scroller").animate({top: new_top}, 10, "linear");
	}
	$("span.new_top").html("*" + new_top + "* [" + (current_top + superScroller_step) + "]");		
	superScroller_update_position_bar (menu_id);
}

/* recursive function that scrolls the area "down" while mouse is held down */
function superScroller_scroll_down(menu_id) {
	var current_top = 0;
	var new_top = 0;
	var max_top = 0;

	current_top = parseInt($("#" + menu_id + " div.inner div.scroller").css("top"));
	$("span.current_top").html("*" + current_top + "* [" + $("#" + menu_id + " div.inner div.scroller").css("top") + "]");
	max_top = 0 - ( $("#" + menu_id + " div.inner div.scroller").height() - $("#" + menu_id + " div.inner").height() );
	$("span.max_top").html("*" + max_top + "*");
	
	new_top = current_top - superScroller_step;			
	if (new_top <= max_top) { // if near or at the bottom
		new_top = max_top; 
		$("#" + menu_id + " div.inner div.scroller").css("top",new_top);
		$("#" + menu_id + " div.inner div.scrollerControls span.down").addClass("disabled");
	}	else if ( $("#" + menu_id).attr("class").search(/goingDown/i) >= 1 ) { // if going up
		$("#" + menu_id + " div.inner div.scroller").animate({top: new_top}, 10, "linear", function(){ superScroller_scroll_down(menu_id); });
	} else {
		$("#" + menu_id + " div.inner div.scroller").animate({top: new_top}, 10, "linear");
	}
	$("span.new_top").html("*" + new_top + "* [" + (current_top + superScroller_step) + "]");
	superScroller_update_position_bar (menu_id);
}

function superScroller_initialize_position_bar (menu_id) {	
	var bar_height = $("#" + menu_id + " .bar").height();
	var scroller_height = $("#" + menu_id + " .scroller").height();
	var areaViewable_height = bar_height / ( scroller_height / $("#" + menu_id + " .inner").height() );
	var areaViewable_top = (bar_height / scroller_height ) * Math.abs(parseInt($("#" + menu_id + " .scroller").css("top")));
	
	$("#" + menu_id + " div.scrollerControls span.bar span.areaViewable").height(areaViewable_height).css("top",areaViewable_top);		
}

function superScroller_update_position_bar (menu_id) {	
	var bar_height = $("#" + menu_id + " .bar").height();
	var scroller_height = $("#" + menu_id + " .scroller").height();
	var areaViewable_top = (bar_height / scroller_height ) * Math.abs(parseInt($("#" + menu_id + " .scroller").css("top")));

	$("#" + menu_id + " div.scrollerControls span.bar span.areaViewable").css("top",areaViewable_top);		
}

function rem_superScroller_initialize () {
	/* do nothing */
}

function superScroller_initialize () {

	$("div.sideNav").not(".noscroll").each(function(){		
		var menu_id = $(this).attr("id");
		var inner_height = $(this).find(".inner ul").height();
		var max_height = parseInt($(this).find(".inner").css("max-height"));
	
		// alert(inner_height + ">" + max_height);
		if (inner_height > max_height ) {
			
			$(this).find(".inner").height(max_height).css({overflow: "hidden"}).prepend("<div class=\"scrollerControls\"><span class=\"up disabled\">Scroll Up</span><span class=\"bar\"><span class=\"areaAbove\"></span><span class=\"areaViewable\"></span><span class=\"areaBelow\"></span></span><span class=\"down\">Scroll Down</span></div>");
			$(this).find(".inner > ul").wrap("<div class=\"scroller\"></div>");
			
			// initialize the heights of the control bar
			superScroller_initialize_position_bar (menu_id);		
		}		
	});

	$("div.scrollerControls span.up").mousedown(function(){
		$(this).addClass("press");
		$(this).parent().parent().parent().parent().addClass("goingUp");		
		superScroller_scroll_up($(this).parent().parent().parent().parent().attr("id"));
		$(this).siblings(".down").removeClass("disabled");
	});

	$("div.scrollerControls span.up").mouseup(function(){			
		$(this).parent().parent().parent().parent().removeClass("goingUp");
		$(this).removeClass("press");
	});

	$("div.scrollerControls span.down").mousedown(function(){
		$(this).addClass("press");		
		$(this).parent().parent().parent().parent().addClass("goingDown");
		superScroller_scroll_down($(this).parent().parent().parent().parent().attr("id"));
		$(this).siblings(".up").removeClass("disabled");
	});

	$("div.scrollerControls span.down").mouseup(function(){
		$(this).parent().parent().parent().parent().removeClass("goingDown");
		$(this).removeClass("press");
	});

}
