/*  Gallery JavaScript framework, version 1.0b2
 *  Uses the Prototype Javascript library -- http://www.prototypejs.org
 *  (c) 2007 Jean Le Clerc, Logiciels Malus Softwares
 *
 *  Gallery is freely distributable under the terms of an MIT-style license.
 *
/*--------------------------------------------------------------------------*/

if(!Malus) var Malus = {};
Malus.Gallery = Class.create();

//*** OPTIONS
//	delay: integer -- seconds between exhibits; '0' means no automatic scrolling
//	slide_changed: fc -- function called when the current slide changes; good place to enable/disable navigational controls; fc(currentExhibit, maxExhibits, paused?)
//	transition: integer -- length of transitions, in seconds; "0" means no transition
Malus.Gallery.prototype = {
	initialize: function(pContainer, pExhibits, pOptions) {
		this.container = $(pContainer);
		this.options = pOptions;
		this.exhibits = pExhibits; // [[exhibit_1],[exhibit_2]...] -- Exhibit_n = [{object_1},{object_2}]
		for(var vID = 0; vID < this.exhibits.length; vID++) {
			for(var vIndex = 0; vIndex < this.exhibits[vID].length; vIndex++) {
				var vTarget = $(this.exhibits[vID][vIndex].id);
				if(vTarget.nodeName.match(/^img$/i)) {
					if(this.exhibits[vID][vIndex].src) {
						var vImage = new Image(1,1);
						vImage = this.exhibits[vID][vIndex].src;
					}
				}
			}
		}
		this.exhibits.currentIndex = 0;
		this._paused = false;
		this._transition = null;
		if((this.options.transition > 0) && (this.options.transition % 2 == 1)) this.options.transition++;
		this.container.setStyle({opacity: 1.0});
		
		this.show(0, true);
		
		this.initialized = true;
	},
	dispose: function() {
	},
	show: function(pExhibitID, pSkipTransition) {
		if((pExhibitID < 0) || (pExhibitID >= this.exhibits.length)) return;
		if(this.autoScroller) {
			clearTimeout(this.autoScroller);
			this.autoScroller = null;
		}
		if(this._transition) {
			this.container.setStyle({"opacity": 1.0});
			clearInterval(this._transition);
			this._transition = null;
		}
		
		if((this.options.transition > 0) && !pSkipTransition) {
			var vHalfTime = (this.options.transition / 2);
			this._transition = setInterval(this._transit, 0.25, this, vHalfTime, pExhibitID, 0);
		} else {
			this._setupExhibit(pExhibitID);
			this.exhibits.currentIndex = pExhibitID;
			this._scheduleNext();
		}
	},
	goto_first: function() {
		this.show(0, false);
	},
	goto_last: function() {
		this.show((this.exhibits.currentIndex - 1), false);
	},
	navigate: function(pDelta) {
		var vNewIndex = this.exhibits.currentIndex + pDelta;
		if(vNewIndex < 0) vNewIndex = (this.exhibits.length - 1);
		else if(vNewIndex >= this.exhibits.length) vNewIndex = 0;
		this.show(vNewIndex, false);
	},
	toggle_play : function() {
		if(this.options.delay > 0) {
			this._paused = !this._paused;
			if(this._paused && this.autoScroller) {
				clearTimeout(this.autoScroller);
				this.autoScroller = null;
				if(this._transition) {
					this.container.setStyle({"opacity": 1.0});
					clearInterval(this._transition);
					this._transition = null;
				}
			} else if(!this._paused) this._scheduleNext();
			if(this.options.slide_changed) this.options.slide_changed(this.exhibits.currentIndex, this.exhibits.length, this._paused);
		}
	},
	
	_setupExhibit: function(pExhibitID) {
		if((pExhibitID < 0) || (pExhibitID >= this.exhibits.length)) return;
		
		//try {
			for(var vIndex = 0; vIndex < this.exhibits[pExhibitID].length; vIndex++) {
				var vTarget = $(this.exhibits[pExhibitID][vIndex].id);
				for(var vProp in this.exhibits[pExhibitID][vIndex]) {
					if(vProp != "id") {
						vTarget[vProp] = this.exhibits[pExhibitID][vIndex][vProp];
					}
				}
			}
		//} catch(e) { }
	},
	_scheduleNext: function() {
		var vGallery = this;
		var vNextExhibit = this.exhibits.currentIndex + 1;
		if(vNextExhibit >= this.exhibits.length) vNextExhibit = 0;
		if(this.options.slide_changed) this.options.slide_changed(this.exhibits.currentIndex, this.exhibits.length, this._paused);
		if(this.options.delay > 0) this.autoScroller = setTimeout(function() { vGallery.show(vNextExhibit); }, this.options.delay * 1000);
	},
	_transit: function(pGallery, pHalfTime, pExhibitID, pOpacityGoal) {
		var vOpacity = parseFloat(pGallery.container.getStyle('opacity'));
		if(isNaN(vOpacity)) vOpacity = 1;
		if(pOpacityGoal == 0) { // fading out
			if(vOpacity > 0) vOpacity -= (1 / (pHalfTime * 4));
			if(vOpacity <= 0) {
				vOpacity = 0;
				clearInterval(pGallery._transition);
				pGallery._setupExhibit(pExhibitID);
				pGallery.exhibits.currentIndex = pExhibitID;
				pGallery._scheduleNext();
				pGallery._transition = setInterval(pGallery._transit, 0.25, pGallery, pHalfTime, pExhibitID, 1);
			}
		} else { // fading in
			if(vOpacity < 1) vOpacity += (1 / (pHalfTime * 2));
			if(vOpacity >= 1) {
				vOpacity = 1;
				clearInterval(pGallery._transition);
				pGallery._transition = null;
			}
		}
		pGallery.container.setStyle({"opacity": vOpacity});
	}
}
