/**
 * @author jus
 */

 /*
function dbg(msg) {
	document.body.innerHTML = '<div>'+ msg +'</div>' + document.body.innerHTML;
}
*/

var GalleryQueue = Class.create();
GalleryQueue.prototype = {
	initialize: function(element) {
		this.element = element;
		this.options = Object.extend({
			width:	70,
			count:  2,
			container_width: 140,
			left: 0,
			mode: '',
			prefix: '',
			request_url: 'http://readme.local/scripts/imagegetter.php',
			mode: '',
			pid: 0,
			getImage: function(image){return '<div class="image" id="img1">' + image + '</div>';},
			onQueueEnd: function(){alert('End of queue')},
			onQueueStart: function(){alert('Start of queue')}
		}, arguments[1] || {});
		this.nextObj = null;
		this.prevObj = null;
		this.request = null;

		this.options.objectsPerPage = Math.round(this.options.container_width / this.options.width);
		this.options.curObjectIndex = 0;
		this.options.firstLoadedIndex = 0;
		this.options.lastLoadedIndex = this.options.objectsPerPage - 1;

		// convert object to array;
		var i = 0;
	
/*		var ids = [];
		while (this.options.ids[i])
		{
			ids[ids.length] = this.options.ids[i];
			i++;
		}
		this.options.ids = ids;
*/
	},

	prev: function() {
		if (! this.mayMove())
			return;

		var nextIndex = this.options.curObjectIndex - 1;

		if (nextIndex < 0)
			return;
	/*	if (! this.isObjectLoadedAt(nextIndex))
		{
			this.loadObjectAt(nextIndex, 'onObjectLoaded', 'left');
			return;
		} */
		this.options.curObjectIndex--;

		var _this = this;
		this.moveEffect = new Effect.Move(this.element, {
			x: this.options.width,
			y: 0,
			duration: 0.50,
			afterFinish: function(effect) {
				_this.moveEffect = null;
			}
		});
	},

	next: function() {
		if (! this.mayMove())
			return;

		var nextIndex = this.options.curObjectIndex + this.options.objectsPerPage;

		if (nextIndex >= this.options.count)
			return;
	/*	if (! this.isObjectLoadedAt(nextIndex))
		{
			this.loadObjectAt(nextIndex, 'onObjectLoaded', 'right');
			return;
		}	*/
		this.options.curObjectIndex++;

		var _this = this;
		this.moveEffect = new Effect.Move(this.element, {
			x: -this.options.width,
			y: 0,
			duration: 0.50,
			afterFinish: function(effect) {
				_this.moveEffect = null;
			}
		});
	},

	mayMove: function() {
		return ! (this.moveEffect || this.request);
	},

	isObjectLoadedAt : function(pos)
	{
		return (pos >= this.options.firstLoadedIndex && pos <= this.options.lastLoadedIndex);
	},

	loadObjectAt : function(pos, callbackMethod, callbackParam)
	{
		var objectId = this.options.ids[pos];
		var _this = this;

		var url =
			this.options.request_url +
			// 'http://localhost/readme.cc/index.php' +
			'?id=' + this.options.pid +
			'&L=' + this.options.L +
			'&' + this.options.prefix + '[mode]=' + this.options.mode + /* this.options.mode + */
			'&' + this.options.prefix + '[uid]=' + objectId +
			'&salt='+ Math.random(); // in order for request never be cached
									// due to MSIE instant return from ajax request
		
		this.request = new Ajax.Request(url,
		{
			method:'get',
			onSuccess: function(transport, json){
				_this.request = null; // in order for mayMove() to work
				try {
					json = eval(transport.responseText);
				} catch (e){}
				if (json == null) {
					//alert(transport.responseText);
				}
				else {
					if (json == false) return _this.options.onQueueStart();
					_this[callbackMethod](json, callbackParam);
				}
			},
			onComplete: function(transport){
				_this.request = null;
			}
		});
	},

	onObjectLoaded : function(loadedObject, position)
	{
		this.options.count++;
		$(this.element).setStyle({width: (this.options.width * this.options.count) + 'px'});
		var objHTML = this.options.getImage(loadedObject);

		if (position == 'right') {
			this.options.lastLoadedIndex++;
			$(this.element).innerHTML += objHTML;
			this.next();
		} else {
			this.options.firstLoadedIndex--;
			$(this.element).innerHTML = objHTML + $(this.element).innerHTML;
			this.prev();
		}
	}
}
