//************************************************* 
// setup the document variables and initialise them
//*************************************************
  	if (document.images) {

//		var baseDisplay;
//		var swapImages;
//		var visited;
//		var imgAdjacency;
//		var sectImages;

//************************************************************
// create the array swapImages to hold all the possible images
// for each section of the display
//************************************************************
	swapImages = new Array();
	for(var i = 0; i < 5; i++)
		swapImages[i] = new Array();
//***************************************************************************
// initialise the swapImages array with an array for each section
// of the display. The position each image occupies is determined
// by the property of the binary system that by summing each possible
// combination of the column values we can generate the numbers in between.
// For each section the array positions are allocated as follows:
// 0 = image with no sections highlighted
// 1,2,4... - the powers of 2 are assigned to the image with one
//				  section highlighted, these should be in the same order as
//				  in the adjacency list for the image in imgAdjacency.
// 3,5,6,7,9... - the numbers not powers of two are assigned to the images
//                which show two or more sections calculated by adding the
//                power of two assigned to the section.
//***************************************************************************

	swapImages[0][0] = new Image();
	swapImages[0][0].src = "navigation/home/section1_0.gif";
	swapImages[0][1] = new Image();
	swapImages[0][1].src = "navigation/home/section1_1.gif";

	swapImages[1][0] = new Image();
	swapImages[1][0].src = "navigation/home/section2_0.gif";
	swapImages[1][1] = new Image();
	swapImages[1][1].src = "navigation/home/section2_1.gif";
	swapImages[1][2] = new Image();
	swapImages[1][2].src = "navigation/home/section2_2.gif";
	swapImages[1][3] = new Image();
	swapImages[1][3].src = "navigation/home/section2_3.gif";

	swapImages[2][0] = new Image();
	swapImages[2][0].src = "navigation/home/section3_0.gif";
	swapImages[2][1] = new Image();
	swapImages[2][1].src = "navigation/home/section3_1.gif";
	swapImages[2][2] = new Image();
	swapImages[2][2].src = "navigation/home/section3_2.gif";
	swapImages[2][3] = new Image();
	swapImages[2][3].src = "navigation/home/section3_3.gif";
	swapImages[2][4] = new Image();
	swapImages[2][4].src = "navigation/home/section3_4.gif";
	swapImages[2][5] = new Image();
	swapImages[2][5].src = "navigation/home/section3_5.gif";
	swapImages[2][6] = new Image();
	swapImages[2][6].src = "navigation/home/section3_6.gif";
	swapImages[2][7] = new Image();
	swapImages[2][7].src = "navigation/home/section3_7.gif";
	swapImages[2][8] = new Image();
	swapImages[2][8].src = "navigation/home/section3_8.gif";
	swapImages[2][9] = new Image();
	swapImages[2][9].src = "navigation/home/section3_9.gif";
	swapImages[2][10] = new Image();
	swapImages[2][10].src = "navigation/home/section3_10.gif";
	swapImages[2][11] = new Image();
	swapImages[2][11].src = "navigation/home/section3_11.gif";
	swapImages[2][12] = new Image();
	swapImages[2][12].src = "navigation/home/section3_12.gif";
	swapImages[2][13] = new Image();
	swapImages[2][13].src = "navigation/home/section3_13.gif";
	swapImages[2][14] = new Image();
	swapImages[2][14].src = "navigation/home/section3_14.gif";
	swapImages[2][15] = new Image();
	swapImages[2][15].src = "navigation/home/section3_15.gif";

	swapImages[3][0] = new Image();
	swapImages[3][0].src = "navigation/home/section4_0.gif";
	swapImages[3][1] = new Image();
	swapImages[3][1].src = "navigation/home/section4_1.gif";
	swapImages[3][2] = new Image();
	swapImages[3][2].src = "navigation/home/section4_2.gif";
	swapImages[3][3] = new Image();
	swapImages[3][3].src = "navigation/home/section4_3.gif";
	swapImages[3][4] = new Image();
	swapImages[3][4].src = "navigation/home/section4_4.gif";
	swapImages[3][5] = new Image();
	swapImages[3][5].src = "navigation/home/section4_5.gif";
	swapImages[3][6] = new Image();
	swapImages[3][6].src = "navigation/home/section4_6.gif";
	swapImages[3][7] = new Image();
	swapImages[3][7].src = "navigation/home/section4_7.gif";

	swapImages[4][0] = new Image();
	swapImages[4][0].src = "navigation/home/section5_0.gif";
	swapImages[4][1] = new Image();
	swapImages[4][1].src = "navigation/home/section5_1.gif";

//***********************************************************
// initialise the image adjacency lists. These lists show the
// sections which form part of any given image.
//***********************************************************
	imgAdjacency = new Array();
	
	imgAdjacency[0] = [1];
	imgAdjacency[1] = [2,3];
	imgAdjacency[2] = [2,3,4,5];
	imgAdjacency[3] = [4,5,6];
	imgAdjacency[4] = [6];

//************************************************************
// initialise the lists of images associated with each section
//************************************************************
	sectImages = new Array();

	sectImages[0] = [0];
	sectImages[1] = [1,2];
	sectImages[2] = [1,2];
	sectImages[3] = [2,3];
	sectImages[4] = [2,3];
	sectImages[5] = [3,4];



		
//*****************************
// initialise the visited array
//*****************************
	visited = new Array();
	for(var i = 0; i < 6; i++)
		visited[i] = 0;

}
//*****************************
// initialise the base display
//*****************************
	baseDisplay = new Array();
	for(var i = 0; i < 5; i++)
		baseDisplay[i] = 0;


//*****************************************
// standard cookie function, takes an array of interger values and 
// a string name and stores them in a cookie.
//*****************************************
function intArray_to_cookie(name, ary)
{
	var cooky = "";
	if(ary)
	{
		var size = ary.length;
		for(var i=0; i<size; i++)
		{
			if(cooky == "")
				cooky += ary[i];
			else
				cooky += ("&" + ary[i]);
		}
		document.cookie = (name + "=" + cooky);
	}
}

//*****************************************
// standard cookie function, takes a string name and looks up the
// corresponding cookie. If the cookie exists then the values are
// parsed out and stored in the array. If the cookie is not found 
// then the array is not changed. The function returns the array.
//*****************************************
function cookie_to_intArray(name, ary)
{
	var cookyVal;
	var allCooky = document.cookie;
	if(allCooky != "")
	{
		var start = allCooky.indexOf(name + "=");
		if(start > -1)
		{
			start += name.length + 1;
			var end = allCooky.indexOf(";", start);
			if(end == -1)
				end = allCooky.length;
			cookyVal = allCooky.substring(start, end);
//window.alert(cookyVal);
			ary = cookyVal.split("&");
		}
	} 
	return ary;
}

//*****************************************
// function to look up the index of an image based on the sections
// which have already been visited. The index is returned.
//*****************************************
function lookupImageIndex(imgNum, sect)
{
	var x = imgAdjacency[imgNum];
	var y = x.length;
	var imgIndex = 0;

	for(var i=0; i<y; i++)
		if((x[i] == sect) || (visited[x[i] - 1] > 0))
			imgIndex = imgIndex + Math.pow(2,i);

	return imgIndex;
}

//*****************************************
// function to look up an image based on the sections which have
// visited. The image is returned.
//*****************************************
function lookupImage(imgNum, sect)
{
	var x = imgAdjacency[imgNum];
	var y = x.length;
	var imgIndex = 0;


	for(var i=0; i<y; i++)
		if((x[i] == sect) || (visited[x[i] - 1] > 0))
			imgIndex = imgIndex + Math.pow(2,i);

	return swapImages[imgNum][imgIndex];
}

//*****************************************
// basic rollover function, uses lookupImage to choose the
// appropriate graphic.
//*****************************************
function swapImage(sect)
{
	x = sectImages[sect - 1];
	y = x.length;

	for(var i=0; i<y; i++)
		document.images["scotland" + x[i]].src = lookupImage(x[i],sect).src;
}

//*****************************************
// mouse out handler for the rollover, retrieves the inactive
// image state from an array of integers. This allows the inactive 
// state of the image to be altered.
//*****************************************
function unswapImage(sect)
{
	x = sectImages[sect - 1];
	y = x.length;

	for(var i=0; i<y; i++)
		document.images["scotland" + x[i]].src = swapImages[ x[i] ][ baseDisplay[x[i]] ].src;
}

//*****************************************
// function to change the inactive image based on which sections
// have been visited. Looks up the current active image, sets this
// as the new inactive image and marks the section as visited.
//*****************************************
function changeBaseImage(sect)
{
	x = sectImages[sect - 1];
	y = x.length;

	for(var i=0; i<y; i++)
		baseDisplay[x[i]] =  lookupImageIndex(x[i], sect);   
                                                     
	visited[sect - 1] = 1;
}

//******************************************
// funtion to set the inactive image on page load. called once the 
// cookie has been checked for data and the visited and baseDisplay
// arrays have been updated.
//******************************************
function setDisplay()
{
	var len = baseDisplay.length;
	for(var i=0; i<len; i++)
		document.images["scotland" + i].src = swapImages[i][baseDisplay[i]].src;
}
