var isIE = false;
var req;
var images = new Array;
var selectedIndex = 0;
var mouseX;

function loadXMLDoc(url) {
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	} else if (window.ActiveXObject) {
		isIE = true;
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function processReqChange() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			processImages();
		} else {
			alert("There was a problem retrieving the XML data:\n" +
			req.statusText);
		}
	}
}

function gallery() {
	loadXMLDoc('gallery.xml');
}

function processImages(){
	var items = req.responseXML.getElementsByTagName("newsitem");
	for (var i=0; i<items.length; i++) {		
		var timestamp = items[i].getElementsByTagName("timestamp");
		timestamp = timestamp[0].firstChild.nodeValue;
		var datestamp = items[i].getElementsByTagName("datestamp");
		datestamp = datestamp[0].firstChild.nodeValue;
		var imgsrc = items[i].getElementsByTagName("imgsrc");
		imgsrc = imgsrc[0].firstChild.nodeValue;
		var imgwidth = items[i].getElementsByTagName("imgwidth");
		imgwidth = imgwidth[0].firstChild.nodeValue;
		var imgheight = items[i].getElementsByTagName("imgheight");
		imgheight = imgheight[0].firstChild.nodeValue;
		var thumbsrc = items[i].getElementsByTagName("thumbsrc");
		thumbsrc = thumbsrc[0].firstChild.nodeValue;
		var headline = items[i].getElementsByTagName("headline");
		headline = headline[0].firstChild.nodeValue;
		var image = new Array(imgsrc,imgwidth,imgheight,timestamp,datestamp,thumbsrc,headline);
		images[images.length] = image;
	}
	prepareThumbs();
}

function prepareThumbs() {
	var thegallery = document.getElementById("gallery");
	var thumbLI = thegallery.getElementsByTagName('LI');
	for (var i=0; i<thumbLI.length; i++) {
		thumbLI[i].varnum = i;
		thumbLI[i].onclick = function () {
			var thumbsrc = this.firstChild.firstChild.getAttribute('src');
			var underscore = thumbsrc.indexOf('-');
			var lastslash = thumbsrc.indexOf('img/');
			var thumbid =  thumbsrc.substring((lastslash+4),underscore);
			selectedIndex = this.varnum;
			swapImage();
			return false;
		}
	}
}

function getReference(thumbsrc) {
	for (var i=0; i<images.length; i++) {
		if(images[i][5].indexOf(thumbsrc) != -1) {
			return i;
		}
	}
	return false;
}

function swapImage() {

	var bigimage = document.getElementById('mainimage');
	setOpacity(bigimage,0);
	var fred = images[selectedIndex][0]
	bigimage.setAttribute('src','img/' +fred );
	bigimage.setAttribute('width',images[selectedIndex][1]);
	bigimage.setAttribute('height',images[selectedIndex][2]);
	bigimage.setAttribute('alt',images[selectedIndex][6]);
	document.getElementById('galleryimage').className = 'disc';

	//alert('0');
	
	bigimage.onload = function () {
		//kill the disc here
		document.getElementById('galleryimage').className = 'nodisc';
		fadeIn('mainimage',0);
	};

	//var headers = document.getElementsByTagName('H3');
	//headers[0].removeChild(headers[0].firstChild);
	//var newheader = document.createTextNode(images[selectedIndex][6])
	//headers[0].appendChild(newheader);
	//var pubdate = document.getElementById('publishdate');
	//pubdate.removeChild(pubdate.firstChild);
	//var newpubdate = document.createTextNode('Published: ' + images[selectedIndex][3] + ', ' + images[selectedIndex][4])
	//pubdate.appendChild(newpubdate);
	
	var thegallery = document.getElementById("gallery");
	var thumbs = thegallery.getElementsByTagName('LI');
	for (var i=0; i<thumbs.length; i++) {
		if(thumbs[i].firstChild.className == 'selected') {
			thumbs[i].firstChild.className = '';
		}
	}
	for (var i=0; i<thumbs.length; i++) {
		if(i == selectedIndex) {
			thumbs[i].firstChild.className = 'selected';
		}
	}
}

function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
	return true;
}

addLoadEvent(gallery);

