var scroller = {
    initialize: function() {
        this.element = $$('div.clip div.container')[0];
        this.items = this.element.getChildren();
        this.imageContainer = $$('div.frame')[0];
        this.effect = new Fx.Tween(this.element, { property: 'top', link: 'ignore', duration: 200 });
        this.current = 0;
    },
    scroll: function(where) {
        if (!$defined(this.effect.timer)) {
            var px = 0, len = this.items.length - 1;
            switch (where) {
                case 'down':
                    if (this.current === len) {
                        px = -this.element.getStyle('top').toInt();
                        this.current = 0;
                    }
                    else {
                        px = -210;
                        this.current++;
                    }
                    break;
                case 'up':
                    if (this.current === 0) {
                        px = len * -210;
                        this.current = len;
                    }
                    else {
                        px = 210;
                        this.current--;
                    }
                    break;
            }
            this.imageContainer.empty().grab(
              this.items[this.current].getElement('img').clone().setOpacity(0).fade('in')
            );
            this.effect.start(this.element.getStyle('top').toInt() + px);
        }
        
    }
};

var feeds = {
    initialize: function(){
        $('feeds-tabs').setStyle('visibility', 'visible').addEvent('click', function(e){
            var target = $(e.target);
            
            if (target.hasClass('feeds-show-blog')) {
                this.show('feeds-blog', target);
            }
            if (target.hasClass('feeds-show-twitter')) {
                this.show('feeds-twitter', target);
            }
            if (target.hasClass('feeds-show-delicious')) {
                this.show('feeds-delicious', target);
            }
            if (target.hasClass('feeds-show-lastfm')) {
                this.show('feeds-lastfm', target);
            }
            if (target.hasClass('feeds-show-hulu')) {
                this.show('feeds-hulu', target);
            }
            
            target.blur();
            return false;
        }.bind(this));
        this.load('http://feeds.feedburner.com/chromasynthetic', 'feeds-blog');
        this.load('http://feeds.delicious.com/v2/rss/chryu', 'feeds-delicious');
        this.load('http://pipes.yahoo.com/pipes/pipe.run?_id=5n7SVdLz3BGmoCmVEpPZnA&_render=rss&username=chrisjaure', 'feeds-twitter');
        this.load('http://www.lala.com/rss/23318@33242/recentlistens', 'feeds-lastfm');
        this.load('http://www.hulu.com/feed/history/cjaure', 'feeds-hulu');
        this.show('feeds-blog', $$('.feeds-show-blog')[0]);
    },
    
    load: function(url, el){
        var feed = new google.feeds.Feed(url);
        feed.setNumEntries(5);
        feed.load(function(result) {
            if (!result.error) {
                var ul = new Element('ul');
                for (var i = 0; i < result.feed.entries.length; i++) {
                    var entry = result.feed.entries[i], date = dateFormat(new Date(entry.publishedDate), 'd mmm yy, h:MM tt');
                    
                    ul.grab(new Element('li', {'class': 'item', html: '<p class="headline"><a href="' + entry.link + '">' + entry.title + '</a></p><p class="date">' + date + '</p>'})).inject(el, 'top');
                }
            }
        });
    },
    
    show: function(id, target) {
        target.getParent().getChildren().removeClass('active');
        target.addClass('active');
        $$('.feed').setStyle('display', 'none');
        $(id).setStyle('display', 'block');
    }
}

Selectors.Pseudo.visible = function(local) {
    return (Element.getStyle(this, 'display') != 'none');
}

var scroller2 = {
    initialize: function(){
        $('feed-items').addEvent('click', function(e){
            var target = $(e.target);
            if (target.hasClass('feed-item') && !target.getParent().hasClass('active')) {
                $$('#feed-items .active').removeClass('active');
                target.getParent().addClass('active');
            }
        });
    }
}

window.addEvent('domready', function(){
  $(document.body).addClass('js');
});
window.addEvent('load', function(){
    scroller.initialize();
    var fadeEffect = new Fx.Tween('portfolio-next', { property: 'opacity', link: 'chain', duration: 300 });
    var attentionGrabber = function(){
        fadeEffect.start(0.25);
        fadeEffect.start(1);
    }.periodical(2500);
    
    $('portfolio-next').addEvent('click', function(){
      scroller.scroll('down');
      this.blur();
      $clear(attentionGrabber);
      return false;
    });
    $('portfolio-prev').addEvent('click', function(){
      scroller.scroll('up');
      this.blur();
      $clear(attentionGrabber);
      return false;
    });
});

google.load("feeds", "1");
google.setOnLoadCallback(function(){
    feeds.initialize();
});
