// this code is courtesy of Darrell Spice, from his Atari homebrew page at:
// http://www.spiceware.org/atari_homebrews_online.html

imgPowerOn    = new Image()
imgPowerOff   = new Image()
imgTVcolor    = new Image()
imgTVbw       = new Image()
imgLDa        = new Image()
imgLDb        = new Image()
imgCartIn     = new Image()
imgCartOut    = new Image()
imgRDa        = new Image()
imgRDb        = new Image()
imgSelectUp   = new Image()
imgSelectDown = new Image()
imgResetUp    = new Image()
imgResetDown  = new Image()

imgPowerOn.src    = "../images/vcs/artie_power_on.png"
imgPowerOff.src   = "../images/vcs/artie_power_off.png"
imgTVcolor.src    = "../images/vcs/artie_tv_color.png"
imgTVbw.src       = "../images/vcs/artie_tv_bw.png"
imgLDa.src        = "../images/vcs/artie_ld_a.png"
imgLDb.src        = "../images/vcs/artie_ld_b.png"
imgCartIn.src     = "../images/vcs/artie_cart_duck.png"
imgCartOut.src    = "../images/vcs/artie_cart_out.png"
imgRDa.src        = "../images/vcs/artie_rd_a.png"
imgRDb.src        = "../images/vcs/artie_rd_b.png"
imgSelectUp.src   = "../images/vcs/artie_select_up.png"
imgSelectDown.src = "../images/vcs/artie_select_down.png"
imgResetUp.src    = "../images/vcs/artie_reset_up.png"
imgResetDown.src  = "../images/vcs/artie_reset_down.png"


function updateJStellaGUI()
{
	// called when JStella changed something so GUI can be updated to reflect it
	UpdateTVtype()
	UpdateLeft()
	UpdateRight()
}

function DoPower()
{
	// toggle power switch
	if (document.jstella.isConsolePowerOn())
	{
		document.images['power'].src=imgPowerOff.src
		document.images['cart'].src=imgCartOut.src
		document.jstella.setDefaultScreenModeToSnow()
		document.jstella.setConsolePowerOn(false)
		document.jstella.refocusKeyboard()
	}
	else
	{
		document.images['power'].src=imgPowerOn.src
		document.images['cart'].src=imgCartIn.src
		//document.jstella.insertCartridge(game,210)
		document.jstella.setConsolePowerOn(true)
		updateJStellaGUI()			
		document.jstella.refocusKeyboard()
	}
}

function DoTVtype()
{
	// clicked on TV Type, toggle the direction
	if (document.jstella.isTVTypeBW())
		document.jstella.setTVTypeBW(false)
	else
		document.jstella.setTVTypeBW(true)
	UpdateTVtype()
}

function UpdateTVtype()
{
	// update TV Type switch based on its current value
	if (document.jstella.isTVTypeBW())
		document.images['tvtype'].src=imgTVbw.src
	else
		document.images['tvtype'].src=imgTVcolor.src
	document.jstella.refocusKeyboard()
}

function DoLeft()
{
	// clicked on Left Difficulty, toggle the direction
	if (document.jstella.isPlayer0Amateur())
		document.jstella.setPlayer0Amateur(false)
	else
		document.jstella.setPlayer0Amateur(true)
	UpdateLeft()
}

function UpdateLeft()
{
	// update Left Difficulty Switch based on its current value
	if (document.jstella.isPlayer0Amateur())
		document.images['left'].src=imgLDb.src
	else
		document.images['left'].src=imgLDa.src
	document.jstella.refocusKeyboard()
}

function DoRight()
{
	// clicked on Right Difficulty, toggle the direction
	if (document.jstella.isPlayer1Amateur())
		document.jstella.setPlayer1Amateur(false)
	else
		document.jstella.setPlayer1Amateur(true)
	UpdateRight()
}

function UpdateRight()
{
	// update Right Difficulty Switch based on its current value
	if (document.jstella.isPlayer1Amateur())
		document.images['right'].src=imgRDb.src
	else
		document.images['right'].src=imgRDa.src
	document.jstella.refocusKeyboard()
}

function DoSelectDown()
{
	// Tell JStella the Select Switch is held down
	document.jstella.setSelectDown(true)
	document.images['select'].src=imgSelectDown.src
}

function DoSelectUp()
{
	// Tell JStella the Select Switch has been released
	document.jstella.setSelectDown(false)
	document.images['select'].src=imgSelectUp.src
}
function DoReset()
{
	// user clicked on RESET switch
	document.jstella.doReset()
}
// getFile and postFileReady from http://www.rodsdot.com/ee/loadExternalHTMLfileINTOdivisionXMLHTTP.asp
function getFile(pURL) {
   if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc 
      xmlhttp=new XMLHttpRequest();
      xmlhttp.onreadystatechange=postFileReady;
      xmlhttp.open("GET", pURL, true);
      xmlhttp.send(null);
   } else if (window.ActiveXObject) { //IE 
      xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
      if (xmlhttp) {
         xmlhttp.onreadystatechange=postFileReady;
         xmlhttp.open('GET', pURL, true);
         xmlhttp.send();
      }
   }
}

// function to handle asynchronous call
function postFileReady() {
   if (xmlhttp.readyState==4)
      if (xmlhttp.status==200) 
         document.getElementById('manual').innerHTML=xmlhttp.responseText;
}
// - End of JavaScript - -->

 
