(function($){
    function VerticalCarrouselManager(options){
        var opts = $.extend(VerticalCarrouselManager.defaults, options), parent = $(opts.listCont), uls = [parent.children("ul").css("top", "0")], parent_inner_h = parent.innerHeight(), mintop = parent.offset().top, maxtop = mintop + parent_inner_h, items = uls[0].children(), iqty = items.length, interval_id = null, that = this;
        if (iqty) {
            this.getOpts = function(){
                return opts
            };
            this.getLists = function(){
                return uls
            };
            this.getContainer = function(){
                return parent
            };
            this.getIntervalId = function(){
                return interval_id
            };
            this.setIntervalId = function(intid){
                interval_id = intid
            };
            this.getMaxTop = function(){
                return maxtop
            };
            this.getMinTop = function(){
                return mintop
            }
            var i = 0, maxitems = iqty + 1;
            while (2 * parent_inner_h > uls[0].height() && i < 50) {
                uls[0].append($(items[i % maxitems]).clone(true));
                i++
            }
            this.listHeight = uls[0].outerHeight(true) + opts.listMargin;
            uls.push(uls[0].clone(true).css("top", this.listHeight + "px").appendTo(parent));
            this.topList = uls[0];
            this.bottomList = uls[1];
            this.listPos = 0;
            
            if (opts.moveUpBtn) {
                $(opts.moveUpBtn).click(function(e){
                    e.preventDefault;
                    that.moveList(VerticalCarrouselManager.DIR_UP)
                })
            }
            if (opts.moveDownBtn) {
                $(opts.moveDownBtn).click(function(e){
                    e.preventDefault;
                    that.moveList(VerticalCarrouselManager.DIR_DOWN)
                })
            }
            if (opts.contMov) {
                this.moveContinuously(opts.contMovdir);
            }
        }
    };
    VerticalCarrouselManager.defaults = {
        listCont: ".gpr-list",
        moveUpBtn: null,
        moveDownBtn: null,
        contMov: true,
        step: 1,
        listMargin: 5,
        ustTriggMovStep: 30,
        interval: 50
    };
    VerticalCarrouselManager.DIR_UP = 1;
    VerticalCarrouselManager.DIR_DOWN = 0;
    VerticalCarrouselManager.prototype = {
        moveContinuously: function(){
            if (this.getIntervalId() === null) {
                var that = this;
                this.setIntervalId(setInterval(function(){
                    that._move()
                }, that.getOpts().interval))
            }
        },
        stopContMov: function(){
            clearInterval(this.getIntervalId());
            this.setIntervalId(null);
        },
        moveList: function(direction){
            var lists = this.getLists(), opts = this.getOpts(), that = this, csstoken = (direction == VerticalCarrouselManager.DIR_UP) ? "-" : "+";
            this.stopContMov();
            this.checkListEnd(direction, opts.ustTriggMovStep, lists[0].offset().top - this.getContainer().offset().top);
            lists[0].stop();
            lists[1].stop();
            this.topList.animate({
                top: csstoken + "=" + opts.ustTriggMovStep + "px"
            }, {
                duration: 500,
                step: function(now){
                    that.listPos = now;
                    that.checkListEnd(direction, opts.ustTriggMovStep)
                },
                complete: function(){
                    that.topList.position = that.topList.offset().top - that.getMinTop();
                }
            });
        },
        checkListEnd: function(direction, step){
            var temp;
            this.bottomList.css("top", (this.listPos + this.listHeight) + "px");
            if (direction == VerticalCarrouselManager.DIR_UP) {
                if (this.listPos <= -this.listHeight) {
                    this.listPos += this.listHeight;
                    temp = this.topList;
                    this.topList = this.bottomList;
                    this.bottomList = temp;
                }
            }
            else {
                if (this.listPos + this.listHeight >= this.getMaxTop()) {
                    this.listPos -= this.listHeight;
                    temp = this.bottomList;
                    this.bottomList = this.topList;
                    this.topList = temp.css("top", this.listPos + "px");
                }
            }
        },
        _move: function(direction){
            this.listPos += this.getOpts().step;
            this.topList.css("top", this.listPos + "px");
            this.checkListEnd();
        }
    };
    $(document).ready(function(){
        var vertCM = new VerticalCarrouselManager({
            listCont: ".feat-loc > div",
            moveUpBtn: ".arrow-up",
            moveDownBtn: ".arrow-down"
        });
        $(".feat-loc").hover(function(){
            vertCM.stopContMov();
        }, function(){
            vertCM.moveContinuously()
        });
    })
})(jQuery)

