// JavaScript Document

window:onload = function(){
setMainNavLinks();
setProjectTypes();
setProjectLinks();
checkURL();
}
var motionU = 0;
var printU = 0;

function getQueryVariable(variable){
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++){
		var pair = vars[i].split("=");
		if (pair[0] == variable){
			return pair[1];
		}
	}
}

function checkURL(){
	var projectID = getQueryVariable("work");
	if (projectID){
		var toOpenExists = document.getElementById(projectID);
		if (toOpenExists){
			var toOpen = toOpenExists.firstChild;
			openProject(toOpen);
		}else{
			alert("Sorry, this page \""+projectID+"\" does not exist.\n");
		}
	}
}

function setMainNavLinks(){
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	var sections = document.getElementById("mainNav");
	var sectionButtons = sections.getElementsByTagName("a");
	for (var i=0;i<sectionButtons.length;i++){
		sectionButtons[i].onclick = function(){
			return openSection(this);
		}
	}
}

function openSection(whichSection){
	var testClass = document.getElementById("container");
	if (testClass.getAttribute("class")=="container"){
		var classType = "class";
	}else{
		var classType = "className";
	}
	var section = whichSection.firstChild.nodeValue;
	if (section == "Recognition"){
		deselectProjects();
		toggleSection("news","open");
		toggleSection("info","close");
		toggleSection("projectTemplate","close");
		var projectListUL = document.getElementById("projectList");
		projectListUL.setAttribute(classType,"projectList");
	}else if (section == "Profile"){
		deselectProjects();
		toggleSection("news","close");
		toggleSection("info","open");
		toggleSection("projectTemplate","close");
		var projectListUL = document.getElementById("projectList");
		projectListUL.setAttribute(classType,"projectList");
	}else if (section == "Work."){
		toggleSection("news","close");
		toggleSection("info","close");
		toggleSection("projectTemplate","close");
		var reelProject = document.getElementById("reel").firstChild;
		openProject(reelProject);
	}
	return false;
}

function deselectProjects(){
	var testClass = document.getElementById("container");
	if (testClass.getAttribute("class")=="container"){
		classType = "class";
	}else{
		classType = "className";
	}
	var projectList = document.getElementById("projects");
	var projectItem = projectList.getElementsByTagName("li");
	for (var i=0; i<projectItem.length; i++){
		if (projectItem[i].getAttribute(classType) == "project current"){
			projectItem[i].setAttribute(classType,"project visited");
		}
	}
}

function toggleSection(section,openClose){
	var testClass = document.getElementById("container");
	if (testClass.getAttribute("class")=="container"){
		var classType = "class";
	}else{
		var classType = "className";
	}
	var sectionToToggle = document.getElementById(section);
	if (openClose == "open"){
		sectionToToggle.setAttribute(classType,"");
	}else if (openClose == "close"){
		sectionToToggle.setAttribute(classType,"hideThis");
	}
}

function setProjectTypes(){
 if (!document.getElementsByTagName) return false;
 if (!document.getElementById) return false;
 var types = document.getElementById("projectType");
 var typeButtons = types.getElementsByTagName("a");
 for (var i=0; i<typeButtons.length; i++){
   typeButtons[i].onclick = function(){
    return underlineType(this);
   }
 }
}

function setProjectLinks(){
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	var testClass = document.getElementById("container");
	if (testClass.getAttribute("class")=="container"){
		var classType = "class";
	}else{
		var classType = "className";
	}
	var projectList = document.getElementById("projects");
	var projectItem = projectList.getElementsByTagName("li");
	for (var i=0; i<projectItem.length; i++){
		if (projectItem[i].getAttribute(classType) == "project"){
			projectItem[i].firstChild.onclick = function(){
				return openProject(this);
			}
		}
	}
}




function openProject(projectLink){
	toggleSection("news","close");
	toggleSection("info","close");
	toggleSection("projectTemplate","close");
	var project = projectLink.parentNode;
	
	deselectProjects();
	
	project.setAttribute(classType,"project current");
	var projectListUL = document.getElementById("projectList");
	projectListUL.setAttribute(classType,"projectList");
	
	var lists = project.getElementsByTagName("ul");
	var divInfo = document.getElementById("insertInfo");
	divInfo.setAttribute(classType,"hideThis");
	for (var i=0;i<lists.length;i++){
		if (lists[i].getAttribute(classType) == "info"){
			var info = lists[i].innerHTML;
			var divInfo = document.getElementById("insertInfo");
			divInfo.firstChild.innerHTML = info;
			divInfo.setAttribute(classType,"");
		}
	}

	var divDescription = document.getElementById("insertDescription");
	divDescription.setAttribute(classType,"hideThis");
	var paras = project.getElementsByTagName("p");
	for (var i=0;i<paras.length;i++){
		if (paras[i].getAttribute(classType) == "description"){
			var description = paras[i].firstChild.nodeValue;
			var divDescription = document.getElementById("insertDescription");
			divDescription.innerHTML = "<p>"+description+"</p>";
			divDescription.setAttribute(classType,"");
		}
	}
	
	

	
	
	var divMovies = document.getElementById("insertMovieLink");
	divMovies.setAttribute(classType,"hideThis");
	for (var i=0;i<lists.length;i++){
		if (lists[i].getAttribute(classType) == "movies"){
			var movieList = lists[i];
			var movies = movieList.getElementsByTagName("a");
			var divMovies = document.getElementById("insertMovieLink");
			divMovies.setAttribute(classType,"");
			if (movies.length == 1){
				var viewQT = "View Project";
				divMovies.innerHTML = "<a href=\"#\">"+viewQT+"</a>";
				var movieLink = movies[0].getAttribute("href");
				divMovies.firstChild.setAttribute("href",movieLink);
			}else if (movies.length > 1){
				var viewQT = "View Project";
				var allLinks = "<a href=\"#\">"+viewQT+"</a>"
				for (var j=0;j<movies.length;j++){
					var movieLink = movies[j].getAttribute("href");
					var linkText = movies[j].firstChild.nodeValue;
					allLinks += "<a href="+movieLink+">"+linkText+"</a>";
				}
				divMovies.innerHTML = allLinks;
			}
			setMovieClick();
		}
	}

	var divImages = document.getElementById("insertImage");
	divImages.setAttribute(classType,"hideThis");
	for (var i=0;i<lists.length;i++){
		if (lists[i].getAttribute(classType) == "pictures"){
			var pictureList = lists[i];
			var pictures = pictureList.getElementsByTagName("li");
			var allImages = "";
			for (var j=0;j<pictures.length;j++){
				var imgAttrs = pictures[j].getElementsByTagName("span");
				if (imgAttrs[0].getAttribute(classType) == "imgDesc"){
					var imgDesc = imgAttrs[0].firstChild.nodeValue;
					var imgSrc = imgAttrs[1].firstChild.nodeValue;
				}else if (imgAttrs[0].getAttribute(classType) == "imgSrc"){
					var imgDesc = "";
					var imgSrc = imgAttrs[0].firstChild.nodeValue;
				}
				
				if (j == (pictures.length-1)){
					if (imgDesc != ""){
						allImages += "<p>"+imgDesc+"</p>";
					}
					allImages += "<li class=\"last\"><img src=\""+imgSrc+"\" width=\"860\" /></li>";
				}else{
					if (imgDesc != ""){
						allImages += "<p>"+imgDesc+"</p>";
					}
					allImages += "<li><img src=\""+imgSrc+"\" width=\"860\" /></li>";
				}
			}
			divImages.firstChild.innerHTML = allImages;
			divImages.setAttribute(classType,"");
		}
	}
	
	document.getElementById("projectTemplate").setAttribute(classType,"");
	return false;
}

function insertAfter(newElement,targetElement){
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement){
		parent.appendChild(newElement);
	}else{
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}


function setMovieClick(){
	var divMovies = document.getElementById("insertMovieLink");
	var movieLinks = divMovies.getElementsByTagName("a");
	for (var i=0;i<movieLinks.length;i++){
		movieLinks[i].onclick = function(){
			return openURLWindow(this.getAttribute("href"), "URLWindow", "1200", "800", "yes", "yes");
		}
	}
}


function openURLWindow(url, name, width, height, menubar, status){
	config = 'width=' + width + ',height=' + height;
	newWindow = window.open(url, name, config, menubar, status);
	var x = (screen.width - width) / 2;
	var y = (screen.height - height) / 2;
	newWindow.moveTo(x, y);
	newWindow.focus();
	return false;
}

function underlineType(whichType){
	var testClass = document.getElementById("container");
	if (testClass.getAttribute("class")=="container"){
		classType = "class";
	}else{
		classType = "className";
	}
	var projectType = whichType.getAttribute(classType);
	var projectListTypes = document.getElementById("projectList");
	var projects = projectListTypes.getElementsByTagName("a");
	var types = document.getElementById("projectType");
	var typeButtons = types.getElementsByTagName("a");
  if (projectType == "p motion" || projectType == "p motion mpunderline"){
		if (motionU == 0){
		  motionU = 1;
		  printU = 0;
		}else if(motionU ==1){
			motionU = 0;
			printU = 0;
		}
  }else if (projectType == "p print" || projectType == "p print mpunderline"){
		if (printU == 0){
		  motionU = 0;
		  printU = 1;
		}else if(printU == 1){
			motionU = 0;
			printU = 0;
		}
  }
  

	if (motionU == 1){
	  whichType.setAttribute(classType,"p motion mpunderline");
	  for (var i=0;i<projects.length;i++){
		  if(projects[i].getAttribute(classType)=="p motion"){
			  projects[i].setAttribute(classType,"p motion mpunderline");
		  }else if(projects[i].getAttribute(classType)=="p motion print"){
			  projects[i].setAttribute(classType,"p motion print mpunderline");
		  }
	  }
  }else if (motionU == 0){
	  typeButtons[0].setAttribute(classType,"p motion");
	  for (var i=0;i<projects.length;i++){
		  if(projects[i].getAttribute(classType)=="p motion mpunderline"){
			  projects[i].setAttribute(classType,"p motion");
		  }
	  }
  }
  if (printU == 1){
	  whichType.setAttribute(classType,"p print mpunderline");
	  for (var i=0;i<projects.length;i++){
		  if(projects[i].getAttribute(classType)=="p print"){
			  projects[i].setAttribute(classType,"p print mpunderline");
		  }else if(projects[i].getAttribute(classType)=="p motion print"){
			  projects[i].setAttribute(classType,"p motion print mpunderline");
		  }
	  }
  }else if (printU == 0){
	  typeButtons[1].setAttribute(classType,"p print");
	  for (var i=0;i<projects.length;i++){
		  if(projects[i].getAttribute(classType)=="p print mpunderline"){
			  projects[i].setAttribute(classType,"p print");
		  }
	  }
  }
  if (motionU == 0 && printU == 0){
	  for (var i=0;i<projects.length;i++){
		  if(projects[i].getAttribute(classType)=="p motion print mpunderline"){
			  projects[i].setAttribute(classType,"p motion print");
		  }
	  }
  }
  return false;
}
