var TINY = {};

function $(i) {
	return document.getElementById(i);
}

function $$(e, p) {
	p = p || document;
	return p.getElementsByTagName(e);
}

TINY.slideshow = function(n) {
	this.imgSpeed = this.speed = 10;
	this.thumbOpacity = this.navHover = 70;
	this.navOpacity = 25;
	this.letterbox = '#000';
	this.n = n;
	this.c = 0;
	this.a = []
};

TINY.slideshow.prototype = {
	init : function(s, z, q) {
		s = $(s);
		var m = $$('li', s), i = 0, w = 0;
		this.l = m.length;
		this.q = $(q);
		this.f = $(z);
		this.o = parseInt(TINY.style.val(z, 'width'));
		for (i; i < this.l; i++) {
			this.a[i] = {};
			var h = m[i], a = this.a[i];
			a.l = $$('a', h)[0] ? $$('a', h)[0].href : '';
			a.p = $$('span', h)[0].innerHTML;
		}
		var imgNr=Math.floor(Math.random()*this.a.length);		
		this.is(imgNr, 0);
	},
	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 i = new Image();
		i.style.opacity = 0;
		i.style.filter = 'alpha(opacity=0)';
		this.i = i;
		i.onload = new Function(this.n + '.le(' + s + ',' + c + ')');
		i.src = this.a[s].p;
	},
	le : function(s, c) {
		this.f.appendChild(this.i);
		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;
		}
		TINY.alpha.set(this.i, 100, this.imgSpeed);
		if (!c) {
			this.at = setTimeout(new Function(this.n + '.mv(1,0)'),
					this.speed * 1000);
		}
		if (this.a[s].l != '') {
			this.q.onclick = new Function(
					'window.location="' + this.a[s].l + '"');
			// this.q.onmouseover=new
			// Function('this.className="'+this.link+'"');
			// this.q.onmouseout=new Function('this.className=""');
			this.q.style.cursor = 'pointer';
		} else {
			this.q.onclick = this.q.onmouseover = null;
			this.q.style.cursor = 'default';
		}
		var m = $$('img', this.f);
		if (m.length > 2) {
			this.f.removeChild(m[0]);
		}
	}
};

TINY.alpha = function() {
	return {
		set : function(e, a, s) {
			e = typeof e == 'object' ? e : $(e);
			var o = e.style.opacity || TINY.style.val(e, 'opacity'), d = a > o * 100 ? 1
					: -1;
			e.style.opacity = o;
			clearInterval(e.ai);
			e.ai = setInterval( function() {
				TINY.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 + ')';
			}
		}
	}
}();

TINY.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);
		}
	}
}();

var slideshow = new TINY.slideshow("slideshow");
window.onload = function() {
	$('slideshow').style.display = 'none';
	$('wrapper').style.display = 'block';	
	
	slideshow.speed = 8;
	slideshow.imgSpeed = 100;
	slideshow.init("slideshow", "image", "imglink");
}