(function($){
        
    var frs = this;
    
    frs.FrontPage = Class.extend({
        init: function(options) {
            this.options = $.extend({}, this.options, options);

            // Search form tabs
            this.searchTabs = new frs.Tabs($('#search .tabs li a'), SearchTab, $('#form-tabs-content'));

            // Top5 tabs
            this.top5Tabs = new frs.Tabs($('#top5 .tabs li a'), Top5Tab, $('#top5Content'), { dataType:'json', title: $('#top5 .header h2') });
            this.lmsTabs = new frs.Tabs($('#lms .tabs li a'), Top5Tab, $('#lmsContent'), { dataType:'json', title: $('#lms .header h2') });

            // Banner
            if (this.options.promotions.length > 1) this.promotions = new MainPromotion($('#main-promotion'), this.options);

            // Newsletter form
            this.newsLetterForm = new NewsletterForm($('#newsLetter'));
        }
    });

    var SearchTab = frs.Tab.extend({
        handleClick: function(e) {
            e.preventDefault();
            var form = frs.searchApp.searchForm,
                self = this;
            $.post(form.options.putStateUrl, form.$el.serialize(), function() {
                $.ajax({
                    type: self.options.tabs.options.type,
                    dataType: self.options.tabs.options.dataType,
                    url: self.$el.attr('data-src'),
                    beforeSend: function(xhr, settings) { self.beforeSend(self, xhr, settings); },
                    success: function(data) { self.success(self, data); },
                    error: function(xhr, status) { self.error(self, xhr, status); }
                });
            });
        }
    });

    var Top5Tab = frs.Tab.extend({
        success: function(self, data) {
            self.options.tabs.options.title.html(data.title);
            this._super(self, data);
        }
    });

    var NewsletterForm = frs.Form.extend({
        init: function(el, options) {
            this.options = $.extend(true, {}, this.options, options);
            this._super(el);

            this.msg = $('#newsletter-message');
        },
        options: {
            formMode: 'compact',
            compactErrPos: 'right'
        },
        events: {
            submit: function(e) {
                e.preventDefault();
                this.validate();
                if (this.isValid) {
                    this.$el.hide();
                    this.msg.html(frs.loader);

                    var self = this,
                        data = {
                            firstName: this.fields.items['firstName'].val(),
                            lastName: this.fields.items['lastName'].val(),
                            email: this.fields.items['email'].val()
                        };
                    $.ajax({
                        cache: false,
                        type: 'POST',
                        url: this.el.action,
                        timeout: 60000,
                        data: data,
                        dataType: 'html',
                        success: function(data, textStatus, XMLHttpRequest) {
                            self.msg.html(data);
                            new NewsletterBack($('#newsletter-back'), { newsletterForm: self });
                        },
                        error:function(XMLHttpRequest, textStatus) {
                            self.msg.html('Uutiskirjeen tilaus ei valitettavasti onnistunut. Voit yrittää uudelleen tai ottaa yhteyttä suoraan Aurinkomatkoihin.');
                            new NewsletterBack($('#newsletter-back'), { newsletterForm: self });
                        }
                    });
                }

                return this.isValid;
            }
        },
        retrieveForm: function() {
            this.msg.empty();
            this.$el.show();
        }
    });
    var NewsletterBack = frs.Node.extend({
        events: {
            click: function(e) {
                e.preventDefault();
                this.options.newsletterForm.retrieveForm();
            }
        }
    });

    var MainPromotion = frs.Node.extend({
        init: function(el, options) {
            this.options = $.extend(true, {}, this.options, options);
            this._super(el, this.options);

            this.caption = this.$el.children('h2');
            this.subCaption = this.$el.children('p');
            this.link = this.$el.children('a');

           this.switcher = new frs.Collection($('#promotion-switcher a'), PromotionSwitcherLink);

            if(this.options.delay) {
                var self = this,
                    index = (this.options.promotions.length > 1) ? 1 : 0;
                this.t = setTimeout(function() { self.change(index); }, this.options.delay);
            }
        },
        options: { delay: 5000 },
        change: function(index) {
            var p = this.options.promotions[index],
                delay = p.delay,
                self = this;

            this.switcher.collection.removeClass('act');
            this.switcher.items[index].$el.addClass('act');

            this.$el.fadeOut('fast', function() {
                self.caption.html(p.caption);
                self.subCaption.html(p.subCaption);
                self.link[0].href = p.url;
                self.link.html(p.linkText);
            }).fadeIn('fast');

            //set delay and execute
            if(this.options.delay) {
                clearTimeout(this.t);
                var nextIndex = (index+1 >= this.options.promotions.length) ? 0 : index+1,
                    nextDelay = (delay) ? delay : this.options.delay;
                this.t = setTimeout(function(){ self.change(nextIndex); }, nextDelay);
            }
        }
    });

    var PromotionSwitcherLink = frs.Node.extend({
        events: {
            click: function(e) {
                e.preventDefault();
                frs.frontPage.promotions.change(this.index);
            }
        }
    });

}).call(frs, jQuery);
