function debug(txt) {
	$("debug").insert({
		top: new Element("p").update(txt)
	});
}
if (!Avalon) {
	var Avalon = {};
}
Avalon.slideshow = function(galeria, opciones) {
	this.id = galeria.id;
	this.elementos = galeria.elementos;
	
	this.infoSpeed = this.imgSpeed = this.speed = 10;
	this.thumbOpacity = this.navHover = 70;
	this.navOpacity = 25;
	this.scrollSpeed = 5;
	this.letterbox = '#000';
	this.n = "slideshow_" + galeria.id;
	this.c = 0;
	this.a = [];
	
	this.auto = true;
	this.speed = 10;
	this.link = "linkhover";
	this.info = "information_" + this.id;
	this.infoSpeed = 10;
	this.thumbs = "slider_" + this.id;
	this.thumbOpacity = 100;
	this.left = "slideleft_" + this.id;
	this.right = "slideright_" + this.id;
	this.scrollSpeed = 8;
	this.spacing = 5;
	this.active = "#fff";
	this.speed = 8;
	this.ventanaNueva = false;
	
	Object.extend(this, opciones);
	this.init("slideshow_" + this.id, "image_" + this.id, "imgprev_" + this.id, "imgnext_" + this.id, "imglink_" + this.id);
};

Avalon.slideshow.prototype = {
	init: function(ul, imagen, anterior, siguiente, enlace) {
		var self = this;
		var i = 0, w = 0;
		this.l = this.elementos.length;
		this.q = $(enlace);
		this.f = $(imagen);
		this.r = $("information_" + this.id);
		this.o = parseInt(Avalon.style.val(imagen, "width"), 10);
		var u = $(this.left), r = $(this.right);
		u.onmouseover = function() {
			Avalon.scroll.init(self.thumbs, -1, self.scrollSpeed);
		};
		u.onmouseout = r.onmouseout = function() {
			Avalon.scroll.cl(self.thumbs);
		};
		r.onmouseover = function() {
			Avalon.scroll.init(self.thumbs, 1, self.scrollSpeed);
		};
		
		this.p = $(this.thumbs);
		for (var i = 0; i < this.l; i++) {
			var g = new Element("img", {
				src: this.elementos[i].miniatura
			});
			$(this.p).insert(g);
			if (g.complete) {
				w += parseInt(g.offsetWidth);
			} else {
				g.onload = function() {
					w += parseInt(g.offsetWidth);
					self.p.style.width = w + 'px';
				};
			} 
			
			// w += parseInt(g.offsetWidth);
			if (i != this.l - 1) {
				g.style.marginRight = this.spacing + 'px';
				w += this.spacing
			}
			this.p.style.width = w + 'px';
			g.style.opacity = this.thumbOpacity / 100;
			g.style.filter = 'alpha(opacity=' + this.thumbOpacity + ')';
			g.onmouseover = function() {
				Avalon.alpha.set(this, 100, 5);
			};
			g.onmouseout = function() {
				Avalon.alpha.set(this, self.thumbOpacity, 5);
			};
			(function() {
				var p = i;
				g.onclick = function() {
					self.pr(p, 1);
				};
			})();
		}
		if (anterior && siguiente) {
			b = $(anterior);
			f = $(siguiente);
			b.style.opacity = f.style.opacity = this.navOpacity / 100;
			b.style.filter = f.style.filter = 'alpha(opacity=' + this.navOpacity + ')';
			b.onmouseover = f.onmouseover = function() {
				Avalon.alpha.set(this, self.navHover, 5);
			};
			b.onmouseout = f.onmouseout = function() {
				Avalon.alpha.set(this, self.navOpacity, 5);
			};
			b.onclick = function() {
				self.mv(-1, 1);
			};
			f.onclick = function() {
				self.mv(1, 1);
			}
		}
		this.auto ? this.is(0, 0) : this.is(0, 1);
	},
	
	mv: function(d, c) {
		var t = this.c + d;
		this.c = t = t < 0 ? this.l - 1 : t > this.l - 1 ? 0 : t;
		this.pr(t, c)
	},
	
	pr: function(t, c) {
		clearTimeout(this.lt);
		if (c) {
			clearTimeout(this.at)
		}
		this.c = t;
		this.is(t, c)
	},
	
	is: function(s, c) {
		var self = this;
		if (this.info) {
			Avalon.height.set(this.r, 1, this.infoSpeed / 2, -1)
		}
		var i = new Image();
		i.style.opacity = 0;
		i.style.filter = 'alpha(opacity=0)';
		this.i = i;
		i.onload = function() {
			self.le(s, c);
		};
		i.src = this.elementos[s].imagen;
		var a = this.p.select('img'), l = a.length, x = 0;
		for (x; x < l; x++) {
			a[x].style.borderColor = x != s ? '' : this.active
		}
	},
	
	le: function(s, c) {
		var self = this;
		$(this.f).insert(this.i);
		this.o = $("image_" + this.id).getWidth();
		var w = this.o - parseInt(this.i.offsetWidth);
		if (w > 0) {
			var l = Math.floor(w / 2);
			this.i.style.borderLeft = l + 'px solid ' + this.letterbox;
			this.i.style.borderRight = (w - l) + 'px solid ' + this.letterbox;
		}
		Avalon.alpha.set(this.i, 100, this.imgSpeed);
		
		this.lt = setTimeout(function() {
			self.nf(s);
		}, this.imgSpeed * 100);
		if (!c) {
			this.at = setTimeout(function() {
				self.mv(1, 0);
			}, this.speed * 1000);
		}
		if (this.elementos[s].enlace != '') {
			this.q.onclick = function() {
				if (self.ventanaNueva) {
					window.open(self.elementos[s].enlace);
				} else {
					location.href = self.elementos[s].enlace;
				}
			};
			this.q.onmouseover = function() {
				this.className = "slideshow_imglink slideshow_imglinkhover";
			};
			this.q.onmouseout = function() {
				this.className = "slideshow_imglink ";
			};
			this.q.style.cursor = 'pointer';
		} else {
			this.q.onclick = this.q.onmouseover = null;
			this.q.style.cursor = 'default';
		}
		
		var m = this.f.select('img');
		if (m.length > 2) {
			this.f.removeChild(m[0]);
		}
	},
	
	nf: function(s) {
		if (this.info) {
			s = this.elementos[s];
			this.r.firstChild.innerHTML = s.texto + '<div class="pie_bloq"><!-- no borrar --></div>';
			this.r.style.height = 'auto';
			var h = parseInt(this.r.offsetHeight, 10);
			this.r.style.height = 0;
			Avalon.height.set(this.r, h, this.infoSpeed, 0);
		}
	}
};

Avalon.scroll = function() {
	return {
		init: function(e, d, s) {
			e = typeof e == 'object' ? e : $(e);
			var p = e.style.left || Avalon.style.val(e, 'left');
			e.style.left = p;
			var l = d == 1 ? parseInt(e.offsetWidth) - parseInt(e.parentNode.offsetWidth) : 0;
			e.si = setInterval(
				function() {
					var l = 0;
					if (d == 1) {
						if (e.offsetWidth < e.parentNode.offsetWidth) {
							l = e.offsetWidth;
							l = 0;
						} else {
							l = parseInt(e.offsetWidth) - parseInt(e.parentNode.offsetWidth);
						}
					}  else {
						l = 0;
					};			
					Avalon.scroll.mv(e, l, d, s);
				},
				20
			);
		},
		mv: function(e, l, d, s) {
			var c = parseInt(e.style.left);
			if (c == l) {
				Avalon.scroll.cl(e);
			} else {
				var i = Math.abs(l + c);
				i = i < s ? i : s;
				var n = c - i * d;
				e.style.left = n + 'px';
			}
		},
		cl: function(e) {
			e = typeof e == 'object' ? e : $(e);
			clearInterval(e.si);
		}
	}
}();
Avalon.height = function() {
	return {
		set: function(e, h, s, d) {
			e = typeof e == 'object' ? e : $(e);
			var oh = e.offsetHeight, ho = e.style.height || Avalon.style.val(e, 'height');
			ho = oh - parseInt(ho);
			var hd = oh - ho > h ? -1 : 1;
			clearInterval(e.si);
			e.si = setInterval(
				function() {
					Avalon.height.tw(e, h, ho, hd, s)
				}, 
				20
			);
		},
		tw: function(e, h, ho, hd, s) {
			var oh = e.offsetHeight - ho;
			if (oh == h) {
				clearInterval(e.si);
			} else {
				if (oh != h) {
					e.style.height = oh + (Math.ceil(Math.abs(h - oh) / s) * hd) + 'px';
				}
			}
		}
	}
}();
Avalon.alpha = function() {
	return {
		set: function(e, a, s) {
			e = typeof e == 'object' ? e : $(e);
			var o = e.style.opacity || Avalon.style.val(e, 'opacity'), d = a > o * 100 ? 1 : -1;
			e.style.opacity = o;
			clearInterval(e.ai);
			e.ai = setInterval(
				function() {
					Avalon.alpha.tw(e, a, d, s)
				}, 
				20
			);
		},
		tw: function(e, a, d, s) {
			var o = Math.round(e.style.opacity * 100);
			if (o == a) {
				clearInterval(e.ai);
			} else {
				var n = o + Math.ceil(Math.abs(a - o) / s) * d;
				e.style.opacity = n / 100;
				e.style.filter = 'alpha(opacity=' + n + ')';
			}
		}
	}
}();
Avalon.style = function() {
	return {
		val: function(e, p) {
			e = typeof e == 'object' ? e : $(e);
			return e.currentStyle ? e.currentStyle[p] : document.defaultView.getComputedStyle(e, null).getPropertyValue(p);
		}
	}
}();





Avalon.miniSlideshow = function(id) {
	this.id = id;
	this.scrollSpeed = 8;
	this.right = "miniSlideShow_slideright_" + this.id;
	this.left = "miniSlideShow_slideleft_" + this.id;
	this.thumbs = "miniSlideShow_slider_" + this.id;
	
	this.init();
};

Avalon.miniSlideshow.prototype = {
	init: function() {
		var self = this;
		var elementos = $(this.thumbs).select(".miniSlideshow_link");
		if (elementos.size()) {
			$(this.thumbs).style.width = (elementos.size() * (elementos[0].getWidth() + 5)) + "px";
		}
		
		var u = $(this.left), r = $(this.right);
		
		u.onmouseover = function() {
			Avalon.scroll.init(self.thumbs, -1, self.scrollSpeed);
		};
		u.onmouseout = r.onmouseout = function() {
			Avalon.scroll.cl(self.thumbs);
		};
		r.onmouseover = function() {
			Avalon.scroll.init(self.thumbs, 1, self.scrollSpeed);
		};
	}
};
