var IdNo = 0;

// create custom object 
function BitstyleRotator (arg) { 
	//Save a reference to myself
	this.id = "myObject" + IdNo++;

	this.bilder = boxBilder;
	this.texte = boxTexte;
	this.boolStop = false;

	window[this.id] = this;

	this.countDown = countDown; 
	this.setTimeOut = mySetTimeout;
	this.giveContent = BitstyleRotatorGiveContent;

	this.stop = BitstyleRotatorStop;
	this.start = BitstyleRotatorStart;

	// meinz
	if (arg == "home") this.fields = boxesHArray;
	else this.fields = boxesArray;

	this.boxes = new Array();

	for (var i in this.fields) this.boxes[this.boxes.length] = new DaField(document.getElementById("box" + this.fields[i]));
}

function BitstyleRotatorGiveContent() {
	var retval;
	if (Math.random() > 0.49)
	{
		strImg = arrGiveRandom(this.bilder).join(";");
		retval = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="150" height="150" id="fader_img" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="wmode" value="transparent" /><param name="movie" value="/swf/fader_img.swf?input=' + strImg + '" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="/swf/fader_img.swf?input=' + strImg + '" quality="high" bgcolor="#ffffff" width="150" height="150" name="fader_img" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" /></object>';
	}
	else
	{
		strText = arrGiveRandom(this.texte).join(";");
		retval = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="150" height="150" id="fader_text" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="/swf/fader_text.swf?input=' + strText + '" /><param name="menu" value="false" /><param name="quality" value="high" /><param name="scale" value="noscale" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" /><embed src="/swf/fader_text.swf?input=' + strText + '" menu="false" quality="high" scale="noscale" wmode="transparent" bgcolor="#ffffff" width="150" height="150" name="fader_text" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	}
	return retval;
}


function BitstyleRotatorStop() {
	this.boolStop = true;
	for (var i in this.fields)
	{
		this.boxes[i].root.innerHTML = '';
	}
}

function BitstyleRotatorStart() {
	this.boolStop = false;
	this.countDown();
}

function DaField(rootObj) {
	this.root = rootObj;
	this.active = (this.root.innerHTML) ? true : false;
}

function countDown () { 

	if (this.boolStop)
	{
		return false;
	}

	//trace(this.bilder);
	//trace(this.texte);

	var boxA = new Array();
	var boxI = new Array();

	//var index = Math.round(Math.random() * (this.boxes.length-1));
	//this.boxes[index].root.innerHTML = "OHO" + index;

	// checken welcher frei .. den mit text oder bild füllen
	for (var i in this.boxes)
	{
		if (this.boxes[i].active) boxA[boxA.length] = this.boxes[i];
		else boxI[boxI.length] = this.boxes[i];
	}

	//trace(boxA);
	//trace(boxI);

	// eine der leeren vollen
	tmpIndex = Math.round(Math.random() * (boxI.length-1));
	boxI[tmpIndex].active = true;
	boxI[tmpIndex].root.innerHTML = this.giveContent();

	// eine der vollen leeren
	tmpIndex = Math.round(Math.random() * (boxA.length-1));
	if (boxA[tmpIndex])
	{
		boxA[tmpIndex].active = false;
		boxA[tmpIndex].root.innerHTML = '';
	}


	// und nochmal
	this.setTimeOut("countDown()", Math.round(Math.random() * 8000 + 2000)); 
}

function mySetTimeout (f,t)
{
	setTimeout("window."+this.id+"."+f, t);
}

function arrGiveRandom(arr) {
	arrRet = new Array();
	arrDbl = new Array();

	// kopieren, das mistding, sonst macht er blödsinn
	for (var i in arr) arrDbl[arrDbl.length] = arr[i];

	var intAnzahl = Math.round(Math.random() * (arrDbl.length-1)) + 2;
	if (intAnzahl > arrDbl.length) intAnzahl = arrDbl.length;

	while (intAnzahl)
	{
		rndIndex = Math.round(Math.random() * (arrDbl.length-1));

		arrRet[arrRet.length] = arrDbl[rndIndex];
		arrDbl.splice(rndIndex, 1);

		intAnzahl--;
	}

	return arrRet;
}