function GImage() {
	this.src;
	this.lowsrc;
	this.id;
	this.title;
	this.link;
	this.linkTtl;
	this.medium;
	this.desc;
	this.year;
}

function loadGallery(url) {
	//call the right constructor for the browser being used
	if (window.ActiveXObject)
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		xhr = new XMLHttpRequest();
	else
		alert("not supported");

	//prepare the xmlhttprequest object
	xhr.open("GET",url,true);
	xhr.setRequestHeader("Cache-Control", "no-cache");
	xhr.setRequestHeader("Pragma", "no-cache");
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				if (xhr.responseText != null) {
					process(xhr.responseXML);
				}
				else {
					alert("Failed to receive Gallery file from the server - file not found.");
					return false;
				}
			}
			else
				alert("Error code " + xhr.status + " received: " + xhr.statusText);
		}
	}

	//send the request
	xhr.send(null);
}

function dispGallery() {
	var wrp = document.getElementById("glWrp");
	var isMobile = detect();
	wrp.innerHTML = "";

	// Images are displayed newest to oldest (newest have higher id's, so list is traversed backwards).
	for( i=(imgCnt); i > 0; i--) {
		var gImg = imgLst[i];

		//var tmp = new Image();
		//tmp.src = gImg.src;
		wrp.innerHTML += "<div class=\"glThmb\" id=\"id" + gImg.id + "\" title=\"" + gImg.title + "\" style=\"background-image: url('" + gImg.lowsrc + "');\"><div id=\"cvr" + gImg.id + "\" class=\"glThmbCvr\" onmouseover=\"cvrOver(this.id)\" onmouseout=\"cvrOut(this.id)\" onclick=\"displayImg('" + i + "', false);\">&nbsp;</div></div>";
	}

	if(currImg != null && currImg != "") displayImg(currImg, false);
	else displayImg(imgCnt, false);
}

function process(xml)
{
	var items = xml.getElementsByTagName("image");
	var properties = new Array("id", "title", "link", "medium", "desc", "year");
	imgCnt = items.length;

	for(i=0; i<items.length; i++) {
		var tmp = items[i];
		var gImg = new GImage();

		gImg.src= tmp.getAttribute("src");
		gImg.lowsrc = tmp.getAttribute("lowsrc");

		var tmpEl = null;
		for (var j=0; j<properties.length; j++) {
			tmpEl = tmp.getElementsByTagName(properties[j])[0];
			if (tmpEl!= null) {
				eval("gImg."+properties[j]+"=tmpEl.childNodes[0].nodeValue");

				if(properties[j] == "link") {
					gImg.linkTtl = tmpEl.getAttribute("title");
				}
			}
		}

		imgLst[gImg.id] = gImg;
		tmp = new Image();
		tmp.src = gImg.lowsrc;
		picLst[i] = tmp;
	}

	eval(dispExec);
}

function loadG() {
	loadGallery("/xml/illustrations.xml");
}

var xhr, imgLst, picLst, imgCnt;
var args = getArgs();
imgLst = new Array();
picLst = new Array();
var dispExec = "dispGallery();";
var currImg = args.i;

var root = "http://www.scottmhallett.com/illustration/";

function cvrOver(id) {
	var outerId = id.replace("cvr","id");
	var el = document.getElementById(outerId);

	opacity(id,40,0,500);
	el.style.borderColor = "#AAA";

	var i = id.replace("cvr","");

}

function cvrOut(id) {
	var outerId = id.replace("cvr","id");
	var el = document.getElementById(outerId);

	opacity(id,0,40,500);
	el.style.borderColor = "#FFF";

}

function displayImg(id, fadeIn)
{
	var el = imgLst[id];

	var disp = document.getElementById("disp");
	var dispImg = document.getElementById("dispImg");
	var dispTtl = document.getElementById("dispTtl");
	var dispMnu = document.getElementById("dispMnu");
	var dispMed = document.getElementById("dispMed");
	var dispLnk = document.getElementById("dispLnk");
	var dispDsc = document.getElementById("dispDsc");

	dispImg.innerHTML = "<img src=\"" + el.src + "\" style=\"height: expression(this.height>600?600:true); max-height: 600px;\" id=\"img" + el.id + "\" alt=\"" + el.title + "\" title=\"" + el.title + "\" />";
	dispTtl.innerHTML = el.title;
	dispMed.innerHTML = el.medium;

	dispDsc.innerHTML = (el.desc != "-")?el.desc:"";
	dispDsc.innerHTML += "<br /><br /><a href=\"" + root + "index.htm?i=" + id + "\" title=\"Permanent Link\">Permanent Link</a>";
	dispDsc.innerHTML += (el.link != "-")?" | <a href=\"" + el.link + "\" title=\"" + el.linkTtl + "\" target=\"_blank\">" + el.linkTtl + "</a>":"";

	if( el.desc == "-" ) dispDsc.style.display = "none";
	else dispDsc.style.display = "block";

	var ms = (fadeIn)?1000:0;

	opacity("disp",0,100,ms);
}

function GetWidth() {
	var x = 0;
	if (self.innerHeight) {
		x = self.innerWidth;
	}
 	else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientWidth;
 	}
	else if (document.body) {
		x = document.body.clientWidth;
	}
 	return x;
}

function GetHeight() {
	var y = 0;
	if (self.innerHeight) {
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		y = document.documentElement.clientHeight;
	}
	else if (document.body) {
 		y = document.body.clientHeight;
	}
	return y;
}

/* Opacity functions */
function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}
