function loadData(URL)
{
// Create the XML request
    xmlReq = null;
    if(window.XMLHttpRequest) xmlReq = new XMLHttpRequest();
    else if(window.ActiveXObject) xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
    if(xmlReq==null) return; // Failed to create the request

// Anonymous function to handle changed request states
    xmlReq.onreadystatechange = function()
    {
        switch(xmlReq.readyState)
        {
        case 0: // Uninitialized
            break;
        case 1: // Loading
            break;
        case 2: // Loaded
            break;
        case 3: // Interactive
            break;
        case 4: // Done!
        // Retrieve image data
			var xml = xmlReq.responseXML;
			var count = xml.getElementsByTagName('image').length;
			var random = Math.floor(Math.random()*count);
			var selImageItem = xml.getElementsByTagName('image')[random];
			var selImage = selImageItem.getAttribute('src');
			var scaling = selImageItem.getAttribute('scaling');
			extractXMP(selImage,scaling);
            break;
        default:
            break;
        }
    }
// Make the request
    xmlReq.open ('GET', URL, true);
    xmlReq.send (null);
}
//function returnData()
//{
//}

function extractXMP(selImage,scaling)
//retrieves selected image xmp file
{
// Create the XML request
    xmlReq2 = null;
    if (window.XMLHttpRequest){
		xmlReq2 = new XMLHttpRequest();
		xmlReq2.overrideMimeType("text/xml");
	} else if(window.ActiveXObject) {
		xmlReq2 = new ActiveXObject("Microsoft.XMLHTTP");
	}
    if(xmlReq2==null) return; // Failed to create the request

// Anonymous function to handle changed request states
    xmlReq2.onreadystatechange = function()
    {
        switch(xmlReq2.readyState)
        {
        case 0: // Uninitialized
            break;
        case 1: // Loading
            break;
        case 2: // Loaded
            break;
        case 3: // Interactive
            break;
        case 4: // Done!
        // Retrieve image data
			var xml2 = xmlReq2.responseXML;
			var filename = selImage.split('\.xmp');
			var extension = 'jpg';
			var imagefile = filename[0] + '\.' + extension;			
			var output = extractXMPDetails(xml2,scaling,imagefile);
			doSomethingWithData(output);
            break;
        default:
            break;
        }
    }

// Make the request
    xmlReq2.open ('GET', selImage, true);
    xmlReq2.send (null);
}

function extractXMPDetails(xml2,scaling,imagefile) {
//general purpose xmp details extraction
//uses separate function to write xhtml output

	var selImage2 = xml2.getElementsByTagNameNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "RDF")[0];
	var rdfDescription = selImage2.getElementsByTagNameNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "Description")[0];
	if (scaling=='auto'){
		var width = '100%';
	} else {
		var width = rdfDescription.getAttributeNS("http://ns.adobe.com/exif/1.0/", "PixelXDimension");
	}
	if (scaling=='auto'){
	} else {
		var height = rdfDescription.getAttributeNS("http://ns.adobe.com/exif/1.0/", "PixelYDimension");
	}
	if (rdfDescription.getAttributeNS("http://ns.adobe.com/exif/1.0/", "DateTimeOriginal")) {
		var date = rdfDescription.getAttributeNS("http://ns.adobe.com/exif/1.0/", "DateTimeOriginal").substring(0,10);
	}
	if (rdfDescription.getAttributeNS("http://ns.adobe.com/photoshop/1.0/", "City")) {
		var city = rdfDescription.getAttributeNS("http://ns.adobe.com/photoshop/1.0/", "City");
	}
	//var state = rdfDescription.getAttribute('photoshop:State');
	if (rdfDescription.getAttributeNS("http://ns.adobe.com/photoshop/1.0/", "Country")) {
		var country = rdfDescription.getAttributeNS("http://ns.adobe.com/photoshop/1.0/", "Country");
	}
	if (selImage2.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/", "description")[0]) {
		var dcDescription = selImage2.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/", "description")[0];
		var dcDescriptionValue = dcDescription.getElementsByTagNameNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "li")[0].firstChild.data;
	}
	if (selImage2.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/", "creator")[0]) {
		var creator = selImage2.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/", "creator")[0];
		var creatorValue = creator.getElementsByTagNameNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "li")[0].firstChild.data;
	}
	if (selImage2.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/", "rights")[0]) {
		var rights = selImage2.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/", "rights")[0];
		var rightsValue = rights.getElementsByTagNameNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "li")[0].firstChild.data;
	}
	if (rdfDescription.getAttributeNS("http://ns.adobe.com/photoshop/1.0/", "Source")){
		if (rdfDescription.getAttributeNS("http://ns.adobe.com/photoshop/1.0/", "Source").substring(0,7)=='http://'){
			var sourceLink = 'Source: <a href="' + rdfDescription.getAttributeNS("http://ns.adobe.com/photoshop/1.0/", "Source") + '">';
			var sourceLink = sourceLink + rdfDescription.getAttributeNS("http://ns.adobe.com/photoshop/1.0/", "Source");
			var sourceLink = sourceLink + '</a>\. ';
		}
		var sourceString = 'Source: ' + rdfDescription.getAttributeNS("http://ns.adobe.com/photoshop/1.0/", "Source") + '\. ';
	} else {
		var sourceString = '';
		var sourceLink = '';
	}		
	if (dcDescriptionValue){
		var description = dcDescriptionValue;
		if (dcDescriptionValue.substr(dcDescriptionValue.length-1,1)!='\.'){
			var description = description + '\. ';
		} else {
			var description = description + ' ';
		}
		var descriptionString = description;
	} else {
		var descriptionString = '';
	}			
	if (city||country){
		if (city){
			var location = city + ', ' + country;
		} else {
			var location = country;
		}
		var locationQuery = ' (<a href="http://maps.google.com/maps?f=d&amp;source=s_d&amp;saddr="' + location + '">Google Maps</a>)';
		var locationString = location + '\. ';
		var locationQuery = locationString + locationQuery + '\. ';
	} else {
		var locationString = '';
		var locationQuery = '';
	}
	if (rightsValue||creatorValue){
		if (rightsValue){
			if (rightsValue == 'Wikimedia Commons'||rightsValue == 'Creative Commons'||rightsValue == 'Public Domain'||rightsValue.substr(0,16) == 'Creative Commons'){
				var rightsString = ' Licenced under ' + rightsValue;
				if (rightsString.substr(rightsString.length-1,1)!='\.'){
					var rightsString = rightsString + '\.';
				}
			} else if (rightsValue.substring(0,3)=='(c)'){
				var rightsString = rightsValue;
				if (rightsString.substr(rightsString.length-1,1)!='\.'){
					var rightsString = rightsString + '\.';
				}				
			} else {
				var rightsString = ' (c) Copyright by ' + rightsValue;
				if (rightsValue.substr(rightsValue.length-1,1)!='\.'){
					var rightsString = rightsString + '\.';
				}						
			}	
		} else if (creatorValue){
			var rightsString = ' (c) Copyright by ' + creatorValue;
			if (creatorValue.substr(creatorValue.length-1,1)!='\.'){
				var rightsString = rightsString + '\.';
			}					
		}
	} else {
		var rightsString = '';
	}

	var altText = '';
	var caption = '';
	if (descriptionString){
		var altText = descriptionString;
		var caption = descriptionString;
	}
	if (sourceString){
		var altText = altText + sourceString;
		if (sourceLink){
			var caption = caption + sourceLink;
		} else {
			var caption = caption + sourceString;
		}
	}
	if (creatorValue){
		var altText = altText + 'Shot by ' + creatorValue;
		var caption = caption + 'Shot by ' + creatorValue;
	}
	if (date){
		var altText = altText + ' on ' + date;
		var caption = caption + ' on <span class="vevent"><span class="dtstart">' + date + '</span><span class="summary" style="display: none">Photo taken</span></span>';
	}			
	if (locationString){
		var altText = altText + ' in ' + locationString;
		var caption = caption + ' in ' + locationQuery;			
	}
	if (rightsString){
		var altText = altText + rightsString;
		var caption = caption + rightsString;				
	}
			
	var output = writeOutput(scaling,imagefile,width,height,altText,caption);
	return output;
}

function writeOutput(scaling,imagefile,width,height,altText,caption)
//writes the xhtml output
{
	var output = '<div style="';
	if (scaling=='auto'){
		var output = output + 'width="100%">';
	} 
	var output = output + '<img src="' + imagefile + '" width="' + width + '" height="' + height + '"';
	if (altText){
		var output = output + ' alt="' + altText + '"';
	}
	var output = output + '/>';
	if (caption){
		var output = output + '<div class="caption">' + caption + '</div>';
	}
	var output = output + '</div>';
	return output;
}