// monopoly.js - common JavaScript for the Washington Monopoly game
// Copyright (c) 2000 Global Arcade

// ------------------------------------------------------------------------------------------
// Used by main game board window

function Square(name, page, imgName, imgFile)
{
	this.name = name;
	this.page = page;
	this.imgname = imgName;
	this.imgfile = imgFile;
}

// initializes game variables
function initGame(path)
{
	gameSquares = new Array();
	gameSquares[0] = new Square("home", "home.html", "Nboard_01_01", "0");
	gameSquares[1] = new Square("philadelphia", "philadelphia.html", "Nboard_01_09", "6");
	gameSquares[2] = new Square("losangeles", "losangeles.html", "Nboard_10_09", "12");
	gameSquares[3] = new Square("irs", "irs.html", "Nboard_10_05", "15");
	gameSquares[4] = new Square("court", "court.html", "Nboard_10_01", "18");
	gameSquares[5] = new Square("whitehouse", "whitehouse.html", "Nboard_02_01", "23");
}

// opens the first page
function startGame()
{
	if (gamePosition != 0)
		leaveSquare(gamePosition);
		
	gamePosition = 0;
	openSquare(0);
	return false;
}

// opens the specified page in a new window
function openSquare(sIndex)
{
	if (sIndex >= 0 && sIndex < gameSquares.length)
	{
	    var square = gameSquares[sIndex];
	    
		// highlight square in page
		document.images[square.imgname].src = "../images/" + square.imgfile + "_down.gif";
		
		// open new page in popup window
		if (gameWindow != 0)
			gameWindow.location.href = getParent(window.location.href) + "/" + square.page;
		else
			gameWindow = window.open(getParent(window.location.href) + "/" + square.page, "gameSquare",
		            	"height=450,width=400,scrollbars=yes,resizable=yes,directories=no,status=no,toolbar=no,location=no");
	}	           
}

// cleans up display for old square
function leaveSquare(sIndex)
{
	if (sIndex >= 0 && sIndex < gameSquares.length)
	{
	    var square = gameSquares[sIndex];
	    
		// unhighlight square in page
		document.images[square.imgname].src = "../images/" + square.imgfile + ".gif";
	}	           
}

// ------------------------------------------------------------------------------------------
// Used by game popup windows

// goes to the next square
function goNext()
{
	// clean up previous square
	leaveSquare(gamePosition);
	
	// move to next square
	var nextIndex = ++gamePosition;
	if (nextIndex < gameSquares.length)
		openSquare(nextIndex);
	else
		gameWindow.close();	
	
	return false;
}

// goes to the next square
function clickNext()
{
	if (window.opener != null) 
	     window.opener.goNext();
	
	return false;
}

// ------------------------------------------------------------------------------------------
// Utility functions

// returns the base path of an URL (minus the html page itself)
function getParent(path)
{
	return path.substring(0, path.lastIndexOf("/"));
}

// for swapping images
function di(id,name){
	if (gamePosition >= 0 && gamePosition < gameSquares.length)
	{
		if (id == gameSquares[gamePosition].imgname)
			return;
			
	}
	
    if (document.images) {document.images[id].src=eval(name+".src"); }
}

// function that displays status bar message

function dm(msgStr) {
  document.returnValue = false;
  if (document.images) { 
     window.status = msgStr;
     document.returnValue = true;
  }
}
var showMsg = navigator.userAgent != "Mozilla/4.0 (compatible; MSIE 4.0; Mac_PowerPC)";
function dmim(msgStr) {
  document.returnValue = false;
  if (showMsg) { 
    window.status = msgStr;
    document.returnValue = true;
  }
}
