String.prototype.pixelTonumber = function(){
	var pixelNum = parseInt(this.replace("px", ""));
	return pixelNum;
}

Number.prototype.numberTopixel = function(){
	var pixelStr = this.toString() + "px";
	return pixelStr;
}

var rQtvCnt = 10;
function initMain(){
	try{
		var objName = 'rQtv';
		
		fcScroll.init(objName, rQtvCnt);
	}catch(e){}
}


fcScroll = {
	totalCount: 0,
	scrollObj: null,
	scrollElementCount: 2,
	scrollElementIndex: 1,
	rIndex: 0,
	scrollTotalPixel: 20,
	scrollingPixel: 20,
	intervalId: null,
	timeoutId: null,
	intervalTime: 20,
	startTime: 5000,
	nextTime: 100,
	restTime: 15000,
	blnScroll: true,
	blnPaging: false,
	blnElementStart: true,
	blnReMakeList: false,
	blnOpacityMinus: true,
	blnNextElement: false,
	opacity: 100,
	processStartTime: 0,
	processEndTime: 0,

	init: function(obj, total){
		try{
			this.totalCount = total;
			this.scrollObj	= obj;
			var oScroll = this;
			this.timeoutId = window.setTimeout(function(){
				oScroll.timeoutId = window.clearTimeout(oScroll.timeoutId);
				oScroll.intervalId = window.setInterval(function(){
					oScroll.scrollRank(oScroll.rIndex);
				}, oScroll.intervalTime);
			}, oScroll.startTime);
		}catch(e){}
	},

	reMakeList: function(obj, set){
		try{
			for(var i=0;i<set;i++){
				var cObj_0 = $(obj+'['+(i+set)+'][0]').cloneNode(true);
				var cObj_1 = $(obj+'['+(i+set)+'][1]').cloneNode(true);
				cObj_0.id = 'temp_0';
				cObj_1.id = 'temp_1';
				$(obj+'['+i+']').appendChild(cObj_0);
				$(obj+'['+i+']').appendChild(cObj_1);
				$(obj+'['+i+']').insertBefore(cObj_0, $(obj+'['+i+'][1]'));
				$(obj+'['+i+'][1]').id 			= obj+'['+i+'][2]';
				$('temp_0').id 					= obj+'['+i+'][1]';
				$('temp_1').id 					= obj+'['+i+'][3]';
				$(obj+'['+(i+set)+']').remove();
			}
			this.blnReMakeList		= true;
			this.scrollElementCount = 4;
		}catch(e){}
	},

	rePlaceList: function(index){
		var cObj = $(this.scrollObj+'['+index+'][0]').cloneNode(true);
		cObj.id = this.scrollObj+'['+index+']['+fcScroll.scrollElementCount+']';
		$(this.scrollObj+'['+index+']').appendChild(cObj);
		$(this.scrollObj+'['+index+']').removeChild($(this.scrollObj+'['+index+'][0]'));
		for(var i=0;i<fcScroll.scrollElementCount;i++){
			$(this.scrollObj+'['+index+']['+(i+1)+']').id	= this.scrollObj+'['+index+']['+i+']';
			$(this.scrollObj+'['+index+']['+i+']').style.top = '0px';
		}
	},

	scrollRank: function(index){
		try{
			if(this.blnElementStart){
				this.blnElementStart	= false;
				this.processStartTime	= new Date();
			}
			if(window.ActiveXObject){
				for(var i=0;i<this.scrollElementCount;i++){
					this.setElementTop($(this.scrollObj+'['+index+']['+i+']'), -1);
				}
				if(this.scrollTotalPixel == Math.abs($(this.scrollObj+'['+index+'][0]').style.top.pixelTonumber())){
					this.blnNextElement = true;
				}
			}else{
				//opacity set
				if(this.blnOpacityMinus)	this.opacity -= 5;
				else						this.opacity += 5;
				for(var i=0;i<this.scrollElementCount;i++){
					this.resetOpacity($(this.scrollObj+'['+index+']['+i+']'), this.opacity);
				}
				this.scrollingPixel--;
				if(this.scrollingPixel == this.scrollTotalPixel/2){
					this.blnOpacityMinus = false;
				}
				//opacity set
				if(this.scrollingPixel == 0){
					this.blnNextElement = true;
				}
			}
			this.checkScroll(index);
		}catch(e){}
	},

	checkScroll: function(index){
		if(this.blnNextElement){
			this.processEndTime = new Date();
			var elapsed = this.processEndTime.getTime() - this.processStartTime.getTime();
			if(elapsed > 1000){
				this.blnScroll = false;
				this.blnPaging = true;
			}
			var oScroll				= this;
			this.blnNextElement		= false;
			this.blnOpacityMinus	= true;
			this.blnElementStart	= true;
			this.scrollingPixel		= this.scrollTotalPixel;
			this.opacity			= 100;
			this.intervalId 		= window.clearInterval(oScroll.intervalId);
			this.rePlaceList(index);
			this.rIndex 			= index+1;
			if(!this.blnPaging){
				//auto
				if(this.totalCount > this.rIndex){//element
					this.nextRank(this.rIndex);
				}else{//unit (element group)
					if(this.scrollElementIndex == this.scrollElementCount){
						this.scrollElementIndex = 1;
					}else{
						++this.scrollElementIndex;
					}
					this.restRank();
				}
			}else{
			//callback
				if(!this.blnScroll){
					this.btnClickRank();
				}else{
					this.pagingCallBack();
					this.blnPaging = false;
				}
				this.restRank();
			}
		}
	},

	nextRank: function(nextIndex){
		try{
			var oScroll = this;
			this.timeoutId = window.setTimeout(function(){
				oScroll.timeoutId = window.clearTimeout(oScroll.timeoutId);
				oScroll.intervalId = window.setInterval(function(){
					oScroll.scrollRank(nextIndex)
				}, oScroll.intervalTime);
			}, oScroll.nextTime);
		}catch(e){}
	},

	restRank: function(){
		try{
			this.rIndex = 0;
			var oScroll = this;
			this.timeoutId = setTimeout(function(){
				oScroll.timeoutId = window.clearTimeout(oScroll.timeoutId);
				if(oScroll.blnScroll){
					oScroll.intervalId = window.setInterval(function(){
						oScroll.scrollRank(oScroll.rIndex)
					}, oScroll.intervalTime);
				}else{
					oScroll.btnClickRank();
				}
			}, oScroll.restTime);
		}catch(e){}
	},

	resetOpacity: function(obj, opacity){
		obj.style.opacity		= opacity/100;
		obj.style.MozOpacity	= opacity/100;
		obj.KhtmlOpacity		= opacity/100;
		obj.style.filter		= "alpha(opacity="+(opacity).toString()+")";
	},

	setElementTop: function(obj, topPixel){
		obj.style.top = eval(obj.style.top.pixelTonumber() + topPixel).numberTopixel();
	},

	btnClickRank: function(){
		var oScroll = this;
		if(this.timeoutId) this.timeoutId = window.clearTimeout(oScroll.timeoutId);
		if(this.intervalId){
			this.blnPaging = true;
			this.pagingCallBack = function(){
				for(var i=oScroll.rIndex;i<this.totalCount;i++){
					oScroll.rePlaceList(i);
				}
			}
		}else{
			this.intervalId = window.clearInterval(oScroll.intervalId);
			for(var i=this.rIndex;i<this.totalCount;i++){
				this.rePlaceList(i);
			}
		}
		this.restRank();
	}
}

Event.observe(window, 'load', function(){
		initMain();
});