var xmlDoc, selectedProject, autoplayTimer, autoplayFlag, tID;
var so = new FlashObject("flvplayer.swf", "mymovie", "400", "300", "7", "#222222");

function setAutoplayButton(which)
{
	if (which == "off")
	{
		$('autoplayLink').title = 'Autoplay Off';
		$('autoplayImage').src = 'images/autoplay_off_gray.gif';
		$('autoplayImage').alt = 'Autoplay Off';
		$('autoplayImage').onmouseover = function () { this.src = 'images/autoplay_off_blue.gif'; return false; };
		$('autoplayImage').onmouseout  = function () { this.src = 'images/autoplay_off_gray.gif'; return false; };
	}
	else
	{
		$('autoplayLink').title = 'Autoplay On';
		$('autoplayImage').src = 'images/autoplay_on.gif';
		$('autoplayImage').alt = 'Autoplay On';
		$('autoplayImage').onmouseover = function () { this.src = 'images/autoplay_on_blue.gif'; return false; };
		$('autoplayImage').onmouseout  = function () { this.src = 'images/autoplay_on.gif'; return false; };	
	}
}

function undoShowVideo()
{
	new Effect.Fade('selectedProjectVideo', {duration:0.5});
	setTimeout("new Effect.Appear('selectedProjectContainer');", 1000);
}

function hideVideo()
{
	new Effect.Fade('selectedProjectVideo', {duration:0.5});
}

function startAutoplay()
{
	autoplayTimer = setTimeout('nextProject(); startAutoplay(); ',8000);
	autoplayFlag = "on";
	hideVideo();
}

function stopAutoplay()
{
	clearTimeout(autoplayTimer);
	autoplayFlag = "off";
}

function clickProject()
{
	stopAutoplay();
	setAutoplayButton("off");
	hideVideo();
	setSelectedProject(this.id);
}

function setSelectedProject(id)
{
	if (selectedProject != null)
		Element.removeClassName(selectedProject,'selected');

	selectedProject = id;
	Element.addClassName(selectedProject,'selected');
	new Effect.Fade('selectedProjectContainer', {duration:0.5});
	setTimeout("loadProject(selectedProject); new Effect.Appear('selectedProjectContainer');", 1000);
}

function showVideo()
{
	new Effect.Fade('selectedProjectContainer', {duration:0.5});
	setTimeout("new Effect.Appear('selectedProjectVideo');", 1000);
}

function loadProject(id)
{
	var x, anchor, i, j, found, proj, theEl;
	
	x = xmlDoc.getElementsByTagName('project');
	found=false;
	
	for (i=0; i<x.length; i++)
	{
		if (x[i].nodeType != 1) continue;
		if (x[i].getAttribute("id") == id)
		{
			found=true;
			proj=x[i];
			break;
		}
	}
	
	if (!found)
	{
		alert('Error: project not found');
		return;
	}
	
	theEl = $('selectedProjectTitle');
	if (proj.childNodes[0].hasChildNodes())
	{
		theEl.innerHTML = proj.childNodes[0].firstChild.nodeValue;
		Element.show('selectedProjectTitle');
	}
	else
		Element.hide('selectedProjectTitle');

	theEl = $('selectedProjectThumb');
	theEl.src = proj.childNodes[9].childNodes[2].firstChild.nodeValue;
	theEl.alt = proj.childNodes[0].firstChild.nodeValue;

	theEl = $('viewGallery');
	theEl.href = proj.childNodes[1].childNodes[0].childNodes[0].firstChild.nodeValue;
	theEl.title = proj.childNodes[1].childNodes[0].childNodes[1].firstChild.nodeValue;
	
	if (proj.childNodes[1].hasChildNodes())
	{
		// has other images
		theEl = $('otherImages');
		$('otherImages').innerHTML = '';
		for (j=1; j<proj.childNodes[1].childNodes.length; j++)
		{
			anchor = document.createElement('A');
			anchor.rel = 'lightbox[images]';
			anchor.href = proj.childNodes[1].childNodes[j].childNodes[0].firstChild.nodeValue;
			if (proj.childNodes[1].childNodes[j].childNodes[1].hasChildNodes())
			    anchor.title = proj.childNodes[1].childNodes[j].childNodes[1].firstChild.nodeValue;
			theEl.appendChild(anchor);
		}
		Element.show('projectRowImages');
	}
	else
		Element.hide('projectRowImages');


	theEl = $('projectType');
	if (proj.childNodes[2].hasChildNodes())
	{
		theEl.innerHTML = proj.childNodes[2].firstChild.nodeValue;
		Element.show('projectRowType');
	}
	else
		Element.hide('projectRowType');
	

	theEl = $('selectedProjectDescription');
	if (proj.childNodes[3].hasChildNodes())
	{
		theEl.innerHTML = proj.childNodes[3].firstChild.nodeValue;
		Element.show('selectedProjectDescription');
	}
	else
		Element.hide('selectedProjectDescription');

	theEl = $('link');
	if (proj.childNodes[4].hasChildNodes())
	{
		theEl.href = proj.childNodes[4].firstChild.nodeValue;
		theEl.title = proj.childNodes[4].firstChild.nodeValue;
		Element.show('projectRowLink');
	}
	else
		Element.hide('projectRowLink');

	theEl = $('sponsor');
	if (proj.childNodes[5].hasChildNodes())
	{
		theEl.innerHTML = proj.childNodes[5].firstChild.nodeValue;
		Element.show('projectRowSponsor');
	}
	else
		Element.hide('projectRowSponsor');

	theEl = $('courses');
	if (proj.childNodes[6].hasChildNodes())
	{
		theEl.innerHTML = proj.childNodes[6].firstChild.nodeValue;
		Element.show('projectRowCourses');
	}
	else
		Element.hide('projectRowCourses');

	theEl = $('team');
	if (proj.childNodes[7].hasChildNodes())
	{
		theEl.innerHTML = proj.childNodes[7].firstChild.nodeValue;
		Element.show('projectRowTeam');
	}
	else
		Element.hide('projectRowTeam');
		
	theEl = $('videoLink');
	if (proj.childNodes[8].hasChildNodes())
	{
	    so.addVariable("file", proj.childNodes[8].childNodes[0].firstChild.nodeValue);
//	    so.addVariable("data", "flvplayer.swf?file=" + proj.childNodes[8].childNodes[0].firstChild.nodeValue);
//	    so.addParam("movie", "flvplayer.swf?file=" + proj.childNodes[8].childNodes[0].firstChild.nodeValue);
	    
		if (proj.childNodes[8].childNodes[1].hasChildNodes())
		    $('videoCaption').innerHTML = proj.childNodes[8].childNodes[1].firstChild.nodeValue;
		else
		    $('videoCaption').innerHTML = '';
		Element.show('projectRowVideo');
	}
	else
		Element.hide('projectRowVideo');
}

function checkFilter(id)
{
	var proj = $(id);
	var projectType = $('projectTypeMenu').options[$('projectTypeMenu').selectedIndex].value;
	if (projectType == 'allProjects' || projectType == '' || Element.hasClassName(proj, projectType))
		return true;
	return false;
}

function firstProject()
{
	var first = $('projects').firstChild;
	while ((first.nodeType != 1) || (!checkFilter(first.id)))
		first = first.nextSibling;
	
	setSelectedProject(first.id);
}

function previousProject()
{
	var prev = $(selectedProject).previousSibling;
	
	if (prev == null)
	{
		lastProject();
		return;
	}
	

	while ((prev.nodeType != 1) || (!checkFilter(prev.id)))
	{
		if (prev == $('projects').firstChild)
		{
			lastProject();
			return;
		}
		
		prev = prev.previousSibling;
	}
	setSelectedProject(prev.id);
}

function toggleAutoplay()
{
	if ($('autoplayImage').src.indexOf('on') == -1)
	{
		// currently stopped		
		startAutoplay();
		setAutoplayButton("on");
		$('autoplayImage').src = 'images/autoplay_on_blue.gif';
		nextProject();
		
	}
	else
	{
		// currently autoplaying
		stopAutoplay();
		setAutoplayButton("off");
		$('autoplayImage').src = 'images/autoplay_off_blue.gif';
	}
}

function nextProject()
{
	var next = $(selectedProject).nextSibling;
	
	if (next == null)
	{
		firstProject();
		return;
	}
	
	while ((next.nodeType != 1) || (!checkFilter(next.id)))
	{
		if (next == $('projects').lastChild)
		{
			firstProject();
			return;
		}
		
		next = next.nextSibling;
	}
	setSelectedProject(next.id);
}

function lastProject()
{
	var last = $('projects').lastChild;
	while ((last.nodeType != 1) || (!checkFilter(last.id)))
		last = last.previousSibling;
	
	setSelectedProject(last.id);
}

function loadProjects()
{
	var x = xmlDoc.getElementsByTagName('project');
	var projects = $('projects');
	var i, j, k, proj, id, name, theClass;
	
	for (i=0; i<x.length; i++)
	{
		id = x[i].getAttribute("id");
		
		proj = document.createElement('IMG');
		proj.setAttribute('id', id);
		proj.setAttribute('onclick', clickProject);
		proj.onclick = clickProject;
			
		// set title and alt text to project name
	    name = x[i].childNodes[0].firstChild.nodeValue;
		proj.setAttribute('title', name);
		proj.setAttribute('alt', name);
			
		// set thumbnail image src
		proj.setAttribute('src', x[i].childNodes[9].childNodes[0].firstChild.nodeValue);
		proj.src = x[i].childNodes[9].childNodes[0].firstChild.nodeValue;
					
        // set class according to project type
		switch (x[i].childNodes[2].firstChild.nodeValue)
		{
			case 'Student group project':
		    	theClass = 'studentGroup';
				break;
							
			case 'Student individual project':
				theClass = 'studentIndividual';
				break;
						
			case 'Faculty research project':
			    theClass = 'faculty';
				break;
		}
		
		Element.addClassName(proj, theClass);

		projects.appendChild(proj);
		firstProject();
	}
}

function importXML()
{

	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = loadProjects;
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) loadProjects()
		};
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	xmlDoc.load("xml/projects.xml?random=" + Math.random());
}


function setUpControls()
{
	$('firstProjectImage').onmouseover = function () { this.src = 'images/control_start_blue.gif'; return false; };
	$('firstProjectImage').onmouseout  = function () { this.src = 'images/control_start.gif'; return false; };
	$('firstProjectImage').onclick     = function() { stopAutoplay(); setAutoplayButton("off"); hideVideo(); firstProject(); };
	
	$('previousProjectImage').onmouseover = function () { this.src = 'images/control_rewind_blue.gif'; return false; };
	$('previousProjectImage').onmouseout  = function () { this.src = 'images/control_rewind.gif'; return false; };
	$('previousProjectImage').onclick     = function() { stopAutoplay(); setAutoplayButton("off");  hideVideo(); previousProject(); };
	
	setAutoplayButton("off");
	stopAutoplay();
	$('autoplayImage').onclick = toggleAutoplay;
	
	$('nextProjectImage').onmouseover = function () { this.src = 'images/control_fastforward_blue.gif'; return false; };
	$('nextProjectImage').onmouseout  = function () { this.src = 'images/control_fastforward.gif'; return false; };
	$('nextProjectImage').onclick     = function() { stopAutoplay(); setAutoplayButton("off");  hideVideo(); nextProject(); };
	
	$('lastProjectImage').onmouseover = function () { this.src = 'images/control_end_blue.gif'; return false; };
	$('lastProjectImage').onmouseout  = function () { this.src = 'images/control_end.gif'; return false; };
	$('lastProjectImage').onclick     = function() { stopAutoplay(); setAutoplayButton("off");  hideVideo(); lastProject(); };
	
	$('videoLink').onclick = showVideo;
	$('leaveVideo').onclick = undoShowVideo;
}

function filterByType()
{
	var i;
	var x = $('projects').childNodes;
	var projectType = $('projectTypeMenu').options[$('projectTypeMenu').selectedIndex].value;
	switch (projectType)
	{
		case 'allProjects':
			for (i=0; i<x.length; i++)
				new Effect.Opacity(x[i].id, {to:1.0});
			break;
			
		case 'studentGroup':
			for (i=0; i<x.length; i++)
			{
				if (Element.hasClassName(x[i],'studentGroup'))
					new Effect.Opacity(x[i].id, {to:1.0});
				else
					new Effect.Opacity(x[i].id, {to:0.3});
			}
			break;
			
		case 'studentIndividual':
			for (i=0; i<x.length; i++)
			{
				if (Element.hasClassName(x[i],'studentIndividual'))
					new Effect.Opacity(x[i].id, {to:1.0});
				else
					new Effect.Opacity(x[i].id, {to:0.3});
			}
			break;
			
		case 'faculty':
			for (i=0; i<x.length; i++)
			{
				if (Element.hasClassName(x[i],'faculty'))
					new Effect.Opacity(x[i].id, {to:1.0});
				else
					new Effect.Opacity(x[i].id, {to:0.3});
			}
			break;
			
		default:
			for (i=0; i<x.length; i++)
				new Effect.Opacity(x[i].id, {to:1.0});
			break;
	}
	
}

function setUpFlashObject()
{
	so.addVariable("file","uploads/test.flv");
	so.addVariable("autoStart","true");
	so.addVariable("repeat","true");
	so.addVariable("wmode","transparent");
    so.addParam("wmode","transparent");
    so.write("flashContent");
}

function setUpMenu()
{
	var projectType = $('projectTypeMenu');
	
	projectType.setAttribute('onchange', 'filterByType');
	projectType.onchange = filterByType;
}

function executeTimer() {
	if (autoplayFlag == "off")
	{
		setAutoplayButton("on");
		startAutoplay();
		nextProject();
	}
}

function resetTimer(e) {
	clearTimeout(tID); // reset the timer
	tID = setTimeout('executeTimer()',20000);
}

function setUpTimer()
{
	tID = setTimeout('executeTimer()',20000);
}

window.onmousemove = resetTimer;
document.onclick = function () { resetTimer(); };

if (document.layers) {
  window.captureEvents(Event.MOUSEMOVE);
}

window.onload = function () { importXML(); setUpControls(); setUpMenu(); setUpTimer(); setUpFlashObject(); initLightbox(); };