/**
 * Author: Bas Wenneker
 * Email: <b.wenneker@gmail.com>
 * Website: <http://www.solutoire.com>
 * Date: June 27th, 2008
 */
var Site = {
    // Storage array fir names of entries, used to identify new entries. 
    entries: [],
    // Timer that counts from 60 to 0.
    timer: 0,
    /**
    * This function is called on 'domready', it initializes the palette and
    * colour feeds, start calling 'poll()' every second.
    */
    init: function() {
        this.paletteFeed = new google.feeds.Feed('http://feeds2.feedburner.com/spagirlbeautytips');
        this.colourFeed = new google.feeds.Feed('http://feeds2.feedburner.com/spagirlbeautytips');
        this.poll.periodical(1000, this);
    },
    /**
    * This function is called every second. When the timer is 0 or lower, it
    * loads new feeds with the Google AJAX Feed API.
    */
    poll: function() {
        if (--this.timer <= 0) {
            this.colourFeed.load(this.onLoadCallback.bind(this));
            this.paletteFeed.load(this.onLoadCallback.bind(this));
            this.timer = 60;
        }
        if ($('refresh') != null) {
            $('refresh').set('html', 'Refreshing feeds in ' + this.timer + ' second(s)...');
        }
    },
    /**
    * This is the callback function that's called when the Feed API returns the
    * JSON. It checks if there are new entries in the returned entries, if there
    * are any, they're added to the list.
    * @param {Object} json - JSON Object from the Google AJAX Feed API.
    */
    onLoadCallback: function(json) {
        if (json.error) return;

        /* only show X at a time , get it from the header */
        var max_entries = 50;

        for (var i = 0; i <= json.feed.entries.length - 1; i++) {
            var entry = json.feed.entries[i];
            if (!this.entries.contains(entry.title)) {
                this.entries.push(entry.title);

                /* this is the full content of each entry*/
                //$('feeds').innerHTML = ('<tr><td class="entry"><a href="{link}" title="{title}">{content}</a></td></tr>').substitute(entry) + $('feeds').innerHTML;
                /* this is the title only of each entry */
                if ($('mini-feeds') != null) {
                    max_entries = $('mini-feeds').getAttribute('max-entries');
                    var num_entries = $$('#mini-feeds .entry').length == null ? 0 : $$('#mini-feeds .entry').length;

                    //check how many entries we have right now, if limit is reached, don't append anymore
                    if (num_entries < max_entries) {
                        $('mini-feeds').innerHTML = $('mini-feeds').innerHTML + ('<div class="list-item entry"><a class="text-color3" href="{link}" target="_blank" title="{title}">{title} ></a></div>').substitute(entry);
                    }
                }

                if ($('feeds') != null) {

                    max_entries = $('feeds').getAttribute('max-entries');
                    var num_entries = $$('#feeds .entry').length == null ? 0 : $$('#feeds .entry').length;

                    //check how many entries we have right now, if limit is reached, don't append anymore
                    if ($$('#feeds .entry').length < max_entries) {
                        $('feeds').innerHTML = $('feeds').innerHTML + ('<div class="list-item entry"><a class="text-color3" href="{link}" target="_blank" title="{title}">{title} ></a></div>').substitute(entry);
                    }
                }
            }
        }
        $$('.entry').set('tween', { duration: 2000 }).highlight().removeClass('entry');
    }
};
// Load Google AJAX Feed API.
google.load('feeds', '1');
// Call the Site.init function when the dom is ready.
window.addEvent('domready', Site.init.bind(Site));
