// JavaScript Document

var divs = document.getElementsByTagName("div");

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	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];
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Auto-crossfading images
//////////
//////////	Crossfades images that are classed "background-image" in a div with id "background-images".
//////////	CSS styles should be applied to set the dimensions of the container, AP the child divs
//////////	and make them display:none by default.
//////////
//////////	Last modified: 2010/10/21
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

var background_images = new Array();
var background_image_index = 0;
var background_timeout = null;
var background_interval = null;

var crossfade_delay_seconds = 5;

function autoadvance_background() {
	background_image_index++;
	if (background_image_index >= background_images.length) {
		background_image_index = 0;
	}
	
	if (background_interval === null) {
		background_interval = setInterval(crossfade_background, 30);
	}
	
	background_timeout = setTimeout(autoadvance_background, crossfade_delay_seconds * 1000);
}

function crossfade_background() {
	var intervalNeeded = false;
	
	for (i = 0; i < background_images.length; i++) {
		var image;
		
		image = background_images[i];
		
		var alpha;
		
		if (i == background_image_index) {
			if (image.style.display != "block") {
				image.style.display = "block";
				setAlpha(image, 0.00);
			}
			alpha = getAlpha(image);
			if (alpha < 1.00) {
				intervalNeeded = true;
				
				alpha += 0.10;
				setAlpha(image, alpha);
			} else {
				clearAlpha(image);
			}
		} else {
			if (image.style.display == "block") {
				alpha = getAlpha(image);
				if (alpha > 0.00) {
					intervalNeeded = true;
					
					alpha -= 0.10;
					setAlpha(image, alpha);
				} else {
					clearAlpha(image);
					image.style.display = "none";
				}
			}
		}
	}
	
	if (!intervalNeeded) {
		clearInterval(background_interval);
		background_interval = null;
	}
}

for (i = 0; i < divs.length; i++) {
	if (divs[i].className == "background-image") {
		background_images[background_images.length] = divs[i];
	}
}
if (background_images.length > 0) {
	background_images[0].style.display = "block";
	
	background_timeout = setTimeout(autoadvance_background, crossfade_delay_seconds * 1000);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Auto-crossfading images
//////////
//////////	Crossfades images that are classed "image" in a div classed "rotating-images".
//////////	CSS styles should be applied to set the dimensions of the container, AP the child imgs
//////////	and make them display:none by default.
//////////
//////////	Last modified: 2011/02/04
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

//	Define variables

var rotating_images = new Array();

var rotating_images_default_delay = 5;
var rotating_images_default_fadespeed = 0.10;

//	Define functions

function rotating_images_autoAdvance(i) {
	if (rotating_images[i].images.length > 1) {
		rotating_images[i].index++;
		
		if (rotating_images[i].index >= rotating_images[i].images.length) {
			rotating_images[i].index = 0;
		}
		
		if (rotating_images[i].interval === null) {
			rotating_images[i].interval = setInterval("rotating_images_crossfade(" + i + ")", 30);
		}
		
		clearTimeout(rotating_images[i].timeout);
		rotating_images[i].timeout = null;
		if (rotating_images[i].data.delay) {
			rotating_images[i].timeout = setTimeout("rotating_images_autoAdvance(" + i + ")", rotating_images[i].data.delay * 1000);
		} else {
			rotating_images[i].timeout = setTimeout("rotating_images_autoAdvance(" + i + ")", rotating_images_default_delay * 1000);
		}
	}
}

function rotating_images_crossfade(i) {
	var intervalNeeded = false;
	
	var fadespeed = rotating_images_default_fadespeed;
	if (rotating_images[i].data.fadespeed) {
		fadespeed = Number(rotating_images[i].data.fadespeed);
	}
	
	if (rotating_images[i].index !== null) {
		if (rotating_images[i].data.type == "background") {
			if (rotating_images[i].images[rotating_images[i].index].style.backgroundImage == "") {
				rotating_images[i].images[rotating_images[i].index].style.backgroundImage = "url('" + rotating_images[i].data.images[rotating_images[i].index] + "')";
			}
		}
	}
	
	for (j = 0; j < rotating_images[i].images.length; j++) {
		var image;
		
		image = rotating_images[i].images[j];
		
		if (j == rotating_images[i].index) {
			if (image.style.display != "block") {
				image.style.display = "block";
				setAlpha(image, 0.00);
			}
			alpha = getAlpha(image);
			alpha += fadespeed;
			if (alpha < 1.00) {
				intervalNeeded = true;
				setAlpha(image, alpha);
			} else {
				clearAlpha(image);
			}
		} else {
			if (image.style.display == "block") {
				alpha = getAlpha(image);
				alpha -= fadespeed;
				if (alpha > 0.00) {
					intervalNeeded = true;
					setAlpha(image, alpha);
				} else {
					clearAlpha(image);
					image.style.display = "none";
				}
			}
		}
	}
	
	if (!intervalNeeded) {
		if (rotating_images[i].index === null) {
			rotating_images[i].index = 0;
		}
		
		clearInterval(rotating_images[i].interval);
		rotating_images[i].interval = null;
	}
}

function rotating_images_control_via_id(id, command, data) {
	for (i = 0; i < rotating_images.length; i++) {
		if (rotating_images[i].data.id == id) {
			switch (command) {
				case "show":
				if (rotating_images[i].images.length > 0) {
					rotating_images[i].images[0].style.display = "block";
					
					if (rotating_images[i].images.length > 1) {
						if (rotating_images[i].timeout === null) {
							if (rotating_images[i].data.delay) {
								rotating_images[i].timeout = setTimeout("rotating_images_autoAdvance(" + i + ")", rotating_images[i].data.delay * 1000);
							} else {
								rotating_images[i].timeout = setTimeout("rotating_images_autoAdvance(" + i + ")", rotating_images_default_delay * 1000);
							}
						}
					}
				}
				break;
				
				case "hide":
				if (rotating_images[i].timeout !== null) {
					clearTimeout(rotating_images[i].timeout);
					rotating_images[i].timeout = null;
				}
				if (rotating_images[i].interval !== null) {
					clearInterval(rotating_images[i].interval);
					rotating_images[i].interval = null;
				}
				for (j = 0; j < rotating_images[i].images.length; j++) {
					var image;
					
					image = rotating_images[i].images[j];
					clearAlpha(image);
					image.style.display = "none";
				}
				rotating_images[i].index = 0;
				break;
				
				case "fadeout":
				if (rotating_images[i].timeout !== null) {
					clearTimeout(rotating_images[i].timeout);
					rotating_images[i].timeout = null;
				}
				if (rotating_images[i].interval !== null) {
					clearInterval(rotating_images[i].interval);
					rotating_images[i].interval = null;
				}
				rotating_images[i].index = null;
				rotating_images[i].interval = setInterval("rotating_images_crossfade(" + i + ")", 30);
				break;
				
				case "fadein":
				if (rotating_images[i].images.length > 0) {
					rotating_images[i].images[0].style.display = "block";
					setAlpha(rotating_images[i].images[0], 0.00);
					rotating_images[i].interval = setInterval("rotating_images_crossfade(" + i + ")", 30);
					
					if (rotating_images[i].images.length > 1) {
						if (rotating_images[i].timeout === null) {
							if (rotating_images[i].data.delay) {
								rotating_images[i].timeout = setTimeout("rotating_images_autoAdvance(" + i + ")", rotating_images[i].data.delay * 1000);
							} else {
								rotating_images[i].timeout = setTimeout("rotating_images_autoAdvance(" + i + ")", rotating_images_default_delay * 1000);
							}
						}
					}
				}
				break;
				
				case "goto":
				if (rotating_images[i].timeout !== null) {
					clearTimeout(rotating_images[i].timeout);
					rotating_images[i].timeout = null;
				}
				if (rotating_images[i].interval !== null) {
					clearInterval(rotating_images[i].interval);
					rotating_images[i].interval = null;
				}
				rotating_images[i].index = data;
				rotating_images[i].interval = setInterval("rotating_images_crossfade(" + i + ")", 30);
				break;
			}
		}
	}
}

//	Execute

for (i = 0; i < divs.length; i++) {
	if (divs[i].className == "rotating-images") {
		obj = new Object();
		obj.element = divs[i];
		obj.data = new Object();
		obj.images = new Array();
		for (j = 0; j < obj.element.childNodes.length; j++) {
			if (obj.element.childNodes[j].nodeType == 1) {
				if (obj.element.childNodes[j].className == "image") {
					obj.images[obj.images.length] = obj.element.childNodes[j];
				} else if (obj.element.childNodes[j].className == "data") {
					s = obj.element.childNodes[j].innerHTML;
					s = s.replace(/\n/g, "");
					s = s.replace(/\r/g, "");
					s = s.replace(/\t/g, "");
					a = s.split(",");
					for (n = 0; n < a.length; n++) {
						b = a[n].split("=");
						obj.data[b[0]] = b[1];
					}
				}
			}
		}
		if (obj.data.images) {
			obj.data.images = obj.data.images.split("|");
			obj.images = obj.data.images.slice();
		}
		if (obj.data.type == "background") {
			for (j = 0; j < obj.images.length; j++) {
				var d = document.createElement("div");
				d.style.display = "none";
				d.style.position = "absolute";
				if (obj.data.width) {
					d.style.width = obj.data.width;
				} else {
					d.style.width = "100%";
				}
				if (obj.data.height) {
					d.style.height = obj.data.height;
				}
				if (typeof(obj.images[j]) != "string") {
					d.style.backgroundImage = "url('" + obj.images[j].src + "')";
				}
				if (obj.data.position) {
					d.style.backgroundPosition = obj.data.position;
					switch (obj.data.position) {
						case "right top":
						d.style.right = "0px";
						d.style.top = "0px";
						break;
						
						case "left top":
						d.style.left = "0px";
						d.style.top = "0px";
						break;
						
						default:
						d.style.left = "0px";
						d.style.top = "0px";
					}
				} else {
					d.style.left = "0px";
					d.style.top = "0px";
				}
				obj.element.appendChild(d);
				obj.images[j] = d;
			}
		}
		obj.index = 0;
		obj.timeout = null;
		obj.interval = null;
		rotating_images[rotating_images.length] = obj;
	}
}
if (rotating_images.length > 0) {
	for (i = 0; i < rotating_images.length; i++) {
		if (rotating_images[i].images.length > 0) {
			if (rotating_images[i].data.start != "hidden") {
				if (rotating_images[i].data.type == "background") {
					if (rotating_images[i].images[0].style.backgroundImage == "") {
						rotating_images[i].images[0].style.backgroundImage = "url('" + rotating_images[i].data.images[0] + "')";
					}
				}
				rotating_images[i].images[0].style.display = "block";
				
				if (rotating_images[i].images.length > 1 && rotating_images[i].data.start != "paused") {
					if (rotating_images[i].timeout === null) {
						if (rotating_images[i].data.delay) {
							rotating_images[i].timeout = setTimeout("rotating_images_autoAdvance(" + i + ")", rotating_images[i].data.delay * 1000);
						} else {
							rotating_images[i].timeout = setTimeout("rotating_images_autoAdvance(" + i + ")", rotating_images_default_delay * 1000);
						}
					}
				}
			}
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	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");
var slider_on_left = document.getElementById("slider-on-left");
var full_height_slider = document.getElementById("full-height-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);
	if (full_height_slider === null) {
		scrollbar_path_height -= 55;
	} else {
		for (i = 0; i < divs.length; i++) {
			if (divs[i].className == "scrollbar") {
				divs[i].style.top = 0 + "px";
			}
		}
	}
	
	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) {
		if (slider_on_left) {
			scrollarea_inner.style.left = "auto";
			scrollarea_inner.style.right = 0 + "px";
		}
		
		scrollbar.style.display = "block";
		if (slider_on_left) {
			scrollbar.style.left = 0 + "px";
			scrollbar.style.right = "auto";
		}
		
		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;
}

function resetParentScrollarea(garbage) {
	if (scrollarea !== null && scrollarea_inner !== null && scrollbar !== null && scrollbar_path !== null && scrollbar_slider !== null) {
		scrollbar_path_top = 16;
		scrollbar_slider_min_height = 10;
		slider_height = 40;
		
		drag_obj = new Object();
		
		new_nudge_position = null;
		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);
		if (full_height_slider === null) {
			scrollbar_path_height -= 55;
		} else {
			for (i = 0; i < divs.length; i++) {
				if (divs[i].className == "scrollbar") {
					divs[i].style.top = 0 + "px";
				}
			}
		}
		
		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) {
			if (slider_on_left) {
				scrollarea_inner.style.left = "auto";
				scrollarea_inner.style.right = 0 + "px";
			}
			
			scrollbar.style.display = "block";
			if (slider_on_left) {
				scrollbar.style.left = 0 + "px";
				scrollbar.style.right = "auto";
			}
			
			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";
			}
		}
	}
}

