var sayHuh = false;
var immediatemode = true;

var effectfps = 15;
if ( window.webkit ) { effectfps = 30; }
if ( window.gecko ) { effectfps = 30; }
if ( window.ie7 ) { effectfps = 15; }
if ( window.ie6 ) { effectfps = 8; }

var imagepreloadutility = new Class (
	{
		loadstarted: false,
		loadcompleted: false,
		completeFunc: null,
		completeFuncCookie: null,
		img: null,
		initialize: function ( url ) {
			this.url = url;
			this.startLoading = this.startLoading.bind(this);
			this.imageLoadedEvent = this.imageLoadedEvent.bind(this);
			this.isLoaded = this.isLoaded.bind(this);
			this.getIMG = this.getIMG.bind(this);
			this.getURL = this.getURL.bind(this);
			this.loadNewUrl = this.loadNewUrl.bind(this);
		},
		load: function ( callbackfunction ) {
				this.completeFunc = callbackfunction;
				this.img.addEvent( 'load', this.imageLoadedEvent );
				this.img.setAttribute ( 'src', this.url );
		},
		startLoading: function ( callbackfunction ) {
			if ( !this.loadstarted ) {
				this.completeFunc = callbackfunction;
				this.img = new Element( 'img' );
				this.img.addClass('loading');
				this.img.addEvent( 'load', this.imageLoadedEvent );
				this.loadstarted = true;
				this.img.setAttribute ( 'src', this.url );
			} else {
				if ( sayHuh ) { alert ('imagepreloadutility: redundant startLoading call'); }			
			}
		},
		imageLoadedEvent: function () {
			this.loadcompleted = true;
			this.img.removeClass('loading');
			this.img.removeEvent( 'load', this.imageLoadedEvent );
			if ( this.completeFunc != null ) { this.completeFunc(); }
		},
		isLoaded: function () {
			return this.loadcompleted;
		},
		getIMG: function () {
			if ( this.img == null ) {
				if ( sayHuh ) { alert ('imagepreloadutility: getIMG called before image object creation'); }			
			}
			return this.img;
		},
		getURL: function () {
			return this.url;
		},
		loadNewUrl: function ( newurl, callbackfunction ) {
			this.url = newurl;
			this.loadstarted = false;
			this.load ( callbackfunction );
		}
	}
);
var imagegallerythumbnailcontroller = new Class (
	{
		revealtime:500,
		thumbnailscale: .5,
		initialize: function ( imagedata ) {
			this.imagedata = imagedata;
			this.processimagedata();
			this.loadThumbnail = this.loadThumbnail.bind(this);
			this.thumbnailLoadedEvent = this.thumbnailLoadedEvent.bind(this);
			this.injectThumbnailInPage = this.injectThumbnailInPage.bind(this);
			this.injectedThumbnail = this.injectedThumbnail.bind(this);
			this.selectThumbnail = this.selectThumbnail.bind(this);
			this.deselectThumbnail = this.deselectThumbnail.bind(this);
			this.mouseOverThumbnail = this.mouseOverThumbnail.bind(this);
			this.mouseOutThumbnail = this.mouseOutThumbnail.bind(this);
			this.setClickHandler = this.setClickHandler.bind(this);
			this.thumbnailClicked = this.thumbnailClicked.bind(this);
			this.injectedThumbnailPosition = this.injectedThumbnailPosition.bind(this);
			this.getThumbnailURL = this.getThumbnailURL.bind(this);
		},
		processimagedata: function () {
			var thumbinfo = this.imagedata.getElement('.thumb');
			this.thumbnailurl = thumbinfo.getAttribute('href');
			this.thumbnailNominalWidth = thumbinfo.getAttribute('width');
			this.thumbnailNominalHeight = thumbinfo.getAttribute('height');

			this.thumbnailWidth = this.thumbnailNominalWidth * this.thumbnailscale;
			this.thumbnailHeight = this.thumbnailNominalHeight * this.thumbnailscale;
		},
		thumbnailLoaded: false,
		loadThumbnail: function ( imageLoadedHandler, id ) {
			this.thumbnailPreloader = new imagepreloadutility ( this.thumbnailurl );
			this.imageLoadedHandler = imageLoadedHandler;
			this.imageLoadedHandlerId = id;
			this.thumbnailPreloader.startLoading ( this.thumbnailLoadedEvent );
		},
		thumbnailLoadedEvent: function ( ) {
			this.thumbnailLoaded = true;
			this.imageLoadedHandler ( this.imageLoadedHandlerId );
		},
		thumbnailInjected: false,
		injectThumbnailInPage: function ( destination, where ) {
			var theImg = this.thumbnailPreloader.getIMG();

			theImg.inject( destination, where );
			this.thumbnailInjected = true;
//			theImg.setStyle('width',1);
//			theImg.setStyle('height',1);
//			theImg.setOpacity(0);
			theImg.setStyle('width',this.thumbnailWidth);
			theImg.setStyle('height',this.thumbnailHeight);
			theImg.setStyle('width',1);
			theImg.setStyle('height',1);
			theImg.setOpacity(0);
			this.revealfx = new Fx.Styles(theImg, { duration:this.revealtime, wait:false, fps:effectfps});
			//this.revealfx.start( { 'opacity': 1, 'width': this.thumbnailWidth , 'height':this.thumbnailHeight } );
			this.revealfx.start( { 'opacity': 1, 'width': this.thumbnailWidth , 'height':this.thumbnailHeight } );

			theImg.addEvent( 'mouseover', this.mouseOverThumbnail );
			theImg.addEvent( 'mouseout', this.mouseOutThumbnail );
			
		},
		injectedThumbnail: function () {
			return this.thumbnailPreloader.getIMG();
		},
		getThumbnailURL: function () {
			return this.thumbnailurl;
		},
		isselected: false,
		selectThumbnail: function () {
			this.isselected = true;
			this.thumbnailPreloader.getIMG().setStyle( 'border-color', '#000' );
		},
		deselectThumbnail: function () {
			this.isselected = false;
			this.thumbnailPreloader.getIMG().setStyle( 'border-color', '#FFF' );		
		},
		mouseOverThumbnail: function ( e ) {
			var eventinfo = new Event(e); eventinfo.stop();
			if ( this.isselected ) {
				eventinfo.target.setStyle( 'border-color', '#000' );
			} else {
				eventinfo.target.setStyle( 'border-color', '#0F0' );
			}
		},
		mouseOutThumbnail: function ( e ) {
			var eventinfo = new Event(e); eventinfo.stop();
			if ( this.isselected ) {
				eventinfo.target.setStyle( 'border-color', '#000' );
			} else {
				eventinfo.target.setStyle( 'border-color', '#FFF' );
			}
		},
		setClickHandler: function ( clickHandler, clickid ) {
			this.clickid = clickid;
			this.clickHandler = clickHandler;
			this.thumbnailPreloader.getIMG().addEvent( 'click', this.thumbnailClicked );
		},
		thumbnailClicked: function ( e ) {
			var eventinfo = new Event(e); eventinfo.stop();
			this.clickHandler ( this.clickid );
		},
		injectedThumbnailPosition: function ( overflown ) {
			var pos = this.thumbnailPreloader.getIMG().getPosition( [ $( overflown ) ] );
			if ( window.ie6 ) { 
				return { 'top': pos.y, 'left': pos.x, 'width': this.thumbnailWidth, 'height': this.thumbnailHeight, 'position': 'absolute' };
			} else {
				var winsize = window.getSize();
				return { 'top': pos.y - winsize.scroll.y, 'left': pos.x - winsize.scroll.x, 'width': this.thumbnailWidth, 'height': this.thumbnailHeight, 'position': 'fixed' };
			}
		}
	}
);
var imagegalleryimagedisplaycontroller = new Class (
	{
		revealtime:400,
		initialize: function ( gallerydiv, imagedata, id ) {
			this.gallerydiv = gallerydiv;
			this.imagedata = imagedata;
			this.id = id;
			this.displayimage = null;
			this.processimagedata();
			this.loadImage = this.loadImage.bind(this);
			this.showImage = this.showImage.bind(this);
			this.hideImage = this.hideImage.bind(this);
			this.injectImageInPage = this.injectImageInPage.bind(this);
			this.injectedImage = this.injectedImage.bind(this);
			this.clickImage = this.clickImage.bind(this);
			this.adjustImagePosition = this.adjustImagePosition.bind(this);
		},
		processimagedata: function () {
			var imageinfo = this.imagedata.getElement('.image');
			this.imageurl = imageinfo.getAttribute('href');
			this.imageNominalWidth = imageinfo.getAttribute('width');
			this.imageNominalHeight = imageinfo.getAttribute('height');
			var thumbinfo = this.imagedata.getElement('.thumb');
			this.thumbnailurl = thumbinfo.getAttribute('href');
			this.thumbnailNominalWidth = thumbinfo.getAttribute('width');
			this.thumbnailNominalHeight = thumbinfo.getAttribute('height');
		},
		imageLoaded: false,
		imageDisplayed: false,
		loadImage: function ( ) {
			this.imagePreloader = new imagepreloadutility ( this.imageurl );
			this.imagePreloader.startLoading ( this.imageLoadedEvent.bind(this) );
		},
		imageLoadedEvent: function ( ) {
			this.imageLoaded = true;
			if ( this.displayingThumbnail ) {
				this.rezUpThumbnail();
			}
		},
		rezUpThumbnail: function ( ) {
			if ( this.displayimage != null ) {
				this.displayimage.removeClass ( 'loading' );
				this.displayimage.setAttribute('src',this.imageurl);
			}
		},
		imageInjected: false,
		displayingThumbnail: false,
		thumbnail: null,
		injectImageInPage: function ( destination, thumbnailposition ) {
			if ( this.imagefx != null ) {
				this.imagefx.stop();
			}
			this.displayingThumbnail = !this.imageLoaded;
			if ( this.displayimage == null ) {
				if ( this.displayingThumbnail ) {
					this.displayimage = new Element( 'img' );
					this.displayimage.setAttribute ( 'src', this.thumbnailurl );
					this.displayimage.addClass ( 'loading' );
				} else {
					displayingThumbnail = false;
					this.displayimage = this.imagePreloader.getIMG();
				}
				this.displayimage.setStyle('position', thumbnailposition.position );
				this.displayimage.setStyle('width', thumbnailposition.width );
				this.displayimage.setStyle('height', thumbnailposition.height );
				this.displayimage.setStyle('top', thumbnailposition.top );
				this.displayimage.setStyle('left', thumbnailposition.left );
				// this.displayimage.addEvent( 'mouseover', this.mouseOverImage.bind(this) );
				// this.displayimage.addEvent( 'mouseout', this.mouseOutImage.bind(this) );
			}
			this.displayimage.inject( destination );
			this.displayimage.addEvent( 'click', this.clickImage );
			this.imageInjected = true;
		},
		imageFXComplete: function ( ) {
			if ( this.imageInjected && !this.imageShouldShow ) {
				this.imageInjected = false;
				this.displayimage.removeEvent( 'click', this.clickImage );
				this.displayimage.remove();
				this.displayimage = null;  /* should we keep this around for next time the image is displayed? */
			}
		},
		removeImage: function ( thumbnailposition ) {
			if ( this.imageInjected ) {
				if ( this.imagefx != null ) { this.imagefx.stop(); }
				this.imagefx = new Fx.Styles(this.displayimage, { duration:this.revealtime, wait:false, fps:effectfps, onComplete: this.imageFXComplete.bind(this) } );
				this.imagefx.start( { 'top': thumbnailposition.top, left: thumbnailposition.left, width: thumbnailposition.width , height: thumbnailposition.height, 'border-width':'2px' } );
			}
		},
		injectedImage: function () {
			return this.imagePreloader.getIMG();
		},
		imageShouldShow: false,
		showImage: function ( destination, thumbnailposition ) {
			this.imageShouldShow = true;
			if ( this.imagefx != null ) { this.imagefx.stop(); }
			if ( !this.imageInjected ) {
				this.injectImageInPage( destination, thumbnailposition );
				this.displayimage.setStyle('border-width','2px');
			}
			this.imagefx = new Fx.Styles(this.displayimage, { duration:this.revealtime, wait:false, fps:effectfps, onComplete: this.imageFXComplete.bind(this) } );
			var thePosition = this.displayPosition();
			this.imagefx.start( { 'top': thePosition.top, left: thePosition.left, width: thePosition.width , height: thePosition.height, 'border-width':'10px' } );
		},
		hideImage: function ( thumbnailposition ) {
			this.imageShouldShow = false;
			this.removeImage( thumbnailposition );
		},
		displayPosition: function () {
			var borderSize = 10;
			var winsize = window.getSize();
			var top = ( winsize.size.y / 2 ) - ( this.imageNominalHeight  / 2 ) - borderSize;
			var thePosition = { 'top': top, 'left': ( winsize.size.x / 2 ) - ( this.imageNominalWidth / 2 ) - borderSize, 'width': this.imageNominalWidth, 'height': this.imageNominalHeight, 'position': 'fixed' }
			if ( window.ie6 ) { 
				// internet explorer stinks //
				thePosition.top += winsize.scroll.y;
				thePosition.left += winsize.scroll.x;
				thePosition.position = 'absolute';
			}
			return thePosition;			
		},
		clickImage: function ( ) {
			this.gallerydiv.ImageClick( this.id );
		},
		mouseOverImage: function () {
		},
		mouseOutImage: function () {
		},
		adjustImagePosition: function ( immediate ) {
			if ( this.displayimage != null ) {
				var thePosition = this.displayPosition();
				if ( immediate ) {
					if ( this.imagefx != null ) { this.imagefx.stop(); }
					this.displayimage.setStyle ( 'top', thePosition.top );
					this.displayimage.setStyle ( 'left', thePosition.left );
					this.displayimage.setStyle ( 'width', thePosition.width );
					this.displayimage.setStyle ( 'height', thePosition.height );
				} else {
					if ( this.imagefx != null ) { this.imagefx.stop(); }
					this.imagefx = new Fx.Styles(this.displayimage, { duration:this.revealtime, wait:false, fps:effectfps, onComplete: this.imageFXComplete.bind(this) } );
					this.imagefx.start( { 'top': thePosition.top, left: thePosition.left, width: thePosition.width , height: thePosition.height, 'border-width':'10px' } );
				}
			}
		}
	}
);
var imagegallerycontrolscontroller = new Class (
	{
		revealtime: 400,
		initialize: function( gallerycontroller, gallerydiv ) {
			this.gallerycontroller = gallerycontroller;
			this.gallerydiv = gallerydiv;
			this.showControls = this.showControls.bind(this);
			this.hideControls = this.hideControls.bind(this);
			this.adjustControls = this.adjustControls.bind(this);
		},
		controlsShow: false,
		revealfx: null,
		showControls: function ( ) {
			if ( !this.controlsShow ) {
				this.controlsShow = true;
				if ( this.controlslivehere == null ) {
					this.createcontrols();
				}
				if ( this.revealfx != null ) { this.revealfx.stop(); }
				this.revealfx = new Fx.Styles(this.controlslivehere, { duration:this.revealtime, wait:false, fps:effectfps});
				this.revealfx.start( { 'opacity': 1 } );				
			}
		},
		createcontrols: function () {
			this.controlslivehere = new Element('div', { 'id':'slideshowcontrols' } );
			this.controlslivehere.setOpacity(0);

			this.nextbutton = new Element('button', { 'id':'nextButton', 'class': 'imagegallerycontrol' } );
			this.nextbutton.setHTML('<span>next</span>');
			this.nextbutton.addEvent( 'click', this.controlsNextCallback.bind(this) );
			this.nextbutton.addEvent( 'mouseover', this.controlsMouseOverCallback.bind(this) );
			this.nextbutton.addEvent( 'mouseout', this.controlsMouseOutCallback.bind(this) );
			this.nextbutton.inject( this.controlslivehere );

			this.prevbutton = new Element('button', { 'id':'prevButton', 'class': 'imagegallerycontrol' } );
			this.prevbutton.setHTML('<span>previous</span>');
			this.prevbutton.addEvent( 'click', this.controlsPrevCallback.bind(this) );
			this.prevbutton.addEvent( 'mouseover', this.controlsMouseOverCallback.bind(this) );
			this.prevbutton.addEvent( 'mouseout', this.controlsMouseOutCallback.bind(this) );
			this.prevbutton.inject( this.controlslivehere );

			this.closebutton = new Element('button', { 'id':'closeButton', 'class': 'imagegallerycontrol' } );
			this.closebutton.setHTML('<span>close</span>');
			this.closebutton.addEvent( 'click', this.controlsCloseCallback.bind(this) );
			this.closebutton.addEvent( 'mouseover', this.controlsMouseOverCallback.bind(this) );
			this.closebutton.addEvent( 'mouseout', this.controlsMouseOutCallback.bind(this) );
			this.closebutton.inject( this.controlslivehere );

			this.controlslivehere.inject( this.gallerydiv );

			this.controlwidth = 0;
			this.controlwidth += this.closebutton.getSize().size.x;
			this.controlwidth += this.nextbutton.getSize().size.x;
			this.controlwidth += this.prevbutton.getSize().size.x;

			var controlPosition = this.calculatedControlPosition();			
			this.controlslivehere.setStyle('position', controlPosition.position );
			this.controlslivehere.setStyle('top', controlPosition.y );
			this.controlslivehere.setStyle('left', controlPosition.x );
			this.controlslivehere.setStyle('visibility','visible');

		},
		controlsMouseOverCallback: function ( e ) { var event = new Event(e); $(event.target).setStyle('color','#F00'); },
		controlsMouseOutCallback: function ( e ) { var event = new Event(e); $(event.target).setStyle('color','#000'); },
		controlsCloseCallback: function ( e ) { e = new Event(e).stop(); this.gallerycontroller.galleryClose(); },
		controlsNextCallback: function (e) { e = new Event(e).stop(); this.gallerycontroller.galleryNext(); },
		controlsPrevCallback: function (e) { e = new Event(e).stop(); this.gallerycontroller.galleryPrevious(); },
		hideControls: function ( ) {
			if ( this.controlsShow ) {
				this.controlsShow = false;
				if ( this.revealfx != null ) { this.revealfx.stop(); }
				this.revealfx = new Fx.Styles(this.controlslivehere, { duration:this.revealtime, wait:false, fps:effectfps, onComplete: this.revealfxcomplete.bind(this) });
				this.revealfx.start( { 'opacity': 0 } );				
				
			}		
		},
		revealfxcomplete: function () {
			if ( !this.controlsShow ) {
				this.controlslivehere.remove();
				this.controlslivehere = null;
			}
		},
		controlposfx: null,
		adjustControls: function ( immediate ) {
			if ( this.controlsShow ) {
				var correctPosition = this.calculatedControlPosition();
				var actualPosition = this.controlslivehere.getPosition();
				if ( ( correctPosition.x != actualPosition.x ) || ( correctPosition.y != actualPosition.y ) ) {
					if ( immediate ) {
						if ( this.controlposfx != null ) { this.controlposfx.stop(); this.controlposfx = null; }
						this.controlslivehere.setStyles( { 'top': correctPosition.y, 'left': correctPosition.x } );
					} else {
						if ( this.controlposfx != null ) { this.controlposfx.stop(); this.controlposfx = null; }
						this.controlposfx = new Fx.Styles(this.controlslivehere, { duration: 500, wait:false, fps:effectfps } );
						this.controlposfx.start( { 'top': correctPosition.y, 'left': correctPosition.x } );
					}
				}
			}
		},
		calculatedControlPosition: function () {
			var winsize = window.getSize();
			if ( window.ie6 ) { 
				var maxy = winsize.scroll.y + winsize.size.y - 70;
				var calcy = ( winsize.size.y * 0.85 ) + winsize.scroll.y;
				if ( maxy < calcy ) { calcy = maxy; }
				return { 
					'x': winsize.scroll.x + ( winsize.size.x / 2 ) - ( this.controlwidth / 2 ), 
					'y': calcy, 
					'position': 'absolute'
				};
			} else {
				var maxy = winsize.size.y - 70;
				var calcy = winsize.size.y * 0.85;
				if ( maxy < calcy ) { calcy = maxy; }
				return { 
					'x': ( winsize.size.x / 2 ) - ( this.controlwidth / 2 ) , 
					'y': calcy , 
					'position': 'fixed' 
				};
			}
		}
	}
);
var imagegallerycontroller = new Class (
	{
		parallelLoaderCount: 6,
		imageDisplayed: false,
		initialize: function( gallerydiv, imagedata ) {
			if ( window.ie6 ) { this.parallelLoaderCount = 3; }
			this.thumbnailcontrollers = new Array();
			this.displaycontrollers = new Array();
			this.gallerydiv = gallerydiv;
			this.imagedata = imagedata;
			this.imageCount = this.imagedata.length;
			this.onscreencontrols = new imagegallerycontrolscontroller ( this, this.gallerydiv );
			this.whereToInject = this.whereToInject.bind(this);
			this.thumbnailControllerForIndex = this.thumbnailControllerForIndex.bind(this);
			this.ImageClick = this.ImageClick.bind(this);
			this.galleryNext = this.galleryNext.bind(this);
			this.galleryPrevious = this.galleryPrevious.bind(this);
			this.galleryClose = this.galleryClose.bind(this);
			this.windowResizeCallback = this.windowResizeCallback.bind(this);
			this.loadNextThumbnail = this.loadNextThumbnail.bind(this);
			window.addEvent( 'resize', this.windowResizeCallback );
			if ( this.injectEmptyGallery() ) { 
				this.populategallery = this.populategallery.bind ( this );
				this.populategallery(); 
			}
		},
		injectEmptyGallery: function () {
			if ( this.gallerydiv ) {
				this.gallerydiv.setStyle('display','block');
				this.displayimageliveshere = new Element('div', { 'id':'displayimageliveshere' } );
				this.thumbnailslivehere = new Element('div', { 'id':'thumbnailslivehere' } );
				this.thumbnailslivehere.inject( this.gallerydiv );
				this.displayimageliveshere.inject( this.gallerydiv );
			}
			return ( this.gallerydiv != null );
		},
		nextToLoad: 0,
		thumbnailLoadCount:0,
		allthumbnailsloaded:false,
		populategallery: function () {
			for ( var x = 0; x < this.parallelLoaderCount; x++ ) { setTimeout ( this.loadNextThumbnail, 1 ); }
		},
		allLoaded: false,
		lastToStartLoading: -1,
		loadNextThumbnail: function () {
			var loadindex = this.nextToLoad;
			this.nextToLoad++;
			if ( loadindex < this.imageCount ) {
				this.thumbnailcontrollers[loadindex] = new imagegallerythumbnailcontroller ( this.imagedata[loadindex] );
				this.lastToStartLoading = loadindex;
				this.thumbnailcontrollers[loadindex].loadThumbnail ( this.thumbnailLoaded.bind( this ), loadindex );
			}
		},
		thumbnailControllerForIndex: function ( index ) {
			return this.thumbnailcontrollers[index];
		},
		thumbnailClicked: function ( index ) {
			this.toggleThumbnailSelection( index );
		},
		thumbnailLoaded: function ( index ) {
			var locationToInject = this.whereToInject( index );
			this.thumbnailcontrollers[index].injectThumbnailInPage ( locationToInject.obj, locationToInject.where );
			this.thumbnailcontrollers[index].setClickHandler ( this.thumbnailClicked.bind(this), index );
			this.thumbnailLoadCount++;
			if ( this.thumbnailLoadCount < this.imageCount ) {
				setTimeout ( this.loadNextThumbnail, 1 );
			} else {
				if ( !this.allthumbnailsloaded ) {
					this.allthumbnailsloaded = true;
					if ( this.imageCount > 0 ) { this.preloadImage(0); }
				}
			}
		},
		whereToInject: function ( index ) {
			if ( this.thumbnailLoadCount > 0 ) {
				// find the first injected item with an index higher and the place is before it
				var cursor = index + 1;
				while ( cursor <= this.lastToStartLoading) {
					if ( this.thumbnailcontrollers[cursor].thumbnailInjected ) {
						return { obj: this.thumbnailcontrollers[cursor].injectedThumbnail(), where: 'before' }; 
					}
					cursor++;
				}
			}
			// if that didn't work just put it at the bottom
			return {  obj: this.thumbnailslivehere, where: 'bottom' };
		},
		thumbnailSelected: false,
		selectedThumbnail: -1,
		toggleThumbnailSelection: function ( index ) {
			if ( this.thumbnailSelected ) {
				var saveSelectionIndex = this.selectedThumbnail;
				this.deselectCurrentThumbnail();
				if ( saveSelectionIndex != index ) {
					this.selectThumbnail(index);
				} else {
					this.onscreencontrols.hideControls();			
				}
			} else {
				this.selectThumbnail(index);
				this.onscreencontrols.showControls();
			}
			this.preloadImage(index+1);
			this.preloadImage(index-1);
		},
		windowResizeCallback: function () {
			if ( this.onscreencontrols.controlsShow ) {
				this.onscreencontrols.adjustControls( immediatemode );
			}
			if ( this.thumbnailSelected ) {
				if ( this.displaycontrollers[this.selectedThumbnail] != null ) {
					this.displaycontrollers[this.selectedThumbnail].adjustImagePosition ( immediatemode );
				}
			}
		},
		selectThumbnail: function ( index ) {
			if ( !this.thumbnailSelected || ( this.selectedThumbnail != index ) ) {
				this.deselectCurrentThumbnail();
				this.selectedThumbnail = index;
				this.thumbnailSelected = true;
				this.thumbnailcontrollers[this.selectedThumbnail].selectThumbnail();
				this.displayImage(index);
			}
		},
		deselectCurrentThumbnail: function ( ) {
			if ( this.thumbnailSelected ) {
				this.thumbnailcontrollers[this.selectedThumbnail].deselectThumbnail();
				this.takedownImage( this.selectedThumbnail );
				this.thumbnailSelected = false;
				this.selectedThumbnail = -1;
			}
		},
		ImageClick: function ( index ) {
			this.toggleThumbnailSelection ( index );
		},
		preloadImage: function ( preloadIndex ) {
			if ( this.imageCount > 0 ) {
				if ( preloadIndex >= this.imageCount ) { preloadIndex = 0; }
				if ( preloadIndex < 0 ) { preloadIndex = this.imageCount - 1; }
				if ( !this.displaycontrollers[preloadIndex] ) {
					this.displaycontrollers[preloadIndex] = new imagegalleryimagedisplaycontroller ( this, this.imagedata[preloadIndex], preloadIndex );
					this.displaycontrollers[preloadIndex].loadImage();
				}
			}
		},
		preloadNext: function ( index ) {
			this.preloadImage(index + 1);
		},
		preloadPrevious: function ( index ) {
			this.preloadImage(index - 1);
		},
		displayingImage: false,
		displayedImageIndex: -1,
		displayImage: function ( index ) {
			this.onscreencontrols.showControls();
			if ( this.displayingImage && ( this.displayedImageIndex != index ) ) {
				this.takedownImage();
			}
			this.displayingImage = true;
			this.displayedImageIndex = index;
			this.preloadImage(index);
			this.displaycontrollers[index].showImage(
				this.displayimageliveshere, this.thumbnailcontrollers[index].injectedThumbnailPosition(this.thumbnailslivehere) 
			);
		},
		takedownImage: function ( ) {
			if ( this.displayingImage ) {
				this.displaycontrollers[this.displayedImageIndex].hideImage(
					this.thumbnailcontrollers[this.displayedImageIndex].injectedThumbnailPosition(this.thumbnailslivehere) 
				);
				this.displayingImage = false;
				this.displayedImageIndex = -1;
			}
		},
		galleryClose: function () {
			if ( this.displayingImage ) { 
				this.takedownImage(); 
				this.onscreencontrols.hideControls();		
			}
		},
		galleryNext: function () {
			if ( this.displayingImage ) {
				var next = this.displayedImageIndex + 1;
				if ( ( next >= this.imageCount ) || ( next < 0 ) ) { next = 0; }
				this.displayImage(next);
			}
			this.toggleThumbnailSelection( next );
			this.preloadNext( next );
		},
		galleryPrevious: function () {
			if ( this.displayingImage ) {
				var previous = this.displayedImageIndex - 1;
				if ( ( previous >= this.imageCount ) || ( previous < 0 ) ) { previous = this.imageCount - 1; }
				this.displayImage(previous);
			}
			this.toggleThumbnailSelection( previous );
			this.preloadPrevious( previous );
		}
	}
);

var applyGallery = new Class(
	{
		initialize: function() {
			this.imagegalleries = new Array();
			var selector = '.imagegallerywrap';
			var objs = $$(selector);
			objs.each(
				function ( obj ) { 
					if ( obj.hasClass('imagegallerywrap') ) {
						var gallerydiv = obj.getElement ( '#imagegallery' );
						this.imagegalleries[this.gallerycount] = new imagegallerycontroller ( gallerydiv, $ES ( '#imglinks .imagedata', obj ) );
					}
				},
				this // note the binding to this //
			);
		}
	}
);

window.addEvent( 'domready', function() { document.applyGallery = new applyGallery(); } );



