 /**********************************
Initialisierung und Start
**********************************/
Array.prototype.in_array = function (what)
{
	for (var a = 0; a < this.length; a ++)
	{
		if (this [a] == what)
		{
			return true;
		} 
		else if (this [a] instanceof Array)
		{
			return this [a].in_array (what);
		}
	}
	return false;
}
function addEvent (elm, evType, fn, useCapture)
{
	if (elm.addEventListener)
	{
		elm.addEventListener (evType, fn, useCapture);
		return true;
	} 
	else if (elm.attachEvent)
	{
		var r = elm.attachEvent ("on" + evType, fn);
		return r;
	}
}

if (use_columns == 2)
{
	addEvent (window, 'load', winOnLoad)
}
else
{
	addEvent (window, 'load', winOnLoad2)
}

function winOnLoad ()
{
	log ('', true);
	imgWidth = 113;
	speed = 40;
	angle = 0;
	imgArraySmall = new Array ();
	imgArraySmall = newImgSmall (imagesSmall);
	imgMatrix = new Array ();
	currentImages = new Array ();
	currentX = 0;
	currentY = 0;
	rows = 4;
	columns = 2;
	steps = 5;
	xImgAsyncWait (imgLoadStatus, appStart, appImgError, '', '', imgArraySmall);
}
function winOnLoad2 ()
{
	log ('', true);
	imgWidth = 113;
	speed = 40;
	angle = 0;
	imgArraySmall = new Array ();
	imgArraySmall = newImgSmall (imagesSmall);
	imgMatrix = new Array ();
	currentImages = new Array ();
	currentX = 0;
	currentY = 0;
	rows = 4;
	columns = 4;
	steps = 5;
	xImgAsyncWait (imgLoadStatus, appStart, appImgError, '', '', imgArraySmall);
}
function appImgError (n, c, e, a)
{
	var s = '\nFinal Image Status:' +
	'\n total=' + n +
	'\n loaded=' + c +
	'\n errors=' + e +
	'\n aborts=' + a;
}
function imgLoadStatus (n, c, e, a)
{
	var s = '\nImage Status:' +
	'\n total=' + n +
	'\n loaded=' + c +
	'\n errors=' + e +
	'\n aborts=' + a;
}
function log (s, bOverwrite)
{
	var log = document.getElementById ('debugLog');
	if (log)
	{
		if (bOverwrite) log.value = s;
		else log.value += s;
	}
}
function newImgSmall (imagesSmall)
{
	for (i = 0; i < imagesSmall.length; i ++)
	{
		imgArraySmall [i] = new Image ();
		imgArraySmall [i].src = imagesSmall [i];
	}
	return imgArraySmall;
}
function imageInit ()
{
	//Matrix mit Bildern erstellen
	var counter = 0;
	var imageMatrix = "";
	for	(y = 0; y < rows; y ++)
	{
		imgMatrix [y] = new Array (columns);
		imageMatrix += '<div class="row">';
		for (x = 0; x < columns; x ++)
		{
			imgMatrix [y][x] = imagesSmall [counter];
			imageMatrix += '<div class="column"><img id="img' + y + x + '" class="image" src="' + imgMatrix [y][x] + '" alt="" /></div>';
			counter ++;
		}
		imageMatrix += '</div>';
	}
	//log(imageMatrix, false);
	xGetElementById ("img_box").innerHTML = imageMatrix;
}
function appStart ()
{
	imageInit ();
	window.setTimeout("imageReplace ()", 3000);
}
function imageReplace ()
{
	//neue Position ermitteln
	newX = Math.round (Math.random () * (columns-1));
	newY = Math.round (Math.random () * 3);
	while ( ! checkPosition())
	{
		newX = Math.round (Math.random () * (columns-1));
		newY = Math.round (Math.random () * 3);
	}
	//neues Bild ermitteln
	newImage = Math.round (Math.random () * (imgArraySmall.length - 1));
	while ( ! checkImage())
	{
		newImage = Math.round (Math.random () * (imgArraySmall.length - 1));
	}
	//Matrix aktualisieren
	imgMatrix[newY][newX] = imagesSmall [newImage];
	currentX = newX;
	currentY = newY;
	currentImage = newImage;
	currentLeftPos = xPageX("img"+currentY+currentX);
	flipImage();
}
/**********************************
Funktionen fr Bildwechsel
**********************************/
function checkPosition ()
{
	if (currentX == newX || currentY == newY)
	{
		return false;
	} 
	else
	{
		return true;
	}
}
function checkImage ()
{
	// mache mehrdimensionales array zu eindimensionalem array > fr in_array
	// mehrdimensionale packts nicht
	tmp = imgMatrix.join(",");
	tmpArray = tmp.split(",");
	if (tmpArray.in_array(imagesSmall [newImage]))
	{
		return false;
	} 
	else
	{
		return true;
	}
}
/**********************************
Bilder drehen
**********************************/
function flipImage() {
	imgNewWidth = Math.abs (Math.round (Math.cos (angle) * imgWidth));
	imgOffset = (imgWidth - imgNewWidth)/2;
	xLeft("img"+currentY+currentX, imgOffset+currentLeftPos);
	xResizeTo("img"+currentY+currentX, imgNewWidth, 110);
	angle += speed / 720 * Math.PI;
	if (imgNewWidth == 0 || imgNewWidth != imgWidth) {
		if (imgNewWidth > 0) {
			setTimeout ("flipImage()", 30)
		}
		else {
			xGetElementById ("img" + newY + newX).src = imgArraySmall[newImage].src;
			xResizeTo("img"+currentY+currentX, 0, 110);
			setTimeout ("flipImage()", 30)
		}
	}
	else {
		setTimeout ("imageReplace()", 3000)
	}
}
