﻿
//Quick mouse over actions
function quickRow(row,sw) {

	var str1 = "spcqRow" + row + "a";
	var str2 = "spcqRow" + row + "b";

	if(sw == 1){
		document.getElementById(str1).style.backgroundColor = "#FBDCB7";
		document.getElementById(str2).style.backgroundColor = "#FBDCB7";
	}else{
		if(row % 2 != 0) {var clr = "#FFFFFF";}else{var clr = "#F0F0F0";}
		document.getElementById(str1).style.backgroundColor = clr;
		document.getElementById(str2).style.backgroundColor = clr;
	}
}


//Quick auto scroll------------
//init
POSE= false;
scrStartID = 0;
scrCnt = 0;
scrLast = 0;
scrCntMax = 6;

//Quick scroll start
function scrStart(n){
	if(n > 0){scrLast = n;}
	clearTimeout(scrStartID);
	if((scrCnt % 2) == 0){
		scrStartID=setTimeout('scrForward()', 2000);
	}else{
		scrStartID=setTimeout('scrReverse()', 2000);
	}
}

function scrForward(){
	var str = "spcqRow" + scrLast + "a";
	SmoothScroll.scrollTo(str,'scrollArea');
}

function scrReverse(){
	SmoothScroll.scrollTo(0,'scrollArea');
}

function scrPose(n){
	if(n == 1){
		POSE= true;
	}else{
		POSE= false;
	}
}


var SmoothScroll = {};
SmoothScroll = {
	targetScrollTop : 0,	// we're gonna make the $(parentid).scrollTop -> targetScrollTop
	dist : 0,
	timer : 0,
	count : 0,
	parentid : 0,
	lastDist : 0,
	//speedStore : [],		// for debug
	options : {},
	defaultOptions : {
		time : 1*1000,	// [ms]
	//	unit : 50			// [ms]
		unit : 1			// [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.scrollTopMax = this.$(parent).scrollHeight - this.$(parent).offsetHeight + parseInt(this.$(parent).style.borderTopWidth) + parseInt(this.$(parent).style.borderBottomWidth);

		if( navigator.userAgent.match( "MSIE" ) ){
			this.targetScrollTop = ( element ) ? this.$(element).offsetTop : 0;
		}else{
			var targetOffsetTop = ( element ) ? this.$(element).offsetTop : this.$(parent).offsetTop;
			this.targetScrollTop = targetOffsetTop - this.$(parent).offsetTop;
		}
		this.targetScrollTop = ( this.targetScrollTop > this.scrollTopMax ) ? this.scrollTopMax : this.targetScrollTop;

		this.dist = this.targetScrollTop - this.$(parent).scrollTop;
		this.lastDist = 0;
		this.timer = setInterval('SmoothScroll.update()', this.options.unit );
		this.count = 0;
		//this.speedStore = [];
		this.update();
	},
	update : function(){
		var dist = this.targetScrollTop - this.$(this.parentid).scrollTop;
	//	var speed = 2 * dist * this.options.unit / ( this.options.time - this.options.unit * this.count );
		var speed = 1 * 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).scrollTop = this.targetScrollTop;

			//Forward
			scrCnt = scrCnt + 1;
			if(scrCnt < scrCntMax){scrStart(0);}

			return;
		}else if( this.lastDist == dist){
			// stuck
			clearInterval( this.timer );
			this.$(this.parentid).scrollTop = this.targetScrollTop;

			//Reverse
			scrCnt = scrCnt + 1;
			if(scrCnt < scrCntMax){scrStart(0);}

			return;
		}else if(POSE){ //added
		//	clearInterval( this.timer );
			return;
		}
		var scrollTop = this.$(this.parentid).scrollTop + speed;
		this.$(this.parentid).scrollTop = scrollTop;
		this.lastDist = dist;
		this.count++;
		if( this.count == this.options.time / this.options.unit ){
			// timeout
			clearInterval( this.timer );
			this.$(this.parentid).scrollTop = this.targetScrollTop;
		}
	},
	$ : function(id) {
		return document.getElementById(id);
	}
}
function gototop(){
	document.getElementById('scrollArea').scrollTop = 0;
}

//END

