/* Copyright 2007 by Oliver Steele.
 * This work is licensed under the MIT License.
 * http://www.opensource.org/licenses/mit-license.php
 */

var gApplication;

$(function() {
    var options = $.jsonCookie('onthisday') || kDefaultOptions;
    var feedCollection = new FeedAggregator(options);
    gApplication = new Application(feedCollection, options);
    for (var key in options.feeds)
        options.feeds[key] && feedCollection.addFeed(key);
    $('#settings-button').click(function(){window.location='settings.html'});
});


/*
 * Application
 */

function Application(feedCollection, options) {
    this.options = options;
    this.feeds = feedCollection;
    this.showFrame = this.showFrame.bind(this);
    feedCollection.onload(this.run.bind(this));
}

Application.prototype.run = function() {
    if (this.timer) return;
    var options = this.options;
    this.showFrame();
    this.timer = setInterval(gApplication.showFrame, options.interval*1000);
}

Application.prototype.stop = function() {
    if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
    }
}

Application.prototype.setOptions = function() {
    var running = this.timer;
    this.stop();
    if (running)
        this.run();
}

Application.prototype.showFrame = function() {
    var item = this.nextItem || this.feeds.nextItem();
    this.setView($('#result'), item);
    this.setHeaders(item);
    var nextItem = this.nextItem = this.feeds.nextItem();
    this.setView($('#prefetch'), nextItem);
}

Application.prototype.setView = function(view, item) {
    var html = item.description;
    html = '<a href="'+item.link+'" target="onthisday">' + html + '</a>';
    view.html(html);
}

Application.prototype.setHeaders = function(item) {
    $('h1').html(item.title);
    $('#source').html('Source: <a href="' + item.link + '">' + item.link + '</a>');
}


/*
 * Feed Aggregator
 */

function FeedAggregator() {
    this.feeds = {};
    this.items = [];
    this.shuffled = [];
    this.loadListener = null;
    // reload every 30 minutes
    setInterval(this.reloadFeeds.bind(this), 30*60*1000);
}

FeedAggregator.prototype.onload = function(callback) {
    this.loadListener = callback;
}

FeedAggregator.prototype.addFeed = function(key) {
    if (key in this.feeds) return;
    this.feeds[key] = null;
    this.fetchFeed(key);
}

FeedAggregator.prototype.reloadFeeds = function() {
    for (var key in this.feeds)
        this.fetchFeed(key)
}

FeedAggregator.prototype.fetchFeed = function(key, callback) {
    var date = new Date,
        dateKey = [1+date.getMonth(), '-', date.getDate()].join('');
    $.getFeed({
        url: ['feeds/', key, '-', dateKey, '.xml'].join(''),
        success: function(feed) {
            this.feeds[key] = feed;
            this.shuffled = [];
            var callback = this.loadListener;
            callback && callback();
        }.bind(this)
    });
}

FeedAggregator.prototype.nextItem = function() {
    if (!this.shuffled.length)
        this.shuffle();
    return this.shuffled.pop();
}

FeedAggregator.prototype.shuffle = function() {
    var feeds = this.feeds,
        shuffled = [];
    for (var key in feeds) {
        var feed = feeds[key];
        if (feed)
            shuffled = shuffled.concat(feed.items)
    }
    this.shuffled = shuffled.shuffle();
}

