
/***************************************************************************
 *
 *   pretty stupid frontpage script, search the link(s)
 *   Copyright (C) 2006 Tim Schnurpfeil, schnubbel@gmail.com
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *
 ***************************************************************************/

//----- CONFIG
var config = new Array("forum.gif", "http://www.palm-bietsch.de/board");
//ALWAYS provide name + link in this array!

var anzZeilen = 8; //how many rows?
var anzReihen = 9; //how many columns?

var debugMode = false; //debug Mode? shows where the links are hidden, useul for config-test and modifications

//----- CONFIG END




var itemCount = (config.length) / 2;

//Error check 1
//did you fill "config" in pairs?
//ok, it only works it you didnt fail twice or so ... ;)
if(Math.ceil(itemCount) != Math.floor(itemCount)){
	
	if(debugMode){
		alert('Odd item count in the config array - this should not be possbile if youve always created pairs, hm?');
	}
	
	stop();
}

//Error Check 2

function Bild (file,link){
	this.file = file;
	this.link = link;
	this.x = null;
	this.y = null;
}

//prepare the images
var img = new Array();

for(var i = 0; i<itemCount*2; i+=2){
	var tmpImg = new Image;
	tmpImg.src = config[i];
	
	var tmpBild = new Bild(tmpImg, config[i+1]);
	
	img.push(tmpBild); 
}

//static stuff
empty = new Image();
empty.src = "empty.gif";
nothing = new Image();
nothing.src = "nothing.gif";


//randomize item positions
for (var i = 0; i<img.length; i++){
	var imgZeile = Math.floor(Math.random()*anzZeilen); //random values
	var imgReihe = Math.floor(Math.random()*anzReihen);
	img[i].x = imgZeile;
	img[i].y = imgReihe;
	
	//check if there is an image at these coords
	var collision;
	for(var c = 0; c<img.length; c++){
		var a = img[c];
		if ((imgZeile == a.x) && (imgReihe == a.y) && (a.file != img[i].file)){
			collision = true;
		}
	}
	if((imgZeile == 0) && (imgReihe == 0)){ //too poor
			collision = true;
	}
		
	if(debugMode){
		document.write("<span style='color:#FF0000; font-family:Arial; font-size:11pt;'>"+ img[i].link + ": " + (imgZeile+1)+" - " + (imgReihe+1)+"</span><br />");
	}
	
	if (collision){
		i--; //do it again
		collision = false;
		if(debugMode){
			document.write("<span style='color:#FF0000; font-family:Arial; font-size:11pt;'>Collision happened - doing last item again</span><br />");
		}
	}
}

for(var zeile = 0; zeile < anzZeilen; zeile++){
	for(var reihe = 0; reihe < anzReihen; reihe++){
		var matchFound;
		
		for(var c = 0; (c<img.length) && !matchFound; c++){
			var a = img[c];
			var coord = zeile + "" + reihe;
			if((a.x == zeile) && (a.y == reihe)){
				
				document.write("<a href=" + a.link + "><img src=empty.gif name=h" + coord + " border=0 onmouseover='document.h" + coord +
				".src=img[" + c + "].file.src' onmouseout='document.h" + coord + ".src=empty.src'/></a>&nbsp;");
				matchFound = true;
			}
		}
		if(!matchFound){
			document.write("<img src='empty.gif' name='a"+zeile+reihe+"' border='0' alt='Nothing' onmouseover='document.a"+zeile+reihe+".src=nothing.src' onmouseout='document.a"+zeile+reihe+".src=empty.src' />&nbsp;");
		}
		matchFound = false;
	}
	document.write("<br />");
}
