function $d(element){
	if(arguments.length>1){
		for(var i=0,elements=[],length=arguments.length;i<length;i++)
			elements.push($d(arguments[i]));
		return elements;
	}
	if(typeof element=="string")
		return document.getElementById(element);
	else
		return element;
}
Function.prototype.bind=function(){
	var args=Array.apply(this,arguments);
	var $fun=this;
	var owner=args.shift();
	return function(){
		$fun.apply(owner,args.concat(arguments));
	}
}
var Class={
	create:function(){
		return function(){
			this.initialize.apply(this,arguments);
		}
	}
}
Object.extend=function(destination,resource){
	for(var property in resource){
		destination[property]=resource[property];	
	}
	return destination;
}
var TabMenu=Class.create();
TabMenu.prototype={
	initialize:function(obj,arrow){
		this.container=$d(obj);
		this.arrow=$d(arrow);
		this.arrDiv=[];
		this.newNum=0;
		for(var i=0;i<this.container.childNodes.length;i++){
			if(this.container.childNodes[i].nodeType!=1)continue;
			this.arrDiv.push(this.container.childNodes[i]);
		}
		this.arrNav=this.arrDiv[1].getElementsByTagName("span");
		for(i=0;i<this.arrNav.length;i++){
			this.arrNav[i].className="other";
			this.arrNav[i].onclick=function(num){
				clearTimeout(this.next);
				this.newNum=num;
				this.opacity();
				this.animation();
				this.slide();
			}.bind(this,i);
		}
		this.arrNav[0].className="current";
		this.slideHeight=this.arrDiv[0].offsetHeight;
		this.next=setTimeout(this.auto.bind(this),3000);
	},
	animation:function(){
		
		this.startPoint=parseInt(this.arrDiv[0].scrollTop,10);
		this.endPoint =this.slideHeight*this.newNum;
		if(this.startPoint!=this.endPoint){
			this.scrollAmount=Math.ceil(Math.abs(this.endPoint-this.startPoint)/12);
			this.arrDiv[0].scrollTop=parseInt(this.arrDiv[0].scrollTop,10)+((this.endPoint<this.startPoint)?-this.scrollAmount:this.scrollAmount);
			setTimeout(this.animation.bind(this),20);
		}else{
			clearTimeout(this.next);
			
			this.next=setTimeout(this.auto.bind(this),3000);
		}
	},
	opacity:function(){
		for(var j=0,length=this.arrNav	.length;j<length;j++){
			this.arrNav[j].className="other";	
		}
		this.arrNav[this.newNum].className="current";
	},
	auto:function(){
		this.newNum+=1;
		if(this.newNum>=this.arrNav.length)this.newNum=0;
		this.opacity();
		this.animation();
		this.slide();
	},
	slide:function(){
		this.startSlidePoint=parseInt(this.arrow.style.top,10);
		this.endSlidePoint =this.arrNav[this.newNum].offsetTop;
		if(this.startSlidePoint!=this.endSlidePoint){
			this.slideAmount=Math.ceil(Math.abs(this.endSlidePoint-this.startSlidePoint)/12);
			this.arrow.style.top=parseInt(this.arrow.style.top,10)+((this.endSlidePoint<this.startSlidePoint)?-this.slideAmount:this.slideAmount)+"px";
			setTimeout(this.slide.bind(this),20);
		}
	}
}
new TabMenu("tabMenu","tabArrow");
