var infoFader2 = new Class({
	initialize: function(options){
		this.options= $extend(
			{
				fxduration:	800,	// number of milliseconds for transition
				slideduration:	4000,	// number of milliseconds to show a slide
				everyPage:	1,	// 0=show  page # / # of pages, 1 = always show a button for each page, greater than one = if <= this number, show a button for each page, else act like 0
				startSlide:	1,	// number of the infopage to start with, "random" for a random one
				startNow:	1,	// 0/false = start paused, 1/true = start started, load = show first slide asap then start show after page has finished loading
				setHeight: 	0,	// 0/false = no, 1/true = set the height of top div to height of tallest subDiv
				
				topDiv:		"infoFader",			// id of div containing all of the info pages
				subDiv:		"info",					// class of div for the info pages
				buttonsDiv:	"infoButtons",			// id for the buttons div
				buttonPrevious:	"infoButtonPrevious",		// id for previous button
				buttonNext:	"infoButtonNext",		// id for next button
				buttonStartStop:"infoButtonStartStop",		// id for start/stop button
				buttonPrefix:	"infoButton",		// id prefix for infopage buttons
				previousText:	"&lt;",				// text to display for previous
				nextText:	"&gt;",					// text to display for next
				startText:	"Start",				// text to display for start
				stopText:	"Pause",				// text to display for stop

				buttonNormalClass:	'infoButton',		// normal class for the buttons
				buttonOverClass:	'infoButtonOver',	// on class for the buttons
				buttonNoHighLightClass:	'infoButtonNoOver'	// class to use for nonhighlight
			}, options || {}
		);
		
		this.periodical = "";
		this.started = 0;
		this.h = 0;
		this.buttons = [];
		
		this.topDiv = $(this.options.topDiv);
		this.topDiv.set('morph',{duration: ""+this.options.fxduration+"", transition: Fx.Transitions.Circ.easeOut, link: "chain"});
		this.subDivs = this.topDiv.getElements('div.'+this.options.subDiv);
		this.subDivs.setStyles({display:'block',opacity:0});
		
		x=this.options.startSlide.toInt();
		if(this.options.startSlide=="random"){
			this.current = Math.floor(Math.random()*this.subDivs.length);
		}else if((x)&&(x<=(this.subDivs.length+1))){
			this.current = this.options.startSlide-1;
		}else{
			this.current = 0;
		}
		
		if((this.options.everyPage>1)&&(this.subDivs.length > this.options.everyPage)) this.options.everyPage=0;
		
		this.makeButtons();
		
		this.subDivs.each(function(sd,i){
			sd.set('morph',{duration: ""+this.options.fxduration+"", transition: Fx.Transitions.Sine.easeOut, link: "chain"});
			sdh = sd.getSize().y;
			if(sdh > this.h ) this.h = sdh;
		},this);
		
		this.buttonsDiv.size = this.buttonsDiv.getSize();
		this.topDiv.size = this.topDiv.getSize();
		
		this.buttonsDiv.setStyles({'marginLeft':(this.topDiv.size.x-this.buttonsDiv.size.x)/2});
		
		this.clickButton(this.current);
		
		this.startOnLoad();
		if(this.options.startNow&&this.options.startNow!="load") this.start();
	},
	
	makeButtons: function(){
		this.buttonsDiv = new Element('div',{'id':this.options.buttonsDiv}).inject(this.topDiv,'bottom');
		this.buttonPrevious = new Element('div',{
			'id':this.options.buttonPrevious,
			'class':this.options.buttonNormalClass,
			'html':this.options.previousText,
			'events':{
				'click':function(){
					this.clickPrevious();
				}.bind(this)
			}
		}).inject(this.buttonsDiv,'bottom');
		
		if(this.options.everyPage){
			this.subDivs.each(function(sd,i){
				this.buttons[i] = new Element('div',{'class':this.options.buttonNormalClass,'id':this.options.buttonPrefix+i,'html':i+1,'events':{'click':function(){this.clickButton(i);}.bind(this)}}).inject(this.buttonsDiv,'bottom');
				//this.buttons[i].set('morph',{duration: ""+this.options.fxduration+"", transition: Fx.Transitions.Sine.easeOut, link: "chain"});
			},this);
		}else{
			this.counter = new Element('div',{'class':this.options.buttonNoHighLightClass , 'id':this.options.buttonPrefix+'Counter', 'html':this.current+" / "+(this.subDivs.length)}).inject(this.buttonsDiv,'bottom');
		}
		this.buttonNext = new Element('div',{'id':this.options.buttonNext,'class':this.options.buttonNormalClass,'html':this.options.nextText,'events':{'click':function(){this.clickNext();}.bind(this)}}).inject(this.buttonsDiv,'bottom');
		this.buttonStartStop = new Element('div',{'id':this.options.buttonStartStop,'class':this.options.buttonNormalClass,'html':this.options.stopText,'events':{'click':function(){this.clickStartStop();}.bind(this)}}).inject(this.buttonsDiv,'bottom');
	},
	
	clickStartStop: function(){
		if(this.started){
			this.stop();
		}else{
			this.start();
		}
	},
	
	start: function(){
		this.periodical = this.next.periodical(this.options.slideduration,this);
		this.started = 1;
		this.buttonStartStop.set('html', this.options.stopText);
		
	},
	
	startOnLoad: function(){
		window.addEvent('load', function(){
			if(this.options.setHeight){
				this.topDiv.size.h=this.h;
				this.topDiv.morph({height:this.h});
				//this.topDiv.setStyle('height',this.h);
			}
			if(this.options.startNow=="load") this.start();
		}.bind(this));
		if(Browser.Engine.trident) window.fireEvent('load');
	},
	
	stop: function(){
		$clear(this.periodical);
		this.started = 0;
		this.buttonStartStop.set('html', this.options.startText);
	},
	
	next: function(){
		this.clearButton();
		this.current++;
		if(this.current>=this.subDivs.length) this.current=0;
		this.setButton();
	},
	
	clearButton: function(){
		this.subDivs[this.current].morph({opacity:0});
		//if(this.options.everyPage) this.buttons[this.current].morph('.'+this.options.buttonNormalClass);
		if(this.options.everyPage) this.buttons[this.current].removeClass(this.options.buttonOverClass);
	},
	
	setButton: function(){
		this.subDivs[this.current].morph({opacity:1});
		if(this.options.everyPage){
			//this.buttons[this.current].morph('.'+this.options.buttonOverClass);
			this.buttons[this.current].addClass(this.options.buttonOverClass);
		}else{
			this.counter.set('text',(this.current+1)+" / "+this.subDivs.length);
		}
	},
	
	clickButton: function(i){
		this.stop();
		this.clearButton();
		this.current=i;
		this.setButton();
	},
	
	clickNext: function(){
		this.stop();
		this.next();
	},
	
	clickPrevious: function(){
		this.stop();
		this.clearButton();
		this.current--;
		if(this.current<0) this.current=this.subDivs.length-1;
		this.setButton();
	}
	
});
