

var _recentTopics = window.IPBoard;

_recentTopics.prototype.recentTopics = {
	ajaxHandler: '',
	updateInterval: '',
	
	init: function()
	{
		document.observe( 'dom:loaded', function()
		{
			$( 'hook_recentTopics' ).select( '.toggle' )[0].observe( 'click', ipb.recentTopics.toggleVisibility );	
		
			ipb.recentTopics.ajaxHandler = new Ajax.PassivePeriodicalUpdater( 'recentTopics', ipb.vars['base_url'] + "app=forums&module=ajax&section=recentTopics", 
			{
				method: 'get', 
				frequency: ipb.recentTopics.updateInterval, 
				decay: 2,
				evalJSON: 'force'
			});
			
			if( ipb.Cookie.get('toggleRecentTopics') == 1 )
			{	
				$( 'recentTopics' ).hide();
				$( 'hook_recentTopics' ).addClassName( 'collapsed' );
				ipb.recentTopics.ajaxHandler.stop();
			}
			
		});
	},
	
	toggleVisibility: function( e )
	{
		if( ipb.board.animating ){ return false; }
		
		Debug.write( 'collapsing' );
		
		var click = Event.element(e);
		var remove = $A();
		var wrapper = $( click ).up( 'div#recentTopicsWrapper' ).down( 'div#recentTopics' );
		Debug.write( wrapper );
		var catname = $( click ).up( 'h3' );
		
		ipb.board.animating = true;
		
		// Get cookie
		var cookie = ipb.Cookie.get( 'toggleRecentTopics' );
		if( cookie == null ){
			cookie = $A();
		} 
		
		Effect.toggle( wrapper, 'blind', {duration: 0.4, afterFinish: function(){ ipb.board.animating = false; } } );
		
		if( catname.hasClassName( 'collapsed' ) )
		{
			catname.removeClassName( 'collapsed' );
			ipb.Cookie.set('toggleRecentTopics', "0", 1);
			
			ipb.recentTopics.ajaxHandler.start();
		}
		else
		{
			new Effect.Morph( $(catname), {style: 'collapsed', duration: 0.4, afterFinish: function(){
				$( catname ).addClassName('collapsed');
				ipb.board.animating = false;
			} });
			ipb.Cookie.set('toggleRecentTopics', "1", 1);
			
			ipb.recentTopics.ajaxHandler.stop();
		}

		
		Event.stop( 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.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']; 
			
			if ( this.runnedOnce == 1 )
			{
				Effect.Pulsate( this.container, { pulses: 3, duration: 1.5 } );
			}
			
			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();