//News Fader version 1.1 written by Tim Gill on 11/03/2010, last edit 12/03/2010

//Please make sure /site_includes/xhr_news_fader.php is in place

Effect.NewsFade = function(element) {
  element = $(element);
  var options = Object.extend({
    from: element.getOpacity() || 1.0,
    to:   0.0,
    afterFinishInternal: function(effect) {
      if (effect.options.to!=0) return;
    }
  }, arguments[1] || { });
  return new Effect.Opacity(element,options);
};

var fader = {
	//**********************************************
	//*                                            *
	//*       Start of stuff you can change        *
	//*                                            *
	//**********************************************
	
	//db variables - shouldn't really need to change
	dbTable : 'cms_news',
	dbDate : 'news_date',
	dbTitle : 'news_title',
	dbUrl : 'news_title_url',
	dbUrlPrefix : '../news/',
	dbSummary : 'news_column_summary',
	dbDisplay : 'news_column_display',
	dbPublish : 'news_publish',
	
	//display settings
	numItems : 3,
	fadeInSpeed : 1,
	displayTime : 3,
	fadeOutSpeed : 1,
	faderHeight : 36,
	
	//dom settings
	faderContainer : 'newsFaderContainer',
	faderBackground : '#262161',
	
	//make below variables blank if not shown on fader
	title : 'newsFaderTitle',
	summary : '',
	readmore : 'newsFaderUrl',
	
	linkInTitle : 0,
	
	
	//**********************************************
	//*                                            *
	//*       End of stuff you can change          *
	//*                                            *
	//**********************************************

	
	//running settings
	count : 0,
	
	//caching variables
	titleArray : [],
	summaryArray : [],
	readmoreArray : [],
	
	next : function() {
		
		if(this.titleArray[this.count]) {
			this.changeFader(this.titleArray[this.count],this.summaryArray[this.count],this.readmoreArray[this.count]);
		} else {
			new Ajax.Request('/main/includes/xhr_news_fader.php', {
				method:     'POST',
				parameters: 'dbTable='+this.dbTable+'&dbDate='+this.dbDate+'&dbTitle='+this.dbTitle+'&dbUrl='+this.dbUrl+'&dbUrlPrefix='+this.dbUrlPrefix+'&dbSummary='+this.dbSummary+'&dbDisplay='+this.dbDisplay+'&dbPublish='+this.dbPublish+'&numItems='+this.numItems+'&count='+this.count,
				onSuccess: function(t) {
					
					var news = t.responseText.evalJSON();
					this.titleArray[this.count] = news.title;
					this.summaryArray[this.count] = news.summary;
					this.readmoreArray[this.count] = news.url;
					
					this.changeFader(news.title,news.summary,news.url);
					
				}.bind(this)
			});
		}
	},
	changeFader : function(newsTitle,newsSummary,newsUrl) {
		
		if(this.title != "") {
			if(this.linkInTitle == 1) {
				$(this.title).update('<a href="'+newsUrl+'">'+newsTitle+'</a>');
			} else {
				$(this.title).update(newsTitle);
			}
		}
		
		if(this.summary != "") {
			$(this.summary).update(newsSummary);
		}
		
		if(this.readmore != "") {
			$(this.readmore).href=newsUrl;
		}
		
		new Effect.Appear(this.faderContainer, { 
			duration: this.fadeInSpeed,
			afterFinish: function() {
				setTimeout(function() {
					new Effect.NewsFade(this.faderContainer, {
						duration: this.fadeOutSpeed,
						afterFinish: function() {
							if(this.count+1 < this.numItems) {
								this.count++;
							} else {
								this.count=0;
							}
							this.next();
						}.bind(this)
					});
				}.bind(this),this.displayTime*1000);
				
			}.bind(this)
		});
	}
}

document.observe( "dom:loaded", function() {
	if(fader.faderHeight > 0) {
		$(fader.faderContainer).setStyle({'height':fader.faderHeight+'px'});
	}
	$(fader.faderContainer).setStyle({'zoom':1});
	$(fader.faderContainer).setStyle({'background':fader.faderBackground});
	fader.next();
});