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);}

function px(str) {return 1*str.replace('px', '');}

Array.prototype.each = function(func){ for(var i=0; i<this.length; i++) func(this[i], i); }

var Turning = new Class.create();

Turning.prototype = {
	initialize: function(pNav, pCol) {
		this.pNav = new Array($(pNav[0]), $(pNav[1])); 
		this.frame = $(pCol[0]);
		this.pCounter = 0;
		this.frame.className = '';
		this.pHeight = this.frame.offsetHeight;
		oCol = (this.frame.getElementsByTagName('DD'))[0];
		this.cols = [oCol, this.frame.appendChild(oCol.cloneNode(true))];
		this.cols[0].id = pCol[1] + '01';
		this.cols[1].id = pCol[1] + '02'; 
		this.pNav[0].onclick = function(){this.scroll(1)}.bind(this);
		this.pNav[1].onclick = function(){this.scroll(-1)}.bind(this);

		if (window.addEventListener) window.addEventListener('DOMMouseScroll', this.wheel.bind(this), false);
    window.onmousewheel = document.onmousewheel = this.wheel.bind(this);
	},
	
	scroll: function(v) {

		if (v>0 && (this.cols[0].style.top=='0px' || this.cols[0].style.top=='')) return;
		if (v<0 && this.pCounter>=(this.cols[1].offsetHeight/this.pHeight-2)) return;   

		this.pCounter += (v<0) ? 1 : -1;  
		for (i=0; i<this.cols.length; i++) {this.cols[i].style.top = -1*(i+this.pCounter)*this.pHeight + 'px';}
	},
	
	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);
  }
}

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);
		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);
	}
}

var Button = new Class.create();
Button.prototype = {
	initialize: function(oName) {
		this.o = $(oName); this.ot = $(oName + '-block');
		this.o.onclick = this.toggle.bind(this);
	},
	
	toggle: function(){
		this.ot.className = (this.ot.className == 'block') ? 'block none' : 'block';
	}
}

window.onload = function() {
	var  scrollArticle = new Turning(['rewind', 'forward'], ['frame', 'column']);
	var navBar = new Menu('header', 'archives');
	var rss = new Button('rss');
	var com = new Button('comment');
	var sha = new Button('share');
}