window.addEvent('domready', function() {
    // Add dynamic input boxes
    var dynamicInput = new DynamicInput('input, textarea');

    // Add news ticker
    new Ticker('newsticker', {
        effect: {
            duration: 500
        }
    });

    // Setup slideshows
    $$('#eyecatcher .slideshow').each(function(element) {
        new Slideshow.Controlled({
            container: element,
            elements: 'li',
            wait: 6000,
            duration: 1250,
            transition: Fx.Transitions.linear,
            autoStart: true,
            controlsContainer: new Element('li', {
                'class': 'navigation'
            })
        });
    });

    // Twitter stream
    var twitterStreamList = new Element('ul').inject('twitter-stream');

    var twitterStreamRequest = new Request.Twitter('ipad_nederland', {
        url: (window.baseUrl || '/') + 'Twitter.ashx',
        retries: 10,
        timeout: 60 * 1000,
        onSuccess: function (tweets) {

            // Add new tweets
            tweets.reverse().each(function (tweet) {
                new Element('li', {
                    html: tweet.text,
                    styles: {
                        opacity: 0
                    },
                    tween: {
                        onComplete: function () {
                            if (this.element.getStyle('opacity').toInt() == 0) {
                                this.element.destroy();
                            }
                        }
                    }
                }).inject(twitterStreamList, 'top').fade(1);
            });

            // Remove excess tweets (more than requested, but without removing new tweets)
            $$(twitterStreamList.getChildren().splice(Math.max(this.options.data.count, tweets.length))).fade(0);
        }
    }).send();

    /* Add focussed style to .round element */
    $$('.round input, .round textarea').addEvents({
        'focus': function() { this.getParent().addClass('focussed'); },
        'blur': function() { this.getParent().removeClass('focussed'); }
    });

    /* Open links in new window */
    $$(document.links).filter(function(a) {
        // Only open link in new window if:
        // - link doesn't already have a target
        // - the domain is different
        // - the link has the class 'external'
        return !a.getProperty('target') &&
               (a.href.test('^http', 'i') && !a.href.test('^' + document.location.href.substring(0, document.location.href.indexOf('/', 8)), 'i')) ||
               a.hasClass('external');
    }).setProperty('target', '_blank');
});
