var havetshus = {

	isFrontpage: document.body.className.indexOf('p_aaa') != -1,

	diver: {
		benchmarkValue: 0,
		benchmarkMin: 100,
		funcCalled: false,
		isFrontpage: document.body.className.indexOf('p_aaa') != -1,
		initialize: function() {
			this.benchmark();
			if (this.isFrontpage)
				this.initializeDivers();
			else {
				var elm = this.getFlashHeader();
				if (elm)
					elm.style.visibility = 'hidden';
				this.initializeLand();
			}
		},

		benchmark: function() {
			var mark = secoya.cookie.get('benchmark');
			if (mark) {
				this.benchmarkValue = mark;
				return;
			}
			var div = document.createElement('div');
			with (div.style) {
				position = 'absolute';
				left = '0';
				top = '0';
				width = '100%';
				height = '540px';
				background = 'white';
				zIndex = '40';
			}
			lightCore.setOpacity(div, 1);
			var parent = document.body;
			parent.appendChild(div);
			var index = 0;
			var start = null;
			var me = this;
			var interval = setInterval(function() {
					if (!start)
						start = new Date();
					index += 10;
					div.style.top = -index+'px';
					var diff = (new Date()).getTime() - start.getTime();
					if (diff > 1000) {
							clearInterval(interval);
							me.benchmarkValue = index;
							div.parentNode.removeChild(div);
							secoya.cookie.set('benchmark', me.benchmarkValue, 60*60*24*7); // save the benchmark for one week
					}
			}, 1);
		},

		initializeLand: function() {
			var me = this;
			var link = document.getElementById('site_nav').getElementsByTagName('h3')[0].getElementsByTagName('a')[0];
			
			link.onclick = function() {
				var href = this.getAttribute('href');
				var target = this.getAttribute('target');
				me.toLand(function(){
					if (target == '_blank')
						window.open(href);
					else
						window.location.href = href;
				});
				return false;
			};
		},

		initializeDivers: function() {
			var me = this;
			var submit = function() {
				var form = this;
				me.dive(function(){
					form.submit();
				});
				return false;
			};
			var makemouseup = function(elm, href, target, object) {
				var mouseup = function(e) {
					e = e || window.event;
					if (e.preventDefault && e.stopPropagation) {
						e.preventDefault();
						e.stopPropagation();
					}
					e.cancelBubble = true;
					me.dive(function(){
						if (target == '_blank')
							window.open(href);
						else
							window.location.href = href;
					});
					return false;
				};
				var click = function(e) {
					e = e || window.event;
					if (e.preventDefault && e.stopPropagation) {
						e.preventDefault();
						e.stopPropagation();
					}
					e.cancelBubble = true;
					return false;
				};
				elm.onmouseup = mouseup;
				elm.onclick = click;
				if (object) {
					object.onmouseup = mouseup;
					object.onclick = click;
				}
			};

			var menus = document.getElementsByTagName('ul');
			for (var i = 0; i < menus.length; i++) {
				var anchors = menus[i].getElementsByTagName('a');
				for (var j = 0; j < anchors.length; j++)
					makemouseup(anchors[j], anchors[j].getAttribute('href'), anchors[j].getAttribute('target'), anchors[j].getElementsByTagName('object')[0]);
			}
			var q = document.getElementById('quicklinks');
			if (q) {
				q = q.childNodes;
				for (var i = 0; i < q.length; i++) {
					if (q[i].nodeType == 1 && q[i].className.indexOf('item') != -1) {
						var a = q[i].getElementsByTagName('a')[0];
						if (a) {
							makemouseup(q[i], a.getAttribute('href'), a.getAttribute('target'), a);
						} else {
							q[i].className += ' nolink';
						}
					}
				}
			}
			var forms = document.getElementsByTagName('form');

			for (i = 0; i < forms.length; i++) {
				forms[i].onsubmit = submit;
			}
		},

		fadeContent: function(endFunc) {
			if (lightCore.browser.getName() == 'IE' && lightCore.browser.getVersion() <= 8)
				return endFunc();

			var elm = document.getElementById('main');
			
			var opacity = 100, step = -5;
			var interval = setInterval(function(){
				if (opacity <= 0) {
					elm.parentNode.removeChild(elm);
					clearInterval(interval);
					endFunc();
				} else {
					lightCore.setOpacity(elm, opacity);
					opacity += step;
				}
			}, 30);
		},

		getFlashHeader: function() {
			var elm = document.getElementById('splash');
			if (elm)
				elm = elm.getElementsByTagName('object')[0];
			return elm;
		},

		fadeHeader: function(endFunc) {
			var elm = this.getFlashHeader();
			if (elm) {
				elm.style.visibility = 'hidden';
				return endFunc();
				var opacity = 100, step = -5;
				var interval = setInterval(function(){
					if (opacity <= 0) {
						elm.style.visibility = 'hidden';
						clearInterval(interval);
						endFunc();
					} else {
						lightCore.setOpacity(elm, opacity);
						opacity += step;
					}
				}, 30);
			} else {
				endFunc();
			}
		},

		doDive: function(endFunc, fadeFunc, start, end, step) {
			if (this.funcCalled || this.benchmarkValue < this.benchmarkMin) {
				endFunc();
			} else {
				var me = this;
				this[fadeFunc](function(){
					me.move(endFunc, start, end, step);
				});
			}
			this.funcCalled = true;
		},

		toLand: function(endFunc) {
			this.doDive(endFunc, 'fadeContent', 576, 0, -7);
		},

		dive: function(endFunc) {
			this.doDive(endFunc, 'fadeHeader', 0, 576, 7);
		},

		move: function(endFunc, start, stop, increment) {
/*			var tags = ['object', 'embed'];
			for (var tag = 0; tag < tags.length; tag++) {
				var objects = document.getElementsByTagName(tags[tag]);
				for (var i = 0; i < elms.length; i++) {
					if (objects[i].StopPlay)
						objects[i].StopPlay();
				}
			}*/
			
			var x=start, y=start, max=Math.max(start, stop), bg = document.body, elm = document.getElementById('site');
			var intervalId = setInterval(function(){
				x += increment;
				if (x < max/2)
					y = max*(Math.pow(x*2, 3))/Math.pow(max, 3)/2;
				else
					y = max*Math.pow((x-max)*2, 3)/Math.pow(max, 3)/2+max;
				bg.style.backgroundPosition = '50% '+(-y)+'px';
				elm.style.marginTop = -y+'px';
				if ((increment > 0 && x >= stop) || increment < 0 && x <= stop) {
					clearInterval(intervalId);
					endFunc();
				}
			}, 40);
		}
	},

	lightbox: {
		galleries: [],
		initialized: false,
		initialize: function() {
			var me = this;
			setTimeout(function(){
				me.doInitialize();
			}, 1);
		},

		doInitialize: function() {
			this.initialized = true;
			if (core)
				return;
			var createThumbs = function(thumbs, uid) {
				var func = function() {
					me.changeImage(this, uid);
					return false;
				};
				for (var i = 0; i < thumbs.length; i++) {
					thumbs[i].onclick = func;
				}
			};
			var thumbs, uid, me = this;
			for (var i = 0; i < this.galleries.length; i++) {
				uid = this.galleries[i];
				thumbs = document.getElementById('thumbs_'+uid);
				if (thumbs) {
					createThumbs(thumbs = thumbs.getElementsByTagName('a'), uid);
					if (thumbs[0])
						thumbs[0].onclick();
				}
			}
			this.galleries = [];
		},

		changeImage: function(anchor, uid) {
			var image = document.getElementById('image_'+uid);
			if (image) {
				while (image.firstChild) {
					image.removeChild(image.lastChild);
				}
				var img = document.createElement('img');
				img.onload = function() {
					lightCore.fixpng(image);
				}
				img.setAttribute('src', anchor.getAttribute('href'));
				image.appendChild(img);
				var txt = document.createElement('p');
				var title = anchor.getAttribute('title');
				if (!title)
					title = '';
				txt.appendChild(document.createTextNode(title));
				image.appendChild(txt);
			}
		},

		addGallery: function(uid) {
			this.galleries.push(uid);
			if (this.initialized)
				this.doInitialize();
		}
	},

	flashnews: {
		id: null,
		height: 521,
		minWidth: 78,
		maxWidth: 944,
		getElement: function() {
			var div = document.getElementById(this.id);
			var result = null;
			if (div) {
				result = div.getElementsByTagName('object')[0];
				if (!result)
					result = div.getElementsByTagName('embed')[0];
				if (!result)
					result = null;
			}
			return result;
		},

		initialize: function() {
			var id = document.getElementById('flash_news') ? 'flash_news' : 'header_news';
			this.id = id;
			var me = this;
			var func = function() {
				var obj = me.getElement();
				if (obj) {
					obj.onmouseover = function() {
						me.show();
					};
					obj.onmouseout = function() {
						me.hide();
					};
				} else {
					setTimeout(func, 300);
				}
			};
			setTimeout(func, 1);
		},

		show: function() {
			var elm = this.getElement();
			if (elm) {
				document.getElementById(this.id).className = 'visible';
				elm.style.width = this.maxWidth+'px';
			}
		},

		hide: function() {
			var elm = this.getElement();
			if (elm) {
				document.getElementById(this.id).className = '';
				elm.style.width = this.minWidth+'px';
			}
		},

		showFull: function() {
			var elm = this.getElement();
			if (elm) {
				elm.onmouseout = null;
			}
		},

		hideFull: function() {
			var me = this;
			var elm = this.getElement();
			if (elm) {
				elm.onmouseout = function(){
					me.hide();
				};
				me.hide();
			}
		}
	},

	initialize: function() {
		if (!core) {
			this.diver.initialize();
			this.lightbox.initialize();
		}
	}

};

lightCore.registerInit(havetshus);

function showfull() {
	havetshus.flashnews.showFull();
}

function hidefull() {
	havetshus.flashnews.hideFull();
}