/*********
Peg object
**********/
function Peg(pegArea, id, type){
this.type = type;
this.id = id;
this.pegArea = pegArea;

this.img = document.createElement('img');
this.img.id = id;

this.setType(type);
}
/**********
set the peg type
***********/
Peg.prototype.setType = function(type){
this.type = type;
this.img.src = ImgSrc(type);
function ImgSrc(type){
switch(type){
case 'AHome':

    return "brownDot.gif";
    
    break;
case 'ABoard':
    return "brownRing.gif";
    break;
case 'AWin':
    return "brownWin.gif";
    break;
    
case 'BHome':
    return "blueDot.gif";
    
    break;
case 'BBoard':
    return "blueRing.gif";
    break;
case 'BWin':
    return "blueWin.gif";
    break;
    
case 'emptyHome':


    return "grayDot.gif";
    
    
    break;
case 'emptyBoard':
    return "grayRing.gif";
    break;
default:
    return "blank.gif"
    break;
}
}
}

/**********
do something when clicked
(event)
***********/
Peg.prototype.clicked = function(){
if(theGame.selectedPegType == ''){
//not holding a peg
        if(this.pegArea.id == 'board'){
        //click was on board
                if(this.type == 'emptyBoard'){
                //click was on hole
                        theGame.msg.innerHTML = 'Select a peg of your colour.';
                        return;
                }else{
                //click on peg
                        theGame.msg.innerHTML = 'Once pegs are placed on the board, they can\'t be moved<br>'
                        if(this.id == theGame.lastPlacedPeg.id){
                        theGame.msg.innerHTML += 'Use undo if you want to take your turn again.';
                        }
                }
        }else if(this.pegArea.id.search(/pegs/) != -1){
        //click on home pegs
                if(theGame.playerTurn != this.type.substr(0,1)){
                //click on wrong color pegs
                        theGame.msg.innerHTML = 'It\'s not your turn.'//+theGame.playerTurn+' '+this.type.substr(0,1);
                }else if(this.type == 'emptyHome'){
                //click on hole
                        theGame.msg.innerHTML = 'You aren\'t holding anything. Select a peg of your colour.';
                        return;
                }else{
                //click on right color peg
                theGame.msg.innerHTML = 'Holding a peg for player '+theGame.playerTurn; //message
                theGame.selectedPegType = this.type; //pick up the peg
                theGame.pegHeld.pegs[0][0].setType(this.type);
                //theGame.playerTurn = (this.type.substr(0,1) == 'A') ? 'B' : 'A'; //change turn
                this.setType('emptyHome'); //change to empty
                
                }
        }
        //end no peg selected               
}else{
//holding a peg
        if(this.type.search(/empty/) != -1){
        //click was on a hole
        if(this.pegArea.id == theGame.selectedPegType.substr(0,1)+'pegs'){
                //click was on peg's home rack
                theGame.msg.innerHTML = 'You put your peg down. It\'s still your turn.';
                //theGame.playerTurn = (this.type.substr(0,1) == 'A') ? 'B' : 'A';
                this.setType(theGame.selectedPegType);
                theGame.selectedPegType = '';
                theGame.pegHeld.pegs[0][0].setType('');
        }else if(this.pegArea.id == 'board'){
        //click was on board
                theGame.msg.innerHTML = 'Placed a peg for player '+theGame.selectedPegType.replace('Home','')+' on the board';
                this.setType(theGame.selectedPegType);
                theGame.selectedPegType = '';
                theGame.pegHeld.pegs[0][0].setType('');
                //for undo
                theGame.lastPlacedPeg = this;
                theGame.playerTurn = (this.type.substr(0,1) == 'A') ? 'B' : 'A'; //change turn
                //check for a winner
                theGame.checkWin(this);
        }
}else if(this.pegArea.id.search(/pegs/) != -1 && this.type.search(/Home/) != -1){
  //click was on another peg in a home rack
  //put the one we're holding back
  this.pegArea.replacePeg().setType(theGame.selectedPegType);
  
  //pick up the new peg
  theGame.selectedPegType = this.type;
  this.setType('emptyHome');
}
}

//end peg selected
}//end function 

/**********
check if another peg is adjacent
***********/
Peg.prototype.isAdjacent = function(pegObj){
//returns boolean

}
/**********
get all the immediately adjacent pegs
***********/
Peg.prototype.getAdjacent = function(){


}
