Math.mtRand = function (min, max) {
	return Math.floor(Math.random() * (max - min + 1)) + min;
};

var body = null,
	elmWrapper = null,
	elmHeader = null,
	elmContainer = null,
	elmMain = null,
	elmFooter = null;

window.onload = function () {
	elmWrapper = document.getElementById('wrapper');
	body = elmWrapper.parentNode;
	elmHeader = document.getElementById('header');
	elmContainer = document.getElementById('container');
	elmMain = document.getElementById('main');
	elmFooter = document.getElementById('footer');
	
	navigation.init();
	layercontrol.init();
	asyncLayer.rebuildStaticLinks();
	imageSlider.init();
	jobAzubis.init();
};

/** Navigation **/
var navigation = new function () {
	var naviNodes = [],
		hoverBar = null,
		naviReady = false,
		naviDefault = null,
		naviDefaultFlag = true;
	
	this.init = function () {
		var nav = $('.mod_mainnav')[0];
		if (nav) {
			naviNodes = nav.children[1].children;
			hoverBar = document.createElement('div');
			hoverBar.className = 'hoverBar';
			nav.appendChild(hoverBar);
			nav.onmouseout = naviToDefaultHandler;
			for (var i = 0, iLen = naviNodes.length; i < iLen; i++) {
				naviNodes[i].children[0].onmouseover = hover;
				if ($(naviNodes[i]).hasClass('active')) {
					naviDefault = naviNodes[i].children[0];
					naviToDefault();
				}
			}
		}
		
	};
	
	function hover() {
		naviDefaultFlag = false;
		animate(this);
	};
	function animate(elm) {
		var left = $(elm).position().left + 15,
			width = elm.clientWidth - 30;
		if (naviReady) {
			// hoverBar.style.left = left + 'px';
			// hoverBar.style.width = width + 'px';
			// $(hoverBar).stop().animate({left: left, width: width}, 300, 'linear');
			// $(hoverBar).stop().animate({left: left, width: width}, 700, 'easeOutExpo');
			$(hoverBar).stop().animate({left: left, width: width}, 500, 'easeOutBack');
			// $(hoverBar).stop().animate({left: left, width: width}, 500, 'easeOutBounce');
			// $(hoverBar).stop().animate({left: left, width: width}, 1500, 'easeOutElastic');
		}
		else {
			naviReady = true;
			hoverBar.style.left = left + 'px';
			hoverBar.style.width = width + 'px';
		}
	}

	function naviToDefaultHandler() {
		naviDefaultFlag = true;
		window.setTimeout(naviToDefault, 200);
	}
	function naviToDefault() {
		if (naviDefaultFlag) {
			naviDefaultFlag = false;
			if (naviDefault) {
				if (naviReady) {
					animate(naviDefault);
				}
				else {
					naviReady = true;
					hoverBar.style.left = ($(naviDefault).position().left + 15) + 'px';
					hoverBar.style.width = (naviDefault.clientWidth - 30) + 'px';
				}
			}
			else {
				naviReady = false;
				$(hoverBar).stop();
				hoverBar.style.width = '0px';
			}
		}
	}
};

/** Layer-Funktionen (zurück, ein-/ausklappen, schließen) **/
var layercontrol = new function () {
	var bgImg = '',
		blur = null,
		btnToggleLayer = null,
		mouseXReal = 0,
		mouseXSlide = window.innerWidth / 2,
		sliderXMax = null,
		working = false,
		sliding = false,
		closed = false;
	
	this.init = function () {
		bgImg = $(body).css('background-image');
		bgImg = bgImg.match(/url\(\"?([^\"\(\)]+)\"?\)/i)[1];
		if (bgImg.lastIndexOf('-blur.jpg') !== -1) {
			btnToggleLayer = $('.mod_layercontrol .btnToggleLayer')[0];
			blur = document.createElement('img');
			blur.id = 'bgblur';
			blur.src = bgImg;
			body.insertBefore(blur, elmWrapper);
			bgImg = bgImg.substring(0, bgImg.length - 9) + '.jpg',
			body.style.backgroundImage = 'url(' + bgImg + ')';
		}
	};
	
	function layerOpenCallback() {
		working = false;
	}
	function layerCloseCallback() {
		elmContainer.className = 'closed';
		working = false;
	}
	
	this.back = function () {
		history.back();
	};
	this.toggleLayer = function () {
		if (!working) {
			working = true;
			if (closed) {
				closed = false;
				btnToggleLayer.className = 'btnToggleLayer';
				elmContainer.className = '';
				$(elmHeader).animate({marginTop: '0px'}, 300);
				$(elmFooter).animate({bottom: '0px'}, 300);
				$(elmContainer).animate({height: '645px'}, 300, layerOpenCallback);
				if (blur) {
					$(blur).animate({opacity: 1}, 300);
				}
			}
			else {
				closed = true;
				btnToggleLayer.className = 'btnToggleLayer closed';
				$(elmHeader).animate({marginTop: '-45px'}, 300);
				$(elmFooter).animate({bottom: '-20px'}, 300);
				$(elmContainer).animate({height: '50px'}, 300, layerCloseCallback);
				if (blur) {
					$(blur).animate({opacity: 0}, 300);
				}
			}
		}
	};
	this.hideLayer = function () {
		var mod_navbuttons = $('.mod_navbuttons')[0];
		if (mod_navbuttons) {
			// mod_navbuttons.style.display = 'block';
			// mod_navbuttons.style.height = (window.innerHeight - 80) + 'px';
			var img = new Image();
			img.src = bgImg;
			sliderXMax = img.width;
			window.onmousemove = slideBackground;
		}
		elmContainer.style.display = 'none';
		if (blur) {
			$(blur).animate({opacity: 0}, 150);
		}
	};
	this.closeJoblayer = function () {
		asyncLayer.removeLayer();
	};
	
	function slideBackground(event) {
		mouseXReal = event.clientX;
		if (!sliding && mouseXReal !== mouseXSlide) {
			sliding = true;
			speed = 0;
			move();
		}
	}
	function move() {
		var diff = Math.round(Math.abs(mouseXReal - mouseXSlide) / 10);
		speed += 5;
		if (speed < 1) {
			speed = 1;
		}
		if (speed > diff) {
			speed = diff;
		}
		if (mouseXReal > mouseXSlide) {
			mouseXSlide += speed;
		}
		else {
			mouseXSlide -= speed;
		}
		reflow();
		if (mouseXReal !== mouseXSlide) {
			window.setTimeout(move, 30);
		}
		else {
			sliding = false;
		}
	}
	function reflow() {
		var factor = mouseXSlide / window.innerWidth;
			left = -factor * (2480 - window.innerWidth);
		body.style.backgroundPosition = left + 'px center';
		// $('.mod_navbuttons')[0].style.left = left + 'px';
		// var a = $('.mod_navbuttons').children('a');
		// a[0].style.marginLeft = (factor * 220) + 'px';
		// a[1].style.marginLeft = (factor * 200) + 'px';
		// a[2].style.marginLeft = (factor * 440) + 'px';
		// a[3].style.marginLeft = (factor * 100) + 'px';
		// a[4].style.marginLeft = (factor * 280) + 'px';
	}
};

/** Startseite (asynchrones Laden der Jobseiten im Layer) **/
var asyncLayer = new function () {
	var indexJoblayer = null;
	
	this.rebuildStaticLinks = function () {
		var layer = document.getElementById('indexJoblayer');
		if (layer) {
			indexJoblayer = layer.parentNode.removeChild(layer);
			var a = $('.asyncLayer a');
			for (var i = 0, iLen = a.length; i < iLen; i++) {
				var url = a[i].href;
				a[i].href = 'javascript:asyncLayer.load(\'' + url + '\');';
			}
		}
	};
	
	function showLayer(html) {
		var bgClass = '',
			tmpClass = html.match(/<body[^<>]*class=\"([a-z0-9 -]*)\"[^<>]*>/i)[1].split(' ');
		for (var i = 0, iLen = tmpClass.length; i < iLen; i++) {
			if (tmpClass[i].substring(0, 5) === 'page-') {
				bgClass = tmpClass[i];
				break;
			}
		}
		var start = html.indexOf('<section id="main">'),
			end = html.lastIndexOf('</section>');
		html = html.substring(start, end);
		start = html.indexOf('<div class="inside">')
		html = html.substring(start);
		
		var layer = indexJoblayer.cloneNode(false);
		layer.className += ' ' + bgClass;
		layer.innerHTML = html;
		elmWrapper.insertBefore(layer, elmFooter);
		
		jobpagenav.init();
		jobAzubis.init();
	}
	this.load = function (url) {
		$.ajax({
			url: url,
			dataType: 'html',
			success: function (html) {
				showLayer(html);
			}
		});
	};
	this.removeLayer = function () {
		var layer = document.getElementById('indexJoblayer');
		if (layer) {
			layer.parentNode.removeChild(layer);
		}
	};
};

/** Videomodul zum Öffnen eines Videos in der Lightbox **/
var videoteaser = new function () {
	var lightbox = null,
		source = [];
	
	this.init = function (newSource) {
		source = newSource;
		var a = $('.mod_video a');
		for (var i = 0, iLen = a.length; i < iLen; i++) {
			a[i].onclick = openLightbox;
		}
	};
	
	function openLightbox() {
		if (!lightbox) {
			lightbox = document.createElement('div');
			lightbox.className = 'mod_lightbox';
			// lightbox.onclick = closeLightbox;
			var html = '<video width="640" height="424" controls="controls">';
			for (var i = 0, iLen = source.length; i < iLen; i++) {
				html += '<source src="' + source[i].src + '" type="' + source[i].type + '" />';
			}
			html += '</video><div class="infoMeta"><a onclick="videoteaser.closeLightbox();">Schließen</a></div>';
			lightbox.innerHTML = html;
			body.appendChild(lightbox);
			// $(lightbox).children('img')[0].onload = scaleLightbox;
		}
	}
	/* function scaleLightbox() {
		var title = $(lightbox).children('h2')[0],
			text = $(lightbox).children('.infoText')[0],
			width = $(lightbox).children('img')[0].width;
		width = Math.max(width, 400);
		width = Math.min(width, 640);
		title.style.width = width + 'px';
		text.style.width = width + 'px';
		title.style.display = 'block';
		text.style.display = 'block';
	} */
	this.closeLightbox = function () {
		if (lightbox) {
			body.removeChild(lightbox);
			lightbox = null;
		}
	}
};

/** hmmh-Blätter auf der Home-Seite **/
var hmmhleafs = new function () {
	var leafcontainer = null,
		leafPath = '/tl_files/azubiweb/img/leafs/',
		leafRepo = {
			blue: [
				'3D95D0_2_0.png', '3D95D0_2_1.png', '3D95D0_2_2.png', '3D95D0_2_3.png',
				'3D95D0_3_0.png', '3D95D0_3_1.png', '3D95D0_3_2.png', '3D95D0_3_3.png'
			],
			green: [
				'A2BD30_2_0.png', 'A2BD30_2_1.png', 'A2BD30_2_2.png', 'A2BD30_2_3.png',
				'A2BD30_3_0.png', 'A2BD30_3_1.png', 'A2BD30_3_2.png', 'A2BD30_3_3.png'
			]
		},
		leaf = [];
	
	var Leaf = function (src) {
		var self = this,
			x = null,
			y = null;
		
		this.node = document.createElement('img');
		this.node.src = leafPath + src;
		
		this.setPosition = function (newX, newY) {
			x = newX;
			y = newY;
			self.node.style.left = newX + 'px';
			self.node.style.top = newY + 'px';
		};
	};
	leaf.push(new Leaf(leafRepo.blue[7]));
	leaf.push(new Leaf(leafRepo.blue[2]));
	leaf.push(new Leaf(leafRepo.green[0]));
	leaf.push(new Leaf(leafRepo.green[5]));
	
	this.init = function () {
		leafcontainer = $('.mod_hmmhleafs')[0];
		if (leafcontainer) {
			leaf.push(new Leaf(leafRepo.blue[Math.mtRand(0, 7)]));
			leaf.push(new Leaf(leafRepo.blue[Math.mtRand(0, 7)]));
			leaf.push(new Leaf(leafRepo.blue[Math.mtRand(0, 7)]));
			leaf.push(new Leaf(leafRepo.blue[Math.mtRand(0, 7)]));
			leaf.push(new Leaf(leafRepo.green[Math.mtRand(0, 7)]));
			leaf.push(new Leaf(leafRepo.green[Math.mtRand(0, 7)]));
			leaf.push(new Leaf(leafRepo.green[Math.mtRand(0, 7)]));
			leaf.push(new Leaf(leafRepo.green[Math.mtRand(0, 7)]));
			
			for (var i = 0, iLen = leaf.length; i < iLen; i++) {
				var x = 20 + Math.mtRand(0, 34) * 25,
					y = Math.mtRand(0, 4) * 25;
				leaf[i].setPosition(x, y);
				leaf[i].node.onmouseover = mousemove;
				leafcontainer.appendChild(leaf[i].node);
			}
			
			leafcontainer.onclick = buildLogo;
		}
	};
	
	function mousemove(event) {
		var x = event.layerX - 25,
			y = event.layerY - 25,
			xySum = Math.abs(x) + Math.abs(y),
			diffX = 0,
			diffY = 0;
		if (x !== 0) {
			diffX = 10 * x / xySum;
		}
		if (y !== 0) {
			diffY = 10 * y / xySum;
		}
		var newTop = this.offsetTop - 5 * diffY,
			newLeft = this.offsetLeft - 5 * diffX;
		
		$(this).stop().animate({top: newTop, left: newLeft}, 200);
		// this.style.top = (this.offsetTop - diffY) + 'px';
		// this.style.left = (this.offsetLeft - diffX) + 'px';
	}
	function buildLogo() {
		leafcontainer.onclick = null;
		for (var i = 0, iLen = leaf.length; i < iLen; i++) {
			leaf[i].node.onmouseover = null;
		}
		
		$(leaf[0].node).animate({top: 37, left: 410}, Math.mtRand(500, 800));
		$(leaf[1].node).animate({top: 37, left: 460}, Math.mtRand(500, 800));
		$(leaf[2].node).animate({top: 62, left: 410}, Math.mtRand(500, 800));
		$(leaf[3].node).animate({top: 62, left: 460}, Math.mtRand(500, 800));
		for (var i = 4, iLen = leaf.length; i < iLen; i++) {
			var top = Math.mtRand(-50, 940),
				left = Math.mtRand(0, 1) * 990 - 50;
			$(leaf[i].node).animate({top: top, left: left}, Math.mtRand(500, 800));
		}
	}
};

/** Layernavigation auf Aktuelles **/
var newsnav = new function () {
	var page = [],
		naviElm = [],
		hoverBar = null,
		naviReady = false,
		naviDefault = null,
		naviDefaultFlag = true;
	
	this.init = function () {
		var nav = $('.mod_layernaviNewsnav'),
			newslist = nav.parent().children('.mod_article').children('.mod_newslist');
		page = newslist.children('.layout_full');
		for (var i = 0, iLen = page.length; i < iLen; i++) {
			page[i].style.display = 'none';
			var date = $(page[i]).children('.info')[0],
				image = $(page[i]).children('.image_container').children('img')[0];
			if (date && image) {
				var title = image.alt;
				if (title === '') {
					title = $(date).children()[0].innerHTML.substr(0, 10);
				}
				var elm = document.createElement('a');
				elm.innerHTML = '<img src="' + image.src + '" alt="" /><span class="title"><span>' + title + '</span></span>';
				elm.articleIndex = i;
				elm.onclick = changeArticle;
				elm.onmouseover = hover;
				nav[0].appendChild(elm);
				naviElm.push(elm);
			}
		}
		page[0].style.display = '';
		hoverBar = document.createElement('div');
		hoverBar.className = 'hoverBar';
		nav[0].appendChild(hoverBar);
		nav[0].onmouseout = naviToDefaultHandler;
		naviDefault = naviElm[0];
		naviToDefault();
		
		var scrollLeft = nav.children('.scrollLeft')[0],
			scrollRight = nav.children('.scrollRight')[0],
			pagination = newslist.children('.pagination').children('ul'),
			pagePrev = pagination.children('li.previous').children('a')[0],
			pageNext = pagination.children('li.next').children('a')[0];
		if (pagePrev) {
			scrollLeft.href = pagePrev.href;
		}
		else {
			scrollLeft.className += ' inactive';
		}
		if (pageNext) {
			scrollRight.href = pageNext.href;
		}
		else {
			scrollRight.className += ' inactive';
		}
	};
	
	function changeArticle() {
		var articleIndex = this.articleIndex;
		for (var i = 0, iLen = page.length; i < iLen; i++) {
			page[i].style.display = 'none';
		}
		page[articleIndex].style.display = '';
		naviDefault = this;
	}
	
	function hover() {
		naviDefaultFlag = false;
		animate(this);
	};
	function animate(elm) {
		var left = $(elm.children[1].children[0]).position().left - 5,
			width = elm.children[1].children[0].offsetWidth + 10;
		if (naviReady) {
			// hoverBar.style.left = left + 'px';
			// hoverBar.style.width = width + 'px';
			// $(hoverBar).stop().animate({left: left, width: width}, 300, 'linear');
			// $(hoverBar).stop().animate({left: left, width: width}, 700, 'easeOutExpo');
			$(hoverBar).stop().animate({left: left, width: width}, 500, 'easeOutBack');
			// $(hoverBar).stop().animate({left: left, width: width}, 500, 'easeOutBounce');
			// $(hoverBar).stop().animate({left: left, width: width}, 1500, 'easeOutElastic');
		}
		else {
			naviReady = true;
			hoverBar.style.left = left + 'px';
			hoverBar.style.width = width + 'px';
		}
	}
	
	function naviToDefaultHandler() {
		naviDefaultFlag = true;
		window.setTimeout(naviToDefault, 200);
	}
	function naviToDefault() {
		if (naviDefaultFlag) {
			naviDefaultFlag = false;
			if (naviDefault) {
				if (naviReady) {
					animate(naviDefault);
				}
				else {
					naviReady = true;
					hoverBar.style.left = ($(naviDefault.children[1].children[0]).position().left - 5) + 'px';
					hoverBar.style.width = (naviDefault.children[1].children[0].offsetWidth + 10) + 'px';
				}
			}
			else {
				naviReady = false;
				$(hoverBar).stop();
				hoverBar.style.width = '0px';
			}
		}
	}
};

/** Image slider **/
var imageSlider = new function () {
	var imgContainer = [],
		pos = 0;
	
	this.init = function () {
		var slider = $('.mod_imageSlider');
		for (var i = 0, iLen = slider.length; i < iLen; i++) {
			imgContainer[i] = slider[i].children[0];
			var url = imgContainer[i].children[0].src;
			imgContainer[i].style.backgroundImage = 'url(' + url + ')';
			imgContainer[i].removeChild(imgContainer[i].children[0]);
		}
		if (slider.length > 0) {
			animate();
		}
	};
	
	function animate() {
		pos -= 50;
		for (var i = 0, iLen = imgContainer.length; i < iLen; i++) {
			$(imgContainer[i]).animate({'background-position': pos}, 1100, 'linear', animate);
		}
	}
};

/** Hover-Effekte auf der Ausbildungsseite (Fahrstuhl) **/
var jobnav = new function () {
	var self = this,
		hoverElm = {};
	
	this.show = function (id) {
		var elm = null;
		if (typeof id === 'string') {
			if (hoverElm[id]) {
				elm = hoverElm[id];
			}
			else {
				elm = document.getElementById(id);
				if (elm) {
					hoverElm[id] = elm;
					elm.onmouseover = self.show;
					elm.onmouseout = self.hide;
				}
				else {
					return false;
				}
			}
		}
		else {
			elm = this;
		}
		$(elm).stop().animate({height: 440}, 300);
	};
	this.hide = function (id) {
		var elm = null;
		if (typeof id === 'string') {
			elm = hoverElm[id] || document.getElementById(id);
			if (!elm) {
				return false;
			}
		}
		else {
			elm = this;
		}
		$(elm).stop().animate({height: 0}, 300);
	};
};

/** Layernavigation auf den Ausbildungsseiten (Tiefenseite) **/
var jobpagenav = new function () {
	var nav = null,
		page = [],
		naviElm = [null],
		hoverBar = null,
		naviReady = false,
		naviDefault = null,
		naviDefaultFlag = true;
	
	this.init = function () {
		nav = $('.mod_layernaviJobpagenav')[0];
		page = $(nav).parent().children('.mod_article');
		page[0].style.display = 'block';
		page[1].style.display = 'none';
		for (var i = 1, iLen = page.length; i < iLen; i++) {
			var headline = $(page[i]).children('.ce_text').children('h2')[0] || $(page[i]).children('.ce_headline')[0];
			var imgIndex = i;
			if (imgIndex === 5) {
				imgIndex = 0;
			}
			var image = $(page[imgIndex]).children('.ce_image').children('.image_container').children('img')[0];
			if (headline && image) {
				var elm = document.createElement('a');
				elm.innerHTML = '<img src="' + image.src + '" alt="" /><span class="title"><span>' + headline.innerHTML + '</span></span>';
				elm.articleIndex = i;
				elm.onclick = changeArticle;
				elm.onmouseover = hover;
				nav.appendChild(elm);
				naviElm.push(elm);
			}
		}
		hoverBar = document.createElement('div');
		hoverBar.className = 'hoverBar';
		nav.appendChild(hoverBar);
		nav.onmouseout = naviToDefaultHandler;
	};
	
	function changeArticle() {
		var articleIndex = this.articleIndex;
		for (var i = 0, iLen = page.length; i < iLen; i++) {
			page[i].style.display = 'none';
		}
		page[articleIndex].style.display = 'block';
		naviDefault = this;
	}
	
	function hover() {
		naviDefaultFlag = false;
		animate(this);
	};
	function animate(elm) {
		var left = $(elm.children[1].children[0]).position().left - 5,
			width = elm.children[1].children[0].offsetWidth + 10;
		if (naviReady) {
			// hoverBar.style.left = left + 'px';
			// hoverBar.style.width = width + 'px';
			// $(hoverBar).stop().animate({left: left, width: width}, 300, 'linear');
			// $(hoverBar).stop().animate({left: left, width: width}, 700, 'easeOutExpo');
			$(hoverBar).stop().animate({left: left, width: width}, 500, 'easeOutBack');
			// $(hoverBar).stop().animate({left: left, width: width}, 500, 'easeOutBounce');
			// $(hoverBar).stop().animate({left: left, width: width}, 1500, 'easeOutElastic');
		}
		else {
			naviReady = true;
			hoverBar.style.left = left + 'px';
			hoverBar.style.width = width + 'px';
		}
	}

	function naviToDefaultHandler() {
		naviDefaultFlag = true;
		window.setTimeout(naviToDefault, 200);
	}
	function naviToDefault() {
		if (naviDefaultFlag) {
			naviDefaultFlag = false;
			if (naviDefault) {
				if (naviReady) {
					animate(naviDefault);
				}
				else {
					naviReady = true;
					hoverBar.style.left = ($(naviDefault.children[1].children[0]).position().left - 5) + 'px';
					hoverBar.style.width = (naviDefault.children[1].children[0].offsetWidth + 10) + 'px';
				}
			}
			else {
				naviReady = false;
				$(hoverBar).stop();
				hoverBar.style.width = '0px';
			}
		}
	}
};

/** Automatischer Bildwechsel auf "Die Azubis" (Ausbildungsseite) **/
var jobAzubis = new function () {
	var imgArr = [],
		imgCount = 0,
		currentImgIndex = 0,
		interval = null;
	
	this.init = function () {
		imgArr = $('.job_azubis .ce_image');
		imgCount = imgArr.length;
		if (imgCount > 0) {
			imgArr[0].style.display = 'block';
			window.clearInterval(interval);
			if (imgCount > 1) {
				interval = window.setInterval(changeImg, 2500);
			}
		}
	};
	
	function changeImg() {
		for (var i = 0; i < imgCount; i++) {
			imgArr[i].style.display = '';
		}
		currentImgIndex++;
		if (currentImgIndex > imgCount - 1) {
			currentImgIndex -= imgCount;
		}
		imgArr[currentImgIndex].style.display = 'block';
	}
};

/** Layernavigation der Galerie **/
var gallerynav = new function () {
	var lightbox = null,
		infoTitle = null,
		infoText = null,
		img = [],
		nextNode = null,
		prevNode = null;
	
	this.init = function () {
		var nav = $('.mod_layernaviGallerynav'),
			newslist = nav.parent().children('.mod_article').children('.mod_newslist');
		infoTitle = nav.children('h2')[0];
		infoText = nav.children('.infoText')[0];
		img = newslist.children('.layout_full');
		for (var i = 0, iLen = img.length; i < iLen; i++) {
			img[i].onmouseover = showImageInfo;
			img[i].onmouseout = hideImageInfo;
			img[i].onclick = openLightbox;
		}
		var scrollLeft = nav.children('.scrollLeft')[0],
			scrollRight = nav.children('.scrollRight')[0],
			pagination = newslist.children('.pagination').children('ul'),
			pagePrev = pagination.children('li.previous').children('a')[0],
			pageNext = pagination.children('li.next').children('a')[0];
		if (pagePrev) {
			scrollLeft.href = pagePrev.href;
		}
		else {
			scrollLeft.className += ' inactive';
		}
		if (pageNext) {
			scrollRight.href = pageNext.href;
		}
		else {
			scrollRight.className += ' inactive';
		}
	};
	
	function showImageInfo() {
		infoTitle.innerHTML = $(this).children('h2')[0].innerHTML;
		infoText.innerHTML = $(this).children('.ce_text')[0].innerHTML;
	}
	function hideImageInfo() {
		infoTitle.innerHTML = '';
		infoText.innerHTML = '';
	}
	function openLightbox(e, node) {
		if (!node) {
			node = this;
		}
		if (!lightbox) {
			lightbox = document.createElement('div');
			lightbox.className = 'mod_lightbox';
			var src = $(node).children('.image_container').children('img')[0].src;
			if (src.lastIndexOf('-thumb') !== -1) {
				var fe = src.substring(src.length - 4);
				src = src.substring(0, src.length - 10) + fe;
			}
			var prev = $(node).prev()[0],
				next = $(node).next()[0];
			var a = document.createElement('a');
			a.className = 'prev';
			if (prev && prev.children[0].className === 'image_container') {
				prevNode = prev;
				a.onclick = switchPrev;
			}
			else {
				a.style.opacity = 0.3;
				a.style.cursor = 'default';
			}
			lightbox.appendChild(a);
			var a = document.createElement('a');
			a.className = 'next';
			if (next && next.children[0].className === 'image_container') {
				nextNode = next;
				a.onclick = switchNext;
			}
			else {
				a.style.opacity = 0.3;
				a.style.cursor = 'default';
			}
			lightbox.appendChild(a);
			var elm = document.createElement('img');
			elm.src = src;
			elm.onclick = closeLightbox;
			lightbox.appendChild(elm);
			elm = document.createElement('h2');
			elm.innerHTML = $(node).children('h2')[0].innerHTML;
			lightbox.appendChild(elm);
			elm = document.createElement('div');
			elm.className = 'infoText';
			elm.innerHTML = $(node).children('.ce_text')[0].innerHTML;
			lightbox.appendChild(elm);
			// lightbox.innerHTML += '<img src="' + src + '" alt="" /><h2>' + $(node).children('h2')[0].innerHTML + '</h2><div class="infoText">' + $(node).children('.ce_text')[0].innerHTML + '</div>';
			body.appendChild(lightbox);
			$(lightbox).children('img')[0].onload = scaleLightbox;
		}
	}
	function scaleLightbox() {
		var navarrows = $(lightbox).children('.prev, .next'),
			img = $(lightbox).children('img')[0],
			title = $(lightbox).children('h2')[0],
			text = $(lightbox).children('.infoText')[0],
			offset = img.offsetLeft - 65,
			width = img.width;
		width = Math.max(width, 400);
		width = Math.min(width, 640);
		for (var i = 0, iLen = navarrows.length; i < iLen; i++) {
			navarrows[i].style.marginTop = img.offsetTop + 'px';
			navarrows[i].style.height = img.height + 'px';
			if (navarrows[i].className === 'prev') {
				navarrows[i].style.marginLeft = offset + 'px';
			}
			else if (navarrows[i].className === 'next') {
				navarrows[i].style.marginRight = offset + 'px';
			}
		}
		title.style.width = width + 'px';
		text.style.width = width + 'px';
		title.style.display = 'block';
		text.style.display = 'block';
	}
	function switchNext() {
		body.removeChild(lightbox);
		lightbox = null;
		openLightbox(null, nextNode);
	}
	function switchPrev() {
		body.removeChild(lightbox);
		lightbox = null;
		openLightbox(null, prevNode);
	}
	function closeLightbox() {
		if (lightbox) {
			body.removeChild(lightbox);
			lightbox = null;
			nextNode = null;
			prevNode = null;
		}
	}
};

/** Google Maps **/
var googleMaps = new function () {
	var elmMap = null,
		map = null;
	
	this.init = function (elmId) {
		elmMap = document.getElementById(elmId);
		var latlng = new google.maps.LatLng(53.08246171213656, 8.788225650787354),
			options = {
				zoom: 14,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
		map = new google.maps.Map(elmMap, options);
		new google.maps.Marker({
			position: latlng,
			map: map,
			title: 'hmmh'
		});
	};
};


/** Google Analytics tracking code **/
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-26904929-1']);
_gaq.push(['_gat._anonymizeIp']);
_gaq.push(['_trackPageview']);
(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


