// pogo news javascript - jsainz@ea.com [phoenix-r4] \\
var articleHolder, count=0;
var articleDir;
var articleArray;

// establishes an http connection ... called from goArticle() \\
function createRequest(){
	try{request = new XMLHttpRequest();}
	catch(trymicrosoft){
		try{request = new ActiveXObject('Msxml2.XMLHTTP');}
		catch(othermicrosoft){
			try{request = new ActiveXObject('Microsoft.XMLHTTP');}
			catch(failed){
				request = null;
			}
		}
	}
	if(request == null) // how should we handle failed connections \\
		alert('could not make request');
}

// navigation object methods \\
// usage: (will accept any ID but specifically designed for next,first,prev,all) \\
// [on:] bT.on(object-id); \\
// [off:] bT.off(object-id); \\
// [clickOn:] bT.clickOn(object-id); \\
// [clickOff:] bT.clickOff(object-id); \\
var bT = {
	on: function(elm) {
		document.getElementById(elm).className = 'btn-on';
		document.getElementById(elm+'-top').className = 'btn-on';
		
	},
	off: function(elm) {
		document.getElementById(elm).className = 'btn-off';
		document.getElementById(elm+'-top').className = 'btn-off';
	},
	clickOn: function(elm) {
		if(elm == 'next'){
			document.getElementById('next').onclick = function(){goArticle(-1)}
			document.getElementById('next-top').onclick = function(){goArticle(-1)}
		}
		else if(elm == 'first'){
			document.getElementById('next').onclick = function(){firstArticle()}
			document.getElementById('next-top').onclick = function(){firstArticle()}
		}
		else if(elm == 'prev'){
			document.getElementById('prev').onclick = function(){goArticle(+1)}
			document.getElementById('prev-top').onclick = function(){goArticle(+1)}
		}
		else if(elm == 'all'){
			document.getElementById('all').onclick = function(){showList()}
			document.getElementById('all-top').onclick = function(){showList()}
		}
	},
	clickOff: function(elm) {
		document.getElementById(elm).onclick = null;
		document.getElementById(elm+'-top').onclick = null;
	}
};

// object oriented array index lookup \\
// usage: nameOfArray.findIndex(VALUE) ... see doHash() \\
Array.prototype.findIndex = function(value){
	var ind = "";
	for (var i=0; i < this.length; i++) {
		if (this[i] == value) {
			return i;
		}
	}
	return ind;
};

// article navigation function \\
function goArticle(dir){
	if(dir == 1){//moving forward
		if(count < articleArray.length-1 && count == articleArray.length-2){
			count = count+1;
			bT.off('prev');
			bT.clickOff('prev');			
		}
		else if(count < articleArray.length-1){
			count = count+1;
			bT.on('next');
			bT.clickOn('next');
			bT.on('prev');
			bT.clickOn('prev');
		}
		else{count = 0;}
	}
	else if (dir == -1){//moving backward
		if(count <= articleArray.length-1){
			bT.on('prev');
			bT.clickOn('prev');
		}
		if(count == 1){
			bT.off('next');
			bT.clickOff('next');
		}
		count = count > 0 ? count-1 : articleArray.length-1;
	}
	else if(dir != 1 || dir != 0 || dir != -1){//going to a specific article
		count = dir;
		bT.on('prev');
		bT.clickOn('prev');
	}
	else {//start at the first article
		count = 0
	}
	if(articleArray.length <= 1){	
		bT.clickOff('next');
		bT.off('next');
		bT.clickOff('prev');
		bT.off('prev');
	}
	if(dir == 0){
		bT.clickOff('next');
		bT.off('next');
	}
	createRequest(); // establish http connection \\
	var url = articleDir + articleArray[count] + '.jsp'; // passes in variables from hotdeployable list of articles (net-2007-body.jsp) \\
	request.open('GET', url, true); // opens file \\
	request.onreadystatechange = updatePage; // after file opens we execute updatePage() which completes the process \\
	request.send(null); // passing null to request ... if we don't pass null, connection fails \\
	window.location.hash = articleArray[count]; // set hash for bookmarks and linking \\
	window.scrollTo(0,0);
}

// receives HTTPRequest response ... ajax fresh! \\
function updatePage(){
	if(request.readyState == 4){ // 4 = loaded \\
		var pogoArticle = request.responseText; // dump response into a variable \\
		document.getElementById('putArticleHere').innerHTML = pogoArticle; // loads article into 'putArticleHere' div \\
	}
}

// action for all link in navigation \\ 
function showList(){
	document.getElementById('tol').style.display = 'block'; // show archive list \\
	document.getElementById('putArticleHere').style.display = 'none'; // hide article \\
	document.getElementById('articleNav').style.display = 'none'; // hide navigation \\
	document.getElementById('articleNav-top').style.display = 'none'; // hide navigation \\

	bT.clickOn('first');
	bT.on('next');
	window.location.hash = 'top';
}

// uuhhh all this does is hit the first article in the array \\
function firstArticle(){
	document.getElementById('tol').style.display = 'none';
	document.getElementById('putArticleHere').style.display = 'block';
	document.getElementById('articleNav').style.display = 'block'; // show navigation \\
	document.getElementById('articleNav-top').style.display = 'block'; // show navigation \\
	goArticle(0);
	bT.clickOn('next');
	bT.clickOn('all');
	bT.on('all');
}

// iterates thru list of links and sets eventListeners for each \\
// LINKS AND ARRAYS MUST MATCH IN LENGTH AND BE IN SAME ORDER \\
function setArticleLinks(){
	var links = document.getElementById('tol').getElementsByTagName('a');
	var num = 0;
	for(var i = 0; i < links.length; i++) {
		links[i].href = "#" + articleArray[i];
		if(window.addEventListener){ // FF
			links[i].addEventListener('click', goToArticle, false);
			links[i].pos = num++;
		} else { // IE
			links[i].attachEvent('onclick', goToArticle);
			links[i].pos = num++;
		}
	}
}

// used by setArticleLinks() \\
function goToArticle(evt){
	document.getElementById('tol').style.display = 'none';
	document.getElementById('putArticleHere').style.display = 'block';
	document.getElementById('articleNav').style.display = 'block'; // show navigation \\
	document.getElementById('articleNav-top').style.display = 'block'; // show navigation \\
	var e_out;
	var ie_var = "srcElement";
	var moz_var = "target";
	var prop_var = "pos";
	// "target" for Mozilla, Netscape, Firefox et al. ; "srcElement" for IE
	evt[moz_var] ? e_out = evt[moz_var][prop_var] : e_out = evt[ie_var][prop_var];
	goArticle(e_out);
	bT.clickOn('next');
	bT.clickOn('all');
	bT.on('all');
}

// if (URL has a hash) {we load the corresponding article!} \\ 
function doHash() {
	if (window.location.hash == '' || window.location.hash == '#' || window.location.hash == '#top' || window.location.hash == 'top'){return true;}
	else {
    // get rid of the hashmark '#' before looking up article \\
    var new_pos = document.location.hash.substr(1,document.location.hash.length);
		
		document.getElementById('tol').style.display = 'none'; // hide list of articles \\
		document.getElementById('putArticleHere').style.display = 'block'; // show article \\
		document.getElementById('articleNav').style.display = 'block'; // show navigation \\
		document.getElementById('articleNav-top').style.display = 'block'; // show navigation \\
		
		bT.clickOn('all');
		bT.on('all');
			
		goArticle(articleArray.findIndex(new_pos)); // lookup array index for article \\
		if(new_pos == articleArray[0]){
			firstArticle();
			bT.clickOff('next');
			bT.off('next');
		}
		else {bT.clickOn('next')}
	}
}