var Core = (function () {
    var modules = {};
    return {
        showError: function (e) {
            console.error(e);
        },
        register: function (name, creator) {
            modules[name] = {
                creator: creator,
                instance: null
            };
        },
        start: function (name) {
            modules[name].instance = modules[name].creator();
            modules[name].instance.init();
        },
        stop: function (name) {
            var module = modules[name];
            if (module.instance) {
                module.instance.destroy();
                module.instance = null;
            }
        },
        startAll: function () {
            for (var name in modules) {
                if (modules.hasOwnProperty(name)) {
                    this.start(name);
                }
            }
        },
        stopAll: function () {
            for (var name in modules) {
                if (modules.hasOwnProperty(name)) {
                    this.stop(modules[name]);
                }
            }
        }
    };
})();

Core.register('sidebar', function () {
    return {
        init: function () {
            $(document).ready(function () {
                var automatic = (function () {
                    var timeout = null,
                        count = maxLength,
                        current = 0;
                    return {
                        reset: function () {
                            clearInterval(timeout);
                        },
                        set: function () {
                            timeout = setInterval(function() {
                                current = (current + 1 === count) ?
                                          0 :
                                          (current + 1);
                                for (var i = countryOffers.length; i--;) {
                                    if (countryOffers[i].count - 1 >= current) {
                                        countryOffers[i].show(current);
                                    }
                                }
                            }, 4000);
                        }
                    };
                }());

                function CountryOffers(country) {
                    var that = this,
                        wrapper = country,
                        list = wrapper.find('ul'),
                        items = list.children('li'),
                        itemWidth = items.eq(0).outerWidth(),
                        isBusy = false,
                        init = function () {
                            that.current = 0;
                            that.count = items.length;
                            
                            list.width(itemWidth * that.count);
                        };

                    init();
                    this.show = function (item) {
                        if (isBusy || that.current === item) {
                            return;
                        } else {
                            isBusy = true;
                            that.current = item;
                            list.animate({
                                left: -item * itemWidth
                            }, {
                                duration: 200,
                                complete: function () {
                                    isBusy = false;
                                }
                            });
                        }
                    };
                    this.showNext = function () {
                        var next = (that.current === that.count - 1) ?
                            0 :
                            (that.current + 1);
                        that.show(next);
                    };
                    this.showPrev = function () {
                        var next = (that.current === 0) ?
                            (that.count - 1) :
                            (that.current - 1);
                        that.show(next);
                    };
                }
                var sidebar = $('.sidebar .offers'),
                    prev = sidebar.find('.prev'),
                    next = sidebar.find('.next'),
                    countries = sidebar.find('.country-box'),
                    countryOffers = [],
                    countryOffer,
                    i, ii,
                    lengths = [],
                    maxLength,
                    automaticSlide = null;
                    
                for(i = 0, ii = countries.length; i < ii; i++) {
                    countryOffer = new CountryOffers(countries.eq(i));
                    countryOffers.push(countryOffer);
                    countries.eq(i).data('count', i);
                    lengths.push(countryOffer.count);
                }
                maxLength = Math.max.apply(Math, lengths);
                
                prev.click(function (ev) {
                    ev.preventDefault();
                    automatic.reset();
                    var item = $(this).closest('.country-box').data('count');
                    countryOffers[item].showPrev();
                });
                next.click(function (ev) {
                    ev.preventDefault();
                    automatic.reset();
                    var item = $(this).closest('.country-box').data('count');
                    countryOffers[item].showNext();
                });

                automatic.set();
                sidebar.hover(function () {
                    automatic.reset();
                }, function () {
                    automatic.set();
                });                
            });
        },
        destroy: function () {  }
    }
});

Core.start('sidebar');


var Site = {
    Airtickets: function () {
    },
    Hotels: function () {   
    },
    Offer: function () {
        $(document).ready(function () {
            var pictures = $('.offer .pictures'),
                items = pictures.find('li'),
                main = pictures.find('.main-picture');
            items.find('a').click(function (ev) {
                ev.preventDefault();
                var link = $(this),
                    item = link.parent();
                    
                items
                    .css('opacity', 0.3)
                    .removeClass('active');
                item
                    .css('opacity', 1)
                    .addClass('active');
                main.attr('src', link.attr('href'))
            });
        });
    }
};



