/*********
PegArea object
**********/
function PegArea(id, numRow, numCol, type){
this.obj = new getObj(id).obj;
this.pegs = new Array();
this.id = id;


//create Pegs and put into Array
for(var row = 0;row<numRow;row++){
    this.pegs[row] = new Array();
    for(var col = 0;col<numCol;col++){
        this.pegs[row][col] = new Peg(this, id+'r'+row+'c'+col, type);
    }
}

//add the peg images to the game
var tempDiv;
for(var row = 0;row<numRow;row++){
    tempDiv = document.createElement('div');
    for(var col = 0;col<numCol;col++){
        tempDiv.appendChild(this.pegs[row][col].img);
    }
    this.obj.appendChild(tempDiv);
}
}


/*******
function to replace a peg in
an empty hole on a home rack
*******/
PegArea.prototype.getEmpty = function(){
if(this.id == 'board')return;
for(var row=0;row<this.pegs.length;row++){
  for(var col=0;col<this.pegs[row].length;col++){
    if(this.pegs[row][col].type == 'emptyHome'){
    return this.pegs[row][col];
    }
  }
}
//returns a peg object
}




PegArea.prototype.getRelated = function(pegRow, pegCol){
/*
valid directions:
vert |
horiz -
diagl \
diagr /
*/

var retArray = new Array();
var row = pegRow;
var col = pegCol;
var c;
var r;

for(var rowi=0;rowi<theGame.board.pegs.length;rowi++){
  for(var coli=0;coli<theGame.board.pegs[rowi].length;coli++){
        r = rowi-row;
        c = coli-col;
  
  if(
      theGame.board.pegs[rowi][coli].type != 'emptyBoard' && //don't include empty pegs
        (
        (c==0 && r==0) || // include peg used for calculations
        rowi == row || //for horizontal (works)
        coli == col || //for vertical (works)
        
        (c == r) || //diagonal left to right (works)
        (r==-c || c==-r) //diag right to left (works)
        )
    ){
      //retArray[retArray.length] = '<br>rowi='+rowi+'coli='+coli+'  '+
                                'row='+row+'col='+col+'  '+
                                'r='+r+' c='+c;
      retArray[retArray.length] = theGame.board.pegs[rowi][coli];
      //board.pegs[rowi][coli].setType('AWin');
    }//end if (line check)
  }//end for

}//end for


return retArray;

}




PegArea.prototype.showPegs = function(){
var tempArr = new Array();

for(var x=0;x<this.pegs.length;x++){
tempArr[tempArr.length] = '\n';
    for(var y=0;y<this.pegs.length;y++){
    tempArr[tempArr.length] = this.pegs[x][y].type;

    }
}

alert(tempArr);

}
PegArea.prototype.clickPeg = function(rowId, colId){
if(this.id.search(/score/) == -1){
this.pegs[rowId][colId].clicked();
}else{
theGame.msg.innerHTML = 'The score can\'t be changed manaully.';
}

}



PegArea.prototype.reset = function(){
        for(var row=0;row<this.pegs.length;row++){
                for(var item=0;item<this.pegs[row].length;item++){
                        if(this.id == 'board'){
                                this.pegs[row][item].setType('emptyBoard');
                        }else if(this.id.search(/score/) != -1){
                                this.pegs[row][item].setType('emptyHome');
                        }else if(this.id.search(/pegs/) != -1){
                                this.pegs[row][item].setType(this.id.substring(0,1)+'Home');
                        }
                }
        }
}

