﻿/**
 * Nowenter lib
 *
 * @desc	prototype 필수
 * @author	lalala2570@nowenter.co.kr
 * @since	1.0
 */


/**
 * total, btnId, layerId, btnDir, term, btnPrefix, btnOnsuffix, imgext
 *
 * total	: 총 갯수
 * btnId	: 숫자부분을 제외한 버튼 아이디
 * layerId	: 숫자부분을 제외한 레이어 아이디
 * btnDir	: 이미지 저장 디렉토리 끝에 슬러시 붙여준다
 * btnPrefix	: 숫자부분을 제외한 버튼 이미지명(확장자 제외)
 * term		: 멈추는 시간 밀리세컨, 기본 2초(2000)
 * btnbtnOnsuffix	: 오버시 이미지 접미사(확장자 제외), 기본 _r
 * imgext	: 이미지 확장자, 기본 gif
 */
var ShowNotice = Class.create();
ShowNotice.prototype = {
	initialize: function(total, btnId, layerId, btnDir, btnPrefix, term, btnOnsuffix, imgext) {
		this.key = 0;
		this.prevKey = total-1;
		this.isRolling = true;
		this.total = total;		
		this.btnId = btnId;
		this.layerId = layerId;
		this.btnDir = btnDir;
		this.btnPrefix = btnPrefix ;
		this.term = (term) ? term : 2000;		
		this.btnOnsuffix = (btnOnsuffix) ? btnOnsuffix : "_r";
		this.imgext = (imgext) ? imgext : "gif";
		
		this.start();
	},
	start : function(){
		
		if(this.isRolling)	this.show(this.key);
		
		setTimeout(this.start.bind(this), this.term);
	
	},
	stop : function(idx){
		this.show(idx);
		this.key = idx;
		this.isRolling = false;
	},
	restart : function(){
		this.isRolling = true;
	},
	show : function(curKey){
		
		
		if(curKey >= this.total-1){
			this.key = 0;
		} else {
			this.key++;
		}
		
		// 이전 숨기기
		$(this.btnId + this.getNum(this.prevKey+1)).src = this.btnDir + this.btnPrefix + this.getNum(this.prevKey+1) + '.' + this.imgext;
		$(this.layerId + this.getNum(this.prevKey+1)).hide();
		
		// 현재 보이기
		$(this.btnId + this.getNum(curKey+1)).src = this.btnDir + this.btnPrefix + this.getNum(curKey+1) + this.btnOnsuffix + '.' + this.imgext;
		$(this.layerId + this.getNum(curKey+1)).show();
		
		this.prevKey = curKey;
	},
	
	
	
	
	// 2자리보다 작으면 앞에 0 붙이기
	getNum: function(num){
		if(num < 10)	return '0'+num;
		else			return num;
	}
}
