//Last updated 3/12/2008 by MW


//#################

//Make an http request to an xml list of event speakers.

//#################


var http_request = false;

//Request the xml from the provided url
//Send the result to parseContents()
function makeRequest(url) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		//Cannot create XMLHTTP instance
		return false;
	}
	
	http_request.onreadystatechange = parseContents;
	http_request.open('GET', url, true);
	http_request.send(null);
	
}


//Parse the contents of the xml file
//Send the results to displayContents()
function parseContents() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
		
			var xmldoc = http_request.responseXML;
			var root = xmldoc.getElementsByTagName('root')[0];
			var spkrList = new Array();
			
			//Load the spkrList array with the info from the xml document
			for (var i = 0; i < root.getElementsByTagName('speaker').length; i++) {
				var node = root.getElementsByTagName('speaker')[i];
				spkrList[i] = new Array();
				spkrList[i][0] = node.getAttribute('firstname');
				spkrList[i][1] = node.getAttribute('lastname');
				spkrList[i][2] = node.getAttribute('org');
				spkrList[i][3] = node.getAttribute('org_url');
				spkrList[i][4] = node.getAttribute('img');
				spkrList[i][5] = node.getAttribute('url');
			}
			
			//Randomize the array based on Fisher-Yates algorithm
			for (var j, x, i = spkrList.length; i; j = parseInt(Math.random() * i), x = spkrList[--i], spkrList[i] = spkrList[j], spkrList[j] = x);
			
		
			//Show the content
			showContents(spkrList);
			
		}
	}
}


function loadXML(url) {
	makeRequest(url);
}


//#################

//Annimate speaker photos

//#################


var perSet = 5;
var numSets; 
var curSet = 0;
var setWidth = 580;
var setHeight = 150;
var isSliding = false;

function slideRight(n) {
	if (!isSliding) {
		isSliding = true;
	
		var curId = "photo_set" + n;
		new Effect.Move(document.getElementById(curId), {x: setWidth, y: 0, duration: 0.7,  transition: Effect.Transitions.sinoidal});
		
		if (n == 0) {
			curSet = numSets - 1;
			nextId = "photo_set" + curSet;
		}
		else {
			curSet --;
			nextId = "photo_set" + curSet;
		}
		
		document.getElementById(nextId).style.left = "" + (-setWidth) + "px";
		new Effect.Move(document.getElementById(nextId), {x: setWidth, y: 0, duration: 0.7,  transition: Effect.Transitions.sinoidal, afterFinish: stopSliding});
	}
}


function slideLeft(n) {
	if (!isSliding) {
		isSliding = true;
	
		var curId = "photo_set" + n;
		new Effect.Move(document.getElementById(curId), {x: -setWidth, y: 0, duration: 0.7,  transition: Effect.Transitions.sinoidal});
		
		if (n == numSets - 1) {
			curSet = 0;
			nextId = "photo_set" + curSet;
		}
		else {
			curSet ++;
			nextId = "photo_set" + curSet;
		}
		
		document.getElementById(nextId).style.left = "" + setWidth + "px";
		new Effect.Move(document.getElementById(nextId), {x: -setWidth, y: 0, duration: 0.7,  transition: Effect.Transitions.sinoidal, afterFinish: stopSliding});
	}
}

function stopSliding() {
	isSliding = false;
}

function fadeOut(target) {
	target.style.opacity = .8;
	target.style.filter = 'alpha(opacity=' + 80 + ')';
}

function fadeIn(target) {
	target.style.opacity = 1;
	target.style.filter = 'alpha(opacity=' + 100 + ')';
}

function highlight(target) {
	target.style.color = "#fff";
}

function subdue(target) {
	target.style.color = "#fd873b";
}


		
function delayedReveal(target, d, numLeft) {
	
	target.style.opacity = 0;
	target.style.filter = 'alpha(opacity=' + 0 + ')';
	
	if (numLeft > 0) {
		new Effect.Opacity(target, {to: 1, duration: .2, delay: d, transition: Effect.Transitions.linear});
	}
	else  {
		new Effect.Opacity(target, {to: 1, duration: .2, delay: d, transition: Effect.Transitions.linear, afterFinish: showArrows});
	}
}

function showArrows(obj) {
	document.getElementById('photo_control_left').style.display = "inline";
	document.getElementById('photo_control_right').style.display = "inline";
}


function showContents(list) {
	if (document.getElementById('speakers')) {
		var objSpeakers = document.getElementById('speakers');
		var curSpkr = 0;
		
		numSets = Math.ceil(list.length / perSet);
		
		var numLeft = list.length;
		var d = 1.5;
		
		for (var i = 0; i < numSets; i++) {
				
			
			
			var photoSet = document.createElement("div");
			photoSet.setAttribute('id', 'photo_set' + i);
			photoSet.className = "photo_set";
			
			//Set the horizontal position to 0;
			photoSet.style.top = "" + (-i * setHeight) + "px";
			photoSet.style.left = "" + (i * setWidth) + "px";
			objSpeakers.appendChild(photoSet);
			
			var numInSet;
			if (i == numSets - 1) {
				numInSet = list.length - perSet * (numSets - 1);
			}
			else {
				numInSet = perSet;
			}
			
			
			for (var j = 0; j < numInSet; j++) {
				var objLink = document.createElement("div");
				objLink.className = "speaker_link";
				objLink.setAttribute('id', 'skr' + i + '' + j);
				photoSet.appendChild(objLink);
				
				numLeft -= 1;
				d += .1;
				delayedReveal(objLink, d, numLeft);
				
				
				var objTxtHolder = document.createElement("span");
				objTxtHolder.className = "text_holder";
				objLink.appendChild(objTxtHolder);
				
				var objSpeakerName = document.createElement("p");
				objSpeakerName.className = "spkr_name";
				objTxtHolder.appendChild(objSpeakerName);
				
				var objNameLink = document.createElement("a");
				objNameLink.setAttribute('href', list[curSpkr][5]);
				objSpeakerName.appendChild(objNameLink);
				
				var objT1 = document.createTextNode(list[curSpkr][0] + ' ' + list[curSpkr][1]);
				objNameLink.appendChild(objT1);
				
				if (list[curSpkr][2] != "") {
					var objOrgName = document.createElement("p");
					objOrgName.className = "org_name";
					objTxtHolder.appendChild(objOrgName);
					
					var objT2 = document.createTextNode(list[curSpkr][2]);
					objOrgName.appendChild(objT2);
					
					objTxtHolder.style.marginTop = "" + (65 - objTxtHolder.offsetHeight) + "px";
					objTxtHolder.style.paddingBottom = "10px";
				}
				else {
					objTxtHolder.style.marginTop = "" + (65 - objSpeakerName.offsetHeight) + "px";
					objTxtHolder.style.paddingBottom = "10px";
				}
				
				
				
				var objHolder = document.createElement("a");
				objHolder.setAttribute('href', list[curSpkr][5]);
				objHolder.className = "photo_holder";
				objLink.appendChild(objHolder);
			
				var objImage = document.createElement("img");
				objImage.setAttribute('id','photo' + i + '_' + j);
				objImage.className = "spkr_photo";
				objImage.setAttribute('src','http://en.oreilly.com' + list[curSpkr][4]);
				objHolder.appendChild(objImage);
				
				
				objTxtHolder.onmouseover = function() {fadeOut(this.nextSibling.firstChild); highlight(this.firstChild.firstChild);};
				objTxtHolder.onmouseout = function() {fadeIn(this.nextSibling.firstChild); subdue(this.firstChild.firstChild);};
				
				objHolder.onmouseover = function() {fadeOut(this.firstChild); highlight(this.previousSibling.firstChild.firstChild);};
				objHolder.onmouseout = function() {fadeIn(this.firstChild); subdue(this.previousSibling.firstChild.firstChild);};
				
				
				curSpkr ++;
				
			}
		}
		
		revealSpkrs(list);
	}
}

