/* TDGallery 1.0.3
1/1/08
*/
var getGalleryContentDivHref = '/TDGallery/getGalleryContentDiv.php';
var getGalleryImageHref = '/TDGallery/getGalleryImage.php';

var Methods = {
	startGallery: function() {
		if (this.debug) console.debug('startGallery');
		
		this.currentPosition = 0;
		setTimeout(function() {
			this.displayImageNext();	
		}.bind(this), this.periodicDelay-2500);
	},
	
	playpause: function() {
		if (this.debug) console.debug('playpause');

		if(this.isPaused) this.resume();
		else this.pause();
	},
	
	pause: function() {
		if (this.debug) console.debug('pause');
		
		this.isPaused=true;
		clearTimeout(this.timeout);
	},
	
	resume: function() {
		if (this.debug) console.debug('resume');
		
		this.isPaused=false;
		this.displayImageNext();
	},
	
	displayImageNext: function() {
		if (this.debug) console.debug('displayImageNext');
		
		var wrapper = $(this.wrapperId);
		
		var currentDiv = wrapper.childElements().last();
		
		var currentImgId = currentDiv.childElements().first().readAttribute('id');
		var index = currentImgId.indexOf('image_id')+9;
		currentImgId = currentImgId.substr(index)-0;
		
		if (this.fullyCached) {
			var index = $A(this.displayedImages).indexOf(currentImgId);
			var next_index = (index+1) % (this.displayedImages.length);
			this.displayImageId(this.displayedImages[next_index]);
		} else {
			new Ajax.Request( getGalleryImageHref, {
				parameters: {'action':'next', 'current_id':currentImgId, 'gallery_uid':this.galleryUid},
				onSuccess: function(transport) {
					var json = transport.responseText.evalJSON();
					var next_image = json['image_id'];

					if (next_image==null || next_image=='') {
						if (this.looping) next_image = this.displayedImages[0];
						this.fullyCached=true;
					}
					console.debug(this.displayedImages);
					this.displayImageId(next_image);
				}.bind(this)
			});
		}
	},
	
	displayImageId: function(image_id) {
		if (this.debug) console.debug('displayImageId');
		image_id = image_id-0; /* force numeric value */

		var wrapper = $(this.wrapperId);
		var currentDiv = wrapper.childElements().last();
		var currentImgId = currentDiv.childElements().first().readAttribute('id');
		var index = currentImgId.indexOf('image_id')+9;
		currentImgId = currentImgId.substr(index);

		var index = $A(this.displayedImages).indexOf(image_id);
		if (this.isPaused) return;
		if (index != -1) {
			var nextImageDiv = $('image_id_'+image_id).up();
			wrapper.appendChild(nextImageDiv);
			setTimeout(function() {
				if(this.isPaused) return;
				Effect.Appear(nextImageDiv);
				
				setTimeout(function() {
					currentDiv.hide()
				}, 1500);
				this.resetTimer();
			}.bind(this), 2500);
			
		}
		else {
			
			new Ajax.Request( getGalleryContentDivHref, {
			  parameters: {'action':'by_id', 'current_id':image_id},
			  onSuccess: function(transport) {
				
				this.currentPosition++;	
				this.displayedImages[this.currentPosition] = image_id;

				var nextImageDiv = transport.responseText.gsub("\n",'');
				var nextImageDiv = nextImageDiv.gsub("\t",'');
				var nextImageDiv = nextImageDiv.gsub("\r", '');
				
				
				var nextImageDiv = $(nextImageDiv);

				Element.insert(wrapper, nextImageDiv);
				nextImageDiv.hide();

				setTimeout(function() {
					if(this.isPaused) return;
					Effect.Appear(nextImageDiv);
					
					setTimeout(function() {
						currentDiv.hide()
					}, 1500);
					this.resetTimer();
				}.bind(this), 2500);
			  }.bind(this)
			});
		}
	},
	
	resetTimer: function() {
		if (this.debug) console.debug('resetTimer');
		
		this.timeout = setTimeout(function() {
			this.displayImageNext();
		}.bind(this), this.periodicDelay);
	}
};

var TDGallery = Class.create(Methods, {
	initialize: function(wrapperId, uid) {
		this.debug=true;
		this.isPaused = false;
		this.currentPosition = 0;
		this.fullyCached = false;
		this.periodicDelay = 5000;  /* Minimum value is 2500 (for load time of following images) -- lower than 2500 requires tampering with Effect.Appear 'delay' property as well as the startGallery delay */
		this.looping=true;
		this.timeout;
		this.displayedImages = [];
		
		this.wrapperId = wrapperId;
		if (typeof uid == 'undefined' ) uid = 0;
		this.galleryUid = uid;
		
		new Ajax.Request(getGalleryImageHref, {
			parameters: {'action':'first', 'gallery_uid':this.galleryUid},
			onSuccess: function(transport) {

				var json = transport.responseText.evalJSON();

				if (json['image_id']!=null) {
					this.displayedImages[this.currentPosition] = json['image_id']-0;
				}
			}.bind(this)
		});
		/* this.startGallery(); // Removed to allow time to make attribute changes after initialization but before starting Gallery (ie. for timing changes) -- call startGallery() from index.js */
	}	
});