var SmoothScroll = {};
SmoothScroll = {
	targetScrollLeft : 0,	// we're gonna make the $(parentid).scrollLeft -> targetScrollLeft
	dist : 0,
	timer : 0,
	count : 0,
	parentid : 0,
	lastDist : 0,
	//speedStore : [],		// for debug
	options : {},
	defaultOptions : {
		time : 1*1000,	// [ms]
		unit : 50			// [ms]
	},
	scrollTo : function( element, parent, options ){
		this.options.time = this.defaultOptions.time;
		this.options.unit = this.defaultOptions.unit;
		if( options ){
			this.options.time = ( options.time ) ? options.time : this.options.time;
			this.options.unit = ( options.unit ) ? options.unit : this.options.unit;
		}
		clearInterval( this.timer );
		this.parentid = parent;

		this.scrollLeftMax = this.$(parent).scrollWidth - this.$(parent).offsetWidth + parseInt(this.$(parent).style.borderLeftWidth) + parseInt(this.$(parent).style.borderBottomWidth);

		if( navigator.userAgent.match( "MSIE" ) ){
			this.targetScrollLeft = ( element ) ? this.$(element).offsetLeft : 0;
		}else{
			var targetOffsetLeft = ( element ) ? this.$(element).offsetLeft : this.$(parent).offsetLeft;
			this.targetScrollLeft = targetOffsetLeft - this.$(parent).offsetLeft;
		}
		this.targetScrollLeft = ( this.targetScrollLeft > this.scrollLeftMax ) ? this.scrollLeftMax : this.targetScrollLeft;

		this.dist = this.targetScrollLeft - this.$(parent).scrollLeft;
		this.lastDist = 0;
		this.timer = setInterval('SmoothScroll.update()', this.options.unit );
		this.count = 0;
		//this.speedStore = [];
		this.update();
	},
	update : function(){
		var dist = this.targetScrollLeft - this.$(this.parentid).scrollLeft;
		var speed = 2 * dist * this.options.unit / ( this.options.time - this.options.unit * this.count );
		//this.speedStore.push( speed );
		speed = ( speed > 0 ) ? Math.ceil( speed ) : Math.floor( speed );
		if( Math.abs(dist) <= Math.abs(speed) ){
			// got there
			clearInterval( this.timer );
			this.$(this.parentid).scrollLeft = this.targetScrollLeft;
			return;
		}else if( this.lastDist == dist ){
			// stuck
			clearInterval( this.timer );
			this.$(this.parentid).scrollLeft = this.targetScrollLeft;
			return;
		}
		var scrollLeft = this.$(this.parentid).scrollLeft + speed;
		this.$(this.parentid).scrollLeft = scrollLeft;
		this.lastDist = dist;
		this.count++;
		if( this.count == this.options.time / this.options.unit ){
			// timeout
			clearInterval( this.timer );
			this.$(this.parentid).scrollLeft = this.targetScrollLeft;
		}
	},
	$ : function(id) {
		return document.getElementById(id);
	}
}
function gototop(){
	document.getElementById('top').scrollLeft = 0;
}

// class要素の追加・変更
function setElementClassById(elem, value) {
   if(document.getElementById) {
         var obj = document.getElementById(elem);
         if(obj) {
            obj.className = value;
         }
   }
}

function clearElement(){
	setElementClassById("totab_a01", "");
	setElementClassById("totab_a02", "");
	setElementClassById("totab_a03", "");
	setElementClassById("totab_a04", "");
	setElementClassById("totab_a05", "");
}

function setElement(elem){
	clearElement();
	setElementClassById(elem,"select");
}
