(function($) {

    $.changer = function() {

        var data = {};
        var timeout1, timeout2, timeout3 = undefined;

        function changeObjects($elem, settings) {


            var next = (data.objs.length - 1 == data.current) ? 0 : (data.current + 1);
            data.objs.eq(data.current).removeClass('changebox-obj-active');
            data.objs.eq(next).addClass('changebox-obj-active');
            data.big_images.eq(data.current).css('z-index', '5').fadeOut(settings.speed);
            data.current = next;
            data.big_images.eq(next).css('z-index', '10').fadeIn(settings.speed, function() {
                clearTimeout(timeout2);
                timeout2 = setTimeout(function() {
                    if (data.running == 1) { changeObjects($elem, settings); }
                }, settings.pause);
            });
        };
        return {
            init: function($elem, settings) {
                data.current = 0;
                data.big_images = $elem.find('.changebox-obj-big');
                data.objs = $elem.find('.changebox-obj');
                data.infoboxs = $elem.find('.changebox-info');
                data.running = 1;


                clearTimeout(timeout1);
                timeout1 = setTimeout(function() {
                    if (data.running == 1) { changeObjects($elem, settings); }
                }, settings.pause);


                data.infoboxs.bind('mouseenter', function() {
                    var index = data.infoboxs.index($(this));
                    clearTimeout(timeout1);
                    clearTimeout(timeout2);
                    data.running = 0;

                    if (data.current != index) {
                        data.current = index;
                        data.big_images.stop().css({ 'z-index': '5', 'opacity': '1' }).hide();
                        data.big_images.eq(index).css({ 'z-index': '10', 'opacity': '1' }).show();
                        data.objs.removeClass('changebox-obj-active');
                        data.objs.eq(index).addClass('changebox-obj-active');
                    }
                }).click(function() {
                    var $link = $(this).find('.main-link');
                    window.location = $link.attr('href');
                    return false;
                });
                data.objs.click(function() {
                    var $link = $('.changebox-obj-active').find('.main-link');
                    window.location = $link.attr('href');
                });

                $elem.hover(function() { }, function() {
                    if (data.running == 0) {
                        data.running = 1;
                        clearTimeout(timeout3);
                        timeout2 = setTimeout(function() {
                            changeObjects($elem, settings);
                        }, settings.pause);
                    }
                });

            }
        };
    } ();

    $.fn.extend({
        changer: function(options) {
            this.each(function() {
                var settings = $.extend({ speed: 2000, pause: 3000
                }, options);
                $.changer.init($(this), settings);
            });
            return this;
        }
    });

})(jQuery);