// JavaScript Document

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Preload images
//////////
//////////	This is the second half of the code for preloading images.
//////////
//////////	The first half is in global_1.js
//////////
//////////	Last modified: 2010/08/04
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

for (i = 0; i < required_images_arr.length; i++) {
	preloaded_images_arr[i] = new Image();
	preloaded_images_arr[i].src = required_images_arr[i];
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Scrollbar
//////////
//////////	Currently a limit of one per page.
//////////
//////////	Last modified: 2010/10/21
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

var scrollarea = document.getElementById("scrollarea");
var scrollarea_inner = document.getElementById("scrollarea-inner");
var scrollbar = document.getElementById("scrollbar");
var scrollbar_path = document.getElementById("scrollbar-path");
var scrollbar_slider = document.getElementById("scrollbar-slider");

if (scrollarea !== null && scrollarea_inner !== null && scrollbar !== null && scrollbar_path !== null && scrollbar_slider !== null) {
	var scrollarea_box_width, scrollarea_box_height, scrollbar_path_height;

	var scrollbar_path_top = 16;
	var scrollbar_slider_min_height = 10;
	var slider_height = 40;
	
	var drag_obj = new Object();
	
	var new_nudge_position = null;
	var nudge_interval = null;
	
	scrollarea_box_width = parseFloat(getStyle(scrollarea, "width"));
	scrollarea_box_height = parseFloat(getStyle(scrollarea, "height"));
	scrollbar_path_height = scrollarea_box_height - (scrollbar_path_top * 2);
	
	scrollbar_path.style.height = scrollbar_path_height + "px";

	if (document.body.offsetHeight) {
		drag_obj.h = scrollarea_inner.offsetHeight;
	} else {
		drag_obj.h = 0;
	}
	drag_obj.h_box = parseFloat(scrollarea.style.height);
	if (isNaN(drag_obj.h_box)) {
		drag_obj.h_box = scrollarea_box_height;
	}
	if (drag_obj.h > drag_obj.h_box) {
		scrollbar.style.display = "block";
		
		scrollarea_inner.style.width = (scrollarea_box_width - 24) + "px";
		drag_obj.h = scrollarea_inner.offsetHeight;
		
		if (navigator.appName.indexOf("Microsoft") > -1) {
			scrollarea.attachEvent("onmousewheel", scrollByMouseWheel);
		} else {
			if (!scrollarea.addEventListener("DOMMouseScroll", scrollByMouseWheel, false)) {
				scrollarea.onmousewheel = scrollByMouseWheel;
			}
		}
		slider_height = Math.round((drag_obj.h_box / drag_obj.h) * scrollbar_path_height);
		if (slider_height < scrollbar_slider_min_height) {
			slider_height = scrollbar_slider_min_height;
		}
		scrollbar_slider.style.height = slider_height + "px";
	} else {
		scrollarea_inner.style.width = scrollarea_box_width + "px";
		
		if (drag_obj.h == 0) {
			scrollarea.style.overflow = "auto";
		}
	}
}

function startScrolling(event) {
	if (navigator.appName.indexOf("Microsoft") > -1) {
		drag_obj.element = window.event.srcElement;
		
		drag_obj.y = parseFloat(drag_obj.element.style.top);
		if (isNaN(drag_obj.y)) {
			drag_obj.y = scrollbar_path_top;
		}
		drag_obj.y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop - drag_obj.y;
		
		document.attachEvent("onmousemove", dragScroller);
		document.attachEvent("onmouseup", stopScrolling);
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else {
		drag_obj.element = event.target;
		
		drag_obj.y = parseFloat(drag_obj.element.style.top);
		if (isNaN(drag_obj.y)) {
			drag_obj.y = scrollbar_path_top;
		}
		drag_obj.y = event.clientY + window.scrollY - drag_obj.y;
		
		document.addEventListener("mousemove", dragScroller, true);
		document.addEventListener("mouseup", stopScrolling, true);
		event.preventDefault();
	}
}

function dragScroller(event) {
	var y, new_y, scroll_ratio;
	
	if (navigator.appName.indexOf("Microsoft") > -1) {
		y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
		window.event.returnValue = false;
	} else {
		y = event.clientY + window.scrollY;
	}
	
	new_y = y - drag_obj.y;
	if (new_y < scrollbar_path_top) {
		new_y = scrollbar_path_top;
	} else if (new_y > scrollbar_path_height + scrollbar_path_top - slider_height) {
		new_y = scrollbar_path_height + scrollbar_path_top - slider_height;
	}
	
	scroll_ratio = parseInt(Math.round((new_y - scrollbar_path_top) / (scrollbar_path_height - slider_height) * 100)) * 0.01;
	
	scrollarea_inner.style.top = 0 - ((drag_obj.h - drag_obj.h_box) * scroll_ratio) + "px";
	
	scrollbar_slider.style.top = new_y + "px";
}

function stopScrolling(event) {
	if (navigator.appName.indexOf("Microsoft") > -1) {
		document.detachEvent("onmousemove", dragScroller);
		document.detachEvent("onmouseup", stopScrolling);
	} else {
		document.removeEventListener("mousemove", dragScroller, true);
		document.removeEventListener("mouseup", stopScrolling, true);
	}
}

function scrollByMouseWheel(event) {
	var delta = 0;
	
	if (!event) {
		event = window.event;
	}
	
	if (event.wheelDelta) {
		delta = -event.wheelDelta/120;
	} else if (event.detail) {
		delta = event.detail/3;
	}
	
	if (delta) {
		var y, new_y, scroll_ratio;
		
		y = parseFloat(scrollbar_slider.style.top);
		if (isNaN(y)) {
			y = 0;
		}
		
		if (delta < 0) {
			new_y = y - 3;
		} else {
			new_y = y + 3;
		}
		
		if (new_y < scrollbar_path_top) {
			new_y = scrollbar_path_top;
		} else if (new_y > scrollbar_path_height + scrollbar_path_top - slider_height) {
			new_y = scrollbar_path_height + scrollbar_path_top - slider_height;
		}
		
		scroll_ratio = parseInt(Math.round((new_y - scrollbar_path_top) / (scrollbar_path_height - slider_height) * 100)) * 0.01;
		
		scrollbar_slider.style.top = new_y + "px";
		scrollarea_inner.style.top = 0 - ((drag_obj.h - drag_obj.h_box) * scroll_ratio) + "px";
	}
	
	if (event.preventDefault) {
		event.preventDefault();
	}
	event.returnValue = false;
}

/*function doubleClickArrow(event) {
	if (!event) {
		event = window.event;
	}
	
	if (event.preventDefault) {
		event.preventDefault();
	}
	event.returnValue = false;
}*/

function nudgeScrollerUp() {
	var top;
	if (new_nudge_position === null) {
		top = parseFloat(scrollarea_inner.style.top);
		if (isNaN(top)) {
			top = 0;
		}
	} else {
		top = new_nudge_position;
	}
	
	new_nudge_position = top + 20;
	
	if (new_nudge_position < 0 - (drag_obj.h - drag_obj.h_box)) {
		new_nudge_position = 0 - (drag_obj.h - drag_obj.h_box);
	} else if (new_nudge_position > 0) {
		new_nudge_position = 0;
	}
	
	if (nudge_interval === null) {
		nudge_interval = setInterval(nudgeScroller, 30);
	}
}

function nudgeScrollerDown() {
	var top;
	if (new_nudge_position === null) {
		top = parseFloat(scrollarea_inner.style.top);
		if (isNaN(top)) {
			top = 0;
		}
	} else {
		top = new_nudge_position;
	}
	
	new_nudge_position = top - 20;
	
	if (new_nudge_position < 0 - (drag_obj.h - drag_obj.h_box)) {
		new_nudge_position = 0 - (drag_obj.h - drag_obj.h_box);
	} else if (new_nudge_position > 0) {
		new_nudge_position = 0;
	}
	
	if (nudge_interval === null) {
		nudge_interval = setInterval(nudgeScroller, 30);
	}
}

function nudgeScroller() {
	var scroll_ratio;
	
	scroll_ratio = 0 - (new_nudge_position / (drag_obj.h - drag_obj.h_box));
	
	scrollarea_inner.style.top = new_nudge_position + "px";
	scrollbar_slider.style.top = scrollbar_path_top + (scroll_ratio * (scrollbar_path_height - slider_height)) + "px";
	
	new_nudge_position = null;
	clearInterval(nudge_interval);
	nudge_interval = null;
}

