<!--//
    
 // Create an array to cache new events from the feed
 // We do this so we can pop them in a steady stream, 
 // not in the bunches they arrive in from the rss feed.
    var COEFeeder_object = '';
function COEFeeder(list_element_id, max_items, url) {
    this.feed_url = "feeder.php";
    this.list_element_id = "#" + list_element_id;
    this.cachedEvents = new Array();
    this.working = false;
    COEFeeder_object = this;
    this.get_fresh_after = 4; // how many interations to wait before trying to get more events.    
    this.fresh_counter = 0;
    this.show_next_interval_int = setInterval(function() {
            
            COEFeeder_object.showNextCached()
            if (COEFeeder_object.needFresh()) {
                COEFeeder_object.getFresh();
            }
            
        },2000); // Shows the next event in the cache
    this.max_items = max_items? max_items : 25;    
    this.last_guid = '';
    this.first_cache_count = 5;
    
    this.cached = 0;
    this.feed_url = url? url : "feeder.php";
    this.firstLoad();
}


COEFeeder.prototype.needFresh = function () { 
    COEFeeder_object.fresh_counter++;
    
    if( COEFeeder_object.fresh_counter >= COEFeeder_object.get_fresh_after) {
        COEFeeder_object.fresh_counter=0;
        return true;
    }
    return false;
}


COEFeeder.prototype.firstLoad = function () {
    var t= Math.random();
    
    jQuery.get(this.feed_url,"r=" + t , function(data) {
            $(data).filter('.item').each(function() {
                    
                    if (COEFeeder_object.cached <= COEFeeder_object.first_cache_count) {
                        COEFeeder_object.cachedEvents.push($(this));                        
                        COEFeeder_object.cached++;
                    } else { 
                        $(COEFeeder_object.list_element_id).prepend($(this)); // Add the new cached item to the visible feed
                    }
                    COEFeeder_object.last_guid = $(this).attr('guid');
                }
                )
                }
        );    
}


COEFeeder.prototype.getFresh = function () {
    var t= Math.random();
    jQuery.get(this.feed_url,"r=" + t  +"&guid=" + this.last_guid, function(data) {
            $(data).filter(".item").each(function() {
                    COEFeeder_object.last_guid = $(this).attr('guid');
                    // If this is a new item add it to the top of the array
                    COEFeeder_object.cachedEvents.push($(this));                        
                    
                }
                )
                }
        );
}
		
// We only one event item animating at a time
// So create a variable to tell us when the animation is done
		
// Pulls the next item out of the cached array.
COEFeeder.prototype.showNextCached = function (){
    if (    COEFeeder_object.working){return;} // Don't start another if still working
        COEFeeder_object.working = true; // Begin working
    var item = this.cachedEvents.shift(); // Pull the next item out of the array
    if (!item) {    COEFeeder_object.working=false; return;} // If we've emptied the cache quit working
    
    $(this.list_element_id).prepend(item); // Add the new cached item to the visible feed
    var h = item.height(); // grab it's dimensions
    item.css("opacity",0); // make it invisible
    item.css("height", 0); // make it flat
		    
    // ANIMATION
    // first,  we expand the new item to it's original height, pushing the stack down
    // second, fade in it's opacity
    // skip,   the easing option is null doesn't work
    // last,   stop working
    item.animate({height: h}, 500).animate({opacity: "1.0"}, 500,null,function(){ COEFeeder_object.clearOld();COEFeeder_object.working=false; });
			
}
    
// Clear the old items out of #feedData based on time.
// Otherwise the list just grows and grows till your computer dies.
    COEFeeder.prototype.clearOld = function () {
        // grab the last child in #hiddenData (the current rss)
        // set it's time as a variable
        // kill all children in #feedData
        var children = $(this.list_element_id ).children();
        //        console.log(children);
        if (children.length > this.max_items) {
            for( x = this.max_items; x < children.length; x++) {
                $(children[x]).remove();
            }
        }
    }
		
		
    //-->
