var TitleChanger = new Class({
  initialize: function() {
  	this.timer = null;
		this.sections = $$('div.section');
		this.loadTitles();
		this.start();
  },
	loadTitles: function() {
		this.sections.each(function(section) {
			section.pageTitle = section.getAttribute('title');
      section.removeAttribute('title');
		});
	},
	start: function() {
		this.timer = this.setTitle.periodical(20, this);
	},
	stop: function() {
		this.timer = $clear(this.timer);
	},
	setTitle: function() {
		document.title = this.getCurrentSection().pageTitle;
	},
	getCurrentSection: function() {
		var current = null;
    this.sections.each(function(section){
			if (window.getScrollTop() >= section.getTop() - Math.ceil(window.getHeight() / 2)) {
        current = section;
      }
		});
    return current;
	}
});


var NavScroller = new Class({
  initialize: function(title_changer) {
    this.changer = title_changer;
    this.loadLinks();
    this.attachEvents();
  },
  loadLinks: function() {
    this.links = $$('ul.navigation li a');
  },
  attachEvents: function() {
    this.links.each(function(link) {
      link.onclick = this.moveWindow.bindWithEvent(this);
    }, this)
  },
  moveWindow: function(event) {
    var event = new Event(event);
		event.preventDefault();
		var element = this.getElementFromLink(event.target);
		if (window.getScrollTop() != element.getTop()) {
		  if (window.getScrollTop() > element.getTop()) { // Up
		    var calc = function(e) { return (window.getScrollTop() - Math.ceil((window.getScrollTop() - e.getTop()) / 2));}
        var comp = function(e) { return (window.getScrollTop() <= e.getTop()); }
		  } else { // Down
		    var calc = function(e) { 
           if ((window.getScrollHeight() - window.getHeight()) < e.getTop()) {
             return (window.getScrollTop() + Math.ceil(Math.abs(((window.getScrollHeight() - window.getHeight()) - window.getScrollTop()) / 2)));
           } else {
             return (window.getScrollTop() + Math.ceil((e.getTop() - window.getScrollTop()) / 2));
           }
         }
         var comp = function(e) { 
           return (window.getScrollTop() >= e.getTop() || window.getScrollTop() >= (window.getScrollHeight() - window.getHeight())); 
         }
		  }
		  
		  var changer = this.changer;
		  changer.stop();
		  var timer = (function() {
		    window.scrollTo(0, calc(element));
		    if (comp(element)) {
		      $clear(timer);
		      changer.start();
		      if (!window.khtml) {
             window.location.hash = element.getAttribute('id');
           }
		    }
		  }).periodical(50);
		}
  },
	getElementFromLink: function(link) {
	  return $(link.getAttribute('href').replace(/#/, ''));
	}
});

window.addEvent('domready', function() {
	var changer = new TitleChanger();
	var scroller = new NavScroller(changer);
});



	var duoPathBookshelf2Text = new Array(-197,-184,-170,-154,-137,-119,-101,-84,-68,-53,-41,-29,-20,-12,-5,-0,3,5,6,6,5,4,2,0);
	var duoPathBookshelf2Visual = new Array(-197,-198,-199,-200,-201,-201,-201,-200,-198,-195,-190,-185,-177,-169,-158,-145,-130,-113,-94,-75,-55,-35,-17,0);
	
	var duoPathCertified2Text = new Array(0,-15,-33,-52,-72,-93,-114,-133,-151,-167,-180,-191,-201,-208,-214,-218,-221,-223,-223,-223,-222,-221,-220,-218,-218);
	var duoPathCertified2Visual = new Array(0,0,2,3,4,5,5,5,3,0,-4,-10,-17,-27,-38,-51,-67,-85,-104,-125,-146,-166,-185,-203,-218);

	var duoPath2Text = new Array(0,-15,-33,-52,-72,-93,-114,-133,-151,-167,-180,-191,-201,-208,-214,-218,-221,-223,-223,-223,-222,-221,-220,-218,-218);
	var duoPath2Visual = new Array(0,0,2,3,4,5,5,5,3,0,-4,-10,-17,-27,-38,-51,-67,-85,-104,-125,-146,-166,-185,-203,-218);

	
	var pathIndex=0; 
	var moveID=null;
	
	delay=10;



	function moveBookshelf2Text()
	{
		clearTimeout(moveID);
		var yPos = duoPathBookshelf2Text[pathIndex];
		pathIndex+=1;
		document.getElementById('bookshelf').style.top=yPos+"px";
		
		if(pathIndex<duoPathBookshelf2Text.length-1)
		{
			moveID=setTimeout("moveBookshelf2Text()",delay);
		}
		else
		{
			document.getElementById("back").style.display="block";
		}
	}
	
	function moveBookshelf2Visual()
	{
		clearTimeout(moveID);
		if(document.getElementById("back").style.display=="block")
		{
			document.getElementById("back").style.display="none";
		}
		var yPos = duoPathBookshelf2Visual[pathIndex];
		pathIndex-=1;
		document.getElementById('bookshelf').style.top=yPos+"px";
		
		if(pathIndex>0){
			moveID=setTimeout("moveBookshelf2Visual()",delay);
		}
	}
	

var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable=no'
win = window.open(mypage,myname,settings)
}




	var duoPathPortfolio2Text = new Array(-197,-184,-170,-154,-137,-119,-101,-84,-68,-53,-41,-29,-20,-12,-5,-0,3,5,6,6,5,4,2,0);
	var duoPathPortfolio2Visual = new Array(-197,-198,-199,-200,-201,-201,-201,-200,-198,-195,-190,-185,-177,-169,-158,-145,-130,-113,-94,-75,-55,-35,-17,0);
	
	var duoPathCertified2Text = new Array(0,-15,-33,-52,-72,-93,-114,-133,-151,-167,-180,-191,-201,-208,-214,-218,-221,-223,-223,-223,-222,-221,-220,-218,-218);
	var duoPathCertified2Visual = new Array(0,0,2,3,4,5,5,5,3,0,-4,-10,-17,-27,-38,-51,-67,-85,-104,-125,-146,-166,-185,-203,-218);

	var duoPath2Text = new Array(0,-15,-33,-52,-72,-93,-114,-133,-151,-167,-180,-191,-201,-208,-214,-218,-221,-223,-223,-223,-222,-221,-220,-218,-218);
	var duoPath2Visual = new Array(0,0,2,3,4,5,5,5,3,0,-4,-10,-17,-27,-38,-51,-67,-85,-104,-125,-146,-166,-185,-203,-218);

	
	var pathIndex=0; 
	var moveID=null;
	
	delay=10;



	function movePortfolio2Text()
	{
		clearTimeout(moveID);
		var yPos = duoPathPortfolio2Text[pathIndex];
		pathIndex+=1;
		document.getElementById('portfolio').style.top=yPos+"px";
		
		if(pathIndex<duoPathPortfolio2Text.length-1)
		{
			moveID=setTimeout("movePortfolio2Text()",delay);
		}
		else
		{
			document.getElementById("portfolioback").style.display="block";
		}
	}
	
	function movePortfolio2Visual()
	{
		clearTimeout(moveID);
		if(document.getElementById("portfolioback").style.display=="block")
		{
			document.getElementById("portfolioback").style.display="none";
		}
		var yPos = duoPathPortfolio2Visual[pathIndex];
		pathIndex-=1;
		document.getElementById('portfolio').style.top=yPos+"px";
		
		if(pathIndex>0){
			moveID=setTimeout("movePortfolio2Visual()",delay);
		}
	}