var sections=[];
var heights=[];
var currentSection=0;
var autoscrolling=false;

function measureSections(){
	sections=[];
	heights=[];
	var lsections=document.getElementsByTagName('section');
	var ourSection="";
	for (var i=0; i<lsections.length; i++){
		ourSection=lsections[i].id;
		sections.push(ourSection);
		var theSectionElement=$("#"+ourSection);
		heights.push(theSectionElement.offset().top);
		//console.log("section: "+ourSection+" height: "+theSectionElement.offset().top);
	}
}

function hideAllSections(){
	var lsections=document.getElementsByTagName('section');
	for (var i=0; i<lsections.length; i++){
		//sections[i].style.display='none';
		var el=document.getElementById('nav-'+lsections[i].id);
		if (el){
			el.className='';
		}
	}
}


function reveal(sectionToReveal){
	autoscrolling=true;
	currentSection=sections.indexOf(sectionToReveal);
	hideAllSections();
	var theSectionToReveal=document.getElementById(sectionToReveal);
	theSectionToReveal.style.display='block';
	var el=document.getElementById('nav-'+sectionToReveal);
	if (el){
		el.className='selected';
	}
	
	if (sectionToReveal=="home"){
		$.scrollTo(0,500);
	} else {
		$.scrollTo(document.getElementById(sectionToReveal),500,{offset:-40});
	}
	
}

$(window).scroll(function () {
	var foundCurrentSection;
	

		var curHeight=$(document).scrollTop();
		
		
		
		if (curHeight<heights[0]-41){
			foundCurrentSection=0;
		} else if (curHeight>=heights[sections.length-1]-41){
			foundCurrentSection=sections.length-1;
		} else {
			for (var i=0; i<sections.length; i++){
				if ((curHeight>=heights[i]-41 && curHeight<heights[i+1]-41)){
					foundCurrentSection=i;
				}
			}
		}
		
		
		var el;
		
		if (foundCurrentSection!=currentSection){
			//console.log("FOUND:"+foundCurrentSection+" TARGET:"+currentSection);
			if (autoscrolling===false){
				currentSection=foundCurrentSection;
				hideAllSections();
				el=document.getElementById('nav-'+sections[currentSection]);
				if (el){
					el.className='selected';
				}
			}
		} else {
			
			if (foundCurrentSection==currentSection && autoscrolling===true){
				autoscrolling=false;
				el=document.getElementById('nav-'+sections[currentSection]);
				if (el){
					el.className='selected';
				}
			}
		
		}
	
});


//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

	var from = Number(arguments[1]) || 0;
	from = (from < 0) ? Math.ceil(from)	: Math.floor(from);
    if (from < 0){
		from += len;
	}
	
	for (; from < len; from++) {
		if (from in this && this[from] === elt){
			return from;
		}
	}
    return -1;
  };
}

$(window).resize(function() {
  measureSections();
});

/*
document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)){ retnode.push(elem[i]);}
	}
	return retnode;
};
*/
