/*
base : https://wex.im/javascript/jquery-slider2
customize : elephancube
update : 2013.09.24
*/
var _prevSp = false;
(function( $ ) {
$.fn.slider = function(options) {
var $this = this;
var settings = {
// Dimensions
'width' : this.width(),
'height' : this.height(),
// Settings
'wait' : 4000,
'fade' : 500,
'direction' : 'left',
'showControls' : true,
'showProgress' : false,
'hoverPause' : true,
'autoplay' : false,
'randomize' : false,
// Callbacks
'slidebefore' : function() {},
'slideafter' : function() {},
'rewind' : function() {}
};
var _timer = false;
var _last = false;
var _this = false;
var _nowPage = 0;
var _clickLock = false;
var _buttonView = [true,true];
var _cycle = function() {
clearTimeout(_timer);
_last = _this;
if (settings.direction == 'right') {
_this = _this.prev('.jquery-slider-element');
} else {
_this = _this.next('.jquery-slider-element');
}
if (!_this.length) {
_rewind();
}
_draw();
if (!$this.hasClass('jquery-slider-paused') && settings.autoplay) {
_timer = setTimeout(_cycle, settings.wait);
}
};
var _rewind = function() {
if (settings.direction == 'right') {
_this = $this.children('.jquery-slider-element').last();
} else {
_this = $this.children('.jquery-slider-element').first();
}
settings.rewind(_this, $this);
};
var _rewindtop = function() {
clearTimeout(_timer);
_last = _this;
_this = $this.children('.jquery-slider-element').first();
var direction = settings.direction;
settings.direction = 'right';
_drawfade();
settings.direction = direction;
};
var _draw = function() {
$this.addClass('jquery-slider-sliding');
if (settings.showProgress) {
$this.find('.jquery-slider-page').removeClass('jquery-slider-page-current');
$this.find('.jquery-slider-page:eq(' + (_this.nextAll('.jquery-slider-element').length) + ')').addClass('jquery-slider-page-current');
}
settings.slidebefore(_this, $this);
if (settings.direction == 'right') {
_this.show().css('left', -settings.width);
} else {
_this.show().css('left', settings.width);
}
_this.show().css('opacity', 1);
_this.stop(true, true).animate({
'left' : (settings.direction == 'right' ? '+=' : '-=') + settings.width + 'px'
}, {
'duration' : settings.fade,
'complete' : function() {
settings.slideafter(_this, $this);
$this.removeClass('jquery-slider-sliding');
_clickLock=false;
}
});
if (_last) {
_last.stop(true, true).animate({
'left' : (settings.direction == 'right' ? '+=' : '-=') + settings.width + 'px'
}, {
'duration' : settings.fade
});
}
};
var _drawfade = function() {
$this.addClass('jquery-slider-sliding');
if (settings.showProgress) {
$this.find('.jquery-slider-page').removeClass('jquery-slider-page-current');
$this.find('.jquery-slider-page:eq(' + (_this.nextAll('.jquery-slider-element').length) + ')').addClass('jquery-slider-page-current');
}
settings.slidebefore(_this, $this);
_this.show().css({'left':0,'opacity':0});
_this.stop(true, true).animate({
'opacity' : 1
}, {
'duration' : settings.fade,
'complete' : function() {
settings.slideafter(_this, $this);
$this.removeClass('jquery-slider-sliding');
_clickLock=false;
}
});
if (_last) {
_last.hide();
}
};
var _next = function() {
if ($this.hasClass('jquery-slider-sliding')) return;
switch(trans[_nowPage+1]){
case 0:
//通常スライド
var direction = settings.direction;
$this.addClass('jquery-slider-paused');
settings.direction = 'left';
_cycle();
settings.direction = direction;
break;
case 1:
//フェード
fadePage(_nowPage+1);
break;
}
};
var _prev = function() {
if ($this.hasClass('jquery-slider-sliding')) return;
/*
var direction = settings.direction;
$this.addClass('jquery-slider-paused');
settings.direction = 'right';
_cycle();
settings.direction = direction;
*/
//戻るをフェードにする
fadePage(_nowPage-1);
};
var _btnupdateflg = false;
var _updatebtns = function() {
if(_nowPage==1){
$this.find('.jquery-slider-control-prev').hide();
$this.find('.jquery-slider-control-rewind').hide();
}else{
$this.find('.jquery-slider-control-prev').show();
$this.find('.jquery-slider-control-rewind').show();
}
if(_nowPage==$this.children('.jquery-slider-element').length){
$this.find('.jquery-slider-control-next').hide();
}else{
$this.find('.jquery-slider-control-next').show();
}
if(btns[_nowPage][0]==false){
$this.find('.jquery-slider-control-prev').hide();
}
if(btns[_nowPage][1]==false){
$this.find('.jquery-slider-control-next').hide();
}
if(_btnupdateflg){
if(_buttonView[0]){
$this.find('.jquery-slider-control-prev').show();
}else{
$this.find('.jquery-slider-control-prev').hide();
}
if(_buttonView[1]){
$this.find('.jquery-slider-control-next').show();
}else{
$this.find('.jquery-slider-control-next').hide();
}
_btnupdateflg = false;
}
};
var getPageNum = function(){
return _nowPage;
};
var _init = function() {
if (options) {
$.extend( settings, options );
}
if (settings.hoverPause) {
$this.bind({
'mouseenter': function() {
$this.addClass('jquery-slider-paused')
clearTimeout(_timer);
},
'mouseleave': function() {
$this.removeClass('jquery-slider-paused');
if (settings.autoplay) {
_timer = setTimeout(_cycle, settings.wait);
}
}
});
}
var positionEls = $('');
$this.addClass('jquery-slider').width(settings.width).height(settings.height);
$this.children().each(function() {
var $tmp = $(this);
_this = $(this).addClass('jquery-slider-element');
positionEls.prepend($('').bind('click', function() {
if ($this.hasClass('jquery-slider-sliding')) return;
_last = _this;
_this = $tmp;
_draw();
}));
});
if (settings.showProgress) {
$this.append(positionEls);
}
if (settings.showControls) {
var controlPrev = $('< 戻る').bind('click', function() {
if(_clickLock==false){
if(_prevSp){
//SPモード同じページでリセット
_prevSp = false;
pageInit(_nowPage);
}else{
_clickLock=true;
_prev();
_nowPage--;
_updatebtns();
}
}
});
var controlNext = $('進む >').bind('click', function() {
_prevSp = false;
if(_clickLock==false){
_clickLock=true;
_next();
_nowPage++;
_updatebtns();
}
});
var controlRewind = $('はじめから').bind('click', function() {
_prevSp = false;
if(_clickLock==false){
_clickLock=true;
_rewindtop();
_nowPage=1;
_updatebtns();
}
});
$this.append(controlPrev);
$this.append(controlNext);
$this.append(controlRewind);
}
if (settings.randomize) {
_this = $this.children('.jquery-slider-element').eq(parseInt($this.children('.jquery-slider-element').length * Math.random()));
}
_rewindtop();
_nowPage = 1;
_updatebtns();
};
//前後ボタンから呼び出し(trans=1 フェード)
var fadePage = function(_num){
if(_num==1){
_rewindtop();
}else{
clearTimeout(_timer);
_last = _this;
var count = 0;
$this.find('.jquery-slider-element').each(function(){
count++;
if(count==_num){
_this = $(this);
}
});
_drawfade();
}
};
//public functions
$.fn.slider.getPage = function(){
return _nowPage;
};
$.fn.slider.jumpPage = function(_num){
if(_num==1){
_rewindtop();
}else{
clearTimeout(_timer);
_last = _this;
var count = 0;
$this.find('.jquery-slider-element').each(function(){
count++;
if(count==_num){
_this = $(this);
}
});
_drawfade();
}
_nowPage = _num;
_updatebtns();
};
$.fn.slider.btnVisible = function(_flg0,_flg1){
_buttonView[0] = _flg0;
_buttonView[1] = _flg1;
_btnupdateflg = true;
_updatebtns();
};
_init();
};
})( jQuery );
var jumpTo = function(_num) {
$('#slider').slider.jumpPage(_num);
};
var btnView = function(_flg0,_flg1) {
$('#slider').slider.btnVisible(_flg0,_flg1);
};