// default to washington - will be changed by state nav above display panels
var statePage = "washington";

function initAdvisors() {

	if(!document.getElementById('advisor-pages')) return false;

	window['oThumbs'] = getElementsByClassName(document.getElementById(statePage), 'div', 'thumb');
	window['oLocation'] = document.getElementById('adv-location');
	window['oRole'] = document.getElementById('adv-role');
	window['oPhoto'] = document.getElementById('adv-photo');
	window['oName'] = document.getElementById('adv-fullname');
	window['oEmail'] = document.getElementById('adv-email').childNodes[0];
	window['oPhone'] = document.getElementById('adv-phone');
	window['oCell'] = document.getElementById('adv-cell');
	window['oNext']  = document.getElementById('adv-next');
	window['oPrev'] = document.getElementById('adv-prev');

	clearDisplay();

	var oNavWA = document.getElementById('nav-washington');
	var oNavOR = document.getElementById('nav-oregon');
//	var oNavCA = document.getElementById('nav-california');
	
	window['currentPage'] = 0;
	window['pageCount'] = 1;
	var count = 0;
	
	for(var i=0; i<oThumbs.length; i++) {
		
		// store which page this element belongs to - pages comprise as many as nine elements
		oThumbs[i].setAttribute('page', pageCount);
		
		// increment pageCount and count
		if(count == 8 && i < oThumbs.length -1) {
			pageCount ++;
			count = 0;
		} else {
			count ++;
		}
		
		oThumbs[i].onclick = function() {
			// populate display
			oLocation.innerHTML = this.getAttribute('location');
			oRole.innerHTML = this.getAttribute('role');
			var nodePosition = (this.childNodes[0].nodeName == 'IMG') ? 0 : 1;
			oPhoto.childNodes[0].src = this.childNodes[nodePosition].src;
			oName.innerHTML = this.getAttribute('fullname');
			oEmail.innerHTML = this.getAttribute('email');
			oEmail.href = "mailto:" + this.getAttribute('email');
			oPhone.innerHTML = this.getAttribute('phone');
			if (this.getAttribute('cell') && this.getAttribute('cell') != "")
			    oCell.innerHTML = "cell: " + this.getAttribute('cell');
			else oCell.innerHTML = "";
		}
		
		oNext.onclick = function() { return navigatePages() }
		oPrev.onclick = function() { return navigatePages() }
		oNavWA.onclick = function() { statePage = 'washington'; initAdvisors(); return false; }
		oNavOR.onclick = function() { statePage = 'oregon'; initAdvisors(); return false; }
//		oNavCA.onclick = function() { statePage = 'california'; initAdvisors(); return false; }
		
	}

	navigatePages();

	if(pageCount < 2) {
		//oNext.className = 'disabled';
		//oPrev.className = 'disabled';
		document.getElementById('adv-controls').className = 'disabled';
	}

}

function navigatePages(sDir) {
	currentPage ++;
	if(currentPage > pageCount) {
		currentPage = 1;
	}
	var sDisplay = '';
	var bDefaultDisplayed = false;
	for(var i=0; i<oThumbs.length; i++) {
		sDisplay = 'block';
		if(oThumbs[i].getAttribute('page') != currentPage) sDisplay = 'none';
		oThumbs[i].style.display = sDisplay;
		if(!bDefaultDisplayed) {
			if(oThumbs[i].getAttribute('page') == currentPage) {
				oThumbs[i].onclick();
				bDefaultDisplayed = true;
			}
		}
	}
	return false;
}

function clearDisplay() {
	document.getElementById('adv-controls').className = '';
	document.getElementById("nav-washington").className = '';
	document.getElementById("nav-oregon").className = '';
//	document.getElementById("nav-california").className = '';
	document.getElementById("washington").style.display = 'none';
	document.getElementById("oregon").style.display = 'none';
//	document.getElementById("california").style.display = 'none';
	if(!statePage || statePage == '' || !document.getElementById(statePage)) statePage = "washington";
	document.getElementById(statePage).style.display = 'block';
	document.getElementById('nav-' + statePage).className = 'active';
}

addEvent(window, 'load', initAdvisors);