/**
 * imageView.js
 * This file manages the image view window in the web page
 * code contained within get metadata from the image collection
 * and compares with location metadata.
 *
 * @author Ross
 */

	// Set default image index and container to hold currently selected image
	// var index = location.search.substr(location.search.indexOf("=")+1);
	var index = 100;
	var newImage = new Image(1536,1024);

    // Array to hold image and location repository
    var xmlDoc;

    //Load XML repository and cluster listings
    importRepository();

	/**
	 * Create and XMLLoad-er to handle the repository
	 * as well as a pointer to the XML it holds
	 */
	function importRepository()
	{
    	xmlDocLoader = new XMLLoad("XML/eXpOutNewGISClusters.xml", init);
    	xmlDoc = xmlDocLoader.getXML();
    }

	/**
	 * Initialize the image conatiner in the web page to a selected image
	 */
	function init(indexPointer)
	{
		currentlySelectedImage = indexPointer;
	
	    index = indexPointer;
		if (document.images) {
			newImage.onload = show_picture;
			newImage.src = xmlDoc.getElementsByTagName('image').item(index).getAttribute('imageFile');  
//			newImage.src = "http://ecologylab.cs.tamu.edu/zmicss/" + xmlDoc.getElementsByTagName('image').item(index).getAttribute('imageFile');  
//			alert(newImage.src);
		}
	}

	/**
	 * Swap out image in image container then call metadata setter
	 */
	function show_picture()
	{
		if (document.images) {
			document['eXpImage'].src = newImage.src;
		}
		set_metadata();
	}

	/**
	 * Sets the image on the web page to call ecologylabPopup
	 */
	function pop_picture()
	{
	  	if (document.images) {
			ecologylabPopup(newImage.src);
		}
	}

	/**
	 * Sets image metadata container with appropriate information
	 * from the respository
	 */
	function set_metadata()
	{
		var currentImage = xmlDoc.getElementsByTagName('image').item(index);
		var currentLocation = xmlDoc.getElementsByTagName('location').item(index);
		var time = currentImage.getAttribute('time');
		var parsedTime = new Date();
    
		parsedTime.setTime(time);
	
	    var zipcode = currentLocation.getAttribute('zipcode');
		// alert('This is my zip code: '+zipcode);
		/*
		document.metadata['latitude'].value=currentLocation.getAttribute('latitudeDegree');
		document.metadata['longitude'].value=currentLocation.getAttribute('longitudeDegree');
		document.metadata['altitude'].value=currentLocation.getAttribute('altitude');
		// document.metadata['neighborhood'].value=currentLocation.getAttribute('neighborhood');
		document.metadata['neighborhood'].value=zipcodesToNeighborhoods[zipcode];
		document.metadata['description'].value=currentImage.getAttribute('description');
		document.metadata['date'].value=parsedTime.toLocaleString();
		// .rightColumn.metadata
		*/
		document.getElementById('latitude').innerHTML = currentLocation.getAttribute('latitudeDegree');
		document.getElementById('longitude').innerHTML = currentLocation.getAttribute('longitudeDegree');
		document.getElementById('altitude').innerHTML = currentLocation.getAttribute('altitude');
		document.getElementById('neighborhood').innerHTML = zipcodesToNeighborhoods[zipcode];
		document.getElementById('date').innerHTML = parsedTime.toLocaleString();		
		document.metadata['description'].value=currentImage.getAttribute('description');
	}

