var infoFader = 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
				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
				showButtonText:	1,					// show button text, or just put in a space (image background button)
				
				data:	false,			// full url to load divs & images from json
				//slideLayout: "<a href=\"{link}\"><img src=\"{image}\" alt=\"{title}\" title=\"{title}\" /></a><p><a href=\"{link}\">{title}</a><br/><br/>{description}</p>",
				slideLayout: [
					{
						'tag':'a',
						'atrr':{
							'href':'link',
							'html':{
								'tag':'img',
								'attr':{
									'src':'image',
									'alt':'title',
									'title':'title'
								}
							}
						}
					},{
						'tag':'a',
						'attr':{
							'href':'link',
							'html':'title'
						}
					},{
						'tag':'br'
					},{
						'tag':'br'
					},{
						'tag':'p',
						'attr':{
							'html':'description'
						}
					}
				],
				
				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"});
		if(this.options.data){
			if(typeOf(this.options.data)=="array"){
				this.makeSlides();
			}else{
				this.loadData();
			}
		}else{
			this.finishStartup();
		}
	},
	
	loadData: function(){
		new Request.JSON({
			url: this.options.data,
			onSuccess: function(data){
				this.options.data = data;
				this.makeSlides();
			}.bind(this)
		}).get();
	},
	
	makeSlides: function(){
		if(typeOf(this.options.slideLayout)=="string"){
			this.options.data.each(function(d){
				var myTags = this.options.slideLayout.substitute(d);
				div = new Element('div',{'class':this.options.subDiv}).inject(this.topDiv);
				Elements.from(myTags).inject(div);
			},this);
			this.finishStartup();
		}else{
			this.imageList = [];
			this.options.data.each(function(d){
				if(d.image) this.imageList.include(d.image);
			},this);
			this.preloadImages();
		}
	},
	
	preloadImages: function(){
		this.images = Asset.images(this.imageList,{
			onComplete: function(){
				this.prelaodImagesComplete();
			}.bind(this)
		});
	},
	
	prelaodImagesComplete: function(){
		this.options.data.each(function(d){
			div = new Element('div',{'class':this.options.subDiv}).inject(this.topDiv);
			this.options.slideLayout.each(function(s){
				this.makeElement(div,s,d);
			},this);
		},this);
		this.finishStartup();
	},
	
	makeElement: function(parent,layout,data){
		if(layout.tag == 'img'){
			this.images.each(function(image){
				if(image.src.contains(data.image)){
					myImg = image.clone();
					Object.each(layout.attr,function(value,key){
						var mv = (data[value]) ? data[value] : value;
						myImg.set(key,mv);
					},this);
					myImg.inject(parent);
				}
			},this);
		}else{
			var ele = new Element(layout.tag).inject(parent);
			Object.each(layout.attr,function(value,key){
				if(typeOf(value)=='array'){
					value.each(function(v){
						if(typeOf(v)=='object'){
							this.makeElement(ele,v,data);
						}else{
							ele.appendText(data[v]);
						}
					},this);
				}else{
					var mv = (data[value]) ? data[value] : value;
					ele.set(key,mv);
				}
			},this);
		}
	},
	
	makeButtons: function(){
		if((this.options.everyPage>1)&&(this.subDivs.length > this.options.everyPage)){
			this.options.everyPage=0;
		}
		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':(this.options.showButtonText)?i+1:"&nbsp;",
					'events':{
						'click':function(){
							this.clickButton(i);
						}.bind(this)
					}
				}).inject(this.buttonsDiv,'bottom');
			},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');
		
		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});
	},
	
	makeFX: function(){
		this.subDivs.each(function(sd,i){
			sd.fade('hide');
			sdh = sd.getSize().y;
			if(sdh > this.h ) this.h = sdh;
		},this);
	},
	
	finishStartup: function(){
		this.subDivs = this.topDiv.getElements('div.'+this.options.subDiv);
		this.subDivs.setStyles({display:'block',opacity:0});
		this.makeFX();
		
		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;
		}
		
		this.makeButtons();
		this.clickButton(this.current);
		
		if(this.options.setHeight){
			var p = (this.topDiv.getFirst('p')) ? this.topDiv.getFirst('p').getSize().y : 0;
			this.h+=this.buttonsDiv.size.y+p;
			this.topDiv.size.h=this.h;
			this.topDiv.morph({height:this.h});
		}
		if(this.options.startNow) this.start();
		//this.topDiv.getChildren('a').each(function(a){
		$$('#'+this.options.topDiv+' a').each(function(a){
			if(a.href){
				if(this.checkHost(a.href,'flavorseal')){
					a.target=="_blank";
				}else{
					a.href=this.mergeQS(a.href);
				}
			}
		},this);
	},
	
	checkHost: function(a,b){
		var c=a.slice(0,a.indexOf("?")),d=c.indexOf(b);
		return d==-1;
	},
	
	mergeQS: function(a){
		var b=a.indexOf("?"),c=(b==-1)?a.length:b,d=a.slice(0,c),e=a.slice(c),f={},g={},h=[];
		e.replace(new RegExp("([^?=&]+)(=([^&]*))?","g"),function($0,$1,$2,$3){f[$1]=$3;});
		location.search.replace(new RegExp("([^?=&]+)(=([^&]*))?","g"),function($0,$1,$2,$3){g[$1]=$3;});
		for(var i in g){if(f[i]===undefined)f[i]=g[i];}
		for(var j in f){h.push(j+"="+f[j]);}
		if(h.length) return d+"?"+h.join("&");
		else return d;
	},
	
	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);
	},
	
	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].fade('out');
		if(this.options.everyPage) this.buttons[this.current].removeClass(this.options.buttonOverClass);
	},
	
	setButton: function(){
		this.subDivs[this.current].fade('in');
		if(this.options.everyPage){
			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();
	}
	
	
});

