window.addEvent('domready', function(){
	$('mainWrapper').set('opacity', 0);
	$('contentWrapper').set('opacity', 0);
		
	showIntro();

	init_menuButtonsTop();
	init_menuButtonsSub();
});

//starts the intro and adds the entry point after 3,5 sec
function showIntro(){
	var introAni = new Element('div', {
		'class': 'intro',
		'id':'intro'
	});
	introAni.inject(document.body,'top');
	setTimeout("introAniEnter()",3500);
}

function introAniEnter(){
	var introAniEnter = new Element('div', {
		'class': 'introEnter',
		'events': {
			'click': function(e){
				e.stop();
				$('mainWrapper').set('tween', {duration: 200});
				$('mainWrapper').tween('opacity', 1);
				
				$('intro').set('tween', {duration: 350});
				$('intro').tween('opacity', 0);
				
				this.destroy();
			},
			'mouseover': function(){

			}
		}
	});
	introAniEnter.inject(document.body,'top');
}

//functions for the top menu bar
function init_menuButtonsTop(){
	var menuButtonLinks = $('top_nodes').getElements('img');
	for(i=0;i<menuButtonLinks.length;i++)
	{
		menuButtonLinks[i].addEvent("click", function(e){
			e.stop();
			var contentType = this.getParent().get('id');
			injectContent(contentType,'top',0);
		});
	}	
}

//functions for the sub menu bar
function init_menuButtonsSub(){
	var menuButtonLinks = $('sub_nodes').getElements('img');
	for(i=0;i<menuButtonLinks.length;i++)
	{
		menuButtonLinks[i].addEvent("click", function(e){
			e.stop();
			var contentType = this.getParent().get('id');
			injectContent(contentType,'sub',0);
		});
	}	
}

//injects the content depending on the contentType
function injectContent(contentType,node,contentPointNumber){

	var myHTMLRequest = new Request.HTML().get('./php/left.php?cID='+contentType+'&nt='+node);
	myHTMLRequest.success = function(responseHTML){		
		$('left').set('html',responseHTML); 
		fadeInTest($('contentWrapper'));
		
		var allSlides = $('left').getElements('ul');
		if(allSlides.length > 0)
		{
			slide(contentType,node);	
		}	
	};
	
	var myHTMLRequest_right = new Request.HTML().get('./php/right.php?cID='+contentType+'&nt='+node+'&cPN='+contentPointNumber);
	myHTMLRequest_right.success = function(responseHTML){
		$('right').set('html',responseHTML);
		hs.updateAnchors();
		fadeInTest($('contentWrapper'));
		
		var allGals = $('right').getElements('table');

		if(allGals.length > 1)
		{
			allGals.each(function(item, index){
				var allImages = item.getElements('img');
				
				allImages.each(function(item, index){
					item.set('opacity',0.4); 
					item.addEvent('mouseover',function(){this.set('tween', {duration: 200});this.tween('opacity', 1);});
					item.addEvent('mouseout',function(){this.set('tween', {duration: 200});this.tween('opacity', 0.4);});					
				});
			});		
		}
	};
}

//injects the content depending on the contentPoint
function injectContentRight(contentType,node,contentPointNumber){
	var myHTMLRequest_right = new Request.HTML().get('./php/right.php?cID='+contentType+'&nt='+node+'&cPN='+contentPointNumber);
	myHTMLRequest_right.success = function(responseHTML){
		$('right').set('html',responseHTML);
		hs.updateAnchors();
		fadeInTest($('contentWrapper'));
		
		var allGals = $('right').getElements('table');

		if(allGals.length > 1)
		{
			allGals.each(function(item, index){
				var allImages = item.getElements('img');
				
				allImages.each(function(item, index){
					item.set('opacity',0.4); 
					item.addEvent('mouseover',function(){this.set('tween', {duration: 200});this.tween('opacity', 1);});
					item.addEvent('mouseout',function(){this.set('tween', {duration: 200});this.tween('opacity', 0.4);});					
				});
			});		
		}
	};
}

//FX Stuff
function slide(contentType,node){
	var list = $$('#accordion li div.collapse');
	var headings = $$('#accordion li span');
	var collapsibles = new Array();
					
	headings.each( function(heading, i) {
		var collapsible = new Fx.Slide(list[i], { 
			duration: 500, 
			transition: Fx.Transitions.quadIn
		});
							
		collapsibles[i] = collapsible;
						
		heading.onclick = function(){
			var cpNumber = heading.get('id');
			injectContentRight(contentType,node,cpNumber);
			
			for(var j = 0; j < collapsibles.length; j++){
				if(j!=i) {
					collapsibles[j].slideOut();
				}
			}
			
			collapsible.toggle();
			
			return false;
		}
		
		if(i!=0)
		collapsible.hide();
	});
}

function firstSlideOpen(collapsibles)
{
	collapsibles[0].slideIn();
}

function fadeOutTest(el)
{
	if(el.get('opacity')==1)
	{
		el.set('tween', {duration: 200});
		el.tween('opacity', 0);
	}		
}

function fadeInTest(el)
{
	if(el.get('opacity')==0)
	{
		el.set('tween', {duration: 200});
		el.tween('opacity', 1);
	}		
}

