// Generic menu collapsing/expanding function
// Collapses the first span in the parrent element
// Changes the first immage in the parrent element
function Collapse(oThis){
 
	if (oThis.parentNode.getElementsByTagName('span')[0].style.display != ''){
		oThis.parentNode.getElementsByTagName('span')[0].style.display = '';
		oThis.parentNode.getElementsByTagName('img')[0].src = "media/icon_down"+sSuffix+".png";
	}
	else {
	 	oThis.parentNode.getElementsByTagName('span')[0].style.display = 'none';
		oThis.parentNode.getElementsByTagName('img')[0].src = "media/icon_right"+sSuffix+".png";
	}
}

// Function triggered when an image link is clicked
// Reveals the image viewer pane and displays a loading message
// Function fetches image data via AJAX and sends it for use
function Show(id){
 
 	// Show the filter layer that dims the page behind the viewer
	oFilter = document.getElementById("layer_filter");
	oFilter.style.display = "block";
	
 	// Show the viewer and display a loading message
	document.getElementById("layer_viewer").style.display = "block";
	document.getElementById("viewer_pic").src = "media/loading"+sSuffix+".gif";

	// Establish the request object
	var oRequest;
				
	// Opera 8.0+, Firefox, Safari
	try{oRequest = new XMLHttpRequest();}

	// Internet Explorer Browsers
	catch (e){
		try{oRequest = new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e) {oRequest = new ActiveXObject("Microsoft.XMLHTTP");}
	}
	
	// Set the event handler for data recieved
	oRequest.onreadystatechange = function(){
		if(oRequest.readyState == 4){
		 
		 	// Send the fetched data to the displayer function
			SetViewerSource(oRequest.responseText);

		}
	}
	
	// Send the request to ther server
	oRequest.open("GET", "viewer.php?image=" + id, true);
	oRequest.send(null);
	
}

// Changes the image in the viewer to the fetched file
// Configures other viewer paramaters as needed
// Called when AJAX data is successfully downloaded
function SetViewerSource(data){

	// Get the necesary viewer elements for processing
	oPic = document.getElementById("viewer_pic");
	oViewer = document.getElementById("viewer");
	oLable = document.getElementById("viewer_lable");

	// Parse the parameters from the data
	var aParams = data.split(",");

	// Set the viewer variables to the fetched values
	oPic.src = aParams[0];
	oPic.style.height = aParams[1];
	oPic.style.width = aParams[2];
	oViewer.style.height = parseInt(aParams[1])+2;
	oViewer.style.width = parseInt(aParams[2])+28;
	oLable.innerHTML = aParams[3];
		
}

// Function to re-hide the enlarged image popup
// Image link action on the viewer block and close text
function Hide(){
		
	document.getElementById("layer_viewer").style.display = 'none';
	document.getElementById("layer_filter").style.display = 'none';
		
}
