var CommonLayout = new Class({
    initialize: function() {
        this.setUpMainMenu();
        this.setUpSearchBox();
        this.setUpInfoBox();
        this.setUpNewsList();
        this.setUpGalleriesList();
        this.setUpSponsors();
    },

    setUpMainMenu: function() {
        $$('#header a').each(function(el) {
            if(el.getProperty('id') != 'logo' && el.getProperty('id') != 'en' && el.getProperty('id') != 'quiz' && !el.getFirst().getProperty('src').test('hov')) {
                el.addEvent('mouseenter', function() {
                    el.getFirst().setProperty('src', el.getFirst().getProperty('src').replace('normal', 'hover'));
                });
                el.addEvent('mouseleave', function() {
                    el.getFirst().setProperty('src', el.getFirst().getProperty('src').replace('hover', 'normal'));
                });
                el.addEvent('mouseenter', function() {
                    el.removeEvent('mouseenter');
                });
            }
        });
    },

    setUpSearchBox: function() {
        var searchField = $('search-field');
        var searchFieldPrompt = $('search-phrase-locale').getProperty('value');
        searchField.setProperty('value', searchFieldPrompt)
        if(searchField) {
            $('search-btn').addEvent('click', function(e) {
                if (!searchField.getProperty('value').clean().length || searchField.getProperty('value').clean() == searchFieldPrompt) {
                    new Event(e).stop();
                    searchField.setProperty('value', 'wpisz szukane słowo...');
                }
            });
            searchField.addEvent('focus', function() {
                if(searchField.getProperty('value').clean() == searchFieldPrompt) {
                    searchField.setProperty('value', '');
                }
            });
        }
    },

    /* news */
    setUpNewsList: function() {
        this.news = $$('#news-box .news');
        this.switchers = $$('#news-box a.change');
        this.size = this.news.length;
        this.interval = null;

        this.switchers.each(function(el) {
            el.addEvent('click', this.changeNewsClicked.bindWithEvent(this, el))
        }, this);

        this.showNews(0);
    },

    changeNewsClicked: function(event, el) {
        new Event(event).stop();
        $clear(this.interval);
        this.showNews(el.get('text').toInt() - 1);
    },

    showNews: function(current) {
        this.news.setStyle('display', 'none');
        this.switchers.removeClass('current');
        this.news[current].setStyle('display', 'block');
        this.switchers[current].addClass('current');
        this.interval = this.showNews.delay(10000, this, (current + 1) % this.size);
    },

    /* galleries */
    setUpGalleriesList: function() {
        this.galleries = $$('#galleries-box .gallery');
        this.galleriesSwitchers = $$('#galleries-box a.change');
        this.galleriesSize = this.galleries.length;
        this.galleriesInterval = null;

        this.galleriesSwitchers.each(function(el) {
            el.addEvent('click', this.changeGalleryClicked.bindWithEvent(this, el))
        }, this);

        this.showGallery(0);
    },

    changeGalleryClicked: function(event, el) {
        new Event(event).stop();
        $clear(this.galleriesInterval);
        this.showGallery(el.get('text').toInt() - 1);
    },

    showGallery: function(current) {
        this.galleries.setStyle('display', 'none');
        this.galleriesSwitchers.removeClass('current');
        this.galleries[current].setStyle('display', 'block');
        this.galleriesSwitchers[current].addClass('current');
        this.galleriesInterval = this.showGallery.delay(10000, this, (current + 1) % this.galleriesSize);
    },
    
    setUpInfoBox: function() {
        if ($('info-bar')) {
            var btn = $('info-box-btn');
            var content = $('info-box-content');
            var span = btn.getElement('span');
            content.setStyle('display', 'none');
            btn.addEvent('click', function(e) {
                new Event(e).stop();
                if(content.getStyle('display') == 'none') {
                    content.setStyle('display', 'block');
                } else {
                    content.setStyle('display', 'none');
                }
            });
            btn.addEvent('mouseenter', function() {
                span.setStyle('text-decoration', 'underline');
            });
            btn.addEvent('mouseleave', function() {
                span.setStyle('text-decoration', 'none');
            });
        }
    },

    setUpSponsors: function() {
        $$('#authors a').each(function(el) {
            el.addEvent('click', function() {
                el.set('target', 'new');
            });
        });
        $$('#sponsors a').each(function(el) {
            if(!el.hasClass('inactive')) {
                el.set('target', 'new');
            } else {
                el.addEvent('click', function(event) {
                    new Event(event).stop();
                })
            }
            el.set('opacity', .7);
            el.addEvent('mouseover', function() {
                el.fade(1);
            })
            el.addEvent('mouseleave', function() {
                el.fade(.7);
            });
        });
    }
});