var gallery = new Class({
	Extends: shadowbox,
	initialize: function(options){
		this.options= $extend(
			{
				fxduration:	800,	// number of milliseconds for transition
				maxWidth: 1024,
				maxHeight: 768,
				hideBackground: true,
				className:	"shadowbox",
				baseClass:	"sbOuter",
				constantContainerSize: false,
				constantImageSize: false,
				constantCaptionWidth: false,	// set to true if you want the caption to be the same width as image (only effective with constantContainerSize)
				
				enableSlideshowAutoStart: false,
				slideshowDelay: 5000,			 // miliseconds before switching to next image (1000 per second)
				
				relBase: 'gallery',
				everyPage:	1,	// 0=show   # / # , 1 = always show a button for each page, greater than one = if <= this number, show a button for each page, else act like 0
				
				previousLabel: ' Previous ',
				nextLabel: ' Next ',
				closeLabel: ' Close ',
				startLabel: ' Start Slideshow ',
				stopLabel: ' Stop Slideshow ',
				runningClass: false,	// set to the class you wanted added to the start/stop button while running
				
				showPrevious:	true,
				showNext:		true,
				showClose:		true,
				showStartStop:	true,
				showCount:		true
			}, options || {}
		);
		this.slides = new Object();
		this.slideShowOn = this.slideShowRunning = false;
		this.links = $$('a');
		this.links.each(function(a,i){
			if(a.hasClass(this.options.className)){
				a.href=a.get('href');
				a.addEvents({
					'click': function(){
						this.clickLink(i);
						return false;
					}.bind(this)
				});
				if (a.rel && a.rel.test(eval('/^'+this.options.relBase+'/i'))){
					if(!this.slides[a.rel]) this.slides[a.rel]=new Array();
					this.slides[a.rel].push(i);
				}
			}
		},this);
	},
	
	createControlDivs: function(){
		if(!this.shadowbox.controls){
			this.shadowbox.controls = new Element('div', {'class':'sbControls'}).inject(this.shadowbox);
			if(this.options.showPrevious){
				this.shadowbox.controls.previous = new Element('div', {'class':'sbButton','html':this.options.previousLabel}).inject(this.shadowbox.controls);
				this.shadowbox.controls.previous.addEvents({
					'click':function(){this.slidePrevious();}.bind(this),
					'mouseover':function(){this.addClass('sbButtonOver');},
					'mouseout':function(){this.removeClass('sbButtonOver');}
				});
			}
			if(this.options.showStartStop){
				this.shadowbox.controls.startStop = new Element('div', {'class':'sbButton','html':this.options.startLabel}).inject(this.shadowbox.controls);
				this.shadowbox.controls.startStop.addEvents({
					'click':function(){this.slideShow();}.bind(this),
					'mouseover':function(){this.addClass('sbButtonOver');},
					'mouseout':function(){this.removeClass('sbButtonOver');}
				});
				if(this.options.enableSlideshowAutoStart) this.shadowbox.controls.startStop.set('html',this.options.stopLabel);
			}
			if(this.options.showCount){
				if((this.options.everyPage)&&((this.options.everyPage==1)||(this.slides[this.link.rel].length <= this.options.everyPage))){
					this.shadowbox.controls.buttons=[];
					this.slides[this.link.rel].each(function(sd,i){
						this.shadowbox.controls.buttons[i] = new Element('div',{'class':'sbButton','html':i+1,'rel':this.slides[this.link.rel][i]}).inject(this.shadowbox.controls);
						this.shadowbox.controls.buttons[i].addEvents({
							'click':function(){this.slideButton(this.slides[this.link.rel][i]);}.bind(this),
							'mouseover':function(){this.addClass('sbButtonOver');},
							'mouseout':function(){this.removeClass('sbButtonOver');}
						});
					},this);
				}else{
					this.shadowbox.controls.counter = new Element('div',{'class':'sbButton','html':" "}).inject(this.shadowbox.controls);
				}
			}
			if(this.options.showNext){
				this.shadowbox.controls.next = new Element('div', {'class':'sbButton','html':this.options.nextLabel}).inject(this.shadowbox.controls);
				this.shadowbox.controls.next.addEvents({
					'click':function(){this.slideNext();}.bind(this),
					'mouseover':function(){this.addClass('sbButtonOver');},
					'mouseout':function(){this.removeClass('sbButtonOver');}
				});
			}
			if(this.options.showClose){
				this.shadowbox.controls.close = new Element('div', {'class':'sbButton','html':this.options.closeLabel}).inject(this.shadowbox.controls);
				this.shadowbox.controls.close.addEvents({
					'click':function(){this.close();}.bind(this),
					'mouseover':function(){this.addClass('sbButtonOver');},
					'mouseout':function(){this.removeClass('sbButtonOver');}
				});
			}
			this.setCounter();
			this.shadowbox.controls.effect = new Fx.Morph(this.shadowbox.controls, {duration: this.options.fxduration, transition: Fx.Transitions.Sine.easeOut, link: 'chain'});
			
		}
		this.shadowbox.controls.effect.start({'opacity': 1});
	},
	
	setCounter: function(){
		if(this.shadowbox.controls&&this.options.showCount){
			if((this.options.everyPage)&&((this.options.everyPage==1)||(this.slides[this.link.rel].length <= this.options.everyPage))){
				$$("div."+this.options.baseClass+' div.sbButtonOn').removeClass('sbButtonOn');
				this.shadowbox.controls.buttons.each(function(sd,i){
					if(sd.get('rel')==this.linkNum) sd.addClass('sbButtonOn');
				},this);
			}else{
				this.shadowbox.controls.counter.set('html',(this.slides[this.link.rel].indexOf(this.linkNum)+1)+" / "+(this.slides[this.link.rel].length));
			}
		}
	},
	
	clickLink: function(link){
		this.createDivs();
		this.resetStyles();
		this.showLink(link);
		if (this.link.rel && this.link.rel.test(eval('/^'+this.options.relBase+'/i'))){
			this.createControlDivs();
		}
		if(this.options.enableSlideshowAutoStart) timeoutID = this.slideShow.delay(this.options.slideshowDelay, this);
		return false;
	},
	
	showLink: function(link){
		this.hideLink();
		this.linkNum = link;
		this.link = this.links[this.linkNum];
	},
	
	completeShowLink: function(){
		this.shadowbox.effect.removeEvents('complete');
		//this.shadowbox.effect.addEvents({'complete':function(){this.shadowbox.spinner.position();}.bind(this)});
		if((this.link.href.test(/jpg/i))||(this.link.href.test(/gif/i))||(this.link.href.test(/png/i))){
			this.loadImage();
		}else{
			this.loadFrame();
		}
		this.setCounter();
	},
	
	hideLink: function(){
		this.shadowbox.effect.addEvents({'complete':function(){/*this.shadowbox.spinner.position();*/this.completeShowLink();}.bind(this)});
		//this.shadowbox.spinner.show();
		this.shadowbox.effect.start({
			'1': {'opacity': '1'},
			'2': {'opacity': '1'},
			'13':{'opacity': '0'},
			'14':{'opacity': '0'},
			'15':{'opacity': '0'},
			'16':{'opacity': '0'},
			'17':{'opacity': '0'},
			'18':{'opacity': '0'}
		});
	},
	
	slideNext: function(){
		c = this.slides[this.link.rel].indexOf(this.linkNum);
		c = (c+1>=this.slides[this.link.rel].length) ? 0 : c+1;
		this.showLink(this.slides[this.link.rel][c]);
		return false;
	},
	
	slideButton: function(link){
		this.showLink(link);
		return false;
	},
	
	slideShow: function(){
		if(this.slideShowOn){
			$clear(this.slideShowRunning);
			if(this.options.showStartStop){
				this.shadowbox.controls.startStop.set('html',this.options.startLabel);
				if(this.options.runningClass) this.shadowbox.controls.startStop.removeClass(this.options.runningClass);
			}
			this.slideShowOn = false;
		}else{
			this.slideNext();
			this.slideShowRunning = this.slideNext.periodical(this.options.slideshowDelay, this);
			if(this.options.showStartStop){
				this.shadowbox.controls.startStop.set('html',this.options.stopLabel);
				if(this.options.runningClass) this.shadowbox.controls.startStop.addClass(this.options.runningClass);
			}
			this.slideShowOn = true;
		}
	},
	
	slidePrevious: function(){
		c = this.slides[this.link.rel].indexOf(this.linkNum);
		c = (c-1<0) ? this.slides[this.link.rel].length-1 : c-1;
		this.showLink(this.slides[this.link.rel][c]);
		return false;
	},
	
	getImageHeight: function(){
		if(this.shadowbox.controls){
			this.calcDims(this.bigImage.width.toInt(),this.bigImage.height.toInt(),this.scrollSizeWithMargins(this.shadowbox.imagebox.caption).y,this.scrollSizeWithMargins(this.shadowbox.controls).y);
		}else{
			this.calcDims(this.bigImage.width.toInt(),this.bigImage.height.toInt(),this.scrollSizeWithMargins(this.shadowbox.imagebox.caption).y);
		}
	},
	
	getFrameHeight: function(){
		if(this.shadowbox.controls){
			this.calcDims(this.options.maxWidth,this.options.maxHeight,this.scrollSizeWithMargins(this.shadowbox.framebox.caption).y,this.scrollSizeWithMargins(this.shadowbox.controls).y);
		}else{
			this.calcDims(this.options.maxWidth,this.options.maxHeight,this.scrollSizeWithMargins(this.shadowbox.framebox.caption).y);
		}
	}
});
