// JavaScript Document

window.onload = pageInit;

 function pageInit() {
	 
	 // If Home Page, Randomize Image and Quote

	if (document.getElementById("main-quote") && document.getElementById("main-image")) {
		randomizequote();
	}
	
	if (document.getElementById("featuredoc")) {
			setfeature();
	}

// Setup Rollovers 

	for (var i=0; i<document.images.length; i++) {
		if ((document.images[i].parentNode.tagName == "A") && ((document.images[i].className == "rollover") || (document.images[i].className == "go") || (document.images[i].className == "largerview"))) {
			 setupRollover(document.images[i]);
		 }
	}

//Setup Link Actions
	for (var i=0; i<document.links.length; i++) {
		if (document.links[i].className == "opener") {
			document.links[i].onclick = openwindow;
		}
		if (document.links[i].className == "closer") {
			document.links[i].onclick = closewindow;
		}
		if (document.links[i].className == "pager") {
			document.links[i].onclick = swappage;
		}
	}

}
	
	

//  Rollover Function
function setupRollover(theImg) {
	theImg.upImg = new Image();
	theImg.upImg.src = theImg.src;
	theImg.onmouseout = function() {
		this.src = this.upImg.src;
	}
	
	theImg.overImg = new Image();
	
	var imgname;
	var overimage;
		
	imgname = theImg.src;
	overimage = imgname.replace(/-up/,'-over');
	theImg.overImg.src = overimage;

		theImg.onmouseover = function() {
		this.src = this.overImg.src;
	}
}

// Window Opener Function

function openwindow() {
	var detailwindow = window.open(this.getAttribute("href"),"detail","width=800,height=650,scrollbars=yes,resizable=yes,menubar=yes,status=yes,toolbar=yes,location=yes");
	detailwindow.focus();
	return false;
	}

// Window Closer Function

function closewindow() {
	window.close();
	return false;
}

// Swap Document Detail Page Function 

function swappage() {
	
	// Swap the Main Image
	var newImg = this.getAttribute("href");
	var documentImage = document.getElementById("docimage");
	documentImage.setAttribute("src",newImg);
	

	// Update Selectors
	var selectors = document.getElementById("thumbnails");
	var selectorimages = selectors.getElementsByTagName("img");
	
	// Change all seclectors to "up" state
	for (i=0; i < selectorimages.length; i++) {
		var imagesrc = selectorimages[i].getAttribute("src");
		imagesrc = imagesrc.replace(/-sel/,"-up");
		selectorimages[i].setAttribute("src", imagesrc);
	}
	
	var currentselector = this.firstChild;
	imagesrc = currentselector.getAttribute("src");
	imagesrc = imagesrc.replace(/-up/,"-sel");
	currentselector.setAttribute("src", imagesrc);
	
	return false;
	
}

// Select Random Quote for Home Page

function randomizequote() {
	var quotenum = Math.floor(Math.random() * 5) + 1;
	var mainimage = document.getElementById("main-image");
	var mainquote = document.getElementById("main-quote");
	
	// Update Images
	var path = "/exhibits/documented-rights/images/";
	var imagesrc = path + "home-image-" + quotenum + ".jpg";
	var quotesrc = path + "home-quote-" + quotenum + ".jpg";
	mainimage.setAttribute("src", imagesrc);
	mainquote.setAttribute("src", quotesrc);	
	
	// Update Hyperlinks
	var linkpath = "/exhibits/documented-rights/exhibit/section" + quotenum + "/";
	var imagelink = mainimage.parentNode
	if (imagelink.tagName == "A") {
		imagelink.setAttribute("href", linkpath);
	}
	var quotelink = mainquote.parentNode
	if (quotelink.tagName == "A") {
		quotelink.setAttribute("href", linkpath);
	}
	
	// Update Alt tags
	var imagealt = new Array();
	var quotealt = new Array();
	imagealt[0] = "Slave Family Engraving";
	imagealt[1] = "Photo of African-American Dockworkers";
	imagealt[2] = "Photo of Suffragettes";
	imagealt[3] = "Civil Rights March Photo";
	imagealt[4] = "Integrated Classroom Photo";
	quotealt[0] = "Slavery is founded on the selfishness of man’s nature––opposition to it on his love of justice.  –– Abraham Lincoln, October 16, 1854";
	quotealt[1] = "Our ignorance was the greatest hold the South had on us. -- former slave John W. Fields, 1930s";
	quotealt[2] = "There never will be complete equality until women themselves help to make laws and elect lawmakers. –– Susan B. Anthony, 1897";
	quotealt[3] = "Will you join the struggle? -- Martin Luther King, ca. 1956";
	quotealt[4] = "There is absolutely no reasonable explanation for racial prejudice. –– Judge J. Waites Waring, June 21, 1951";
	mainimage.setAttribute("alt", imagealt[quotenum - 1]);
	mainquote.setAttribute("alt", quotealt[quotenum - 1]);
}

// Set Featured Document Based on the Month

function setfeature() {
	
	var path="/exhibits/documented-rights/exhibit/";
	var doclinks = new Array();
	doclinks[0] = "section3/detail/complaint-bill.html";
	doclinks[1] = "section4/detail/mlk.html";
	doclinks[2] = "section3/detail/suffrage-parade.html";
	doclinks[3] = "section2/detail/azor-passengers.html";
	doclinks[4] = "section3/detail/handbill.html";
	doclinks[5] = "section2/detail/douglass-letter.html";
	doclinks[6] = "section1/detail/habeas-corpus-writ.html";
	doclinks[7] = "section1/detail/cinque-jurisdiction-plea.html";
	doclinks[8] = "section4/detail/hernandez-statement.html";
	doclinks[9] = "section1/detail/brig-alo-manifest.html";
	doclinks[10] = "section3/detail/mif-constitution.html";
	doclinks[11] = "section4/detail/fitzgerald-complaint.html";
	
	
	// Get current month
	var today = new Date();
	var thismonth = today.getMonth() + 1;
	
	// Set featured doc image
	var featureddoc = "/exhibits/documented-rights/images/features/" + thismonth + "-featured-doc.jpg"
    var docimage = document.getElementById("featuredoc");
	docimage.setAttribute("src", featureddoc);
	
	// Set links
	thismonth = thismonth - 1;
	
	var doclink = docimage.parentNode
	if (doclink.tagName == "A") {
		doclink.setAttribute("href", path + doclinks[thismonth]);
	}
	
	var golink = document.getElementById("featurelink");
	golink.setAttribute("href", path + doclinks[thismonth]);
}
