// JavaScript Document

addEvent(window, 'load', initializeFlowLists, false);


function initializeFlowLists(){
	
	var bodyEl = document.getElementsByTagName("body")[0];
	lists = getElementsByClassName(bodyEl, "ul", "flowlist");
	
	for(var i = 0; i <lists.length; i++ ){
		new FlowList(lists[i]);
	}
}

function FlowList(list){
	
	this.list = list;
	
	this.listItems = this.list.getElementsByTagName("li");
	
	this.currentIndex = 0;
	
	var posOfHash = location.href.lastIndexOf('#');
	if (posOfHash != -1)
	{
	  // move past the hash
	  posOfHash = posOfHash + 1;
	  
    var idTag = location.href.substr(posOfHash, location.href.length-posOfHash);
	
	  for (lstInd = 0; lstInd<this.listItems.length; lstInd++)
	  {
	    if (this.listItems[lstInd].id == idTag)
	    {
	      this.currentIndex = lstInd;
	      break;
	    }
	  }
	}
	
	this.refreshListItems();
	
	this.previousButton;
	this.nextButton;
	
	this.addButtons();	
}

FlowList.prototype.refreshListItems = function(){
	
	
	for(var i = 0; i < this.listItems.length; i++){
		if(i!=this.currentIndex){
			this.listItems[i].style.display = "none";
		}
		else{
			this.listItems[i].style.display = "block";
		}
	}
}

FlowList.prototype.addButtons = function(){
	
	
	var myself =this;
	
	this.previousButton = document.createElement("a");
	this.previousButton.style.display="none";
	this.previousButton.innerHTML ="previous";
	this.previousButton.className="previousButton";
	this.list.parentNode.appendChild(this.previousButton);
	addEvent(this.previousButton, "click", function (evt){myself.previousButtonClicked(evt);}, false);
	
	this.nextButton = document.createElement("a");
	this.nextButton.innerHTML ="next";
	this.nextButton.className="nextButton";
	this.list.parentNode.appendChild(this.nextButton);
	addEvent(this.nextButton, "click", function (evt){myself.nextButtonClicked(evt);}, false);

  this.enableButtons();	
}

FlowList.prototype.enableButtons = function(){
  if (this.currentIndex != this.listItems.length-1){
	  this.nextButton.style.display="block";
	}
	else
	{
	  this.nextButton.style.display="none";
	}
	
	if (this.currentIndex != 0){
	  this.previousButton.style.display="block";
	}
	else
	{
	  this.previousButton.style.display="none";
	}
}

FlowList.prototype.previousButtonClicked = function(){
	
	if(this.currentIndex == 0){
		return;
	}
	else{
		new Effect.Opacity(this.listItems[this.currentIndex], {duration:2, from:1.0, to:0});
		this.listItems[this.currentIndex].style.display="none";
		this.currentIndex--;
		new Effect.Opacity(this.listItems[this.currentIndex], {duration:2, from:0, to:1.0});
		this.listItems[this.currentIndex].style.display="block";
		
	}
	
  this.enableButtons();	
}

FlowList.prototype.nextButtonClicked = function(){
		
	if(this.currentIndex == (this.listItems.length-1)){
		return;
	}
	else{
		new Effect.Opacity(this.listItems[this.currentIndex], {duration:2, from:1.0, to:0});
		this.listItems[this.currentIndex].style.display="none";
		this.currentIndex++;
		new Effect.Opacity(this.listItems[this.currentIndex], {duration:2, from:0, to:1.0});
		this.listItems[this.currentIndex].style.display="block";
		
	}
	this.enableButtons();
}



