// $Id: control.js,v 1.4 2010/08/08 01:54:16 pwh Exp $

// Instantiation of the Keyboard object. ---------------------------------------

var keyboard = new Keyboard()


// Instantiation of the Display object. ----------------------------------------

var display = null			// Wait for start after HTML has loaded.


// Keyboard input. -------------------------------------------------------------

// Key pressed.
function keydown ( e )

{
   keyboard.keyPressed ( e )

   return ( false )
}


// Key released.
function keyup ( e )

{
   keyboard.keyReleased ( e )

   return ( false )
}


// Make sure keypad is cleared when cursor leaves the grid.
function setOffGridTimer ()

{
   if ( display )
	display.offGrid = setTimeout ( "display.hideKeyPad()", 50 )
}


function clearOffGridTimer ()

{

   if ( display && display.offGrid ) {

	clearTimeout ( display.offGrid )
	display.offGrid = null
   }
}


function offGrid ()

{
   if ( display ) display.hideKeyPad ()
}


// Display key pad if cursor is over an empty cell.
function overCell ( row, column )

{
   if ( display ) {

	display.hideKeyPad ()		// Must do this because the display
					// doesn't know the mouse has moved.

	// Is the cell empty?
	if ( display.isEmpty ( row, column ) ) {

		display.showKeyPad ( row, column )
		display.offGrid = setTimeout ( "display.hideKeyPad()", 1000 )
	}
   }

   return ( false )
}


// Display key pad in response to a mouse click on the cell.
function cellClicked ( row, column )

{
   if ( display ) display.showKeyPad ( row, column )

   return ( false )
}


// Mouse has moved over a key on the key pad.
function overKey ( row, column, digit )

{
   if ( display ) {

	if ( display.offGrid ) {

		clearTimeout ( display.offGrid )
		display.offGrid = null
	}

	if ( ! display.checkErrors ( digit ) ) display.keyOn ( digit )
   }

   return ( false )
}


// Mouse has moved off a key on the key pad.
function offKey( row, column, digit )

{
   if ( display ) {

	display.keyOff ( digit )
	setOffGridTimer ()
   }

   return ( false )
}


// A digit on the key pad was clicked.
function keyClicked ( row, column, digit )

{
   if ( display ) {

	if ( keyboard.shift && display.solving ) {

		display.clearDigit ()

		if ( display.isMarkup ( digit ) ) display.clearMarkup ( digit )
		else display.setMarkup ( digit )

	} else {

		var oldDigit = display.getDigit ( row, column )

		if ( digit == oldDigit ) {
			
			display.clearDigit ()

		} else if ( display.setDigit ( digit ) ) {

			display.keyOff ( digit )
			display.hideKeyPad ()
			if ( display.isFull () ) {

				if ( display.isSolved () )
						alert ( "Congratulations!" )
				else {

					if ( display.showErrors )
alert ( "There are errors (indicated by the red digits)." )
					else
alert ( "There are errors, try the Show Errors option to help find them." )
				}
			}
		}
	}
   }

   return ( false )
}


// The Reset Button was clicked.
function reset ()

{
   if ( display && confirm ( "You are about to clear the entire board and start over.\nThis action cannot be undone." ) ) display.reset ()

   return ( false )
}


// The Clear Button was clicked.
function clearGuesses ()

{
   if ( display && confirm ( "You are about to clear everything except the clues.\nThis action cannot be undone." ) ) display.clearGuesses ()

   return ( false )
}


// The Save Checkpoint Button was clicked.
function saveCheckpoint ()

{
   if ( display ) display.saveCheckpoint ()

   return ( false )
}


// The Restore Checkpoint Button was clicked.
function restoreCheckpoint ()

{
   if ( display ) display.restoreCheckpoint ()

   return ( false )
}


// The Autofill Button was clicked.
function autofill ()

{
   if ( display ) display.autofill ()

   return ( false )
}


// The Clear Markup Button was clicked.
function clearMarkup ()

{
   if ( display ) display.autoclear ()

   return ( false )
}


// The Show Errors checkbox was clicked.
function toggleErrors ()

{
   if ( display ) display.toggleShowErrors ()

   return ( false )
}


// A Show Digits digit was clicked.
function toggleDigits ( digit )

{
   if ( display ) display.toggleDigits ( digit )

   return ( false )
}


// The Solve It button was clicked.
function solveIt ()

{
   if ( display ) display.solveIt ()

   return ( false )
}


// Body loaded event. ----------------------------------------------------------

// When the page has loaded, activate the display.
function start ()

{
   display = new Display( "display" )

   return ( false )
}

// Preload the graphics. -------------------------------------------------------

// Constructor for digit graphics preload object.
function Preload ( baseName )

{
   this.images = new Array()

   for ( var i = 1; i < 10; i++ ) {

	this.images[i] = new Image()
	this.images[i].src = "images/" + baseName + "-" + i + ".gif"
   }
}


// Preload the graphics
var blank = new Image()
blank.src = "images/Blank.gif"

var clearImg = new Image()
clearImg.src = "images/Clear.gif"

var highlight = new Image()
highlight.src = "images/highlight.gif"

var cluesPreload = new Preload ( "Clue" )
var digitsPreload = new Preload ( "Digit" )
var digitErrorsPreload = new Preload ( "digitError" )
var clueErrorsPreload = new Preload ( "clueError" )
var smBluesPreload = new Preload ( "smBlue" )
var smBlacksPreload = new Preload ( "smBlack" )
var smRed = new Preload ( "smRed" )
var bgBluesPreload = new Preload ( "bgBlue" )
var bgBlacksPreload = new Preload ( "bgBlack" )
var bgRedPreload = new Preload ( "bgRed" )
