// JavaScript Document
if ( ! window.Prototype )
	throw new Exception('Can\'t work without prototype');
	
//if ( ! window.Scriptaculous )
//	throw new Exception('Can\'t work without scriptaculous');
	
if ( ! window.BrowserDetect )
	throw new Exception('Can\'t work without BrowserDetect');
	
if ( ! window.hensam )
	var hensam = { Projects : { mehrdafon : {} } };
else if ( ! (hensam.Projects ) )
	hensam.Projects = { mehrdafon : {} } ;
else if ( ! hensam.Projects.mehrdafon  )
	hensam.Projects.mehrdafon = {};
	
hensam.Projects.mehrdafon.Newsflash = Class.create({
    newsElement : 'newsContent',	
	backLink : 'previousNews',
	nextLink : 'nextNews',
	offset : 0,
	numberEntries : 0,
	isFullyInitialized : false,
	initialRefreshInterval : 10,
	refreshInterval : 10,
	intervalId : null,
	lastContent : '',

	newsClickedHandler : function(event) {
		window.location.href = this.newsElement.down('a').href;
	},
	
	updateParameters : function(transport) {
		
		$('news').down('img').hide();
		
		this.offset++;
		if ( this.offset >= this.numberEntries )
		{
			this.offset = 0;
		}
		if ( this.lastContent == transport.responseText )
		{
			this.refreshInterval *= 2;
			window.clearInterval(this.intervalId);
			this.intervalId = window.setInterval(this.startNewsflash.bind(this), this.refreshInterval*1000);
		}
		else
		{
			this.refreshInterval = this.initialRefreshInterval;
			this.lastContent = transport.responseText;
		}
		
		this.newsElement.down('a').hide();
		this.newsElement.observe('click', this.newsClickedHandler.bindAsEventListener(this));
		
		if ( window.EqualHeight ) 
		{
			window.EqualHeight.fromResize = true;
			window.EqualHeight.expandShadows();
		}
		window.setTimeout(this.showLoadingIcon.bind(this), (this.refreshInterval-2)*1000);
	},
	
	showLoadingIcon : function() {
		$('news').down('img').show();
	},
	
	startNewsflash : function(transport) {
		if ( ! this.isFullyInitialized ) 
		{
			this.numberEntries = transport.responseText.replace(/^\s+|\s+$/g, '');
			this.numberEntries *= 1;
			if ( this.numberEntries <= 0 )
			{
				return;
			}
		}
			
		
		new Ajax.Updater(this.newsElement, '/news/index.php', {
			method : 'get',
			parameters : {
				'ajax' : 'true',
				'offset' : this.offset
			},
			onComplete : this.updateParameters.bindAsEventListener(this),
			onFailure : function() {  }
		});
		
		if ( ! this.isFullyInitialized )
		{
			this.intervalId = window.setInterval(this.startNewsflash.bind(this), this.refreshInterval*1000);
			this.isFullyInitialized = true;
		}
	},
	
	gotoNextNews : function(event) {
		event.stop();
		if ( this.isFullyInitialized)
		{
			this.refreshInterval = this.initialRefreshInterval;
			
			window.clearInterval(this.intervalId);
			
			if ( this.offset >= this.numberEntries )
			{
				this.offset = 0;
			}
			
			this.intervalId = window.setInterval(this.startNewsflash.bind(this), this.refreshInterval*1000);
			
			this.startNewsflash();
		}
	},
	
	gotoLastNews : function(event) {
		event.stop();
		if ( this.isFullyInitialized)
		{
			this.refreshInterval = this.initialRefreshInterval;
			
			window.clearInterval(this.intervalId);
			this.offset -= 2;
			if ( 0 > this.offset )
			{
				this.offset = this.numberEntries-1;
			}
			
			this.intervalId = window.setInterval(this.startNewsflash.bind(this), this.refreshInterval*1000);
			
			this.startNewsflash();
		}		
	},
	
	registerContentLoader : function(event) {
		this.newsElement = $(this.newsElement);
		if ( ! this.newsElement )
		{
			//alert('no news container available');
			return;
		}
		this.backLink = $(this.backLink);
		this.nextLink = $(this.nextLink);
		
		this.backLink.observe('click', this.gotoLastNews.bindAsEventListener(this));
		this.nextLink.observe('click', this.gotoNextNews.bindAsEventListener(this));
		
		new Ajax.Request('/news/index.php', {
			method : 'get',
			parameters : {
				'ajax' : 'true',
				'count' : 'true'
			},
			onSuccess : this.startNewsflash.bindAsEventListener(this)
		});
		
	},

	initialize : function() {
		if ( BrowserDetect.browser == 'Explorer' )
		{
			Event.observe(window, 'load', this.registerContentLoader.bindAsEventListener(this));
		}
		else
		{
		    Event.observe(document, 'dom:loaded', this.registerContentLoader.bindAsEventListener(this));
		}
	}
});

new hensam.Projects.mehrdafon.Newsflash();
