
// URL tagging stuff
var publicationTag = "publication";
var dateTag = "date";

// Directories and files
var pressFolder = "press";
var fullImageName = "main";
var headerImageName = "header";
var imageFileExtension = ".jpg";

// Finding publication data
var dataPerPublication = 2;
var publicationNameOffset = 0;
var publicationDateOffset = 1;

// Which publication do we show by default
var defaultPublicationNumber = 22;

// These are all the bits we are going to display - they should
// be in this array in date order
var pressPublications = new Array(
	"crafts",			"may_2006",
	"daily_mail",	"february_2006",
	"elle",				"february_2005",
	"es_magazine",			"september_2004",
	"es_magazine",			"august_2003",
	"ft_weekend",			"february_2002",
	"harpers_",				"march_2006",
	"harpers_",				"december_2005",
	"harpers_",				"september_2003",
	"quintessentially_starry",	"2004",
	"sunday_times_magazine",		"december_2005",
	"tatler",				"march_2002",
	"tatler",				"february_2002",
	"telegraph_magazine",		"september_2005",
	"telegraph_magazine",		"november_2003",
	"telegraph_magazine",		"march_2002",
	"telegraph_magazine",		"december_2002",
	"telegraph_magazine",		"august_2002",
	"telegraph_magazine",		"september_2001",
	"the_daily_telegraph",		"september_2003",
	"times_magazine",			"june_2004",
	"times_style_magazine",		"june_2002",
	"vogue",				"september_2008",
	"vogue",				"march_2006",
	"vogue",				"october_2005",
	"vogue",				"september_2004",
	"vogue",				"september_2003",
	"vogue",				"december_2002",
	"vogue",				"september_2001",
	"you_magazine",			"october_2004",
	"you_magazine",			"march_2002",
	"34_magazine",			"2004"	
);


/*******************************************************************************
*
*	WritePressMenu
*
*	Writes out the press menu - highlights the currently selected item. 
*
*******************************************************************************/

function WritePressMenu( selectedPublication )
{
	// Check the input value - default if necessary
	if ( selectedPublication == uninitialisedVariable )
		selectedPublication = pressPublications[(defaultPublicationNumber * dataPerPublication) + publicationNameOffset];

	// We have multiple rows for each publication - so we need to check that
	// we aren't writing out the publications twice
	var previousPublication = "";

	for ( var i = 0; i < pressPublications.length; i += dataPerPublication )
	{
		if ( pressPublications[i + publicationNameOffset] != previousPublication )
		{
			// What is the file path that this calls?
			document.write( "<A href='press.html" + searchDelimiter
						+ tagDelimiter + publicationTag + "=" + pressPublications[i + publicationNameOffset] 
						+ tagDelimiter + dateTag + "=" + pressPublications[i + publicationDateOffset] + "' " );

			// What is the style of the link - should it be highlighted?
			if ( pressPublications[i + publicationNameOffset] == selectedPublication )
				document.write( "class='Selected'>" );
			else
				document.write( "class='Available'>" );

			// Hacky but required for silly names
			if ( pressPublications[i + publicationNameOffset] == "harpers_" )
			{
				document.write( "harpers & queen<BR><BR></A>" );
			}
			else
			{
				// Write out the name of the publication
				// alert( varToReadable( pressPublications[i + publicationNameOffset] ) + "<BR></A>" );
				document.write( varToReadable( pressPublications[i + publicationNameOffset] ) + "<BR><BR></A>" );
			}
	
			// Store this name so we don't duplicate it
			previousPublication = pressPublications[i + publicationNameOffset];
		}
	}
}


/*******************************************************************************
*
*	WritePressDisplay
*
*	Writes out the page section showing all the pages from a particular
*	publication.  Takes a publication name and date as arguments.
*
*******************************************************************************/

function WritePressDisplay( selectedPublication, selectedDate )
{
	// Check the input values - default if necessary
	if ( selectedPublication == uninitialisedVariable )
		selectedPublication = pressPublications[(defaultPublicationNumber * dataPerPublication) + publicationNameOffset];

	// Find the first publication date for the selected publication
	if ( selectedDate == uninitialisedVariable )
	{
		selectedDate = FindDataByIndex(	pressPublications,
								dataPerPublication,
								selectedPublication,
								publicationDateOffset );
	}
	
	// We need to find the first row in our data that applies to this publication
	// and how many rows it has
	var firstPublicationRow = 0;
	var numberOfPublicationRows = 0;
	var firstRowFound = false;
	for ( var i = 0; i < pressPublications.length; i += dataPerPublication )
	{
		if ( pressPublications[i + publicationNameOffset] == selectedPublication )
		{
			// Save the location of the first row
			if ( !firstRowFound ) 
			{
				firstPublicationRow = ( i / dataPerPublication );
				firstRowFound = true;
			}

			// Increment the total number of rows
			numberOfPublicationRows++;
		}
	}

	// Some table layout variables
	var thumbnailSpacerSize = "10px";
	var mainImageName = "mainImage";
			
	// Start our table
	document.write( "<TABLE><TR>" );

	// Write out a spacer cell
	document.write( "<TD width='10'></TD>" );

	// Write out the main image - silly nesting sorts out formatting
	document.write( "<TD width='315px' height='350px'><TABLE><TR><TD><IMG galleryimg='no' src='" + pressFolder + "/"
				+ selectedPublication + "/"
				+ selectedDate + "/"
				+ fullImageName + imageFileExtension + "' "
				+ "name='" + mainImageName + "'></TD></TR></TABLE></TD>" );

	// Write out a spacer cell
	document.write( "<TD width='20'></TD>" );

	// Kick off the subtable for the thumbnails
	document.write( "<TD valign='top'><TABLE>" );

	// Loop through the available thumbnails and set them out nicely
	for ( i = 0; i < numberOfPublicationRows; ++i )
	{
		// Where do we find the date for this thumbnail?
		var dateTableOffset = ( ( i + firstPublicationRow ) * dataPerPublication ) + publicationDateOffset;

		// Start off a new row
		document.write( "<TR>" );

		// Build the thumbnail image path
		var thumbnailImagePath = pressFolder + "/" + selectedPublication + "/"
						+ pressPublications[ dateTableOffset ]
						+ "/" + headerImageName + imageFileExtension;

		// Build the main image path - we need this to be written out in quotes
		var fullImagePath = "\"" + pressFolder + "/" + selectedPublication + "/"
						+ pressPublications[ dateTableOffset ]
 						+ "/" + fullImageName + imageFileExtension + "\"";
		
		// Write out the thumbnail cell
		document.write( "<TD width='100px'><A href='JavaScript:SwapImage( \""
					+ mainImageName + "\", " + fullImagePath + " );'><IMG galleryimg='no' src='"
					+ thumbnailImagePath  + "'></A></TD>" );
		
		// Finish off this row
		document.write( "</TR>" );	

		// Now write out a spacer row - start...
		document.write( "<TR>" );

		// ...all empty cells...
		document.write( "<TD height='" + thumbnailSpacerSize + "'></TD>" );

		// ...and relax
		document.write( "</TR>" );
		
	}

	// Finish off the subtable
	document.write( "</TABLE></TD>" );

	// Finish the main table
	document.write( "</TR></TABLE>" );
}


