/*************************************

jFade 1.0.0 - Fader Script für jQuery
Carsten Ruppert 2009-03-11

Copyright 2009 HEAD. MARKETING-PARNTER

*************************************/


function GetRandom(min, max){
	if(min > max){
		return(-1);
		}
	if(min == max){
		return(min);
		}
	var r = parseInt(Math.random() * (max+1));
	return(r + min <= max ? r + min : r);
	}


function jFade(nodeid,nodename,autorun,random){
	var jfade = this;
	
	this.nodeid = undefined;
	this.nodename = undefined;
	this.container = undefined;
	this.autorun = false;
	
	this.stack = new Array();
	this.speed = 4000;
	this.animspeed = 800;
	this.current = 0;
	this.random = random;
	
	this.run = function(){
		setTimeout(this.animate,this.speed);
		return;
		}
	this.getElements = function(){
		var child;
		for(var i = 0; i < this.container.childNodes.length; i++){
			child = this.container.childNodes[i];
			if(child.nodeName.toLowerCase() == this.nodename){
				this.stack[this.stack.length] = child;
				child.style.position = 'absolute';
				child.style.top = '0';
				child.style.left = '0';
				}
			}
		
		// Anzeige setzen
		if(this.random){
			this.current = GetRandom(0, this.stack.length -1);
			}
		for(i = 0; i < this.stack.length; i++){
			child = this.stack[i];
			if(this.random){
				if(i != this.current){
					child.style.display = 'none';
					}
				}else{
				if(i > 0){
					child.style.display = 'none';
					}
				}
			}
		return;
		}
	this.animate = function(){
		var next = jfade.current + 1 == jfade.stack.length ? 0 : jfade.current + 1;
		$(jfade.stack[jfade.current]).fadeOut(jfade.animspeed);
		$(jfade.stack[next]).fadeIn(jfade.animspeed);
		jfade.current = next;
		jfade.run();
		return;
		}

	if(nodeid){
		this.nodeid = nodeid;
		this.nodename = nodename;
		this.autorun = autorun;
		if(this.container = document.getElementById(this.nodeid)){
			//this.container.style.position = 'relative';
			this.getElements();
			if(this.stack.length > 0){
				if(this.autorun){
					this.run();
					}
				}else{
				return false;
				}
			}
		}
	}

