/*********************************************************
* All sources in Hellbound are Copyright (c) 2008 Konijn *
* I Konijn, release all code and data under the terms of *
* of the GNU General Public License (version 2), as well *
* as under the traditional Angband license. Distribution *
* is allowed both under the terms of the GPL (version 2) *
* or under the terms of the traditional Angband license. *
*********************************************************/

//Global Objects

var view;          //The js objects that is accessed by all other source

//Private Objects

var _console;       //The js object that is the actual HTML element
var _cursor;        //Location of cursor, only for writing purposes, not display
var _rows;          //Size of view in rows
var _columns;       //Size of view in columns

//Shortcut, for speed purposes
var cx = 0;  //Cursox x
var cy = 0;  //Cursor y
var ox = 0;  //offset x
var oy = 0;  //offset y


//Initialization

function initView(){
  _console = document.getElementById("view");
  _columns=80
  _rows=27
  _cursor = new Object();
  view = new Object();
  for(row=0;row<_rows;row++)
      view[row]=new Array();
  view.clear = _clearView;
  view.draw  = _drawView;
  view.print = _putString;
  view.printReversed = _putStringReverse;
  view.printCentered = _putCenteredString;
  view.cursor = _cursor;
  view.cursor.set = _setCursor;
  view.cursor.advance = _advanceCursor;
  view.cursor.retreat = _retreatCursor;
  view.showCave = _showCave;
  view.clear();
  view.message = function( message ){};
  putIntro();
  view.draw();
}

//Public Functions

//no public functions

//Private Functions

function _clearView(){
  cx = 0;
  cy = 0;
  for(col=0;col<_columns;col++)
    for(row=0;row<_rows;row++)
      view[row][col]='&nbsp;';
}

//TODO: This could be way more efficient..
function _drawView(){
  var s = "";

  for(row=0;row<_rows;row++)
    s = s + view[row].join("") + "<br>"

  _console.innerHTML = s;
  cx = 0;
  cy = 0;
}

function _putString( s ){

  s = s + "";
  ta = s.split("");
  for(position=0;position<s.length;position++){
    c = s.charAt(position);
    view[cy][cx] = (c==' '?'&nbsp;':c);
    view.cursor.advance();
  }

}

function _putStringReverse( s ){
  s = (s + "").reverse(); //Stringify and reverse
  for(position=0;position<s.length;position++){
    c = s.charAt(position);
    view[cy][cx] = (c==' '?'&nbsp;':c);
    view.cursor.retreat();
  }
}

function _putCenteredString( s ){
  var col = Math.ceil( ( _columns - s.length ) / 2 );
  _setCursor( col , cy );
  _putString( s );
}

function _setCursor( column , row ){
  cx = column;
  cy = row;
}

function _advanceCursor(){
  cx++;
  if( cx == _columns ){
    cx = 0;
    cy++;
  }
  if( cy == _rows ){
    cy = 0;
  }
}

function _retreatCursor(){
  cx--;
  if( cx == -1 ){
    cx = _columns-1;
    cy--;
  }
  if( cy < 0 ) cy = 0;
}

function _showCave(  ){

  var s    = "";
  var cell = 0;
  var x = 0;
  var y = 0;


  //going east or south
  if( ox + _columns - player.x < 11 ) ox++;
  if( oy + _rows    - player.y < 11 ) oy++;

  if( player.y - oy < 11 ) oy--;
  if( player.x - ox < 11 ) ox--;


  //Bounding east and south
  if( ox + _columns > cave.currentWidth  ) ox = cave.currentWidth  - _columns;
  if( oy + _rows    > cave.currentHeight ) oy = cave.currentHeight - _rows;

  //Bounding west and north
  if( ox < 0 ) ox = 0;
  if( oy < 0 ) oy = 0;

  try{
  for( y = 0 ; y < _rows ; y++ ){
   for( x = 0 ; x < _columns ; x++ ){
     //if (cave[x + offsetX][y + offsetY].m_idx != 0)
     //  s = s + "@"
     //else 
     view[y][x] =  features[ cave[x + ox][y + oy].feat ].symbol ;
   }
  }
  }catch( e ){
    prompt( (x+ox) + "  " + (y+oy) );
  }

  view[player.y-oy][player.x-ox] = "@";
    
  view.draw();

}
