﻿$.fn.EasySlider = function(options) {
    options = $.extend({
        prevId: 'prevBtn',
        nextId: 'nextBtn',
        auto: false,
        timeDelay: 2000,
        overStop: false,
        slideBy: 1,
        elementObj: 'ul'
    }, options);

    return this.each(function() {
        var obj = $(this);
        var totLength = $(" > " + options.elementObj, obj).length;
        var ts = totLength - 1;
        var t = 0;

        if (totLength > options.slideBy) {

            obj.css("position", "relative");
            obj.css("overflow", "hidden");

            $("#" + options.prevId).css("cursor", "hand").click(function() {
                movelist("prev", false);
            });

            $("#" + options.nextId).css("cursor", "hand").click(function() {
                movelist("next", false);
            });

            function movelist(dir, clicked) {
                switch (dir) {
                    case "next":
                        t = (t >= ts) ? 0 : t + 1;
                        break;
                    case "prev":
                        t = (t <= 0) ? ts : t - 1;
                        break;
                    default:
                        t = 0;
                        break;
                };

                showcontent();

                if (clicked) clearTimeout(timeout);

                if (options.auto && !clicked) {
                    clearTimeout(timeout);
                    timeout = setTimeout(function() {
                        movelist("next", false);
                    }, options.timeDelay);
                }
            }

            //컨텐츠 숨김, 표시
            function showcontent() {
                var nowObj = $(" > " + options.elementObj, obj);
                nowObj.each(function() {
                    var n = nowObj.index($(this)[0]);
                    if (n == t) {
                        $(this).show();
                    }
                    else {
                        $(this).hide();
                    }
                });
            }

            //init
            var timeout;
            //처음로딩 시 첫번째 element가 show
            movelist("", false);

            if (options.overStop) {
                obj.bind("mouseover", function() {
                    clearTimeout(timeout);
                });

                obj.bind("mouseout", function() {
                    timeout = setTimeout(function() {
                        movelist("next", false);
                    }, options.timeDelay);
                });
            }
        }
    });
};
