/** 
 * @fileOverview offibaSlider library
 * 
 * @author OFFIBA.com
 * @version 200100107
 *
 */

/** 
 * コンストラクター
 *
 * @class OffibaSliderはスライド用のライブラリです
 * 
 * @param {Object} param	初期化用オブジェクト
 *
 */ 
function OffibaSlider(param) {
	
	//パラメータ初期化
	this._element = $(param.element);
	this._nextBtn = param.nextBtn;
	this._prevBtn = param.prevBtn;
	this._distance = (typeof(param.distance) =='undefined' || parseInt(param.distance) == 0)? 400 : parseInt(param.distance);
	this._duration = (typeof(param.duration) =='undefined' || parseInt(param.duration) == 0)? 1000 : parseInt(param.duration);
	this._origWidth;
	this._width;
	this._unitNum;
	this._unitWidth;
	this._maxDisplayWidth = screen.width; //ディスプレイの最大横幅
	this._leftEdge;
	this._rightEdge;
	this._windowWidth = $(window).width();
	
	//スライドを初期化
	this._init(param.callback);

}


/**
 * 初期化する
 *
 * @param  {Function}	callback	初期化後に実行する関数
 * @return {Void}
 */
OffibaSlider.prototype._init = function(callback) {
	
	var _this = this;
	
	//オリジナルの幅を取得
	var _origWidth = 0;
	$(this._element).children().each(function(){
		_origWidth += $(this).width();
	});
	this._origWidth = _origWidth;
	
	//ループ用コピー処理
	this._unitNum = Math.ceil(this._maxDisplayWidth/this._origWidth)+1;
	this._unitWidth = this._origWidth*this._unitNum;
	var _copyNum = this._unitNum*3;
	var newHtml = [$(this._element).html()];
	for(var i=1; i<_copyNum; i++) {
		newHtml[i] = newHtml[0];
	}
	//console.log(newHtml.length)
	$(this._element).html(newHtml.join(''));
	
	//幅を設定
	this._width = this._origWidth*_copyNum; 
	$(this._element).css({position: 'absolute', left: -this._origWidth, width: this._width});
	
	//左端、右端設定
	this._setEdge();
	
	//ボタン初期化
	$([this._prevBtn, this._nextBtn]).each(function(){
		$(this).hover(
			//over
			function(){
				$(this).stop().animate({backgroundColor: 'red'}, 100);
			},
			//out
			function(){
				$(this).stop().animate({backgroundColor: 'black'}, 100);
			}
		);
	});
	$(this._prevBtn).click(function(){ _this._prev("easeOutCirc"); return false; });
	$(this._nextBtn).click(function(){ _this._next("easeOutCirc"); return false; });
	/*$(this._prevBtn).hover(
		function(){
			clearInterval(_this._intervalId);
			_this._prev();
			_this._intervalId = setInterval(function(){_this._prev()}, 350);
		},
		function(){
			clearInterval(_this._intervalId);
			_this._prev("easeOutCirc");
		}
	);
	$(this._nextBtn).hover(
		function(){
			clearInterval(_this._intervalId);
			_this._next();
			_this._intervalId = setInterval(function(){_this._next()}, 350);
		},
		function(){
			clearInterval(_this._intervalId);
		}
	);*/
	
	
	//ウィンドウリサイズ
	//$(window).resize(function(){_this._resize();});
	
	if(typeof(callback) == "function") {
		callback();	
	}
	
}


/**
 * 左端、右端を設定する
 *
 * @return {Void}
 */
OffibaSlider.prototype._setEdge = function() {
	//this._leftEdge  = -(this._origWidth);
	//this._rightEdge = $(window).width()-(this._width-this._origWidth);
	//this._rightEdge = -(this._origWidth*Math.round(this._maxDisplayWidth/this._origWidth));
	this._leftEdge  = -(this._origWidth*this._unitNum*1);
	this._rightEdge = -(this._origWidth*this._unitNum*2);
}


/**
 * ウィンドウがリサイズした際の対処
 *
 * @return {Void}
 */
OffibaSlider.prototype._resize = function() {
	//this._setEdge();
	//console.log(this._windowWidth-$(window).width());
	//this._windowWidth = $(window).width();
}


/**
 * 任意の方向へスライドする
 *
 * @param {Number} _left left位置(px)
 * 
 * @return {Void}
 */
OffibaSlider.prototype._slide = function(_left,_start,easing) {
	
	easing = (typeof easing == "undefined")? "linear" : easing;
		
	//console.log(_left);
	$(this._element).stop().css({left: _start}).animate({left: _left}, this._duration, easing);
	
}


/**
 * 次のスライドを表示
 * 
 * @return {Void}
 */
OffibaSlider.prototype._next = function(easing) {
	
	var _distance = -1*this._distance;
	var _left = $(this._element).position().left + _distance;
	if(_left < this._rightEdge) {
		//_left = this._leftEdge - ( Math.abs(_left)-Math.abs(this._rightEdge) );
		_left += this._unitWidth;
		
	} 
	
	this._slide(_left, _left-_distance, easing);
	
}


/**
 * 前のスライドを表示
 * 
 * @return {Void}
 */
OffibaSlider.prototype._prev = function(easing) {
	
	var _distance = this._distance;
	var _left = $(this._element).position().left + _distance;
	if(_left > this._leftEdge) {
		//_left = this._rightEdge + ( Math.abs(this._leftEdge)-Math.abs(_left) );
		_left -= this._unitWidth;
	}
	
	this._slide(_left, _left-_distance, easing);
	
}

