// -*- mode: javascript; coding: utf-8 -*-

var miniPhoto = function () {};

miniPhoto.prototype = {
	DND_NONE: 0,
	DND_START: 1,

	IN_OUT_THRESHOLD: 20, // ms
	CLICK_THRESHOLD: 40, // ms
	INERTIA_INTERVAL: 50, // ms
	INERTIA_START_THRESHOLD: 40, // ms

	THUMB_WIDTH: 200,
	THUMB_HEIGHT: 100,
	
	renderThumb: function (imgData) {
		var width = (this.THUMB_WIDTH * this.THUMB_HEIGHT) / imgData.h;
		var url;
		if (imgData.url)
			url = imgData.url;
		else
			url = 'index.php?section=fotografia&gallery=' + imgData.gal + '&photo=' + imgData.id;
		var onclick = "";
		if (imgData.onclick)
			onclick = 'onclick="' + imgData.onclick + '" ';
		return '<div><a href="' + url + '" ' + onclick + 'target="_top"><img src="' + imgData.src + '" style="width: ' + width + 'px" alt="" title="" /></a></div>';
	},

	inertiaStart: function () {
		var mini = this;
		if (this.inertiaInterval)
			this.inertiaStop ();
		if (this.dnd.delta) {
			this.inertiaInterval = window.setInterval (
				function () { 
					if (!mini.imagesOffset (mini.dnd.delta))
						mini.inertiaStop ();
				}, this.INERTIA_INTERVAL);
		}
	},

	inertiaStop: function () {
		window.clearInterval (this.inertiaInterval);
		this.inertiaInterval = 0;
		this.dnd.delta = 0;
	},

	imagesOffset: function (delta) {
		this.dnd.left += delta;

		var res = true;

		if (this.dnd.left > 0) {
			this.dnd.left = 0;
			res = false;
		}

		if (this.dnd.left < this.minleft) {
			this.dnd.left = this.minleft;
			res = false;
		}

		this.thumbs.style.left = this.dnd.left + "px";

		return res;
	},

	getOffset: function () {
		return this.dnd.left;
	},

	imageClick: function (image) {
		if (this.miniPhoto) return arguments.callee.apply (this.miniPhoto, [ this ]);

		if (this.dnd.delta == 0 && !this.dnd.moving) {
			if (image.userOnclick)
				image.userOnclick ();
			return true;
		}
		this.dnd.moving = false;
		return false;
	},

	imageMouseDown: function (evt) {
		if (this.miniPhoto) return arguments.callee.apply (this.miniPhoto, arguments);
		if (!evt) evt = window.event;

		if (this.dnd.delta != 0)
			this.dnd.moving = true;
	},

	imageMouseOver: function (evt, idx) {
		if (this.miniPhoto) return arguments.callee.apply (this.miniPhoto, [evt, this.num]);
		if (!evt) evt = window.event;

		if (this.caption) {
			var str = this.imgData[idx].caption;
			if (!str) str = "";
			this.caption.innerHTML = str;
		}
	},

	thumbsClick: function (evt) {
		if (this.miniPhoto) return arguments.callee.apply (this.miniPhoto, arguments);
		if (!evt) evt = window.event;

		if (this.inertiaInterval && new Date ().getTime () - this.clickStart < this.CLICK_THRESHOLD)
			this.inertiaStop ();
	},

	thumbsMouseDown: function (evt) {
		if (this.miniPhoto) return arguments.callee.apply (this.miniPhoto, arguments);
		if (!evt) evt = window.event;
		
		this.inertiaStop ();
		this.dnd.state = this.DND_START;
		this.dnd.start = evt.clientX;
		this.clickStart = new Date ().getTime ();
		
		return false;
	},

	thumbsMouseUp: function (evt) {
		if (this.miniPhoto) return arguments.callee.apply (this.miniPhoto, arguments);
		if (!evt) evt = window.event;

		if (this.dnd.state == this.DND_START && 
		    new Date ().getTime () - this.dnd.stamp < this.INERTIA_START_THRESHOLD)
			this.inertiaStart ();
		else
			this.dnd.delta = 0;
		
		this.dnd.state = this.DND_NONE;

		return false;
	},

	thumbsMouseMove: function (evt) {
		if (this.miniPhoto) return arguments.callee.apply (this.miniPhoto, arguments);
		if (!evt) evt = window.event;
		
		if (this.dnd.state != this.DND_START)
			return;
		this.dnd.stamp = new Date ().getTime ();
		this.dnd.delta = evt.clientX - this.dnd.start;
		this.imagesOffset (this.dnd.delta);
		this.dnd.start = evt.clientX;
		this.dnd.moving = true;

		return false;
	},

	thumbsMouseOut: function (evt) {
		if (this.miniPhoto) return arguments.callee.apply (this.miniPhoto, arguments);
		if (!evt) evt = window.event;

		this.lastIn = new Date ().getTime ();

		if (!this.inertiaInterval) {
			var mini = this;
			this.outTimeout = dodo.delayRun (function () { mini.inertiaStart (); }, this.IN_OUT_THRESHOLD * 0.9);
		}

		return false;
	},

	thumbsMouseOver: function (evt) {
		if (this.miniPhoto) return arguments.callee.apply (this.miniPhoto, arguments);
		if (!evt) evt = window.event;
		
		if (new Date ().getTime () - this.lastIn > this.IN_OUT_THRESHOLD) {
			this.lastIn = 0;
			this.dnd.state = this.DND_NONE;
		} else if (this.outTimeout) {
			dodo.clearTimeout (this.outTimeout);
			this.outTimeout = 0;
		}
	},

	thumbsSetEvents: function (miniPhoto) {
		if (!this.thumbs) return arguments.callee.apply (miniPhoto);

		var thumbs = this.thumbs;
		var thumbs_w = 0;

		for (var i = 0, child; child = thumbs.childNodes[i]; i++)
			if (child.nodeName.toLowerCase () == 'div') {
				thumbs_w += child.clientWidth;
				child.miniPhoto = child.firstChild.miniPhoto = this;
				if (child.firstChild.onclick)
					child.firstChild.userOnclick = child.firstChild.onclick;
				child.firstChild.onclick = this.imageClick;
				child.onmousedown = this.imageMouseDown;
				
				if (this.imgData[i].caption) {
					child.num = i;
					child.onmouseover = this.imageMouseOver;
				}
			}

		this.minleft = - thumbs_w + thumbs.parentNode.offsetWidth;

		thumbs.onclick = this.thumbsClick;
		thumbs.onmouseup = this.thumbsMouseUp;
		thumbs.onmousedown = this.thumbsMouseDown;
		thumbs.onmousemove = this.thumbsMouseMove;
		thumbs.onmouseout = this.thumbsMouseOut;
		thumbs.onmouseover = this.thumbsMouseOver;

		this.dnd = {
			state: this.DND_NONE, 
			left: 0, 
			start: 0, 
			delta: 0,
			moving: false
		};
	},

	thumbsInit: function (offset) {
		var thumbs = this.thumbs;
		thumbs.onmouseover = function () {};
		var html = "";
		for (var i = 0, data; data = this.imgData[i]; i++)
			html += this.renderThumb (data);
		thumbs.innerHTML = html;
		var mini = this;
		dodo.delayRun (function () { 
			mini.thumbsSetEvents (mini); 
			if (offset) mini.imagesOffset (offset);
		});
	},

	thumbsMouseOverInit: function (evt) {
		if (this.miniPhoto) return arguments.callee.apply (this.miniPhoto, arguments);

		this.thumbsInit ();
	},

	init: function (div, img_data, initial_num, offset) {
		if (offset) {
			offset = parseInt (offset, 10);
			initial_num = img_data.length;
		} else
			offset = 0;

		if (!div)
			return this;

		for (var i = 0, child; child = div.childNodes[i]; i++) {
			var str = child.className;
			if (str && str.indexOf (" ") > 0)
				str = str.substr (0, str.indexOf (" "));
			switch (str) {
			case "thumbs": this.thumbs = child; break;
			case "caption": this.caption = child; break;
			}
		}

		if (!this.thumbs)
			return this;
		this.imgData = img_data;
		var html = "";
		for (var i = 0; i < initial_num; i++)
			if (img_data[i])
				html += this.renderThumb (img_data[i]);
		this.thumbs.innerHTML = html;
		this.thumbs.miniPhoto = this;

		if (offset)
			this.thumbsInit (offset);
		else
			this.thumbs.onmouseover = this.thumbsMouseOverInit;

		return this;
	}
}


