if (!Class) {
	// Prototype.js has not been detected, so declare Class declaration ourselves
	var Class = {
	  create: function() {
	    return function() { 
	      this.initialize.apply(this, arguments);
	    }
	  }
	}
}

if (!Function.prototype.bind) {
	// Prototype.js has not been detected, so declare bind extension ourselves
	Function.prototype.bind = function(object) {
	  var __method = this;
	  return function() {
	    __method.apply(object, arguments);
	  }
	}
}

if (!Function.prototype.bindAsEventListener) {
	// Prototype.js has not been detected, so declare bindAsEventListener extension ourselves
	Function.prototype.bindAsEventListener = function(object) {
	  var __method = this;
	  return function(event) {
	    __method.call(object, event || window.event);
	  }
	}
}


if (!TDX) {
	// Declare TDX namespace
	var TDX = {};
}


TDX.PhotoObject = Class.create();
TDX.PhotoObject.prototype = {
	
	initialize: function(id, thumbPath, path, comment) {
		this.id = id;
		this.thumbPath = thumbPath;
		this.path = path;
		this.comment = comment;
	}

}


TDX.PhotoPicker = Class.create();
TDX.PhotoPicker.prototype = {

	initialize: function(element, photoobj, thumbSize, startPhotoID, options) {
		if (typeof element == 'string') element = document.getElementById(element);
		this.picker = element;
		this.photoobj = photoobj;
		this.thumbSize = thumbSize;
		this.startPhotoID = startPhotoID;
	  this.options  = arguments[4] || {};

		this.plTarget = 0;
		this.plIndex = 0;
		this.imgIndex = 0;
		this.timer = null;

		this.pickerlist = this._construct();
		this._fill();
	}, 

	showNext: function() {
		if (this.plIndex < this.photoobj.length-2)
		{
			this.plIndex++;
			this.plTarget = document.getElementById(this.picker.id + '_img' + this.plIndex).offsetLeft * -1;
			this._move();
		}
	}, 
	
	showPrevious: function() {
		if (this.plIndex > 0)
		{
			this.plIndex--;
			this.plTarget = document.getElementById(this.picker.id + '_img' + this.plIndex).offsetLeft * -1;
			this._move();
		}
	}, 

	show: function(photoID) {
		if (photoID > 0) {
			for (var i = 0; i < this.photoobj.length; i++) {
				if (this.photoobj[i].id == photoID) {
					this.plIndex = i
					if (i >= this.photoobj.length-2) this.plIndex = this.photoobj.length-2;
					if (this.plIndex < 0) this.plIndex = 0;
					this.imgIndex = i;
					this.plTarget = document.getElementById(this.picker.id + '_img' + this.plIndex).offsetLeft * -1;
					this._move();
				}
			}
		}
	}, 

	_construct: function() {
		this.picker.style.position = 'relative';
		this.picker.style.width = ((this.thumbSize + 2) * 2) + 'px';
		this.picker.style.height = (this.thumbSize + 2) + 'px';
		this.picker.style.overflow = 'hidden';

		var divPhotoList = document.createElement('DIV');
		divPhotoList.id = this.picker.id + '_list';
		divPhotoList.style.position = 'absolute';
		divPhotoList.style.left = '0px';
		divPhotoList.style.top = '0px';
		divPhotoList.style.width = ((this.thumbSize + 2) * this.photoobj.length) + 'px';
		divPhotoList.style.height = (this.thumbSize + 2) + 'px';

		this.picker.appendChild(divPhotoList);
		divPhotoList = null;
		return document.getElementById(this.picker.id + '_list');
	}, 

	_fill: function() {
		for (var i = 0; i < this.photoobj.length; i++) {
			var img = document.createElement('img');
			img.id = this.picker.id + '_img' + i;
			img.src = this.photoobj[i].thumbPath;
			img.style.borderTop = '1px solid #ffffff';
			img.style.borderBottom = '1px solid #ffffff';
			img.style.borderLeft = '1px solid #ffffff';
			img.style.borderRight = '1px solid #ffffff';
			img.width = this.thumbSize;
			img.height = this.thumbSize;
      if ( typeof document.implementation != 'undefined' &&
         document.implementation.hasFeature('HTML',   '1.0') &&
         document.implementation.hasFeature('Events', '2.0') &&
         document.implementation.hasFeature('CSS',    '2.0') ) {
         img.addEventListener('mouseover', this._thumb_onMouseOver.bindAsEventListener(this),  false);
         img.addEventListener('click', this._thumb_onClick.bindAsEventListener(this), false);
      } else {
         img.attachEvent('onmouseover', this._thumb_onMouseOver.bindAsEventListener(this));
         img.attachEvent('onclick', this._thumb_onClick.bindAsEventListener(this));
      }

			this.pickerlist.appendChild(img);
			if (this.photoobj[i].id == this.startPhotoID) {
				this.plIndex = i;
				this.imgIndex = i;
			}
		}
		
		this.pickerlist.style.left = (-1 * (this.thumbSize + 2) * this.plIndex) + 'px';
	}, 

	_move: function() {
		var stepDuration = 10;
		var stepSize = 8;
		if (this.options.duration) stepDuration = this.options.duration;
		if (this.options.step) stepSize = this.options.step;
	
		if (this.pickerlist.offsetLeft > this.plTarget) {
			var diff = this.pickerlist.offsetLeft - this.plTarget;
			if (diff > stepSize) diff = stepSize;
			this.pickerlist.style.left = (this.pickerlist.offsetLeft - diff) + 'px';
			this.timer = setTimeout(this._move.bind(this), stepDuration);
		}	else if (this.pickerlist.offsetLeft < this.plTarget) {
			var diff = this.plTarget - this.pickerlist.offsetLeft;
			if (diff > stepSize) diff = stepSize;
			this.pickerlist.style.left = (this.pickerlist.offsetLeft + diff) + 'px';
			this.timer = setTimeout(this._move.bind(this), stepDuration);
		}	else {
			clearTimeout(this.timer);
		}
	}, 

	_thumb_onMouseOver: function(e) {
    if (arguments.length == 0) e = event;
    var eventTarget = e.target ? e.target : e.srcElement;

		var idx = parseInt((eventTarget.id).substring( (this.picker.id + '_img').length ));
		if (this.photoobj[idx].id > 0) eventTarget.style.cursor = 'pointer';
	}, 

	_thumb_onClick: function(e) {
    if (arguments.length == 0) e = event;
    var eventTarget = e.target ? e.target : e.srcElement;

		var newImgIndex = parseInt((eventTarget.id).substring( (this.picker.id + '_img').length ));
		if (this.photoobj[newImgIndex].id > 0) {
			if (newImgIndex != this.imgIndex) {
				this.imgIndex = newImgIndex;
				if (this.options.onClick) this.options.onClick(this.photoobj[newImgIndex]);
			}
		}
	}

}


TDX.PhotoSlideshow = Class.create();
TDX.PhotoSlideshow.prototype = {

	initialize: function(photoobj, onChange, options) {
		this.photoobj = photoobj;
		this.onChange = onChange;
	  this.options  = arguments[2] || {};
		if (this.options.duration == undefined) this.options.duration = 10;		
		this.options.duration = this.options.duration * 1000;

		this.timer = null;
		this.isPaused = false;
		this.isFinished = true;
		this.slideIndex = this._reset();
	}, 

	start: function() {
		if (this.slideIndex >= 0 && this.isFinished) {
			this.isPaused = false;
			this.isFinished = false;
			this._iterate();
		}
	}, 

	pause: function() {
		if (this.slideIndex >= 0 && !this.isFinished) {
			if (this.isPaused) {
				this.isPaused = false;
				this._iterate();
			} else {
				this.isPaused = true;
				clearTimeout(this.timer);
			}
		}
	}, 

	stop: function() {
		if (this.slideIndex >= 0 && !this.isFinished) {
			this.isPaused = false;
			this.isFinished = true;
			if (this.timer != null) clearTimeout(this.timer);
			this.slideIndex = this._reset();
		}
	}, 

	getDuration: function() {
		return (this.options.duration / 1000);
	}, 

	setDuration: function(duration) {
		this.options.duration = duration * 1000;
	}, 

	_reset: function() {
		var idx = -1;
		for (var i = 0; i < this.photoobj.length; i++) {
			if (this.photoobj[i].id > 0) {
				idx = i;
				break;
			}
		}
		return idx;
	},


	_iterate: function() {
		if (this.photoobj[this.slideIndex].id > 0) {
			if (this.onChange) this.onChange(this.photoobj[this.slideIndex]);
		}

		var b = true;
		while (b) {
			this.slideIndex++;
			if (this.slideIndex >= this.photoobj.length) {
				b = false;
				this.isFinished = true;
			}	else {
				if (this.photoobj[this.slideIndex].id > 0) b = false;
			}
		}

		if (!this.isFinished) {
			this.timer = setTimeout(this._iterate.bind(this), this.options.duration);
		} else {
			clearTimeout(this.timer);
			this.slideIndex = this._reset();
			if (!(this.options.loop == undefined)) {
				if (this.options.loop) {
					this.isFinished = false;
					this.timer = setTimeout(this._iterate.bind(this), this.options.duration);
				}
			}	else {
				if (this.options.afterFinish) this.options.afterFinish();
			}
		}
	}

}
