
var _recentTopics = window.IPBoard;

_recentTopics.prototype.recentTopics = {
	ajaxHandler: '',
	updateInterval: '',
	
	init: function()
	{
		
	},
	
	pushUpdate: function() 
	{
			ipb.recentTopics.ajaxHandler = new Ajax.PassivePeriodicalUpdater( 'recentTopics', ipb.vars['base_url'] + "app=forums&module=ajax&section=recentTopics&secure_key=" + ipb.vars['secure_hash'],
            {
                method: 'get',
                frequency: ipb.recentTopics.updateInterval,
                decay: 2,
                evalJSON: 'force'
            });
			ipb.recentTopics.ajaxHandler.start();
	},
	
	pushUpdateMyTopics: function() 
	{
			ipb.recentTopics.ajaxHandler = new Ajax.PassivePeriodicalUpdater( 'recentMyTopics', ipb.vars['base_url'] + "app=forums&module=ajax&section=recentTopics&filter=mytopics&secure_key=" + ipb.vars['secure_hash'],
            {
                method: 'get',
                frequency: ipb.recentTopics.updateInterval,
                decay: 2,
                evalJSON: 'force'
            });
			ipb.recentTopics.ajaxHandler.start();
	},
	
	toggleVisibility: function( e )
	{
		
	}
};

//------------------------------
// @author: adrianscott
// http://www.fluther.com/disc/9117/ajaxperiodicalupdater-only-update-if-the-content-has-changed/#quip58902
//------------------------------
Ajax.PassivePeriodicalUpdater = Class.create(Ajax.Base, 
{
	initialize: function($super, container, url, options) 
	{
		$super(options);
		this.onComplete = this.options.onComplete;
		
		this.frequency = this.options.frequency;
		this.decay = this.options.decay;
		
		this.updater = { };
		this.container = $(container);
		this.url = url;
		this.runnedOnce = 0;
		this.lastTidTimestamp = '';

		this.start();
	},
		
	start: function() 
	{
		this.options.onComplete = this.updateComplete.bind(this);
		this.updater = new Ajax.Request(this.url, this.options);
		//this.onTimerEvent();
	},
		
	stop: function() 
	{
		this.updater.options.onComplete = undefined;
		//clearTimeout(this.timer);
		(this.onComplete || Prototype.emptyFunction).apply(this, arguments);
	},
		
	updateComplete: function(t) 
	{
		if (t.responseJSON['last_tid_timestamp'] == this.lastTidTimestamp ) 
		{
			this.decay = this.decay * this.options.decay;
		} 
		else 
		{
			this.decay = this.options.decay;
			this.container.update( t.responseJSON['html'] );
			//this.lastTidTimestamp = t.responseJSON['last_tid_timestamp']; 
			
			this.runnedOnce = 1;
		}
		
		//this.timer = this.onTimerEvent.bind(this).delay(this.decay * this.frequency);
	},
		
	onTimerEvent: function() {
		this.updater = new Ajax.Request(this.url, this.options);
	}
});

ipb.recentTopics.init();
