// JavaScript Document

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

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	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/22
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

//	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;
				}
				if (data == "prev") {
					rotating_images[i].index = rotating_images[i].index - 1;
					
					if (rotating_images[i].index < 0) {
						rotating_images[i].index = rotating_images[i].images.length - 1;
					}
				} else if (data == "next") {
					rotating_images[i].index = rotating_images[i].index + 1;
					
					if (rotating_images[i].index >= rotating_images[i].images.length) {
						rotating_images[i].index = 0;
					}
				} else {
					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.id) {
				rotating_images[i].data.id = "autoid-" + i;
			}
			
			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);
						}
					}
				}
				
				if (rotating_images[i].data.controls && rotating_images[i].images.length > 1) {
					rotating_images[i].controls = document.createElement("div");
					rotating_images[i].controls.style.display = "none";
					rotating_images[i].controls.style.position = "absolute";
					rotating_images[i].controls.style.right = 5 + "px";
					rotating_images[i].controls.style.bottom = 5 + "px";
					
					obj = document.createElement("img");
					obj.src = "images/_global/button-24x24-roundedGrey-prev.png";
					obj.width = 24;
					obj.height = 24;
					obj.style.cursor = "pointer";
					obj.style.marginRight = 5 + "px";
					f = "rotating_images_control_via_id('" + rotating_images[i].data.id + "', 'goto', 'prev');";
					if (obj.setAttribute) {
						obj.setAttribute("onclick", f);
					} else {
						obj.onclick = new Function(f);
					}
					rotating_images[i].controls.appendChild(obj);
					
					/*obj = document.createElement("img");
					obj.src = "images/_global/button-24x24-roundedGrey-pause.png";
					obj.width = 24;
					obj.height = 24;
					obj.style.cursor = "pointer";
					obj.style.marginRight = 5 + "px";
					rotating_images[i].controls.appendChild(obj);*/
					
					obj = document.createElement("img");
					obj.src = "images/_global/button-24x24-roundedGrey-next.png";
					obj.width = 24;
					obj.height = 24;
					obj.style.cursor = "pointer";
					f = "rotating_images_control_via_id('" + rotating_images[i].data.id + "', 'goto', 'next');";
					if (obj.setAttribute) {
						obj.setAttribute("onclick", f);
					} else {
						obj.onclick = new Function(f);
					}
					rotating_images[i].controls.appendChild(obj);
					
					rotating_images[i].element.appendChild(rotating_images[i].controls);
					
					rotating_images[i].element.onmouseover = new Function("rotating_images[" + i + "].controls.style.display='block';");
					rotating_images[i].element.onmouseout = new Function("rotating_images[" + i + "].controls.style.display='none';");
				}
			}
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Scrollareas
//////////
//////////	
//////////
//////////	Last modified: 2011/05/24
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

//	Define variables

var scrollareas = new Array();

var scrollbar_width = 24;
var scrollbar_path_top = 16;
var scrollbar_slider_min_height = 10;
var scrollbar_slider_height = 40;

//	Define functions

function startScrolling(e) {
	if (!e) {
		e = window.event;
		t = e.srcElement;
	} else {
		t = e.target;
	}
	
	for (i = 0; i < scrollareas.length; i++) {
		if (scrollareas[i].slider == t) {
			o = scrollareas[i];
		}
	}
	
	o.scroll_y = parseFloat(getStyle(o.slider, "top"));
	
	document.body.onmousemove = dragScroller;
	document.body.onmouseup = new Function("stopScrolling();");
	
	if (navigator.appName.indexOf("Microsoft") > -1) {
		o.scroll_y = e.clientY + document.documentElement.scrollTop + document.body.scrollTop - o.scroll_y;
		
		e.cancelBubble = true;
		e.returnValue = false;
		if (event.returnValue) {
			event.returnValue = false;
		}
	} else {
		o.scroll_y = e.clientY + window.scrollY - o.scroll_y;
		
		e.preventDefault();
	}
}

function dragScroller(e) {
	if (!e) {
		e = window.event;
		t = e.srcElement;
	} else {
		t = e.target;
	}
	
	for (i = 0; i < scrollareas.length; i++) {
		if (scrollareas[i].slider == t) {
			o = scrollareas[i];
		}
	}
	
	var y1, y2, scroll_ratio;
	
	if (navigator.appName.indexOf("Microsoft") > -1) {
		y1 = e.clientY + document.documentElement.scrollTop + document.body.scrollTop;
		
		e.cancelBubble = true;
		e.returnValue = false;
		if (event.returnValue) {
			event.returnValue = false;
		}
	} else {
		y1 = e.clientY + window.scrollY;
		
		e.preventDefault();
	}
	
	y2 = y1 - o.scroll_y;
	if (y2 < scrollbar_path_top) {
		y2 = scrollbar_path_top;
	} else if (y2 > o.path_height + scrollbar_path_top - o.slider_height) {
		y2 = o.path_height + scrollbar_path_top - o.slider_height;
	}
	
	scroll_ratio = parseInt(Math.round((y2 - scrollbar_path_top) / (o.path_height - o.slider_height) * 100)) * 0.01;
	
	o.inner.style.top = 0 - ((o.inner_height - o.height) * scroll_ratio) + "px";
	o.slider.style.top = y2 + "px";
}

function stopScrolling() {
	document.body.onmousemove = null;
	document.body.onmouseup = null;
}

function scrollByMouseWheel(e) {
	if (!e) {
		e = window.event;
		t = e.srcElement;
	} else {
		t = e.target;
	}
	
	o = null;
	
	for (i = 0; i < scrollareas.length && !o; i++) {
		p = t;
		while (p && !o) {
			if (p == scrollareas[i].element) {
				o = scrollareas[i];
			} else {
				p = p.parentNode;
			}
		}
	}
	
	var delta = 0;
	
	if (e.wheelDelta) {
		delta = -e.wheelDelta/120;
	} else if (e.detail) {
		delta = e.detail/3;
	}
	
	if (delta) {
		var y1, y2, scroll_ratio;
		
		y1 = parseFloat(getStyle(o.slider, "top"));
		
		if (delta < 0) {
			y2 = y1 - 3;
		} else {
			y2 = y1 + 3;
		}
		
		if (y2 < scrollbar_path_top) {
			y2 = scrollbar_path_top;
		} else if (y2 > o.path_height + scrollbar_path_top - o.slider_height) {
			y2 = o.path_height + scrollbar_path_top - o.slider_height;
		}
		
		scroll_ratio = parseInt(Math.round((y2 - scrollbar_path_top) / (o.path_height - o.slider_height) * 100)) * 0.01;
		
		o.inner.style.top = 0 - ((o.inner_height - o.height) * scroll_ratio) + "px";
		o.slider.style.top = y2 + "px";
	}
	
	if (e.preventDefault) {
		e.preventDefault();
	}
	e.returnValue = false;
	if (event.returnValue) {
		event.returnValue = false;
	}
}

function nudgeScrollerUp(e) {
	if (!e) {
		e = window.event;
		t = e.srcElement;
	} else {
		t = e.target;
	}
	
	for (i = 0; i < scrollareas.length; i++) {
		if (scrollareas[i].arrow_up == t) {
			o = scrollareas[i];
		}
	}
	
	var y1, y2, scroll_ratio;
	
	y1 = parseFloat(getStyle(o.inner, "top"));
	
	y2 = y1 + 20;
	
	if (y2 < 0 - (o.inner_height - o.height)) {
		y2 = 0 - (o.inner_height - o.height);
	} else if (y2 > 0) {
		y2 = 0;
	}
	
	scroll_ratio = 0 - (y2 / (o.inner_height - o.height));
	
	o.inner.style.top = y2 + "px";
	o.slider.style.top = scrollbar_path_top + (scroll_ratio * (o.path_height - o.slider_height)) + "px";
}

function nudgeScrollerDown(e) {
	if (!e) {
		e = window.event;
		t = e.srcElement;
	} else {
		t = e.target;
	}
	
	for (i = 0; i < scrollareas.length; i++) {
		if (scrollareas[i].arrow_down == t) {
			o = scrollareas[i];
		}
	}
	
	var y1, y2, scroll_ratio;
	
	y1 = parseFloat(getStyle(o.inner, "top"));
	
	y2 = y1 - 20;
	
	if (y2 < 0 - (o.inner_height - o.height)) {
		y2 = 0 - (o.inner_height - o.height);
	} else if (y2 > 0) {
		y2 = 0;
	}
	
	scroll_ratio = 0 - (y2 / (o.inner_height - o.height));
	
	o.inner.style.top = y2 + "px";
	o.slider.style.top = scrollbar_path_top + (scroll_ratio * (o.path_height - o.slider_height)) + "px";
}

function resetParentScrollarea(element) {
	if (typeof(element) == "string") {
		element = document.getElementById(element);
	} else if (typeof(element) != "object") {
		return false;
	}
	if (element === null) {
		return false;
	}
	
	var parentScrollarea = null;
	
	while (element.offsetParent !== null && parentScrollarea === null) {
		if (element.className == "scrollarea") {
			for (i = 0; i < scrollareas.length; i++) {
				if (scrollareas[i].element === element) {
					parentScrollarea = scrollareas[i];
				}
			}
		}
		
		element = element.offsetParent;
	}
	
	if (parentScrollarea !== null) {
		parentScrollarea.inner_height = parseFloat(getStyle(parentScrollarea.inner, "height"));
		
		if (parentScrollarea.inner_height > parentScrollarea.height) {
			parentScrollarea.scrollbar.style.display = "block";
			
			parentScrollarea.inner.style.width = (parentScrollarea.width - scrollbar_width) + "px";
			parentScrollarea.inner_height = parseFloat(getStyle(parentScrollarea.inner, "height"));
			
			parentScrollarea.path.style.height = parentScrollarea.path_height + "px";
			
			if (!parentScrollarea.element.addEventListener || !parentScrollarea.element.addEventListener("DOMMouseScroll", scrollByMouseWheel, false)) {
				parentScrollarea.element.onmousewheel = scrollByMouseWheel;
			}
			
			n = Math.round((parentScrollarea.height / parentScrollarea.inner_height) * parentScrollarea.path_height);
			if (n < scrollbar_slider_min_height) {
				n = scrollbar_slider_min_height;
			}
			
			parentScrollarea.slider_height = n;
			parentScrollarea.slider.style.height = n + "px";
		} else {
			parentScrollarea.inner.style.width = parentScrollarea.width + "px";
			
			if (parentScrollarea.height == 0) {
				parentScrollarea.element.style.overflow = "auto";
			}
		}
	} else {
		return "no";
	}
}

// Execute

for (i = 0; i < divs.length; i++) {
	if (divs[i].className == "scrollarea") {
		o = new Object();
		
		o.element = divs[i];
		
		o.inner = null;
		o.scrollbar = null;
		o.arrow_up = null;
		o.path = null;
		o.slider = null;
		o.arrow_down = null;
		
		o.width = parseFloat(getStyle(o.element, "width"));
		o.height = parseFloat(getStyle(o.element, "height"));
		
		o.path_height = o.height - (scrollbar_path_top * 2);
		o.slider_height = null;
		
		scrollareas[scrollareas.length] = o;
	} else if (divs[i].className == "inner") {
		for (j = 0; j < scrollareas.length; j++) {
			if (divs[i].parentNode == scrollareas[j].element) {
				scrollareas[j].inner = divs[i];
				scrollareas[j].inner_height = parseFloat(getStyle(divs[i], "height"));
			}
		}
	} else if (divs[i].className == "scrollbar") {
		for (j = 0; j < scrollareas.length; j++) {
			if (divs[i].parentNode == scrollareas[j].element) {
				scrollareas[j].scrollbar = divs[i];
			}
		}
	} else if (divs[i].className == "arrow-up") {
		for (j = 0; j < scrollareas.length; j++) {
			if (divs[i].parentNode == scrollareas[j].scrollbar) {
				scrollareas[j].arrow_up = divs[i];
				scrollareas[j].arrow_up.onmousedown = nudgeScrollerUp;
			}
		}
	} else if (divs[i].className == "path") {
		for (j = 0; j < scrollareas.length; j++) {
			if (divs[i].parentNode == scrollareas[j].scrollbar) {
				scrollareas[j].path = divs[i];
			}
		}
	} else if (divs[i].className == "slider") {
		for (j = 0; j < scrollareas.length; j++) {
			if (divs[i].parentNode == scrollareas[j].scrollbar) {
				scrollareas[j].slider = divs[i];
				scrollareas[j].slider.onmousedown = startScrolling;
			}
		}
	} else if (divs[i].className == "arrow-down") {
		for (j = 0; j < scrollareas.length; j++) {
			if (divs[i].parentNode == scrollareas[j].scrollbar) {
				scrollareas[j].arrow_down = divs[i];
				scrollareas[j].arrow_down.onmousedown = nudgeScrollerDown;
			}
		}
	}
}

for (i = 0; i < scrollareas.length; i++) {
	o = scrollareas[i];
	
	if (o.inner_height > o.height) {
		o.scrollbar.style.display = "block";
		
		o.inner.style.width = (o.width - scrollbar_width) + "px";
		o.inner_height = parseFloat(getStyle(o.inner, "height"));
		
		o.path.style.height = o.path_height + "px";
		
		if (!o.element.addEventListener || !o.element.addEventListener("DOMMouseScroll", scrollByMouseWheel, false)) {
			o.element.onmousewheel = scrollByMouseWheel;
		}
		
		n = Math.round((o.height / o.inner_height) * o.path_height);
		if (n < scrollbar_slider_min_height) {
			n = scrollbar_slider_min_height;
		}
		
		o.slider_height = n;
		o.slider.style.height = n + "px";
	} else {
		o.inner.style.width = o.width + "px";
		
		if (o.height == 0) {
			o.element.style.overflow = "auto";
		}
	}
}

