(function ($) {
    $.fn.widgetSliding = function (opts) {

        opts = $.extend({
            "normal": 81,
            "min": 108,
            "max": 0
        }, opts);

        return this.each(function () {

            var ele = $(this), cells = ele.children(".widget-sliding-cell"),
                pos = 0;

            cells.each(function (i) {
                if (i > 0) {
                    $(this).css({
                        "left": pos
                    })
                }
                pos = pos + $(this).width() - opts.normal;
            });

            cells.hover(function () {
                var index = cells.index(this), pos = 0;
                cells.each(function (i) {
                    if (i > 0) {
                        pos = pos - (i === index + 1 ? opts.max : opts.min);
                        $(this).stop()
                            .animate({"left": pos});
                    }
                    pos = pos + $(this).width();
                    $(this).children("div").stop()
                            .animate({"marginLeft": (i === index ? 0 : - opts.min / 2)});
                });
            }, function () {
                var index = cells.index(this), pos = 0;
                cells.each(function (i) {
                    if (i > 0) {
                        $(this).stop()
                            .animate({"left": pos});
                    }
                    pos = pos + $(this).width() - opts.normal;
                    $(this).children("div").stop().
                            animate({"marginLeft": - opts.normal / 2});
                });
            });
        });
    };
}(jQuery));
