//
// Vertical Scrolling DIV
// (C) 2006 Random Sequence / Johnnie Walker
//
// THIS CODE IS STILL UNDER ACTIVE DEVELOPMENT. IT IS NOT FINAL.
// And, it may absolutely not be used without express written permission from Random Sequence / Johnnie Walker.
// In short, a cleaned up / public release is planned for the future. Please keep an eye on www.randomsequence.com

function rsVScroller(containerObj,maskObj,contentObj) {

	this.setPage = function(page){
		this.currentPage = page;
		for (var i=0; i<this.pagers.length; i++) {
			this.pagers[i].className = "rsScrollerPager";
		}
		this.pagers[page].className = "rsScrollerPagerCurrent";
		this.scrollToPage();		
	};              
	
	this.nextPage = function() {
		if (this.currentPage < (this.totalPages-1)) {
			this.setPage(this.currentPage+1);
		}
	}                                         
	
	this.previousPage = function() {
		if (this.currentPage > 0) {
			this.setPage(this.currentPage-1);
		}		
	}
	 
	this.scrollToPage = function() {
		var targetScrollTop = me.maskHeight * me.currentPage;
		var currentScrollTop = me.mask.scrollTop;
		
		var willMove = false;
		
		var distance = Math.sqrt(Math.pow(targetScrollTop - currentScrollTop,2));
		var speed = Math.floor(distance/8)+1;				

		if (currentScrollTop < targetScrollTop) {
			me.mask.scrollTop = (currentScrollTop+speed);
			willMove = true;
		} else if (currentScrollTop > targetScrollTop) {
			me.mask.scrollTop = (currentScrollTop-speed);
			willMove = true;
		}
		
		if (willMove) {
			setTimeout(me.scrollToPage,15);
		}		
	}
	
	var me = this;

	this.container = containerObj;
	this.mask = maskObj;
	this.content = contentObj;

	this.scrollPosition = 0;
	this.currentPage = 0;

	this.totalHeight = this.content.scrollHeight;
	this.maskHeight = this.mask.offsetHeight;	
	this.totalPages = Math.floor(this.totalHeight/this.maskHeight);
	if (this.totalHeight - (Math.floor(this.totalHeight/this.maskHeight) * this.maskHeight) > 10) {
		this.totalPages++;
	}
	
	this.content.style.height = this.totalPages * this.maskHeight+"px";
	this.totalHeight = this.content.scrollHeight;	

	this.pagers = new Array;
	this.pagerContainer = document.createElement("div");
	this.pagerContainer.className = "rsScrollerPagerContainer";
	this.container.appendChild(this.pagerContainer);

	if (this.totalPages > 1) {
		
		this.previousAnchor = document.createElement("a");
		var previousAnchorContainer = document.createElement("div");
		previousAnchorContainer.appendChild(this.previousAnchor);
		previousAnchorContainer.className = "rsScrollerPager rsScrollerPagerPrevious";		
		this.previousAnchor.appendChild(document.createTextNode("previous")); 
		this.previousAnchor.setAttribute("href","#previous");  
		this.previousAnchor.scroller = this;                              		
		this.previousAnchor.onclick = function() {this.scroller.previousPage(); return false;};
		this.pagerContainer.appendChild(previousAnchorContainer);
		                                         
		this.pagerContainer.appendChild(document.createTextNode(" "));

		for (var i=0; i<this.totalPages; i++) {
			var pager = document.createElement("a");
/*			pager.setAttribute("href","#");*/
			pager.className = "rsScrollerPager";
			pager.setAttribute("href","#page"+(i+1));  
			pager.onclick = function(){this.scroller.setPage(this.page); return false;};
			pager.scroller = this;
			pager.page = i;		
			pager.appendChild(document.createTextNode(i+1));
			this.pagerContainer.appendChild(pager);   
			this.pagerContainer.appendChild(document.createTextNode(" "));   			
			this.pagers.push(pager);
			
			// now find any other elements classed to link to this page
			var anchors = getElementsByClassName(document,"a",contentObj.id+""+(i+1));
			for (var j=0; j<anchors.length; j++) {
				anchors[j].scroller = this;	
/*				anchors[j].setAttribute("href","#");				*/
				anchors[j].page = i;									
				anchors[j].onclick = pager.onclick;
			}
		}     	   
		                                         
		this.pagerContainer.appendChild(document.createTextNode(" "));  
		this.nextAnchor = document.createElement("a");   				 								
		var nextAnchorContainer = document.createElement("div");				                           
		nextAnchorContainer.appendChild(this.nextAnchor);
		nextAnchorContainer.className = "rsScrollerPager rsScrollerPagerNext"; 		
		this.nextAnchor.appendChild(document.createTextNode("next")); 
		this.nextAnchor.setAttribute("href","#next");
		this.nextAnchor.scroller = this;  
		this.nextAnchor.onclick = function() {this.scroller.nextPage(); return false;};
		this.pagerContainer.appendChild(nextAnchorContainer);     		

		if (this.pagers[0] != null) {						
			this.pagers[0].className = "rsScrollerPagerCurrent";
		}		
	}

	
/*	alert("total:"+this.height+" mask:"+this.maskHeight+" pages:"+this.totalPages+" ");*/
	
}

function setupScrollers() {	
	
	var scrollers = getElementsByClassName(document,"div","rsVerticalScroller");    
	for(var i=0; i<scrollers.length; i++) {
		scrollers[i].normalize();
		var container = document.createElement("div");
		var mask = document.createElement("div");
		container.className = "rsVerticalScrollerContainer";
		mask.className = "rsVerticalScrollerMask";		
		scrollers[i].parentNode.replaceChild(container, scrollers[i]);
		container.appendChild(mask);
		mask.appendChild(scrollers[i]);
		scrollers[i].style.display = "block";		
		var rsVerticalScroller = new rsVScroller(container,mask,scrollers[i]); 
		
		var links = scrollers[i].getElementsByTagName("a");
		for (var j=0; j<links.length; j++) {
			var images = links[j].getElementsByTagName("img");
			if (images[0]) {                         				
				links[j].onclick = function() {
					var movie = document.getElementById('movie');
					if (movie != null) {
						movie.style.display = "none";			
					}
					var mainimg = document.getElementById('mainimg');    
					mainimg.style.display = "block";			
					mainimg.removeAttribute("width");
					mainimg.removeAttribute("height")			
					mainimg.src=this.getAttribute("href"); 						
					document.getElementById('mainimgtitle').innerHTML='<b>'+this.getAttribute("title")+'</b> ';					
					return false;
				}
			}
		}
	}            
}

addLoadEvent(setupScrollers);
