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

var setOptions = {
	
	//set variables that will be used in multiple functions
	content : null,
	lineSpacing : null, 
	highlightKw : null,
	highlightText : null,
	hideQuotation : null,
	
	//set functions
	init : function() {
		// Object detection
		if (!document.createElement) {return;}
		
		// Build the Customize Article Display block
		setOptions.buildCADblock();
		
		// Acquire content block for use throughout document
		setOptions.content = setOptions.getObj('content');
		
		// Assign event handlers and functions called
		setOptions.addEvent(setOptions.lineSpacing,'click',setOptions.adjustLineSpacing);
		setOptions.addEvent(setOptions.highlightKw,'click',setOptions.highlightKeywords);
		setOptions.addEvent(setOptions.highlightText,'click',setOptions.highlightTextHover);
		setOptions.addEvent(setOptions.hideQuotation,'click',setOptions.hideTheQuotation);	
		
		// Get Keywords with no duplicates and output
		setOptions.outputKWs();
		
		// Build Print URLs and insert supercript tags after links in content
		setOptions.outputPurls();
		
	},
	
	buildCADblock : function () {
		// Create Customize Article Display block
		var holderDiv = document.createElement('div');
		var theH3 = document.createElement('h3');
		var theForm = document.createElement('form');
		theForm.id = 'custartdisp';
		var theButton = [], theP = [];
		for (var i=0; i<4; i++) {
			theP[i] = document.createElement('p');
			theButton[i] = document.createElement('input');
			theButton[i].type = 'button';
			switch (i) {
				case 0: theButton[i].value = 'Increase Line Spacing';
								theButton[i].id = 'linespace'; 
								setOptions.lineSpacing = theButton[i]; break;
				case 1: theButton[i].value = 'Highlight Keywords';
								theButton[i].id = 'highkey'; 
								setOptions.highlightKw = theButton[i]; break;
				case 2: theButton[i].value = 'Highlight Text on Hover';
								theButton[i].id = 'hightext'; 
								setOptions.highlightText = theButton[i]; break;
				case 3: theButton[i].value = 'Hide Quotations';
								theButton[i].id = 'hidequote'; 
								setOptions.hideQuotation = theButton[i]; break;				
			}
			theP[i].appendChild(theButton[i]);
			theForm.appendChild(theP[i]);
		}
		
		// Build the Customize Article Display block in memory, insert into sidecol
		theH3.appendChild(document.createTextNode('Customize Article Display'));
		holderDiv.appendChild(theH3);
		holderDiv.appendChild(theForm);
		var theSideCol = setOptions.getObj('sidecol');
		var otherSideH3s = theSideCol.getElementsByTagName('h3');
		theSideCol.insertBefore(holderDiv,otherSideH3s[0]);
	},
	
	outputKWs : function() {
	// Build Keyword List with no duplicates
		var allKws = setOptions.content.getElementsByTagName('span');
		var kwData = [];
		for (var h=0; h<allKws.length; h++) {
			kwData[h] = allKws[h].firstChild.nodeValue;
		}
		if(kwData.splice) {
			var lastDiv = setOptions.content.getElementsByTagName('div');
			ldLength = lastDiv.length;
			var theKWp = document.createElement('p');
			var kwStart = document.createElement('strong')
			kwStart.appendChild(document.createTextNode('Keywords: '));
			theKWp.appendChild(kwStart);		
			for (var i=0; i<kwData.length; i++) {
				for (var j=i+1; j<kwData.length; j++) {
					if (kwData[i] == kwData[j]) {
						kwData.splice(j,1);
					}
				}
				if (i<kwData.length-1) {
					theKWp.appendChild(document.createTextNode(kwData[i] + ', '));
				} else {
					theKWp.appendChild(document.createTextNode(kwData[i]));
					}
				
			}
			setOptions.content.insertBefore(theKWp,lastDiv[ldLength-1]);		
		}
	},
	
	outputPurls : function() {
	// Build Print URLs and insert supercript tags after links in content		
		var allUrls = setOptions.content.getElementsByTagName('a');
		if (allUrls[0] != null) {
			var urlDiv = document.createElement('div');
			urlDiv.className = 'printonly'; 
			var aHrefs = [], theLIs = [], linkTags = [];
			for (var h=0; h<allUrls.length; h++) {
				aHrefs[h] = allUrls[h].href;
				linkTags[h] = document.createElement('sup');
				linkTags[h].className = 'printonly';
			}
			if(aHrefs.splice) {
				var lastDiv = setOptions.content.getElementsByTagName('div');
				ldLength = lastDiv.length;			
				var aH4 = document.createElement('h4')
				aH4.appendChild(document.createTextNode('Links in this article:'));
				var theOL = document.createElement('ol');
				var numLinks = aHrefs.length;
				
				// Start loop(s) at 1 to skip #maincontent target for jumplink					
				for (var i=1; i<aHrefs.length; i++) {	
					// Start nested loop at 1 more than outside loop 
					// This assumes earlier duplicates have all been caught
					for (var j=i+1; j<aHrefs.length; j++) {
						if (aHrefs[i] == aHrefs[j]) {
							aHrefs.splice(j,1);						
						}
					}
					theLIs[i] = document.createElement('li');
					theLIs[i].appendChild(document.createTextNode(aHrefs[i]));
					theOL.appendChild(theLIs[i]);
				}
			
				// Append unique numbers in URLs
				for (var k=1; k<numLinks; k++) {
					for (var l=1; l<aHrefs.length; l++) {
						// Compare each URL on page to the unique URLs and write the 
						// unique reference number for each tag 
						if (allUrls[k].href == aHrefs[l]) {
							linkTags[k].appendChild(document.createTextNode('(' + l + ')'));
						}
					}
					allUrls[k].appendChild(linkTags[k]);
				}			
				urlDiv.appendChild(aH4);
				urlDiv.appendChild(theOL);
				// Insert URL div into document before copyright div
				setOptions.content.insertBefore(urlDiv,lastDiv[ldLength-1]);		
			}
		}	
	},
	
	// Functions to handle button events
	adjustLineSpacing : function() {	
  	if (this.value == 'Increase Line Spacing') {
		  setOptions.content.className = 'lhincrease';
		  this.value = 'Decrease Line Spacing';
		} else {
				setOptions.content.className = '';
				this.value = 'Increase Line Spacing';
			} 
	},
	
	highlightKeywords : function() {
		var kws = setOptions.content.getElementsByTagName('span');
		var kwlength = kws.length;
		if (this.value == 'Highlight Keywords') {
			for (var i = 0; i < kwlength; i++) {
				kws[i].className = 'keyword';
			}
			this.value = 'No Keyword Highlight';
		}	else {
				for (var i = 0; i < kwlength; i++) {
					kws[i].className = '';
				}
				this.value = 'Highlight Keywords';
			}
	},
	
	highlightTextHover : function() {
		var paras = setOptions.content.getElementsByTagName('p');
		var paraslength = paras.length;
		if (this.value == 'Highlight Text on Hover') {
			for (var i = 0; i < paraslength; i++) {
			  setOptions.addEvent(paras[i],'mouseover',setOptions.hoveron);
				setOptions.addEvent(paras[i],'mouseout',setOptions.hoveroff);
			}
			this.value = 'No Text Highlights';
		} else {
				for (var i = 0; i < paraslength; i++) {
			 		setOptions.removeEvent(paras[i],'mouseover',setOptions.hoveron);
					setOptions.removeEvent(paras[i],'mouseout',setOptions.hoveroff)
				}
				this.value = 'Highlight Text on Hover';		
			}	
	},
	
	hoveron : function() {  // for use with highlightTextHover
  	this.className = 'jshover';
	},
	hoveroff : function() {  // for use with highlightTextHover
  	this.className = '';
	},
	
	hideTheQuotation : function() {
		var blockq = document.getElementsByTagName('blockquote');
		var blockqlength = blockq.length;
		if (this.value == 'Hide Quotations') {
			for (var i=0; i<blockqlength; i++) {
			  blockq[i].className = 'hideit';
				this.value = 'Show Quotations';
			}
		} else {
				for (var i=0; i<blockqlength; i++) {
			  	blockq[i].className = '';
			  	this.value = 'Hide Quotations';
			  }
			}
		},	
	
	// improve performance by reducing object lookups 
	getObj : function(idvalue) {
		return document.getElementById(idvalue);
	},

	// associate nodes with events and functions
  addEvent : function(obj, type, func) {
       if (obj.addEventListener) {obj.addEventListener(type, func, false);}
       else if (obj.attachEvent) {
           obj["e" + type + func] = func;
           obj[type + func] = function() {obj["e" + type + func] (window.event);}
           obj.attachEvent("on" + type, obj[type + func]);
       }
       else {obj["on" + type] = func;}
   },
   
   // remove events associated with addEvent
   removeEvent : function (obj, type, func) {
    	// W3C approach for non-IE modern browsers
    	if (obj.removeEventListener) {
      	  obj.removeEventListener(type, func, false);
	    }
  	  // Windows IE approach
    	else if (obj.detachEvent) {
      	  obj.detachEvent("on" + type, obj[type + func]);
        	obj[type + func] = null;
        	obj["e" + type + func] = null;
    	}
    	// browsers supporting neither W3C or IE methods
    	else {
      	  obj["on" + type] = null;
    	}
		}
}

setOptions.addEvent(window, 'load', setOptions.init);
