window.addEvent('load', function() {
	initDom();
});

function initDom() {
	if ($('events-container')) {
		initScrollbar($('events-container'), $('events-scrollbar'), $('events-handle'));
		initScrollbarButtons('events');
		initThumbButtons('events');
		initBigImgClick('events');
	}
	if ($('konzeption-container')) {
		$$('.csc-frame').inject($('konzeption-container-box'), 'top');
		initScrollbar($('konzeption-container'), $('konzeption-scrollbar'), $('konzeption-handle'));
		initScrollbarButtons('konzeption');
		initThumbButtons('konzeption');
		initBigImgClick('konzeption');
	}
	if ($('inside-container')) {
		$$('.csc-frame').inject($('inside-container-box'), 'top');
		initScrollbar($('inside-container'), $('inside-scrollbar'), $('inside-handle'));
		initScrollbarButtons('inside');
		initThumbButtons('inside');
		initBigImgClick('inside');
	}
	if ($('nlarchive')) {
		initScrollbar($('nlarchive'), $('nlarchive-scrollbar'), $('nlarchive-handle'));
		initScrollbarButtons('nlarchive');
	}

	if (document.URL.test('impressum')) {
		openImprint();
	}

	if ($('insideSubMenu')) {
		$('insideSubMenu').inject($('inside-container-box'), 'bottom');
	}
}

var objSlider;
function initScrollbar(content, scrollbar, handle, horizontal, ignoreMouse) {

	var steps = (horizontal ? (content.getScrollSize().x - content.getSize().x) : (content.getScrollSize().y - content.getSize().y));
	objSlider = new Slider(scrollbar, handle, {
		steps: steps,
		mode: (horizontal ? 'horizontal' : 'vertical'),
		onChange: function(step) {
			var x = (horizontal ? step : 0);
			var y = (horizontal ? 0 : step);
			content.scrollTo(x, y);
		}
	}).set(0);

	if (!(ignoreMouse)) {
		$$(content, scrollbar).addEvent('mousewheel', function(e) {
			e = new Event(e).stop();
			var step = objSlider.step - e.wheel * 30;
			objSlider.set(step);
		});
	}

	$(document.body).addEvent('mouseleave', function() { objSlider.drag.stop(); });
}

function initScrollbarButtons(sPage) {
	$(sPage + '-scrollbar-arrow-top').addEvent('click', function () { objSlider.set((objSlider.step - 15)); });
	$(sPage + '-scrollbar-arrow-top').addEvent('mouseover', function() { this.setStyle('background', 'url(fileadmin/p/img/btn-scrollbar-top.gif) no-repeat'); });
	$(sPage + '-scrollbar-arrow-top').addEvent('mouseout', function() { this.setStyle('background', 'none'); });
	$(sPage + '-scrollbar-arrow-btm').addEvent('click', function () { objSlider.set((objSlider.step + 15)); });
	$(sPage + '-scrollbar-arrow-btm').addEvent('mouseover', function() { this.setStyle('background', 'url(fileadmin/p/img/btn-scrollbar-btm.gif) no-repeat'); });
	$(sPage + '-scrollbar-arrow-btm').addEvent('mouseout', function() { this.setStyle('background', 'none'); });
}

var iImageScroll = 0;
var iImageLength = 0;
var sBigImageBoxBackground = '';
function initThumbButtons(sPage) {
	if ($(sPage + '-big-btn-left')) {
		$(sPage + '-big-btn-left').addEvent('click', function() { scrollBig('bck', sPage); });
		$(sPage + '-big-btn-left').addEvent('mouseover', function() { this.setStyle('background', 'url(fileadmin/p/img/btn-left-ovr.gif) no-repeat'); });
		$(sPage + '-big-btn-left').addEvent('mouseout', function() { this.setStyle('background', 'url(fileadmin/p/img/btn-left.gif) no-repeat'); });
	}
	if ($(sPage + '-big-btn-right')) {
		$(sPage + '-big-btn-right').addEvent('click', function() { scrollBig('fwd', sPage); });
		$(sPage + '-big-btn-right').addEvent('mouseover', function() { this.setStyle('background', 'url(fileadmin/p/img/btn-right-ovr.gif) no-repeat'); });
		$(sPage + '-big-btn-right').addEvent('mouseout', function() { this.setStyle('background', 'url(fileadmin/p/img/btn-right.gif) no-repeat'); });
	}
	if ($(sPage + '-big-image-box')) {
		sBigImageBoxBackground = $(sPage + '-big-image-box').getStyle('background-image');
	}
	if ($$('.' + sPage + '-item-image')) {
		$$('.' + sPage + '-item-image').each(function(obj) {
			obj.addEvent('click', function() {
				iImageScroll = obj.getFirst().getProperty('value');
				openThumbsBig(obj, sPage);
			});
			obj.addEvent('contextmenu', function(e) {
				var e = new Event(e).stop();
			});
			obj.getFirst().setProperty('value', iImageLength);
			iImageLength++;
		});
		openThumbsBig($$('.' + sPage + '-item-image')[0], sPage);
	}
}
function openThumbsBig(obj, sPage) {
	var iId = obj.getParent().getProperty('id').replace('img_', '');
	$(sPage + '-big-image').setProperty('src', 'index.php?id=58&tx_ttnews[tt_news]=' + iId);

	var sDesc = obj.getNext().innerHTML;
	if (sDesc != '') {
		$(sPage + '-big-image-box').setStyle('background-image', sBigImageBoxBackground);
		$(sPage + '-big-desc-txt').innerHTML = sDesc;
		$(sPage + '-big-desc-hover-box').setStyle('display', 'block');
		$(sPage + '-big-desc-hover').addEvent('mouseover', function() { $(sPage + '-big-desc-box').setStyle('display', 'block'); });
		$(sPage + '-big-desc-hover').addEvent('mouseout', function() { $(sPage + '-big-desc-box').setStyle('display', 'none'); });
	}
	else {
		$(sPage + '-big-desc-hover-box').setStyle('display', 'none');
		$(sPage + '-big-desc-hover').removeEvents();
		$(sPage + '-big-image-box').setStyle('background-image', 'none');
		$(sPage + '-big-desc-box').setStyle('display', 'none');
		$(sPage + '-big-desc-txt').innerHTML = '';
	}

	if (iImageScroll == 0) {
		$(sPage + '-big-btn-left').setStyle('display', 'none');
	}
	else if (iImageScroll > 0) {
		$(sPage + '-big-btn-left').setStyle('display', 'block');
	}
	if (iImageScroll == (iImageLength - 1)) {
		$(sPage + '-big-btn-right').setStyle('display', 'none');
	}
	else if (iImageScroll < (iImageLength - 1)) {
		$(sPage + '-big-btn-right').setStyle('display', 'block');
	}
}
function scrollBig(sDirection, sPage) {
	if (sDirection=='fwd') {
		if (iImageScroll < (iImageLength - 1)) {
			iImageScroll++;
		}
		openThumbsBig($$('.' + sPage + '-item-image')[iImageScroll], sPage);
	}
	else if (sDirection=='bck') {
		if (iImageScroll > 0) {
			iImageScroll--;
		}
		openThumbsBig($$('.' + sPage + '-item-image')[iImageScroll], sPage);
	}
}

var sCurrentPage = '';
function initBigImgClick(sPage) {
	sCurrentPage = sPage;
	/*if ($(sPage + '-big-image')) {
		$(sPage + '-big-image').addEvent('click', function() { scrollBig('fwd', sPage) });
		$(sPage + '-big-image').addEvent('contextmenu', function(e) {
			var e = new Event(e).stop();
		});
	}*/
}
function clickBigImg() {
	scrollBig('fwd', sCurrentPage);
}

function openImprint() {
	$('imprintWrap').setStyle('display', 'block');
}
function closeImprint() {
	$('imprintWrap').setStyle('display', 'none');
}

function closeThx() {
	$('thxWrap').setStyle('display', 'none');
}

function openArchive() {
	$('nlarchiveWrap').setStyle('visibility', 'visible');
}

