//
//
// Pagetop.js
//
//
Pagetop = function()
{
	this.els = [];
	this.onLoad(this, "init");
};

Pagetop.prototype.add = function(_id)
{
	this._id = _id;
}
Pagetop.prototype.addClassName = function(_classname)
{
	this._classname = _classname;
}
Pagetop.prototype.onLoad = function(scope, callback)
{
	if(window.addEventListener) window.addEventListener('load', function(){ scope[callback](); }, false);
	if(window.attachEvent) window.attachEvent('onload', function(){ scope[callback](); });
}
Pagetop.prototype.init = function()
{
	if(this._className) this.els = this.getElementsByClassName(this._classname);
	else if(this._id) this.els.push(document.getElementById(this._id));
	//
	var e = this.els;
	for(var i=0,len=e.length; i<len; i++)
	{
		var a = e[i].getElementsByTagName("a")[0]
		a.removeAttribute("href");
		a.style.cursor = "pointer"; 
		this.setOnClick(a, this, "doScroll");
	}
}
Pagetop.prototype.setOnClick = function(el, scope, callback)
{
	if(el.addEventListener) el.addEventListener('click', function(){ scope[callback](); return false; }, false);
	if(el.attachEvent) el.attachEvent('onclick', function(){ scope[callback](); return false; });
}
Pagetop.prototype.doScroll = function()
{
	var myInt = setInterval(function()
	{
		var cur = document.documentElement.scrollTop || document.body.scrollTop;
		//console.log(cur);
		var d = 0-cur;
		if(Math.abs(d)>1) {
			var cur = cur+d/3;
			//console.log(cur);
			scrollTo(0,cur);
		} else {
			scrollTo(0,0);
			clearInterval(myInt);
		}
	},100);
}
Pagetop.prototype.getElementsByClassName = function(_classname)
{
	var e = [];
	var a = document.getElementsByTagName("*");
	for(var i=0,len=a.length; i<len; i++)
	{
		var n = a[i].className;
		if(n&&n==_classname) e.push(a[i]);
	}
	return e;
}