
function slideView( objOption ) {

    // 商品タイプ（pets: ペット items:その他商品）
    this.view_type = objOption.view_type;

    // 移動量
    this.move = objOption.move;

    // 残り移動量
//    this.remain = objOption.move;
    this.remain = 0;

    // 現在表示しているアイテム番号
    this.now = objOption.now;

    // 現在並んでいるアイテム数
    this.max = objOption.max;

    // スクロール可否フラグ
    this.scroll = true;

    // 両端スクロールフラグ
    this.lastscroll = false;

    // ストック回数
    this.stock = 0;

    // 押されたボタン(0:left 1:right)
    this.which = 0;

    // 対象スライドのx座標
    this.x = 0;

    // setIntervalID
    this.time_id = null;

    // 反転フラグ
    this.reverse = false;

    // 左ボタンエレメント
    this.left_button = document.getElementById( objOption.left_id );
    this.left_button._this = this;

    // 右ボタンエレメント
    this.right_button = document.getElementById( objOption.right_id );
    this.right_button._this = this.left_button._this;

    this.left_button.onclick = this.btnClickLeft;
    this.right_button.onclick = this.btnClickRight;
}

slideView.prototype.init = function() {

    if(this.view_type == 'pets') {
        var el = document.getElementById('pet-list');
    } else if( this._this.view_type == 'items' ) {
        var el = document.getElementById('item-list');
    }
    el.style.left = '0px';

    // 残り移動量
    this.remain = 0;

    // 現在表示しているアイテム番号
    this.now = 0;

    // スクロール可否フラグ
    this.scroll = true;

    // 両端スクロールフラグ
    this.lastscroll = false;

    // ストック回数
    this.stock = 0;

    // 押されたボタン(0:left 1:right)
    this.which = 0;

    // 対象スライドのx座標
    this.x = 0;

    // setIntervalID
    this.time_id = null;

    // 反転フラグ
    this.reverse = false;
}

slideView.prototype.btnClickLeft = function() {

    if( this._this.view_type == 'pets' ) {
        var strId = 'pet-list';
    } else if( this._this.view_type == 'items' ) {
        var strId = 'item-list';
    }
    if( this._this.max - 2 > ( this._this.now + 1) ) {

        if(
            ( !this._this.scroll && this._this.which ) ||
            this._this.lastscroll
        ) {
            return;
        }
        if(!this._this.scroll) {
            if( this._this.max - 2 > ( this._this.now + 1) ) {
                this._this.stock++;
            }
            clearInterval( this._this.time_id );
        }
        this._this.which = 0;
        this._this.scroll = false;
        this._this.now++;
        this._this.slide( strId, 'left', 15 );
    } else {
        if(!this._this.scroll) {
            return;
        }
        this._this.lastscroll = true;
        this._this.which = 0;
        this._this.scroll = false;
        this._this.remain = 50*2;
        this._this.last( strId, 'left', 10, 50 );
    }
}

slideView.prototype.btnClickRight = function() {

    if( this._this.view_type == 'pets' ) {
        var strId = 'pet-list';
    } else if( this._this.view_type == 'items' ) {
        var strId = 'item-list';
    }

    if( 0 < this._this.now ) {

        if(
            (!this._this.scroll && !this._this.which) ||
            this._this.lastscroll
        ) {
            return;
        }
        if(!this._this.scroll) {
            if( 0 < this._this.now ) {
                this._this.stock++;
            }
            clearInterval( this._this.time_id );
        }
        this._this.which = 1;
        this._this.scroll = false;
        this._this.now--;
        this._this.slide( strId, 'right', 15 );
    } else {
        if(!this._this.scroll) {
            return;
        }
        this._this.lastscroll = true;
        this._this.which = 1;
        this._this.scroll = false;
        this._this.remain = 50*2;
        this._this.last( strId, 'right', 15, 50 );
    }
}

slideView.prototype.slide = function( id, type, speed ) {

    var el = document.getElementById(id);
    if (document.all) {
        this.x = el.offsetLeft;
    } else {
        this.x = el.offsetLeft;
    }
    el.style.left = this.x + 'px';
    var move = this.move * ( this.stock + 1 );
    this.remain = this.remain + this.move;
    var objMine = this;
    this.time_id = setInterval(
        function() {

            var el = document.getElementById(id);
            if( objMine.remain <= 0 ) {
                clearInterval( objMine.time_id );
                objMine.stock = 0;
                objMine.scroll = true;
                return;
            }

            if(objMine.remain < 30) {
                speed = Math.round(speed/2);
            }
            if( objMine.remain < speed ) {
                speed = objMine.remain;
            }

            if( type == 'left' ) {
                objMine.x = objMine.x - speed;
                el.style.left = objMine.x + 'px';
            } else if( type == 'right' ) {
                objMine.x = objMine.x + speed;
                el.style.left = objMine.x + 'px';
            } else {
                clearInterval( objMine.time_id );
                objMine.stock = 0;
                objMine.scroll = true;
                return;
            }
            objMine.remain = objMine.remain - speed;
        },
        50
    );
}

slideView.prototype.last = function( id, type, speed, range ) {

    var el = document.getElementById(id);
    if (document.all) {
        this.x = el.offsetLeft;
    } else {
        this.x = el.offsetLeft;
    }
    el.style.left = this.x + 'px';
    var objMine = this;
    this.time_id = setInterval(
        function() {
            var el = document.getElementById(id);
            if( objMine.reverse && objMine.remain <= 0 ) {
                clearInterval( objMine.time_id );
                objMine.stock = 0;
                objMine.scroll = true;
                objMine.reverse = false;
                objMine.lastscroll = false;
                return;
            }

            if( objMine.reverse ) {
                if((objMine.remain / range)*100 < 15) {
                    speed = Math.round(speed/2);
                }
                if( objMine.remain < speed ) {
                    speed = objMine.remain;
                }
            }

            if( !objMine.reverse && ( objMine.remain - speed ) < range ) {
                speed = objMine.remain - range;
            }

            if( objMine.reverse ) {

                if( type == 'left' ) {
                    objMine.x = objMine.x + speed;
                    el.style.left = objMine.x + 'px';
                } else if( type == 'right' ) {
                    objMine.x = objMine.x - speed;
                    el.style.left = objMine.x + 'px';
                } else {
                    clearInterval( objMine.time_id );
                    objMine.stock = 0;
                    objMine.scroll = true;
                    objMine.reverse = false;
                    objMine.lastscroll = false;
                    return;
                }
            } else {

                if( type == 'left' ) {
                    objMine.x = objMine.x - speed;
                    el.style.left = objMine.x + 'px';
                } else if( type == 'right' ) {
                    objMine.x = objMine.x + speed;

                    el.style.left = objMine.x + 'px';
                } else {
                    clearInterval( objMine.time_id );
                    objMine.stock = 0;
                    objMine.scroll = true;
                    objMine.reverse = false;
                    objMine.lastscroll = false;
                    return;
                }
            }

            objMine.remain = objMine.remain - speed;
            if(!objMine.reverse && (range == objMine.remain) ) {
                objMine.reverse = true;
            }
        },
        50
    );
}

