var gallery = {
	initialize: function(element, options) {
		this.setOptions({
			showInfopane: true,
			fadeDuration: 1000,
			timed: true,
			delay: 7000,
			manualData: [],
			populateData: true,
			elementSelector: "div.ssElement",
			subtitleSelector: "p",
			imageSelector: "img.full",
			defaultTransition: "fade",
			slideInfoZoneOpacity: 1.0,
			slideInfoZoneSlide: false,
			baseClass: 'jdGallery'
		}, options);
		this.fireEvent('onInit');
		this.currentIter = 0;
		this.lastIter = 0;
		this.maxIter = 0;
		this.galleryElement = element;
		this.galleryData = this.options.manualData;
		this.galleryInit = 1;
		this.galleryElements = Array();
		this.galleryElement.addClass(this.options.baseClass);
		this.populateFrom = element;
		this.populateData();
		element.style.display="block";
		this.constructElements();
		if (this.options.showInfopane) this.initInfoSlideshow();
		this.doSlideShow(1);
	},
	populateData: function() {
		currentArrayPlace = this.galleryData.length;
		options = this.options;
		var data = $A(this.galleryData);
		data.extend(this.populateGallery(this.populateFrom, currentArrayPlace));
		this.galleryData = data;
		this.fireEvent('onPopulated');
	},
	populateGallery: function(element, startNumber) {
		var data = [];
		options = this.options;
		currentArrayPlace = startNumber;
		element.getElements(options.elementSelector).each(function(el) {
			elementDict = {
				image: el.getElement(options.imageSelector).getProperty('src'),
				number: currentArrayPlace,
				transition: this.options.defaultTransition,
				link: el.getElement(options.imageSelector).getAttribute('longdesc')
			};
			elementDict.extend = $extend;
			if (options.showInfopane)
				elementDict.extend({
					description: el.getElement(options.subtitleSelector).innerHTML
				});
			data.extend([elementDict]);
			currentArrayPlace++;
			el.remove();
		});
		return data;
	},
	constructElements: function() {
		el = this.galleryElement;
		this.maxIter = this.galleryData.length;
		var currentImg;
		for(i=0;i<this.galleryData.length;i++) {
			var currentImg = new Fx.Styles(
				new Element('div').addClass('slideElement').setStyles({
					'position':'absolute',
					'left':'0px',
					'right':'0px',
					'margin':'0px',
					'padding':'0px',
					'backgroundPosition':"center center",
					'opacity':'0'
					}).injectInside(el),
				'opacity',
				{duration: this.options.fadeDuration}
			);
			currentImg.element.setStyle('backgroundImage', "url('" + this.galleryData[i].image + "')");
			currentImg.element.setAttribute('title', this.galleryData[i].link);
			currentImg.element.addEvent('click', function() {
				window.location = this.title;
			});
			this.galleryElements[parseInt(i)] = currentImg;
		}
	},
	startSlideShow: function() {
		this.fireEvent('onStart');
		this.lastIter = this.maxIter - 1;
		this.currentIter = 0;
		this.galleryInit = 0;
		this.galleryElements[parseInt(this.currentIter)].set({opacity: 1});
		if (this.options.showInfopane)
			this.showInfoSlideShow.delay(0, this);
		this.prepareTimer();
	},
	nextItem: function() {
		this.fireEvent('onNextCalled');
		this.nextIter = this.currentIter+1;
		if (this.nextIter >= this.maxIter)
			this.nextIter = 0;
		this.galleryInit = 0;
		this.goTo(this.nextIter);
	},
	prevItem: function() {
		this.fireEvent('onPreviousCalled');
		this.nextIter = this.currentIter-1;
		if (this.nextIter <= -1)
			this.nextIter = this.maxIter - 1;
		this.galleryInit = 0;
		this.goTo(this.nextIter);
	},
	goTo: function(num) {
		this.clearTimer();
		if (this.options.showInfopane) {
			this.slideInfoZone.clearChain();
			this.hideInfoSlideShow().chain(this.changeItem.pass(num, this));
		} else
			this.currentChangeDelay = this.changeItem.delay(0, this, num);
		this.prepareTimer();
	},
	changeItem: function(num) {
		this.fireEvent('onStartChanging');
		this.galleryInit = 0;
		if (this.currentIter != num) {
			for(i=0;i<this.maxIter;i++) {
				if ((i != this.currentIter)) this.galleryElements[i].set({opacity: 0});
			}
			gallery.Transitions[this.galleryData[num].transition].pass([
				this.galleryElements[this.currentIter],
				this.galleryElements[num],
				this.currentIter,
				num], this)();
			this.currentIter = num;
		}
		this.doSlideShow.bind(this)();
		this.fireEvent('onChanged');
	},
	clearTimer: function() {
		if (this.options.timed)
			$clear(this.timer);
	},
	prepareTimer: function() {
		if (this.options.timed)
			this.timer = this.nextItem.delay(this.options.delay, this);
	},
	doSlideShow: function(position) {
		if (this.galleryInit == 1) {
			imgPreloader = new Image();
			imgPreloader.onload=function(){
				this.startSlideShow.delay(1000, this);
			}.bind(this);
			imgPreloader.src = this.galleryData[0].image;
		}
		else {
			if (this.options.showInfopane) {
				this.showInfoSlideShow.delay((0 + this.options.fadeDuration), this);
			}
		}
	},
	log: function(value) {
		if(console.log)
			console.log(value);
	},
	initInfoSlideshow: function() {
		this.slideInfoZone = new Fx.Styles(new Element('div').addClass('slideInfoZone').injectInside($(this.galleryElement))).set({'opacity':0});
		var slideInfoZoneDescription = new Element('p').injectInside(this.slideInfoZone.element);
		this.slideInfoZone.normalHeight = this.slideInfoZone.element.offsetHeight;
		this.slideInfoZone.element.setStyle('opacity',0);
	},
	changeInfoSlideShow: function() {
		this.hideInfoSlideShow.delay(0, this);
		this.showInfoSlideShow.delay(0, this);
	},
	showInfoSlideShow: function() {
		$('spinner').style.visibility = "hidden";
		this.fireEvent('onShowInfopane');
		this.slideInfoZone.clearTimer();
		element = this.slideInfoZone.element;
		element.getElement('p').setHTML(this.galleryData[this.currentIter].description);
		if(this.options.slideInfoZoneSlide)
			this.slideInfoZone.start({'opacity': [0, this.options.slideInfoZoneOpacity], 'height': [0, this.slideInfoZone.normalHeight]});
		else
			this.slideInfoZone.start({'opacity': [0, this.options.slideInfoZoneOpacity]});
		return this.slideInfoZone;
	},
	hideInfoSlideShow: function() {
		$('spinner').style.visibility = "visible";
		this.fireEvent('onHideInfopane');
		this.slideInfoZone.clearTimer();
		if(this.options.slideInfoZoneSlide)
			this.slideInfoZone.start({'opacity': 0, 'height': 0});
		else
			this.slideInfoZone.start({'opacity': 0});
		return this.slideInfoZone;
	}
};

gallery = new Class(gallery);
gallery.implement(new Events);
gallery.implement(new Options);

gallery.Transitions = new Abstract ({
	fade: function(oldFx, newFx, oldPos, newPos){
		oldFx.options.transition = newFx.options.transition = Fx.Transitions.linear;
		oldFx.options.duration = newFx.options.duration = this.options.fadeDuration;
		if (newPos > oldPos) newFx.start({opacity: 1});
		else {
			newFx.set({opacity: 1});
			oldFx.start({opacity: 0});
		}
	}
});

function startGallery() {
	var pondGallery = new gallery($('thebox'));
	document.gallery = pondGallery;
}

window.onDomReady(startGallery);

