// JavaScript Document for Homework 2, INP271, Mike Behnke

window.onload = function() {
	// Object detection
	if (!document.getElementById && !document.getElementsByTagName) {return;}
	
	// Acquire button elements
	var lineSpacing = getObj('linespace'); 
	var highlightKw = getObj('highkey');
	var highlightText = getObj('hightext');
	var hideQuotation = getObj('hidequote');
	
	// Assign event handlers and functions called
	lineSpacing.onclick = adjustLineSpacing;
	highlightKw.onclick = highlightKeywords;  
	highlightText.onclick = highlightTextHover;
	hideQuotation.onclick = hideTheQuotation; 	
}

// Add Functionality to buttons

function adjustLineSpacing() {
  var lineSpacing = getObj('linespace'); 
	var content = document.getElementById('content');
  if (lineSpacing.value == 'Increase Line Spacing') {
	  content.className = 'lhincrease';
	  lineSpacing.value = 'Decrease Line Spacing';
	} else {
			content.className = '';
			lineSpacing.value = 'Increase Line Spacing';
		} 
}

function highlightKeywords () {
	var highlightKw = getObj('highkey');
	var content = document.getElementById('content');
	var kws = content.getElementsByTagName('span');
	var kwlength = kws.length;
	if (highlightKw.value == 'Highlight Keywords') {
		for (var i = 0; i < kwlength; i++) {
			kws[i].className = 'keyword';
		}
		highlightKw.value = 'No Keyword Highlight';
	}	else {
			for (var i = 0; i < kwlength; i++) {
				kws[i].className = '';
			}
			highlightKw.value = 'Highlight Keywords';
		}
}

function highlightTextHover() {
	var highlightText = getObj('hightext');
	var content = document.getElementById('content');
	var paras = content.getElementsByTagName('p');
	var paraslength = paras.length;
	if (highlightText.value == 'Highlight Text on Hover') {
		for (var i = 0; i < paraslength; i++) {
		  paras[i].onmouseover = paras[i].onfocus = hoveron;
			paras[i].onmouseout = paras[i].onblur = hoveroff;
		}
		highlightText.value = 'No Text Highlights';
	} else {
			for (var i = 0; i < paraslength; i++) {
		 		paras[i].onmouseover = paras[i].onfocus = null;
				paras[i].onmouseout = paras[i].onblur = null;
			}
			highlightText.value = 'Highlight Text on Hover';		
		}
	
}

function hoveron() {  // for use with highlightTextHover
  this.className = 'jshover';
}

function hoveroff () {  // for use with highlightTextHover
  this.className = '';
}

function hideTheQuotation() {
	var hideQuotation = getObj('hidequote');
	var blockq = document.getElementsByTagName('blockquote')[0];
	if (hideQuotation.value == 'Hide Quotations') {
		blockq.className = 'hideit';
		hideQuotation.value = 'Show Quotations';
	} else {
			blockq.className = '';
			hideQuotation.value = 'Hide Quotations';
		}
	}


// improve performance by reducing object lookups 
// reduce code volume
// function from INP271 Lecture 5 example
function getObj(idvalue) {
  return document.getElementById(idvalue);
}
