// 'Globals'
var xmlDoc;

// The main image list
var imageList = null;
var curImage = 0 ;

// The index of images by ID
var imageIndex = null;

// Portrait & landscape image lists
var portraitImages = null;
var landscapeImages = null;

// The image indices
var curIndex ;
var la_all       = null;
var la_recent    = null;
var la_thisyear  = null;
var la_lastyear  = null;
var la_older     = null;
var la_favourite = null;

var fl_all    = null;
var fl_pink   = null;
var fl_yellow = null;
var fl_white  = null;
var fl_blue   = null;
var fl_purple = null;
var fl_orange = null;
var fl_red    = null;
var fl_other  = null;

var tr_all    = null;
var tr_spring = null;
var tr_summer = null;
var tr_autumn = null;
var tr_winter = null;

var ln_all           = null;
var ln_sunrise       = null;
var ln_sunset        = null;
var ln_sea           = null;
var ln_wales         = null;
var ln_canada        = null;
var ln_cornwall      = null;
var ln_lake_district = null;
var ln_other         = null;

var mi_all       = null;
var mi_mono      = null;
var mi_birds     = null;
var mi_snow      = null;
var mi_buildings = null;

// 'Constants'
var XML_DATA = "MainExhibition/exhibition120108.xml";
var THIS_YEAR = "2011";
var LAST_YEAR = "2010";
var RECENT_SIZE = 42 ;

// Functions to deal with XML parsing and setting up lists of images
function loadxml() 
{
//	xmlDoc=document.implementation.createDocument("","",null);
//	xmlDoc.load(XML_DATA);
//	xmlDoc.onload= readXML;

	// IE7 and later, Firefox, Chrome, Opera and Safari all claim to support XMLHttpRequest
	if ( window.XMLHttpRequest )
	{
		xmlDoc = new XMLHttpRequest();
	}
	else
	{
		// Earlier browsers do it their own way ...
		xmlDoc = new ActiveXObject( "Microsoft.XMLHTTP" );
	}

	// Send the request to the server.  Use POST rather than GET to pursuade the browser not to cache the XML file
	xmlDoc.onreadystatechange = readXML;
	xmlDoc.open(POST_GET, XML_DATA, true);
	xmlDoc.send();
}

// Read in the XML gallery definition file
function readXML() 
{
	var params;
	var imageId;

	// If the file hasn't loaded, exit
  	if ( ( xmlDoc.readyState != 4 ) || ( xmlDoc.status != 200 ) )
    	{
		return;
	}

	// Get the document
//	var xdoc = xmlDoc.documentElement;
	var xdoc = xmlDoc.responseXML.documentElement;

	// Create the arrays
	curImage = 0 ;
	imageList = new Array(xdoc.childNodes.length)

	imageIndex = new Array();

	portraitImages  = new Array();
	landscapeImages = new Array();

	// Create the indices
	la_all       = new Array();
	la_recent    = new Array();
	la_thisyear  = new Array();
	la_lastyear  = new Array();
	la_older     = new Array();
	la_favourite = new Array();

	fl_all    = new Array();
	fl_pink   = new Array();
	fl_yellow = new Array();
	fl_white  = new Array();
	fl_blue   = new Array();
	fl_purple = new Array();
	fl_orange = new Array();
	fl_red    = new Array();
	fl_other  = new Array();

	tr_all    = new Array();
	tr_spring = new Array();
	tr_summer = new Array();
	tr_autumn = new Array();
	tr_winter = new Array();

	ln_all           = new Array();
	ln_sunrise       = new Array();
	ln_sunset        = new Array();
	ln_sea           = new Array();
	ln_wales         = new Array();
	ln_canada        = new Array();
	ln_cornwall      = new Array();
	ln_lake_district = new Array();
	ln_other         = new Array();

	mi_all        = new Array();
	mi_mono       = new Array();
	mi_birds      = new Array();
	mi_snow       = new Array();
	mi_buildings  = new Array();

	// Go through each of the child nodes	
	for ( var i=0; i < xdoc.childNodes.length; i++)
	{
		var curElement = xdoc.childNodes[i];
		// Only interested in "im" elements (FireFox seems to count whitespace as an element, so have to ignore it)
		if ( curElement.tagName == "im" )
		{
			// Create a new image element
			imageList[ curImage ] = new imageElement();

			// Iterate over the collection of attributes
			for ( var j = 0 ; j < curElement.attributes.length; j++ )
			{
				var curAttr = curElement.attributes[j];
				processAttributes ( curAttr.nodeName, curAttr.nodeValue );
				//alert ( curElement.attributes[j].nodeName + " = " + curElement.attributes[j].nodeValue );
			}

			// Set the image description.  Different browser writers use different attributes for the same thing. 
			if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
			{ //IE
				imageList[ curImage ].description = curElement.text;
			}
			else
			{ // Everything else
				imageList[ curImage ].description = curElement.textContent;
			}

			// If this is a 'large' image, then add it to the list of portrait or landscape images to be selected at random for the home and about pages
			if ( imageList[ curImage ].size == "L" )
			{
				if ( imageList[ curImage ].orientation == "P" )
					portraitImages[ portraitImages.length ] = curImage ;
				if ( imageList[ curImage ].orientation == "L" )
					landscapeImages[ landscapeImages.length ] = curImage ;
			}

			// Next image
			curImage++;
			//alert (xdoc.childNodes[i].tagName);
		}

	}

	// Set up the home page display
	xmlLoaded();
	resize();
	moveThumb ( MENU_HOME );

	// Get any URL parameters, and turn them into an array
	params = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	
	// We only accept a single parameter of the form iid=xxxxx
	if ( params.length ==  1 )
	{
		imageId = params[ 0 ].split( "=" );

		// If we have an image ID, and it looks valid, then 
		if ( ( imageId[ 1 ] > 0 ) && ( imageIndex[ imageId[ 1 ] ] >= 0 ) )
		{
			moveThumb ( MENU_LATEST ) ;
			lmLatest ( LM_LAT_ALL, true );
			displayImage ( imageIndex[ imageId[ 1 ] ] ) ;
		}
	}
} 


function imageElement()
{
	this.id = "";
	this.name = "";
	this.year="";
	this.orientation = "";
	this.size = "";
	this.title = "";
	this.favourite = false;
}

// Process the attributes for each image
function processAttributes ( attrName, attrValue )
{
	var isFlower    = false;
	var isTree      = false;
	var isLandscape = false;
	var isMisc      = false;

	switch ( attrName )
	{
		case "id":
			imageList[ curImage ].id = attrValue;

			// We also index all images by ID. These should be unique ...
			var t = parseFloat( attrValue );
			if ( typeof ( imageIndex[ t ] ) != "undefined" )
			{
				alert ( "Duplicate Image ID found: " + attrValue );
			}
			else
			{
				imageIndex[ t ] = curImage;
			}
			break ;
		case "na":
			imageList[ curImage ].name = attrValue;
			break ;
		case "yr":
			imageList[ curImage ].year = attrValue;
			if ( attrValue == THIS_YEAR )
			{
				la_thisyear[ la_thisyear.length ] = curImage ;
			}
			else if ( attrValue == LAST_YEAR )
			{
				la_lastyear[ la_lastyear.length ] = curImage ;
			}
			else
			{
				la_older[ la_older.length ] = curImage ;
			}
			break ;
		case "or":
			imageList[ curImage ].orientation = attrValue;
			break ;
		case "sz":
			imageList[ curImage ].size = attrValue;
			break ;
		case "tt":
			imageList[ curImage ].title = attrValue;
			break ;
		case "fv":
			imageList[ curImage ].favourite = true;
			la_favourite[ la_favourite.length ] = curImage ;
			break ;
		case "si": // similar images
			break ;

		// Flower categories
		case "fl-pi":
			isFlower = true;
			fl_pink[ fl_pink.length ] = curImage;
			break ;
		case "fl-ye":
			isFlower = true;
			fl_yellow[ fl_yellow.length ] = curImage;
			break ;
		case "fl-wh":
			isFlower = true;
			fl_white[ fl_white.length ] = curImage;
			break ;
		case "fl-bl":
			isFlower = true;
			fl_blue[ fl_blue.length ] = curImage;
			break ;
		case "fl-pu":
			isFlower = true;
			fl_purple[ fl_purple.length ] = curImage;
			break ;
		case "fl-or":
			isFlower = true;
			fl_orange[ fl_orange.length ] = curImage;
			break ;
		case "fl-re":
			isFlower = true;
			fl_red[ fl_red.length ] = curImage;
			break ;
		case "fl-ot":
			isFlower = true;
			fl_other[ fl_other.length ] = curImage;
			break ;

		// Tree categories
		case "tr-sp":
			isTree = true;
			tr_spring[ tr_spring.length ] = curImage;
			break ;
		case "tr-su":
			isTree = true;
			tr_summer[ tr_summer.length ] = curImage;
			break ;
		case "tr-au":
			isTree = true;
			tr_autumn[ tr_autumn.length ] = curImage;
			break ;
		case "tr-wi":
			isTree = true;
			tr_winter[ tr_winter.length ] = curImage;
			break ;

		// Landscape categories
		case "ln-sr":
			isLandscape = true;
			ln_sunrise[ ln_sunrise.length ] = curImage;
			break ;
		case "ln-ss":
			isLandscape = true;
			ln_sunset[ ln_sunset.length ] = curImage;
			break ;
		case "ln-se":
			isLandscape = true;
			ln_sea[ ln_sea.length ] = curImage;
			break ;
		case "ln-wa":
			isLandscape = true;
			ln_wales[ ln_wales.length ] = curImage;
			break ;
		case "ln-ca":
			isLandscape = true;
			ln_canada[ ln_canada.length ] = curImage;
			break ;
		case "ln-co":
			isLandscape = true;
			ln_cornwall[ ln_cornwall.length ] = curImage;
			break ;
		case "ln-ld":
			isLandscape = true;
			ln_lake_district[ ln_lake_district.length ] = curImage;
			break ;
		case "ln-ot":
			isLandscape = true;
			ln_other[ ln_other.length ] = curImage;
			break ;

		// Misc categories
		case "mi-mo":
			isMisc = true;
			mi_mono[ mi_mono.length ] = curImage;
			break ;
		case "mi-ba":
			isMisc = true;
			mi_birds[ mi_birds.length ] = curImage;
			break ;
		case "mi-sf":
			isMisc = true;
			mi_snow[ mi_snow.length ] = curImage;
			break ;
		case "mi-bu":
			isMisc = true;
			mi_buildings[ mi_buildings.length ] = curImage;
			break ;

		default:
			alert ( "Unknown attribute '" + attrName + "' with value '" + attrValue + "'" ) ;
			return;
	}

	// Deal with the 'all' indices
	if  ( ( la_all.length == 0 ) || ( la_all[ la_all.length - 1 ] != curImage ) )
	{
		la_all[ la_all.length ] = curImage ;

		// Only store the last 'RECENT_SIZE' images in recent list
		if ( curImage < RECENT_SIZE )
		{
			la_recent[ la_recent.length ] = curImage ;
		}
	}

	if  ( isFlower && ( ( fl_all.length == 0 ) || ( fl_all[ fl_all.length - 1 ] != curImage ) ) )
	{
		fl_all[ fl_all.length ] = curImage ;
	}

	if  ( isTree && ( ( tr_all.length == 0 ) || ( tr_all[ tr_all.length - 1 ] != curImage ) ) )
	{
		tr_all[ tr_all.length ] = curImage ;
	}

	if  ( isLandscape && ( ( ln_all.length == 0 ) || ( ln_all[ ln_all.length - 1 ] != curImage ) ) )
	{
		ln_all[ ln_all.length ] = curImage ;
	}

	if  ( isMisc && ( ( mi_all.length == 0 ) || ( mi_all[ mi_all.length - 1 ] != curImage ) ) )
	{
		mi_all[ mi_all.length ] = curImage ;
	}
}

