var IndexController = {
    init: function() {
        // Preload images
        this.attachObservers();
        this.createTweetWidget();

        $.ajax({
          url: 'http://api.twitter.com/1/users/lookup.json?screen_name=advocatennieuws',
          success: function(data) {
             $('span.twitter').append($('<span></span>').html(data[0].followers_count).addClass('counter'));
             $('span.twitter').append($('<span></span>').addClass('nub-1'));
             $('span.twitter').append($('<span></span>').addClass('nub-2'));
          },
          dataType: 'JSONP'
        });

        setTimeout(function() { IndexController.rotateBanner(1); }, 17000);
    },

    rotateBanner: function(current) {
        var current = current;
        next    = current + 1 > $('div.rotator').length ? 1 : current + 1;

        $('div#rotator' + current).css('z-index', 0);
        $('div#rotator' + next).css('z-index', 1);

        $('div#rotator' + next).fadeIn(
                                       2000,
                                       function() { $('div#rotator' + current).css('display', 'none'); }
                                   );

        setTimeout(function() { IndexController.rotateBanner(next); }, 17000);
    },

    checksubmit:function() { 
        if($('form input#goto-plaatsnaam').val() != '' &&
           $('form input#goto-subject').val() != '' &&
           $('form input#goto-plaatsnaam').val() != $('form input#goto-plaatsnaam').attr('placeholder') &&
           $('form input#goto-subject').val() != $('form input#goto-subject').attr('placeholder')) {
            $(this).parents('form').submit();
        }
    },
    attachObservers: function() {
        // Submit form if both of the quick-search elements are set.
        $('form input#goto-plaatsnaam').autocomplete({
            source: '/search/ajaxplaatsnaam',
            minLength:3,
            delay:200,
            search: function(event, ui) {
                // Filter out postal codes
                if(!isNaN($(this).val()[0]) && $(this).val()[0] != ' ') return false;
            }
        }).change(this.checksubmit);

        $('form input#goto-subject').autocomplete({
            source: '/search/ajaxsubject',
            minLength:0,
            delay:200,
            search: function() {
                $(this).unbind('focus');
            }
        }).focus(function(){
            if($(this).val() != '') return;

            $(this).autocomplete("search","");
        }).change(this.checksubmit);
    },
    createTweetWidget: function() {
        $.ajax({
            url: '/index/gettweets',
            dataType: 'json',
            success: function(data) {
                var list = $('<ul></ul>').addClass('tweet-tree')
                                         .css('width', $(data).length*370 + 'px');

                overlay = $('<div></div>').addClass('overlay')
                                          .html($('<a></a>').html($('<img />').attr('src', '/media/images/layout/twitter_logo.png')
                                                                              .attr('alt', ''))
                                                            .attr('href', 'http://twitter.com/AdvocatenNieuws')
                                                            .click(function() { return ! window.open($(this).attr('href')); } ));

                $('div.twitter-widget').html(overlay)
                                       .append(list);

                $(data).each(function() {
                    if(this.retweeted_status) {
                        this.user = this.retweeted_status.user;
                        this.text = this.retweeted_status.text;
                    }

                    var listitem = $('<li></li>');

                    var image = $('<img />').attr('src', this.user.profile_image_url)
                                            .attr('alt', '');
                    var text  = $('<span></span>').html(this.text.replace(/(http:\/\/\S+)/g, '<a target="_blank" href="$1">$1</a>'))
                                                  .addClass('text');
                    var name = $('<span></span>').html('<strong>' + this.user.screen_name + '</strong>' + this.user.name)
                                                       .addClass('name');
                    var created_at = $('<span></span>').html(this.created_at)
                                                       .addClass('created_at');

                    listitem.append(image)
                            .append(name)
                            .append(text)
                            .append(created_at);
                    list.append(listitem);

                    if(text.css('height') == '54px' || text.css('height') == '55px') {
                        name.css('margin-top', '11px');
                    }
                    else if(text.css('height') == '36px' || text.css('height') == '37px') {
                        name.css('margin-top', '21px');
                    }
                    else if(text.css('height') == '18px' || text.css('height') == '19px') {
                        name.css('margin-top', '30px');
                    }
                });
                IndexController.animateTwitterList(list, true);
                $('div.twitter-widget').mouseover(function() {
                    list.stop();
                }).mouseout(function() {
                    IndexController.animateTwitterList(list, false);
                });
            },
            error: function() {
                $('div.twitter-widget').css('visibility', 'hidden');
            }
        });
    },
    animateTwitterList: function(list, reset) {
        start = 904;
        if(reset) {
            list.css('left', 904);
        }
        else {
            start = parseInt(list.css('left').replace('px',''));
        }

        list.animate({left: '-=' + (list.find('li').length * 350 + start)},
                     list.find('li').length * 5000 + start,
                     "linear",
                     function() { IndexController.animateTwitterList(list, true); } );
    }
};

$(function() {
    IndexController.init();
});
