/* addEventFunc 関数
#
#　イベントの追加用
#
--------------------------------------------------------------------*/
var addEventFunc = function(func){
	try {
		window.addEventListener('load', func, false);
	} catch (e) {
		window.attachEvent('onload', func);
	}
}

/* objCheck 関数
#
#　オブジェクトチェック用関数
#
#　戻り値
#　　true: 有効なオブジェクト
#　　false: 無効なオブジェクト（undefined,null,''）
#
--------------------------------------------------------------------*/
function objCheck(obj) {
	return (typeof obj == 'undefined' || obj == null || obj == '') ? false : true;
}

/* bnscroll 関数
#
#　オブジェクトのスクロール
#　ループなし
#
--------------------------------------------------------------------*/
var bnscroll = function(wrap, btns){
	this.wrap = wrap;
	this.btns = btns;
	this.item;
	this.time;
}
bnscroll.prototype = {
	setup: function (){
		var self = this;
		this.item = this.wrap.getElementsByTagName('li');
		this.wrap.style.position = 'absolute';
		this.wrap.style.width = this.fixW * this.item.length + 'px';
		this.wrap.parentNode.style.height = this.wrap.offsetHeight + 'px';
		var parent = this.wrap.parentNode.parentNode;
		parent.onmouseover = function(){ self.stop = true; }
		parent.onmouseout  = function(){ self.stop = false; }
		this.btns.prev.onclick = function(){ return self.action(0); };
		this.btns.next.onclick = function(){ return self.action(1); };

		this.sort();
		this.time = setTimeout(function(){ self.auto(); }, this.delay);
	},
	auto: function(){
		var self = this;
		if(this.stop){
			if(this.time) clearTimeout(this.time);
			this.time = setTimeout(function(){ self.auto(); }, this.delay);
		} else {
			this.flg = 1;
			this.vector = 1;
			this.scroll();
		}
	},
	action: function(vector){
		if(this.flg) return false;
		this.vector = vector;
		(vector) ? this.now++ : this.now--;
		if(this.now < 0){
			this.now = this.item.length-10;
		} else if(this.now >= this.item.length){
			this.now = 0;
		}
		this.flg = 1;
		this.scroll();
		return false;
	},
	scroll: function(){
		if(this.time) clearTimeout(this.time);
		var self = this;
		var left = parseInt(this.width/this.speed);
		this.width = ((left <= this.accept) && (left >= -this.accept))
			? this.width - this.accept
			: this.width - left;
		this.wrap.style.left = (!this.vector)
			? - this.width + 'px'
			: - this.fixW*2 + this.width + 'px';
		if(this.width == 0){
			this.sort();
			this.time = setTimeout(function(){ self.auto(); }, this.delay);
			return false;
		}
		this.time = setTimeout(function(){ self.scroll() }, 10);
	},
	sort: function(){
		var obj = (!this.vector)
				? this.item[this.item.length-1]
				: this.item[0];
		(!this.vector)
				? this.wrap.insertBefore(obj ,this.item[0])
				: this.wrap.appendChild(obj);

		this.wrap.style.left = -this.fixW + 'px';
		this.width = this.fixW;
		this.flg = 0;
		return false;
	}
}
bnscroll.prototype.now    = 0;		//現在表示しているアイテム
bnscroll.prototype.fixW   = 600;	//アイテムのスクロール幅（固定）
bnscroll.prototype.width  = 600;	//アイテムのスクロール幅
bnscroll.prototype.speed  = 10;	//スクロール速度（数が小さい程早い）
bnscroll.prototype.accept = 1;	//最小スクロール値
bnscroll.prototype.delay  = 3000;	//アイテムの表示時間
bnscroll.prototype.vector = 0;	//スクロール方向フラグ
bnscroll.prototype.flg    = 0;		//ボタン操作可否フラグ
bnscroll.prototype.stop   = 0;		//オートスクロール停止フラグ

function ScrollStart() {
	var wrap = document.getElementById('bnscroller');
	if(objCheck(wrap) == false) return false;
	var btns = {
		prev : document.getElementById('slidePrev'),
		next : document.getElementById('slideNext')
	};
	var scroll = new bnscroll(wrap, btns);
	//設定変更
	scroll.fixW  = 154;	//アイテムのスクロール幅（固定）
	scroll.width = 154;	//アイテムのスクロール幅
	//スライド開始
	scroll.setup();
}
addEventFunc(ScrollStart);

