window.ardk = (function(){
	function getId(id) {
		return document.getElementById(id);
	};
	function getElementsByClassName(className, tag) {
		if (this.getElementsByClassName) {
			return this.getElementsByClassName(className);
		}
		if (!tag)
			tag = '*';
		var results = [];
		var elements = this.getElementsByTagName(tag);
		className = new RegExp('(?:^|\\s)'+className+'(?:\\s|$)');
		for (var i = 0; i < elements.length; i++) {
			if (className.test(elements[i].className)) {
				results.push(elements[i]);
			}
		}
		return results;
	};

	(function ie6(){
		var browser = lightCore.getBrowser();
		if (browser.getName() == 'IE' && browser.getVersion() < 7) {
			function mouseover() {
				this.className += ' hover';
			};
			function mouseout() {
				this.className = this.className.replace(/ hover/, '');
			};
			var spots = getId('spots');
			if (spots) {
				var e = getElementsByClassName.call(spots, 'spot', 'div');
				for (var i = 0; i < e.length; i++) {
					e[i].onmouseover = mouseover;
					e[i].onmouseout = mouseout;
				}
			}
		}
	})();
	(function calendarArrows() {
		var scrollDiv, fx = new secoya.fx({
			from: 0,
			to: 0,
			duration: 0.3,
			type: function(x) {return secoya.fx.power(x, 5);},
			callback: function(x) {
				scrollDiv.scrollTop = x;
			}
		});
		function scroll(div, add) {
			if (fx.running)
				fx.resetToEnd();
			scrollDiv = div;
			fx.from = div.scrollTop;
			fx.to = div.scrollTop + add;
			fx.start();
		};
		function setup(calendar) {
			var up = getElementsByClassName.call(calendar, 'up')[0];
			var down = getElementsByClassName.call(calendar, 'down')[0];
			calendar = getElementsByClassName.call(calendar, 'items')[0];
			if (up && down) {
				up.onclick = function() {
					scroll(calendar, -77);
				};
				down.onclick = function() {
					scroll(calendar, 77);
				};
			}
		};
		var s = getId('spots');
		if (s) {
			var calendars = getElementsByClassName.call(s, 'calendar', 'div');
			for (var i = 0; i < calendars.length; i++) {
				setup(calendars[i]);
			}
		}
	})();
	var slideshows = [];
	var timeouts = {};
	function slideshow(){
		for (var i = 0; i < slideshows.length; i++)
			slideshows[i].resetToEnd();
		slideshows = [];
		for (i in timeouts) {
			clearTimeout(timeouts[i]);
		}
		timeouts = {};

		var spots = getId('spots');
		function setupSlideshow(parent, className) {
			var
				fx,
				elements = getElementsByClassName.call(parent, className),
				index = elements.length-1,
				element,
				nextElement,
				run = function() {
					element.style.zIndex = 0;
					element = nextElement;
					if (--index < 0)
						index = elements.length-1;
					nextElement = elements[index];
					element.style.zIndex = 2;
					nextElement.style.zIndex = 1;
					lightCore.setOpacity(nextElement, 100);
					fx.start();
				};
			if (elements.length > 1) {
				element = elements[index--];
				nextElement = elements[index];
				element.style.zIndex = 1;
				nextElement.style.zIndex = 2;
				slideshows.push(fx = new secoya.fx({
					from: 100,
					to: 0,
					duration: 1,
					callback: function(x) {
						lightCore.setOpacity(element, x);
					},
					finish: function() {
						var id = setTimeout(run, 5000);
						element.style.zIndex = 1;
						nextElement.style.zIndex = 2;
						timeouts[id] = true;
						setTimeout(function(){
							delete timeouts[id];
						}, 5000);
					}
				}));
				setTimeout(run, 5000);
			}
		};
		if (spots) {
			var elements = getElementsByClassName.call(spots, 'slideshow', 'div'), elm;
			for (var i = 0; i < elements.length; i++) {
				elm = getElementsByClassName.call(elements[i], 'images', 'div');
				if (elm.length)
					setupSlideshow(elm[0], 'image');
			}
		}
	};
	slideshow();
	function calendar() {
		function click() {
			var child = this;
			do {
				child = child.nextSibling;
			} while(child && child.nodeType != 1);
			if (child) {
				if (child.className.indexOf('hidden') == -1) {
					child.className = 'child hidden';
					this.className = this.className.replace(/\s*expanded\s*/, '');
				} else {
					child.className = 'child';
					this.className += ' expanded';
				}
			}
		};
		var t = getId('contentText');
		if (t) {
			t = getElementsByClassName.call(t, 'calendar', 'table')[0];
			if (t) {
				t = t.getElementsByTagName('tr');
				for (var i = 0; i < t.length; i++) {
					if (t[i].className.indexOf('parent') != -1) {
						t[i].onclick = click;
					}
				}
			}
		}
	};

	calendar();

	var splash = (function(){
		var
			fx,
			elements = [],
			index = 0,
			element,
			nextElement,
			timeout,
			selector,
			running = !core,
			run = function(setIndex) {
				selector.childNodes[index].className = 's';
				element.style.zIndex = 0;
				element = nextElement;
				if (typeof setIndex == 'number') {
					index = setIndex;
				} else if (++index >= elements.length)
					index = 0;
				selector.childNodes[index].className = 's active';
				nextElement = elements[index];
				element.style.zIndex = 2;
				nextElement.style.zIndex = 1;
				lightCore.setOpacity(nextElement, 100);
				fx.start();
			};
		function createSelector() {
			if (selector && selector.parentNode) {
				selector.parentNode.removeChild(selector);
			}
			selector = document.createElement('div');
			selector.className = 'selector';
			var div;
			for (var i = 0; i < elements.length; i++) {
				div = document.createElement('div');
				div.className = 's';
				div.appendChild(document.createTextNode(i+1));
				div.onclick = function(event) {
					clearTimeout(timeout);
					fx.resetToEnd();
					running = false;
					var s = getId('splash');
					if (s)
						s.className = 'show';
					var newIndex = parseInt(this.firstChild.nodeValue)-1;
					if (newIndex != index)
						run(newIndex);
				};
				selector.appendChild(div);
			}
			if (elements.length) {
				if (elements.length <= index)
					selector.childNodes[index].className = 's active';
				else
					selector.firstChild.className = 's active';
			}
			return selector;
		};
		return function(reloadItems) {
			var s = getId('splash');
			clearTimeout(timeout);
			if (fx && !reloadItems) {
				fx.kill();
				running = !core;
			}
			if (s) {
				elements = getElementsByClassName.call(s, 'item', 'div');
				s.appendChild(createSelector());
				if (elements.length > 1) {
					if (!reloadItems)
						index = Math.min(elements.length-1, index);
					element = elements[index];
					nextElement = elements[0];
					index = 0;
					element.style.zIndex = 1;
					nextElement.style.zIndex = 2;
					fx = new secoya.fx({
						from: 100,
						to: 0,
						duration: 1,
						callback: function(x) {
							lightCore.setOpacity(element, x);
						},
						finish: function() {
							if (running)
								timeout = setTimeout(function(){run();}, 5000);
							element.style.zIndex = 1;
							nextElement.style.zIndex = 2;
						}
					});
					if (running)
						timeout = setTimeout(function(){run();}, 5000);
				}
			}
		};
	})();

	splash();

	return {
		calendar: calendar,
		splash: splash,
		slideshow: function() {
			if (core) {
				slideshow();
			}
		},
		toggleLogin: function(self) {
			var loginForm = document.getElementById("loginForm");
			var main = document.getElementById("main");
			
			main.appendChild(loginForm);
			var login = self.parentNode.parentNode;
			var hidden = login.className == 'login';
			var overlay = getId('loginOverlay');
			if (overlay) {
				loginForm.className = '';
				overlay.parentNode.removeChild(overlay);
			} else {
				loginForm.className = 'login_visible';
				var div = document.createElement('div');
				div.id = 'loginOverlay';
				div.onclick = function() {
					ardk.toggleLogin(self);
				};
				document.body.appendChild(div);
			}
		},
		togglePassword: function(self) {
			var form = self.parentNode.parentNode.getElementsByTagName('form')[0];
		},
		toggleLightbox: (function() {
			var
				lightbox,
				overlay,
				className = 'hidden',
				fx = new secoya.fx({
					from: 0,
					to: 96,
					duration: 0.5,
					callback: function(x) {
						lightCore.setOpacity(overlay, x);
					},
					finish: function() {
						overlay.className = lightbox.className = className;
					}
				});
			function show() {
				className = overlay.className = 'visible';
				lightCore.setOpacity(overlay, 0);
				fx.resetToEnd();
				fx.from = 0;
				fx.to = 96;
				fx.start();
			};
			function hide() {
				className = lightbox.className = 'hidden';
				fx.resetToEnd();
				fx.to = 0;
				fx.from = 96;
				fx.start();
			};
			function visible() {
				return overlay.className == 'visible';
			};
			var lastShownElement = null;
			function decorateElement(h3) {
				var next = h3;
				do {
					next = next.nextSibling;
				} while (next && next.nodeType != 1 && next.className != 'menuitem');
				if (next) {
					h3.onclick = function() {
						if (lastShownElement)
							lastShownElement.className = 'menuitem';
						next.className = 'menuitem visible';
						lastShownElement = next;
					}
				}
			};
			function decorate() {
				var h3s = lightbox.getElementsByTagName('h3');
				for (var i = 0; i < h3s.length; i++) {
					decorateElement(h3s[i]);
				}
				return h3s;
			};
			var result = function(start) {
				lightbox = getId('lightbox');
				overlay = getId('lightboxOverlay');
				if (lightbox) {
					if (visible()) {
						hide();
					} else {
						show();
						var h3s = decorate();
						if (h3s.length) {
							if (!start || !isNumber(start))
								start = 1;
							start--;
							start = Math.max(0, start);
							start = Math.min(h3s.length-1, start);
							h3s[start].onclick();
						}
					}
				}
			};
			result.reload = decorate;
			return result;
		})()
	};
})();

function toggleLightbox(start) {
	ardk.toggleLightbox(start);
};