/**
* @name PGN viewer
* @copyright 2006 Karsten Nymann Pedersen
* @version 2.00
* @author Karsten Nymann Pedersen
* @license Buyware
* @mail pgnreader@rapax.dk
*/

var pgn_board_creator = function(){
  this.elm = new tool_element;
  this.frm = new Array;
  this.innerfrm = new Array;
  this.frameTal = new Array();
  this.frameBog = new Array();
  this.field = new Array();
  this.styleVal = new Array();
  this.number = 0;
}

pgn_board_creator.prototype.createBorder = function(obj,objId,oWidth,oHeight,oTop,oLeft,oBorderWidth,oBorderType,oBorderColor)
{
  newFrame = this.elm.element("div",obj,"id='"+objId+"'","style='position: absolute; width: "+parseInt(oWidth)+settings.measure+"; height:"+parseInt(oHeight)+settings.measure+"; top: "+parseInt(oTop)+settings.measure+"; left: "+parseInt(oLeft)+settings.measure+"; border: "+parseInt(oBorderWidth)+settings.measure+" "+oBorderType+" "+oBorderColor+";'");
  return newFrame;
}

pgn_board_creator.prototype.showFrameNr = function(framObj)
{

  for (t=1; t<=8; t++)
  {
    this.frameTal[t] = this.elm.element("div",framObj,"classname='numbers'","id='number_id_"+t+"'","style='position: absolute; left: "+settings.fieldWidth*.15+settings.measure+"; color: "+settings.borderLetterColor+"'");
    this.span = this.elm.element("span",this.frameTal[t],"style='font-size: "+settings.frameFontsize+";'");
    this.elm.textNode(t,this.span);
  }
  for (t=1; t<=8; t++)
  {
    this.frameBog[t] = this.elm.element("div",framObj,"classname='numbers'","id='letters_id_"+t+"'","style='position: absolute; top: "+(settings.fieldWidth*8.55+settings.innerBorderColor.length*2)+settings.measure+"; color: "+settings.borderLetterColor+"'");
    this.span = this.elm.element("span",this.frameBog[t],"style='font-size: "+settings.frameFontsize+";'");
    this.elm.textNode(String.fromCharCode(t+96),this.span);
  }
}

pgn_board_creator.prototype.reverseBoard = function()
{
  if (settings.boardReverse)
  {
    for (t=1; t<=8; t++)
    {
      this.frameTal[t].style.top = settings.fieldWidth*t-settings.fieldWidth*.25+settings.measure;
      this.frameBog[t].style.left = settings.fieldWidth*(9-t)+settings.measure;
    }
    for (x=0; x<8; x++)
    {
      for (y=0; y<8; y++)
      {
        feltNr=y+x*8;
        feltNr=63-feltNr
        this.field[feltNr].style.top= settings.fieldWidth*x+settings.measure;
        this.field[feltNr].style.left= settings.fieldWidth*y+settings.measure;
      }
    }
  }
  else
  {
    for (t=1; t<=8; t++)
    {
      this.frameTal[t].style.top = settings.fieldWidth*(9-t)-settings.fieldWidth*.25+settings.measure;
      this.frameBog[t].style.left = settings.fieldWidth*t+settings.measure;
    }
    for (x=0; x<8; x++)
    {
      for (y=0; y<8; y++)
      {
        feltNr=y+x*8;
        this.field[feltNr].style.top= settings.fieldWidth*x+settings.measure;
        this.field[feltNr].style.left= settings.fieldWidth*y+settings.measure;
      }
    }
  }
}

pgn_board_creator.prototype.createFelter = function(framObj)
{
  for (xcf=0; xcf<8; xcf++)
  {
    for (ycf=0; ycf<8; ycf++)
    {
      feltNr=ycf+xcf*8;
      if (parseInt((feltNr+xcf)/2)==(feltNr+xcf)/2) this.fieldColor=settings.bgColorWhite; else this.fieldColor = settings.bgColorBlack;
      this.field[feltNr] = this.elm.element("div",framObj,"id='"+feltNr+"felt'","style='position: absolute; width: "+settings.fieldWidth+settings.measure+"; height: "+settings.fieldWidth+settings.measure+"; background-color: "+this.fieldColor+"';");
//      this.field[feltNr].setAttribute("tabindex",feltNr);

/*
      if (this.mainObj.dragMode)
      {
        this.elm.addEventMousedown(this.field[feltNr],this.onmousedown,this.field[feltNr],parentObj);
        this.elm.addEventMouseover(this.field[feltNr],this.onmouseover,this.field[feltNr],parentObj);
        this.elm.addEventMouseout(this.field[feltNr],this.onmouseout,this.field[feltNr],parentObj);
      }

*/
	
    }
  }
}

pgn_board_creator.prototype.createBoard = function()
{
  this.boardWidth = settings.outerBorderColor.length*2+ settings.innerBorderColor.length*2 + settings.fieldWidth*9;
  this.frm[0] = this.createBorder(settings.boardContainer,"frame_0",this.boardWidth,this.boardWidth,0,0,settings.outerBorderWidth,settings.outerBorderType,settings.outerBorderColor[0]);
  for (n=1; n<settings.outerBorderColor.length; n++){
  	this.frm[n] = this.createBorder(this.frm[n-1],+"frame_"+n,this.boardWidth-n*settings.outerBorderWidth*2,this.boardWidth-n*settings.outerBorderWidth*2,0,0,settings.outerBorderWidth,settings.outerBorderType,settings.outerBorderColor[n]);
	}
	this.mainBorder = this.frm[n-1];
	this.mainBorder.style.backgroundColor = settings.mainBorderColor;
	this.showFrameNr(this.mainBorder);
  this.innerBoardWidth = settings.innerBorderColor.length*2 + settings.fieldWidth*8;
 	this.innerfrm[0] = this.createBorder(this.mainBorder,+"innerframe_0",this.innerBoardWidth,this.innerBoardWidth,settings.fieldWidth/2,settings.fieldWidth/2,settings.innerBorderWidth,settings.innerBorderType,settings.innerBorderColor[0]);
  for (n=1; n<settings.innerBorderColor.length; n++){
  	this.innerfrm[n] = this.createBorder(this.innerfrm[n-1],+"innerframe_"+n,this.innerBoardWidth-n*settings.innerBorderWidth*2,this.innerBoardWidth-n*settings.innerBorderWidth*2,0,0,settings.innerBorderWidth,settings.innerBorderType,settings.innerBorderColor[n]);
	}
	this.fieldCont = this.createBorder(this.innerfrm[n-1],+"innerframe_"+n,this.innerBoardWidth-n*settings.innerBorderWidth*2,this.innerBoardWidth-n*settings.innerBorderWidth*2,0,0,settings.innerBorderWidth,settings.innerBorderType,"#000000");
	this.fieldCont.style.backgroundColor = "#FFFFFF"
	this.createFelter(this.fieldCont)
	this.reverseBoard();
}

pgn_board_creator.prototype.createPiece = function(obj,brik_type)
{
  var image=settings.imageMap+settings.pieceMap+brik_type+ "." +settings.imageType;
  this.brik = this.elm.element("img",obj,"src='"+image+"'","id='id"+brik_type+"_"+this.number+"'","style='width: 100%; height: 100%;'");
  this.number++;
  return this.brik;
}

pgn_board_creator.prototype.insertPieces = function(boardArray)
{
  if (!boardArray) return false;
  settings.oldVariantNr[settings.gameNumber] = settings.variation;
	settings.oldMoveNr[settings.gameNumber] = settings.moveNr;
  if (document.getElementById("mn"+settings.moveNr+"_"+settings.variation)) {
   if (this.oldMove) 
	 {
	   this.oldMove.className = this.oldClassName;
		}
	 this.oldMove =document.getElementById("mn"+settings.moveNr+"_"+settings.variation);
	 this.oldClassName = this.oldMove.className
	 this.oldMove.className = "activeMove";
	}
  boardArray = boardArray.split("");
  for (xip=0; xip<8; xip++)
  {
    for (yip=0; yip<8; yip++)
    {
      feltNr=yip+xip*8;
      if (boardArray[feltNr]==boardArray[feltNr].toLowerCase()) this.pieceColor="s"; else this.pieceColor="h";
      if (this.field[feltNr].hasChildNodes() && boardArray[feltNr]=="-") this.field[feltNr].removeChild(this.field[feltNr].childNodes[0]);
			if (this.field[feltNr].type != boardArray[feltNr] && boardArray[feltNr] != "-")
			{
      	if (this.field[feltNr].hasChildNodes()) this.field[feltNr].removeChild(this.field[feltNr].childNodes[0]);
			  this.createPiece(this.field[feltNr],this.pieceColor+boardArray[feltNr].toLowerCase())
			}
			this.field[feltNr].type = boardArray[feltNr];
    }
  }
  return true;
}
pgn_board_creator.prototype.nullStilBoard = function()
{
}

pgn_board_creator.prototype.resizeBoard = function()
{
  this.boardWidth = settings.outerBorderColor.length*2+ settings.innerBorderColor.length*2 + settings.fieldWidth*9;

  for (n=0; n<settings.outerBorderColor.length; n++){
    this.frm[n].style.width = this.boardWidth-n*settings.outerBorderWidth*2+"px";
    this.frm[n].style.height = this.boardWidth-n*settings.outerBorderWidth*2+"px";
	}
  this.innerBoardWidth = settings.innerBorderColor.length*2 + settings.fieldWidth*8;
  for (n=0; n<settings.innerBorderColor.length; n++){
    this.innerfrm[n].style.width = this.innerBoardWidth-n*settings.innerBorderWidth*2+"px";
    this.innerfrm[n].style.height = this.innerBoardWidth-n*settings.innerBorderWidth*2+"px";
	}
	this.fieldCont.style.width = this.innerBoardWidth-n*settings.innerBorderWidth*2+"px";
	this.fieldCont.style.height = this.innerBoardWidth-n*settings.innerBorderWidth*2+"px";
	for (n=0; n<64; n++)
	{
	  this.field[n].style.width = settings.fieldWidth + "px";
	  this.field[n].style.height = settings.fieldWidth + "px";
	}
	this.reverseBoard();
}

