/*
 * UpPhoto Image Gallery
 * Client-side stuff
 * Ronen Agranat 2008
 */
 
/**
 * Get element by id.
 */
function _gel(id) {
 	return document.getElementById(id);
}

/**
 * Show this photo in the big photo img
 */
function show(id/*, title, text*/) {
	if (id == null) {
		return;
	}
	//alert("Showing " + id);
	var bigPhoto = _gel("bigPhoto");
	var photo_display = _gel("photo_display");
	photo_display.src = "getPhoto.php?id=" + id;
	bigPhoto.style.display = "block";
	requestImage(id);
	jump = 0;
	loading();
}

function loading() {
	_gel("photo_title").innerHTML += " (loading...)";
	var loading = _gel("loading");
	loading.style.display = "none";
	var big = _gel("photo_display");
	//img.left = parseInt(parseInt(content.offsetWidth) - COVER_EXPANDED_SIZE)/2 + "px";
	var pos = findPos(big);
	//alert(pos[0] + " " + pos[1]);
	var x = pos[0];
	var y = pos[1];
	// $E.G. this is how to centre
	var SIZE = 128;
	var dx = (big.width - SIZE)/2;
	var dy = (big.height - SIZE)/2;
	loading.style.position = "absolute";
	loading.style.top = (y + dy) + "px"; // px is crucial for firefox
	loading.style.left =  (x + dx) + "px";
	loading.style.display = "block";
	//alert ((x + dx) + " " + (y + dy));
}

var jump = 0;

function jumpToBigImage() {
	if (++jump == 2) {
		location.href = "#big";
		_gel("loading").style.display = "none";
	}
}

function requestImage(id) {
	loadXMLDoc("image.php?id=" + id);
}

function imageLoaded() {
	jumpToBigImage();
}


/*
 * Javascript Bible 6ed AJAX
 */
var req = null;

// retrieve XML document as document object
function loadXMLDoc(url) {
   // branch for native XMLHttpRequest object
   if (window.XMLHttpRequest) {
      try {
         req = new XMLHttpRequest();
      } catch(e) {
         req = null;
      }
   // branch for IE/Windows ActiveX version
   } else if (window.ActiveXObject) {
      try {
         req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(e) {
         try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
         } catch(e) {
            req = null;
         }
      }
   }

   if (req) {
      req.open("GET", url, true);
      req.onreadystatechange = processTitleText;
      req.setRequestHeader("Content-Type", "text/xml");
      req.send("");
   }
}

/*
 * Photo title and text is ready. Display
 */
function processTitleText() {
   if (req.readyState == 4 && req.status == 200) {
      var xmlDoc = req.responseXML;
      if (xmlDoc != null) {
		var root_node = xmlDoc.getElementsByTagName('response').item(0);
		var title = root_node.firstChild.firstChild.data;
		var text = xmlDoc.getElementsByTagName('text').item(0).firstChild.data;
		var next = xmlDoc.getElementsByTagName('next').item(0).firstChild.data;
		var prev = xmlDoc.getElementsByTagName('prev').item(0).firstChild.data;

		_gel("photo_title").innerHTML = (title == "null") ? "Untitled" : title;
		_gel("photo_text").innerHTML = (text == "null") ? "No description." : text;
		_gel("prev_image").href = "javascript:show(" + prev + ")";
		_gel("next_image").href = "javascript:show(" + next + ")";
		var photo_display = _gel("photo_display");
		photo_display.style.cursor = "pointer";
		_gel("photo_display_link").href = "javascript:show(" + next + ")";
		
		jumpToBigImage();
      } else {
      	alert("Error retrieving XML");
      }
   }
}
/**
 * Find the absolute position of an object by traversing DOM and
 * accumulating the parent offsets.
 * http://www.quirksmode.org/js/findpos.html
 */

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		return [curleft,curtop];
	}
}
	