// Author: Thuan Huynh <thuan@gmx.fr>
// Special effects are under a GPL license. You can use it freely but need to give credits to the author 
// and never use deritative works commercially. For further contact, please email the author.
 

 // The two following functions have been written by Sam Stephenson <sam@conio.net> 
var Class = {
  create: function() {return function() {this.initialize.apply(this, arguments);}}
}

Function.prototype.bind = function(object) {
  var __method = this;
  return function() {__method.apply(object, arguments);}
}

function $(objectName) {return document.getElementById(objectName);}

// --------------------------------
// 2-COLUMNS NAVIGATION

var Turning = new Class.create();

Turning.prototype = {
	initialize: function(nav, mScroll) {
		this.nav = {l:$(nav[0]), r:$(nav[1])};
		o = $('content'); 
		this.entries = o.getElementsByTagName('DD');
		this.p = 4;
		this.install();
		this.nav.l.onclick = function(){this.scroll(-1)}.bind(this);
		this.nav.r.onclick = function(){this.scroll(1)}.bind(this);
		// Mouse scrolling
		if (window.addEventListener) window.addEventListener('DOMMouseScroll', this.wheel.bind(this), false);
    window.onmousewheel = document.onmousewheel = this.wheel.bind(this);
	},
	
	install: function() {
		for (i=0; i<this.entries.length; i++) {
			this.entries[i].onmouseover = function(){ this.className += ' ' + 'active';	}.bind(this.entries[i]);
			this.entries[i].onmouseout = function(){ 
				this.className = this.className.replace(/active/, '');	}.bind(this.entries[i]); 
			if (i>this.p) this.tip(i);
		}
	},
	
	scroll: function(dp) {
		if (dp>0) {
			if (this.p+dp >= this.entries.length) return; 
			this.tip(this.p - 4);
			after = new Fx(this.entries[this.p+dp]);
			this.tip(this.p + dp);
			after.fade();
		} else { 
			if (this.p-4+dp < 0) return;
			this.tip(this.p);
			pre = new Fx(this.entries[this.p - 4 + dp]);
			this.tip(this.p - 4 + dp);
			pre.fade();
		}
		this.p += dp;
	},
	
	tip: function(i) {
		var str = /\bout\b/;
		if (str.test(this.entries[i].className)) this.entries[i].className = this.entries[i].className.replace(str, '');
		else this.entries[i].className += ' ' + 'out';
	},
	
	wheel: function(event){
  	var delta = 0;
  	if (!event) event = window.event;
  	if (event.wheelDelta) {
  		delta = event.wheelDelta/120; 
  		if (window.opera) delta = -delta;
  	} else if (event.detail) {
  		delta = -event.detail/3;
  	}
  	if (delta) this.handle(delta);
    if (event.preventDefault) event.preventDefault();
		event.returnValue = false;
  }, 
	
	handle: function(delta) {
  	if (delta < 0) this.scroll(1);
  	else this.scroll(-1);
  }
}

// --------------------------------
// SPECIAL EFFECTS

var Fx = new Class.create();
Fx.prototype = {
	initialize: function(o) {	
		this.o = o;	
		this.reset(); 
	},
	
	reset: function() {
		this.opa = 0;
		this.setOpacity(this.opa);
		if (this.timer) clearInterval(this.timer);
	},
	
	fade: function(){
		if (this.timer) clearInterval(this.timer);
		if (this.opa >= 1) {return;}
		this.opa += 0.04;
		this.setOpacity(this.opa);
		this.timer = setInterval(this.fade.bind(this), 8);
	},
	
	setOpacity: function(opa) {
		this.o.style.opacity = this.o.style.MozOpacity = this.o.style.KhtmlOpacity = opa; 
		this.o.style.filter = 'alpha(opacity=' + opa*100 + ')';
	}
}

var Menu = new Class.create();
Menu.prototype = {
	initialize: function(hook, oName) {
		this.a = $(hook); this.o = $(oName); this.a.appendChild(this.o); // Rearrange elements
		el = this.o.getElementsByTagName('LI'); this.el = new Array(el.length);
		for (i=0; i<el.length; i++) {this.el[i] = new Fx(el[i]);}
		this.t = new Array(el.length);
		
		this.a.onmouseover = function(){
			if (this.a.className == '') 
				for(i=0; i<this.el.length; i++) { this.t[i] = setTimeout(this.el[i].fade.bind(this.el[i]), i*64); }
			this.a.className = 'hover';
		}.bind(this);

		this.a.onmouseout =  function(){
			this.a.className = '';
			for(i=0; i<this.el.length; i++) { if (this.t[i]) clearTimeout(this.t[i]); }
			setTimeout(function(){
				if (this.a.className == '') for(i=0; i<this.el.length; i++) { this.el[i].reset(); }
			}.bind(this), 2);
		}.bind(this);
	}
}

// --------------------------------
// MAIN FUNCTION

window.onload = function() {
	var navPage = new Turning(['rewind', 'forward'], 0); 
	var navBar = new Menu('header', 'archives');
}
