function Spiel(){
    this.farben=["red","yellow","orange","green","blue"];
    this.feld = new Array();
    this.kacheln = new Array();
    this.divAll = document.body.appendChild(document.createElement("div"));
    this.init = function(){
        for (var i=0; i<10;i++){
            var e=new Array();
            for (var j=0;j<10;j++){
                e.push (new Kachel(this.divAll));
            }
            this.kacheln.push(e);
            this.append("br");
        } // for
        this.append("p");
        var p =this.append("h1");
      //  p.style.fontSize="32pt";
        this.txtCount=p.appendChild(document.createTextNode("0"));
        this.append("hr");
	// Steuerleiste
        this.divButtons= this.append("div");
        for (var i=0; i<this.farben.length;i++){
	    var k = new Kachel (this.divButtons,i,this);
	    k.setColor (i,this.farben);
	   
        } // for
        this.btnNeu= this.append("input");
        this.btnNeu.type="button";
        this.btnNeu.value="Neues Spiel";
        this.btnNeu.o=this;
        this.btnNeu.onclick=function(){
            this.o.neuesSpiel()
        };
           
    } // init
    this.neuesSpiel=function(){
        this.count=0;
        this.btnNeu.style.visibility="hidden";
        this.divButtons.style.visibility="visible";

        for (var i=0; i<10;i++){
            for (var j=0;j<10;j++){
                var zufall=Math.floor(Math.random() * this.farben.length);
                var k=this.kacheln [i][j];
                k.setColor(zufall,this.farben);
            }         
        } // for
    }

    this.append = function(element){
        return this.divAll.appendChild (document.createElement(element));
    }
    this.gewaehlt = function(neu){
	   this.txtCount.nodeValue = ++this.count;
        var alt=this.kacheln[0][0].index;
	for (var i=0;i<10;i++)
          for (var j=0;j<10;j++)
            this.kacheln[i][j].used=false;

        this.tauschen (0,0,alt,neu);
	
        if (this.check())
            this.ende();
    }
    this.tauschen = function (x,y,alt,neu){
        var k =this.kacheln[x][y];
       
	if (k.used)
		return;
        k.used=true;
	 if (k.index == alt){

	    k.setColor (neu,this.farben);
            for (var i= (x==0 ? 0 : x-1) ;i <= (x==9? 9 :x+1); i++){
                this.tauschen(i,y,alt,neu);
            }
            for (var j=(y==0 ? 0:y-1);j<= (y==9?9:y+1 );j++){
                this.tauschen(x,j,alt,neu);
            }       
        } // if
    } // tauschen
    this.check = function (){
        var ind = this.kacheln[9][9].index;
        for (var i=0;i<10;i++)
            for (var j=0;j<10;j++)
                if (this.kacheln[i][j].index!=ind)
                    return false;
        return true;
    }
    this.ende = function(){
        this.divButtons.style.visibility="hidden";
        this.btnNeu.style.visibility="visible";

    }
    this.init();
    this.neuesSpiel();
} // Spiel

function Kachel (knoten,index,spiel){
 	this.el= knoten.appendChild(document.createElement("button"));
	this.used=false;
	if (index)
		this.index=index;
	else
		this.index=0;

	with (this.el.style){
        	width="4em";
        	height="4em";
        	border="none";
	}
	if (spiel){
		this.el.kachel=this;
        	this.spiel=spiel;

		this.el.onclick=function(){
			this.kachel.spiel.gewaehlt (this.kachel.index);
		}
	} // if spiel
        this.setColor = function(index,farben){
	    this.index=index
            this.el.style.background=farben[index];
       }
}


