/*************************************** 100자의견 슬라이드 ********************************************************/
var SlideShow = function(objId, options ){
	this.list = $(objId.list);
	this.btnPrevPage = $('prevPage'); this.btnPrevPage.oldCss = this.btnPrevPage.className;
	this.btnNextPage = $('nextPage'); this.btnNextPage.oldCss = this.btnNextPage.className;

	// 기본옵션
	this.options = {
		stepWidth : 115, // width of step
		stepPerPage : 5, 
		defaultIndex :0,
		pageLoop : true
	}
	Object.extend( this.options, options );
	
	this.init(this.options.defaultIndex);
}

SlideShow.prototype = {
	init : function(idx) {		
		this.item = this.list.childNodes;
		if( this.item.length == 0 ) return false;
		for(var i=0,cnt=this.item.length;i<cnt;++i) AddEvent(this.item[i],"click",this.selectItem.bindEvent(this, i))
		if(idx) this.options.defaultIndex = idx;
		if( this.options.defaultIndex<0 ) {this.options.defaultIndex=0;} else if( this.options.defaultIndex>=this.item.length ){this.options.defaultIndex=this.item.length-1;}
		// 기본값
		this.timer = null;
		this.index = this.options.defaultIndex;
		this.totalPage = Math.ceil( this.item.length / this.options.stepPerPage );
		this.cursor = Math.floor( this.index / this.options.stepPerPage ) * this.options.stepPerPage;
		this.curX = 0;
		this.trgX = this.options.stepWidth*this.cursor*-1;
		this.list.style.width = this.item.length * this.options.stepWidth + "px";
		this.isInit = true;
		this.selectAction();
		this.isInit = false;
	},
	prevStep : function(){this.unselectAction();this.index--;this.selectAction();},
	nextStep : function(){this.unselectAction();this.index++;this.selectAction();},
	prevPage : function(){this.cursor -= this.options.stepPerPage;if( this.options.pageLoop == false && this.cursor<0){this.cursor=0;}this.checkCursor( this.cursor );},
	nextPage : function(){this.cursor += this.options.stepPerPage;if( this.options.pageLoop == false && this.cursor>=this.item.length){this.cursor=(this.totalPage-1)*this.options.stepPerPage;}this.checkCursor( this.cursor );},
	selectItem : function( event, num ){this.unselectAction();this.index = num;this.selectAction();	},
	unselectAction : function(){this.item[this.index].className = this.item[this.index].className.replace("on","");},
	selectAction : function(){
		this.checkCursor();
		this.item[this.index].className += " on";
		$('slideCur').innerHTML = this.index+1;
		getStockData(this.index);
	},
	checkCursor : function( cursor ){
		if( this.index >= this.item.length ) {
			this.index = 0;
			this.cursor = Math.floor( this.index / this.options.stepPerPage ) * this.options.stepPerPage;
		} else if( this.index < 0 ) {
			this.index = this.item.length-1;
			this.cursor = Math.floor( this.index / this.options.stepPerPage ) * this.options.stepPerPage;
		}
					
		if( cursor == undefined || cursor == null ){//기본
			this.cursor = Math.floor( this.index / this.options.stepPerPage ) * this.options.stepPerPage;
		} else {// 임의의 cursor 설정
			this.cursor = cursor;
			if( this.cursor < 0 ){
				this.cursor = Math.floor( (this.item.length-1) / this.options.stepPerPage ) * this.options.stepPerPage;
			} else if ( this.cursor >= this.item.length) {
				this.cursor = 0;
			}
		}
		if (this.isInit) {
			this.list.style.left = Math.round(this.options.stepWidth * this.cursor * -1)  + "px";
		}
		else {
			this.trgX = this.options.stepWidth * this.cursor * -1;
			this.curX = this.list.offsetLeft;
			var self = this;
			if (this.trgX != this.curX) {
				clearInterval(this.timer);
				this.timer = setInterval(this.enterFrame.bind(this), 10);
			}
		}
	},
	enterFrame : function(){
		if( this.trgX != this.list.offsetLeft ){
			this.curX += 0.3*(this.trgX  - this.curX );
			this.list.style.left = Math.round(this.curX)  + "px";
		} else {
			clearInterval( this.timer );
		}
	}

}

var AutoSlideOption = function( options ){
	this.options = {
		handle : null,
		mode : "stop",
		speed : [5000, 2500]
	}
	this.init(options);
}
AutoSlideOption.prototype = {
	init : function(options) {		
		Object.extend( this.options, options );
		this.timer = null;
		this.exec( this.options.mode );
	},
	exec : function( mode ){
		this.options.mode = mode;
		clearInterval( this.timer );
		switch( mode ){
			case "stop":break;
			case "slow":
				this.timer = setInterval( this.play.bind(this), this.options.speed[0] );
				break;
			case "fast":
				this.timer = setInterval( this.play.bind(this), this.options.speed[1] );
				break;
		}
	},
	play : function(){
		this.options.handle.nextStep();
	}
}

var slideShow=null, autoSlide=null;
