
var req;

//main image

function retrieveImage(image, imageId, domainId, maxWidth, maxHeight) {
	toggleOpacity(image, 20);	//fadeOut('mi', 100);
	var t = document.getElementById("photoTitle");
	t.innerHTML = "loading image . . ."
	
	var oldImage = document.getElementById("mi");
	oldImage.style.visibility = 'hidden'; 

	var url = "/retrieveImage?sel=" + encodeURIComponent(imageId) + "&d=" + encodeURIComponent(domainId) + 
			"&maxWidth=" + encodeURIComponent(maxWidth) + "&maxHeight=" + encodeURIComponent(maxHeight) +
			"&dummy=" + new Date().getTime();
   if (typeof XMLHttpRequest != "undefined") {
       req = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   req.open("GET", url, true);
   req.onreadystatechange = imageXML;
   req.send(null);
}

function imageXML() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            parseImageData();// update the HTML DOM based on whether or not message is valid
        }
    }
}

function parseImageData() {
	var id, source, domainId, height, width, title, notes = "";
	if(req.responseXML == null) return;
	if(req.responseXML.getElementsByTagName("id")[0] != null)
		id = req.responseXML.getElementsByTagName("id")[0].childNodes[0].nodeValue;
	if(req.responseXML.getElementsByTagName("source")[0] != null)
 		source = req.responseXML.getElementsByTagName("source")[0].childNodes[0].nodeValue;
	if(req.responseXML.getElementsByTagName("domainId")[0] != null)
 		domainId = req.responseXML.getElementsByTagName("domainId")[0].childNodes[0].nodeValue;
	if(req.responseXML.getElementsByTagName("height")[0] != null)
 		height = req.responseXML.getElementsByTagName("height")[0].childNodes[0].nodeValue;
	if(req.responseXML.getElementsByTagName("width")[0] != null)
 		width = req.responseXML.getElementsByTagName("width")[0].childNodes[0].nodeValue;
	if(req.responseXML.getElementsByTagName("title")[0] != null)  
 		title = req.responseXML.getElementsByTagName("title")[0].childNodes[0].nodeValue;
	if(req.responseXML.getElementsByTagName("notes")[0] != null)
 		notes = req.responseXML.getElementsByTagName("notes")[0].childNodes[0].nodeValue;
	if(title == "no title")
		title = "";
	if(notes == "no notes")
		notes = "";

	updateImageSpace(id, source, domainId, height, width, title, notes);
}

function updateImageSpace(imageId, imageSource, domainId, height, width, title, notes) {
    var mdiv = document.getElementById("mi");
	mdiv.src = "";
	var mainImageSpace = document.getElementById("mainImageSpace");
	mdiv.onload = function() { makeVisible(mdiv, imageId, title, notes);}
	mdiv.src = imageSource;
	mdiv.height = height;
	mdiv.width = width;
	mainImageSpace.height = height;
	mainImageSpace.width = width;
}

// image display functions

function makeVisible(i, thumbId, title, notes, thumb) {

	i.style.visibility = 'visible'; 
	fadeIn('mi', 0);
	fadeIn('t'+thumbId, 0);

	var photoTitle = document.getElementById("photoTitle");
	photoTitle.innerHTML = title;

	var photoNotes = document.getElementById("photoNotes");
	photoNotes.innerHTML = notes;

   toggleOpacity(image, 100);	
}

// albums

function selectAlbum(a, name, albumId, domainId) {

	var url = "/retrieveAlbum?albumId=" + encodeURIComponent(albumId) + "&domainId=" + encodeURIComponent(domainId);
	if (typeof XMLHttpRequest != "undefined") {
       req = new XMLHttpRequest();
	   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   req.open("GET", url, true);
   req.onreadystatechange = albumXML;
   req.send(null);
}

function albumXML(a, name, albumId, domainId) {

	if (req.readyState == 4) {
        if (req.status == 200) {
            parseAlbumData();// update the HTML DOM based on whether or not message is valid
        }
    }
}

function parseAlbumData() {
	var id = req.responseXML.getElementsByTagName("id")[0].childNodes[0].nodeValue;
	if(id != null && id == "-1") {
		//alert("same album, dude");
		return false;
	}
	var albumName = req.responseXML.getElementsByTagName("name")[0].childNodes[0].nodeValue;
	var t = document.getElementById("photoTitle");
	t.innerHTML = "loading images in " + albumName + "...";
	fadeOut('thumbs', 100);
	fadeOut('mi', 100);
//alert("album name: " + albumName);
	var thumbs = req.responseXML.getElementsByTagName("thumbs")[0].childNodes[0].nodeValue;
	//alert("thumbs: " + thumbs);
	var thumbList = req.responseXML.getElementsByTagName("thumbList")[0].childNodes[0].nodeValue;
	//alert("thumbList: " + thumbList);
	if(thumbs > 1 || thumbs == 0)
	t.innerHTML = "loading " + thumbs + " images in " + albumName + "...";
	else
	t.innerHTML = "loading " + thumbs + " image in " + albumName + "...";
}

function updateAlbum(imageId, imageSource, domainId, height, width, title, notes) {
    var mdiv = document.getElementById("mi");
	mdiv.src = "";
	var mainImageSpace = document.getElementById("mainImageSpace");
	mdiv.onload = function() { makeVisible(mdiv, imageId, title, notes);}
	mdiv.src = imageSource;
	mdiv.height = height;
	mdiv.width = width;
	mainImageSpace.height = height;
	mainImageSpace.width = width;
}
