window.addEvent('domready', function() {	
	
	//$('leftNavi').setStyle('height', $('leftNavi').getComputedSize().height); //??
	var naviTable = $('zwei_spalten');
	var contentContainer = $('contentTD').getElements('div');
	var contentTdHeight = 0;
	contentContainer.each(function(item, index) {
		contentTdHeight += item.getStyle('height').toInt();
	})
	//naviTable.setStyle('height', contentTdHeight.toInt())
	
	var blockRows = $$($('leftNavi').getElements('div.navibox'));
	var blockHeads = $$(blockRows.getFirst('div'));
	var blockContents = $$(blockHeads.getNext('div'));
	
	blockHeads.setStyle('position', 'relative');
	blockContents.setStyles({'margin-top': '-7px', 'overflow': 'hidden'});
	
	var blockHeights = new Array();
	var blockPaddingTop = new Array();
	var blockPaddingBottom = new Array()
	blockHeads.each(function(item, index) {
		var content = item.getNext('div');
		var contentHeight = content.getSize().y;
		blockHeights.push(contentHeight);
		blockPaddingTop.push(content.getStyle('padding-top').toInt());
		blockPaddingBottom.push(content.getStyle('padding-bottom').toInt());
		
		if (index + 1 != blockHeads.length)
			content.getParent('td').setStyle('height', 42);
		else {
			var leftRows = naviTable.getElements('td');
			var lastHeight = contentTdHeight.toInt();
			leftRows.each(function(item, index) {
				if (item.getProperty('id') != 'contentTD' && item != content.getParent('td')) {
					lastHeight = lastHeight - item.getComputedSize().totalHeight;
				}
			})
			if (lastHeight.toInt() > 42)
				content.getParent('td').setStyle('height', lastHeight.toInt());
		}
		
		if (document.cookie.indexOf(item.getProperty('class') + '=') != -1) {
			var cookiePos = document.cookie.indexOf(item.getProperty('class') + '=');
			if (document.cookie.substring(cookiePos + item.getProperty('class').length + 1, cookiePos + item.getProperty('class').length + 1 + 5) == 'close') {
				content.setStyles({
					'height': 0,
					'padding-top': 0,
					'padding-bottom': 0
				});
			}
		}
	})
	
	var lastBox = $(naviTable.getElement('tbody').getLast('tr').getFirst('td'));
	var lastHeight = lastBox.getStyle('height').toInt();
     
	blockHeads.addEvent('click', function() {
		var content = this.getNext('div');
                
		var i = blockHeads.indexOf(this);
		
		if (content.getStyle('height').toInt() == 0) { // ist geschlossen wird geöffnet
			if (this.getParent() != lastBox) {
				var lastTween = new Fx.Tween(lastBox);
				lastTween.start('height', lastBox.getStyle('height').toInt() - (blockHeights[i] - blockPaddingTop[i] - blockPaddingBottom[i]));
				lastHeight = lastBox.getStyle('height').toInt();
			}
			var contentMorph = new Fx.Morph(content, {
				onComplete: function() {
					//if (i + 1 != blockHeads.length)
						//content.getParent('td').setStyle('height', blockHeights[i] + 42);
				}
			});
			contentMorph.start({
				'height': blockHeights[i] - blockPaddingTop[i] - blockPaddingBottom[i],
				'padding-top': blockPaddingTop[i],
				'padding-bottom': blockPaddingBottom[i]
			});
			document.cookie = this.getProperty('class') + '=open';
		} else {
			if (this.getParent() != lastBox) {
//                                console.log('this.getParent()=\n')
//                                console.log(this.getParent())
//                                console.log('lastBox=')
//                                console.log(lastBox)
//				var lastTween = new Fx.Tween(lastBox);
//				lastTween.start('height', lastBox.getStyle('height').toInt() + (blockHeights[i] - blockPaddingTop[i] - blockPaddingBottom[i]));
//				lastHeight = lastBox.getStyle('height').toInt();

			}
			var contentMorph = new Fx.Morph(content);
			contentMorph.start({
				'height': 0,
				'padding-top': 0,
				'padding-bottom': 0
			});
			if (i + 1 != blockHeads.length) { // wenn es nicht die letzte Box ist
//                                console.log('this.getParent()=\n')
//                                console.log(this.getParent())
//                                console.log('lastBox=')
//                                console.log(lastBox)
				if (this.getParent() != lastBox){                                    
					content.getParent('td').setStyle('height', 42);
                                }
				else{
					content.getParent('td').setStyle('height', lastHeight);
                                }
			}
			document.cookie = this.getProperty('class') + '=close';
		}
	});
	
	
	var topButton = $$('div.n_Ob2').getElement('span');
	if (topButton.length > 0) {
		topButton[0].addEvent('click', function() {
			var scrollFX = new Fx.Scroll(window).toTop();
		})
	}

})
