/* ############################################################################################ */
/* Variablen Initialisieren */
/* ############################################################################################ */

var bildname = new Array();
var bildprojekt = new Array();
var currentbild = 0;

/* ############################################################################################ */
/* Master Objekt */
/* ############################################################################################ */

var slide = {
	cnt: 0
	,show: {}
};

/* ############################################################################################ */
/* onReady */
/* ############################################################################################ */

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);
		}
	}
};

/* ############################################################################################ */
/* Preloading */
/* ############################################################################################ */

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;
};

/* ############################################################################################ */
/* Initialisierung */
/* ############################################################################################ */

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;
	for ( var idx = 1; idx < lst.length; ++idx ) {
		var conf = lst[idx].innerHTML.split('|'); /* Daten bei '|' trennen */
		if (6 != conf.length) continue; /* 6 Variablen pro Bild */

		var img = document.createElement('img'); /* img-Element erstellen */
		img.src = conf[1]; /* Var_1 == src */
		img.alt = conf[2]; /* Var_2 == alt-Text */
		st = img.style;
		if ('0' != conf[3] && '' != conf[3]) st.left = '-' + conf[3] + 'px'; /* Position links */
		if ('0' != conf[4] && '' != conf[4]) st.top = '-' + conf[4] + 'px'; /* Position oben */

		var a = document.createElement('a'); /* a-Element erstellen */
		a.className = 'bg';
		a.href = conf[0]; /* Var_0 == link */
		a.href = a.href.replace(/&amp;/g, '&'); /* bei links &amp; in & umwandeln */
		a.title = conf[2]; /* Var_2 == Titel */
		a.appendChild(img);

		var el = mk_wrap_el();
		el.innerHTML = conf[5]; /* Var_5 == bei hover anzeigen der Beschreibung */
		el.insertBefore(a,el.firstChild);

		show.list[cnt] = {}
		show.list[cnt].el = el;
		++cnt;
		
		/* Cookie-Scroll ---- Bild_Uprojekt */
		bildname[cnt-2] = conf[1]; /* src in Array bildname[] */
		var temp = bildname[cnt-2].split('/'); /* bei '/' trennen */
		bildprojekt[cnt-2] = temp[5]; /* http:/ / url / Sprache / Projekt / Uprojekt / big / Bild.jpg */
		bildname[cnt-2] = temp[7];    /* 0	/	1	/	2	/	3	/	4	/	5	/	6	/	7	*/
	}
	show.cnt = cnt;
	
	slide.show[slide.cnt] = show;
	++slide.cnt;
};

/* ############################################################################################ */
/* Fading */
/* ############################################################################################ */

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);

		currentbild = show.cur-1; /* aktuelles Bild (1 bis x) */
		
		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];
	else {
		show.i_nxt = show.list[cur + 1];
		/* warten bis i_nxt definiert ist */
		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);
};

/* ############################################################################################ */
/* Steuerung */
/* ############################################################################################ */

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

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

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

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

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

/* ############################################################################################ */
/* Util */
/* ############################################################################################ */

util.add_event(window, 'load', slide.onReady);

/* ############################################################################################ */
/* Get / Set Funktionen */
/* ############################################################################################ */

slide.getBildname = function (i) {
	return 	bildname[i];
}

slide.getBildprojekt = function (i) {
	return 	bildprojekt[i];
}

slide.getCurrentbild = function () {
	return 	currentbild;
}