
function findCurrentGrpId(grpPrfixId) {
	var idx=1;
	while(true) {
		var grpObj = document.getElementById(grpPrfixId+"_"+idx); //ex) roll0_0 부터 해당 ID로 되어 있는 Element가 있는지 찾는다.
		if(grpObj==null) return -1; // 한개로 없을 경우. -1로 리턴한다.
		if(grpObj.style.display=="") return idx; // 해당 Element 가 보여지게 되어 있을 경우 해당 index를 돌려준다.
		idx++; //보여지는 화면을 못 찾았을 경우 index 증가 후 다시 검색.
	}
}

function findNextRollObject(grpPrfixId, direct, idx) {
	if(!idx) idx = findCurrentGrpId(grpPrfixId); //idx 입력 안되면 계산.
	if(idx==-1) return null;
	var grpObj = null;
	if(direct) { // 오른쪽
		grpObj = document.getElementById(grpPrfixId+'_'+(idx+1));
		if(grpObj==null) grpObj = document.getElementById(grpPrfixId+'_1');
	} else { // 왼쪽
		if(idx==1) {
			grpObj = document.getElementById(grpPrfixId+'_'+(window[grpPrfixId+'_max']));
		} else {
			grpObj = document.getElementById(grpPrfixId+'_'+(idx-1));
		}
	}
	return grpObj;
}

function startRolling(grpPrfixId, rotateTime, isRollingFade) {
var idx=1;
var idxMax = 0;

if (rotateTime == null || isNaN(rotateTime)) {
	rotateTime = 2000
}

if (isRollingFade == null || isRollingFade == 'undefined') {
	isRollingFade = false;
} else {
	isRollingFade = true;
}

while(true) { //최대값 조회
var grpObj = document.getElementById(grpPrfixId+"_"+idx);
if(grpObj==null) {
idxMax = idx-1;
break;
}
idx++;
}
window[grpPrfixId+'_max'] = idxMax;
try {
if(parseInt(window[grpPrfixId+'_tmid'],10)>0) stopRolling(grpPrfixId);
}catch(e) {
}

window[grpPrfixId + '_tmid'] = setInterval('moveLeft(\'' + grpPrfixId + '\')', rotateTime);
}

function stopRolling(grpPrfixId) {
clearInterval(window[grpPrfixId+'_tmid']);
window[grpPrfixId+'_tmid'] = 0;
}
startRolling
stopRolling

function moveLeft(grpPrfixId) {
	var idx = findCurrentGrpId(grpPrfixId);
	var rollObj = findNextRollObject(grpPrfixId, true, idx);
	if(rollObj==null) return;
	
	document.getElementById(grpPrfixId+"_"+idx).style.display="none";
	rollObj.style.display="";
}

function moveRight(grpPrfixId) {
	var idx = findCurrentGrpId(grpPrfixId);
	var rollObj = findNextRollObject(grpPrfixId, false, idx);
	if(rollObj==null) return;
	document.getElementById(grpPrfixId+"_"+idx).style.display="none";
	rollObj.style.display="";
}
function moveDirect(grpPrfixId, cidx) {
var idx = findCurrentGrpId(grpPrfixId);
var rollObj = document.getElementById(grpPrfixId+"_"+cidx);
if(rollObj==null) return;
document.getElementById(grpPrfixId+"_"+idx).style.display="none";
rollObj.style.display="";
}

