
// Uninitialised values - make them trackable
var uninitialisedVariable = "UNINITIALISED";

// Some general stuff
var searchDelimiter = "?";
var tagDelimiter = "&";

// What are our main categories?
var mainCategories = new Array(
	"collections",
	"press",
	"events",
	"archive",
	"contact"
);

// How many images in our random selection
var numRandomImages = 21;
var randomImagesFileType = ".jpg"
var randomImagePath = "collections/2005_autumn_winter/full/"

/*******************************************************************************
*
*	WriteMainMenu
*
*	Writes out the main menu - highlights the currently selected item. 
*
*******************************************************************************/

function WriteMainMenu( selectedCategory )
{
	for ( i = 0; i < mainCategories.length; ++i )
	{
		// What file does this item link to
		document.write( "<A href='" + mainCategories[i] + ".html' " );

		// What is the style of the link - should it be highlighted?
		if ( mainCategories[i] == selectedCategory )
			document.write( "class='Selected'>" );
		else
			document.write( "class='Available'>" );

		// Write out the name of the category
		document.write( varToReadable( mainCategories[i] ) + "<BR></A>" );
	}
}


/*******************************************************************************
*
*	GetRandomImage
*
*	Returns a string to tell us what random image to use
*
*******************************************************************************/

function GetRandomImage()
{
	// Start off with the random image path
	var returnString = randomImagePath;

	// Add a random number for the image
	var imageNumber = Math.floor( Math.random() * ( numRandomImages - 1 ) );

	// Finish up the path name
	returnString += imageNumber + randomImagesFileType;

	// Let them have it
	return returnString;	
}


/*******************************************************************************
*
*	varToReadable
*
*	Takes a variable name at converts it into readable form by replacing
*	underscores with spaces. 
*
*******************************************************************************/

function varToReadable( varName )
{
	var returnName = varName;

	while ( returnName.indexOf( "_" ) > 0 )
	{
		returnName = returnName.replace( "_", " " );
	}

	return returnName;
}


/*******************************************************************************
*
*	getValueByTag
*
*	Takes a string containing tags and values in the form...
*
*		"...<delimiter>tag1=value1<delimiter>tag2=value2..."
* 
*	...and the tag for which the value should be returned.
*
*******************************************************************************/

function getValueByTag( contentString, delimiter, tagName )
{
	// Create a string to search for
	var searchString = delimiter + tagName + "=";

	// Find the starting index of our search string
	var valueStartIndex = contentString.indexOf( searchString );

	// Make sure that we found the search string
	if ( valueStartIndex >= 0 )
	{
		// Move to the true value start
		valueStartIndex += searchString.length;

		// Find the next delimiter after the start of our value
		var valueEndIndex = contentString.indexOf( delimiter, valueStartIndex );

		// If this was the last tag/value pair we need to set the end index
		if ( valueEndIndex < 0 )
			valueEndIndex = contentString.length;

		// Return the value substring
		return contentString.substr( valueStartIndex, valueEndIndex - valueStartIndex );
	}

	// We found no tag
	return uninitialisedVariable;
}
	

/*******************************************************************************
*
*	FindDataByIndex
*
*	Finds some information from a multiple level array by matching the first
*	'index' level.
*
*******************************************************************************/

function FindDataByIndex( multiLevelArray, arrayLevels, dataIndex, dataLevel )
{
	// Set up our return value
	var returnData = uninitialisedVariable;

	// Loop through the given array until we find the index
	for ( var i = 0; i < multiLevelArray.length; i += arrayLevels )
	{
		if ( dataIndex == multiLevelArray[i] )
		{
			returnData = multiLevelArray[i + dataLevel];
			break;
		}
	}

	// Return the return data
	return returnData;
}


/*******************************************************************************
*
*	SwapImage
*
*	Updates the path for a particular image name
*
*******************************************************************************/

function SwapImage( imageName, newImagePath )
{
	document[imageName].src = newImagePath;
}



