var is_scrolling	= false;
var oldIndex		= null;
var menuLinks		= new Array();
var naviLinks		= new Array();

var nextLink		= null;
var previousLink	= null;

Event.observe(window, 'load', function() {
	/*
	*	page must start with a clean hash!
	*/
	if(window.location.hash != "") {
		window.location.href = window.location.protocol+"//"+window.location.host+window.location.pathname;
	}
	/*
	*	capture event after firing and prevent the action
	*/
	naviLinks = $$('#navigation_list li a');
	
	naviLinks.each(function(link) {
		link.observe('click', link_click);
		link.onclick = function() { return false; }
		menuLinks.push(link.getAttribute('href').substring(link.getAttribute('href').lastIndexOf('#')+1));
	});
	/*
	*	same for the scrolling links
	*/
	$$('#scroll_up a').first().observe('click', function() {
													activateLink(previousLink);
												});
	$$('#scroll_up a').first().onclick = function() { return false; }
	$$('#scroll_down a').first().observe('click', function() {
														activateLink(nextLink);		
												  });
	$$('#scroll_down a').first().onclick = function() { return false; }
	/*
	*	set first content focused
	*/
	activateLink(0);
});


function link_click() {
	var index = menuLinks.indexOf(this.getAttribute('href').substring(this.getAttribute('href').lastIndexOf('#')+1));
	activateLink(index);
}

function activateLink(index) {
	/*
	*	if content is still focusing return
	*/
	if(!is_scrolling) {
		/*
		*	if content is allready focused return
		*/
		if(index == oldIndex) {
			return;
		}
		/*
		*	if first content is selected there's no scrolling up
		*	if last content is selected there's no scrolling down either
		*/
		if(index == 0) {
			$$('#scroll_up a').first().setStyle({display:'none'});
		} else if(index == (menuLinks.length-1)) {
			$$('#scroll_down a').first().setStyle({display:'none'});
		}
		/*
		*	if scrolling links were disabled the last time make them visible again
		*/
		if(oldIndex == 0) {
			$$('#scroll_up a').first().setStyle({display:'block'});
		} else if(oldIndex == (menuLinks.length-1)) {
			$$('#scroll_down a').first().setStyle({display:'block'});
		}
		/*
		*	set the scrolling links to the next/previous content
		*/
		nextLink = index+1;
		previousLink = index-1;
		/*
		*	animate the contents to focus the selectet content
		*/
		is_scrolling = true;
		var target = $(menuLinks[index]);
		
		var top			= 155;
		var offset		= target.positionedOffset().top;
		
		var morph		= top - offset;
		var height		= target.getDimensions().height + 240 + 80;

		
		new Effect.Parallel([
		  new Effect.Morph($('content'), { 
		  								sync: true,
		  								style: 'top:'+morph+'px',
		  								transition:Effect.Transitions.sinoidal
		  							}), 
		  new Effect.Morph($('content_frame'), { 
		  								sync: true, 
		  								style: 'height:'+height+'px',
		  								transition:Effect.Transitions.sinoidal
		  							}) 
		], {
			
			duration:0.5,
			afterFinish:function() {
				is_scrolling = false;
			}
		});
		
		/*
		*	mark the selected link's content as focused
		*/
		if(oldIndex !== null) {
			naviLinks[oldIndex].removeClassName('active');
		}
		naviLinks[index].addClassName('active');
		
		oldIndex = index;
	}
}
