/*
	Slide show.
	(c) 2009 Ingo Struck.
	All rights reserved.

	Roughly inspired by Andreas Bergers slideshow (http://www.bretteleben.de)
	This implementation, however, uses a step normalized to 1, is cleaned up
	and simplified, fetches content and config from html and works on more than
	simple images.
*/
var slide = {
	cnt: 0
	,show: {}
};

slide.onReady = function () {
	var sl = $('slide');
	if (!sl) return;
	var sh = sl.getElementsByTagName('ul');
	for ( var idx = 0; idx < sh.length; ++idx )
		slide.create(idx,sh[idx]);

	for ( var idx = 0; idx < slide.cnt; ++idx ) {
		var show = slide.show[idx];
		if (show.act) {
			show.act = false;
			slide.go(idx); // start only works w/ act == false
		}
	}
};

slide.preload = function (idx,num) {
	var show = slide.show[idx];
	var lst = show.list[num];
	var stage = $('stage'+idx);
	stage.appendChild(lst.el);
	lst.img = {}
	lst.img.complete = true;
};

slide.create = function (idx,inf) {
	var lst = inf.getElementsByTagName('li');
	if (3 > lst.length) return;
	/* defaults */
	var show = {
		f_cnt: 20
		,t_stay: 3000
		,t_fade: 500
		,act: 1
		,shuffle: 1
	};
	/* cfg */
	var cfg = lst[0].innerHTML.split(';');
	for ( var idx = 0; idx < cfg.length; ++idx) {
		var ent = cfg[idx].split(':');
		if (2 == ent.length && show[ent[0]]) show[ent[0]] = parseInt(ent[1].trim());
	}
	/* sanity */
	if ( 1 > show.f_cnt) show.f_cnt = 1;
	/* adjust */
	show.t_fade = show.t_fade / show.f_cnt;

	show.list = {};
	show.f_stp = 1 / show.f_cnt;
	show.f_cur = 1;
	show.cur = 0;
	var stage = $('stage'+slide.cnt);
	show.stage = stage;
	show.width = stage.clientWidth+'px';
	show.height = stage.clientHeight+'px';

	/* wrap-copy primary stage */
	show.list[0] = {}

	var mk_wrap_el = function () {
		var el = document.createElement('div');
		el.className = 'bg';	
		var st = el.style;
		st.visibility = 'hidden';
		st.position = 'absolute';
		st.left = '0';
		st.top = '0';
		st.width = stage.clientWidth+'px';
		st.height = stage.clientHeight+'px';
		return el;
	};

	var el = mk_wrap_el();
	el.innerHTML = stage.innerHTML;
	stage.innerHTML = '';
	stage.appendChild(el);

	show.list[0].el = el;
	/* fake pre-loaded image */
	show.list[0].img = {};
	show.list[0].img.complete = true;
	var cnt = 1;
	var st_ratio = stage.clientWidth / stage.clientHeight;
	for ( var idx = 1; idx < lst.length; ++idx ) {
		var conf = lst[idx].innerHTML.split('|');
		if (8 != conf.length) continue;

		var img = document.createElement('img');
		img.src = conf[1];
		img.alt = conf[2];
		var width = conf[3], height = conf[4];
		var ratio = width / height;
		st = img.style;
		if ('0' != conf[5] && '' != conf[5]) st.left = '-' + conf[5] + 'px';
		if ('0' != conf[6] && '' != conf[6]) st.top = '-' + conf[6] + 'px';
		if (ratio > st_ratio) {
			st.height = '100%';
		} else {
			st.width = '100%';
		}

		var a = document.createElement('a');
		a.className = 'bg';
		a.href = conf[0];
		a.title = conf[2];
		a.appendChild(img);

		var el = mk_wrap_el();
		el.innerHTML = conf[7];
		el.insertBefore(a,el.firstChild);

		show.list[cnt] = {}
		show.list[cnt].el = el;
		++cnt;
	}
	show.cnt = cnt;

	slide.show[slide.cnt] = show;
	++slide.cnt;
};

slide.fade = function (idx) {
	var show = slide.show[idx];
	show.f_cur = show.f_cur - show.f_stp;
	if (0 > show.f_cur) show.f_cur = 0;
	slide.setvis(show.i_cur.el,show.f_cur);

	if (0 < show.f_cur) {
		if (show.act) {
			show.timo = setTimeout(function(){ slide.fade(idx); }, show.t_fade);
		} else slide.prep(idx);
	} else {
		show.f_cur = 1;
		++show.cur;
		if (show.cur >= show.cnt) show.cur = 0;

		slide.setvis(show.i_cur.el,0,'hidden',1);
		slide.setvis(show.i_nxt.el,1,'visible',2);
		show.timo = setTimeout(function(){ slide.prep(idx); }, show.t_stay);
	}
};

slide.setvis = function (elem,opa,v,z) {
	var sty = elem.style;
	sty.opacity = opa;
	sty.MozOpacity = opa;
	sty.filter='Alpha(Opacity='+(100*opa)+')';
	if (v) sty.visibility = v;
	if (z) sty.zIndex = z;
};

slide.prep = function (idx) {
	var show = slide.show[idx];
	clearTimeout(show.timo);
	var cur = show.cur;
	show.f_act = 0;
	show.i_cur = show.list[cur];
	if (cur >= show.cnt - 1) show.i_nxt = show.list[0]; /* must have had that before */
	else {
		show.i_nxt = show.list[cur + 1];
		/* wait unitl i_nxt is defined */
		if (!show.i_nxt.img) {
			slide.preload(idx,cur + 1);
		} 
		
		if (!(show.i_nxt.img && show.i_nxt.img.complete)) {
			show.timo = setTimeout(function(){ slide.prep(idx); }, 200);
			return;
		}
	}

	slide.setvis(show.i_cur.el,1,'visible',2);
	slide.setvis(show.i_nxt.el,1,'visible',1);
	
	if (show.act) show.timo = setTimeout(function(){ slide.fade(idx); }, show.t_stay);
};

slide.go = function (idx) {
	var show = slide.show[idx];
	if (show.act) return;

	show.act = true;
	slide.prep(idx);
};

slide.stop = function (idx) {
	var show = slide.show[idx];
	show.act = false;
	clearTimeout(show.timo);
};

slide.next = function (idx) {
	var show = slide.show[idx];
	show.act = false;
	clearTimeout(show.timo);
};

slide.prev = function (idx) {
	var show = slide.show[idx];
	show.act = false;
	clearTimeout(show.timo);
};
