// b3rtScroller 1.1
// Copyright 2010, H. poort
// This software may not be used without a valid license

function b3rtScroller(){this.targetId='';this.scrollerId='';this.scrollerWidth=0;this.scrollerHeight=0;this.scrollerInterval=25;this.scrollerSteps=1;this.pauseOnMouseOver=true;this.debugShowOverflow=false;this.debugDisableClipping=false;this.debugDisableSmartCloning=false;this.debugSmartCloningErrorMargin=0;this.targetElement=null;this.containerElement=null;this.contentElement=null;this.contentCloneElement=null;this.currentPosition=0;this.intervalTimer=null;this.pauseScrolling=false};b3rtScroller.prototype.run=function(){if(((typeof this.targetId)!='string')||(this.targetId==''))return false;if(((typeof this.scrollerWidth)!='number')||(this.scrollerWidth<1))return false;if(((typeof this.scrollerHeight)!='number')||(this.scrollerHeight<1))return false;if((typeof this.scrollerInterval)!='number'||(this.scrollerInterval<=0))return false;if(((typeof this.scrollerSteps)!='number')||(this.scrollerSteps<1)||(this.scrollerSteps>=this.scrollerHeight))return false;if(((typeof this.debugSmartCloningErrorMargin)!='number')||(this.debugSmartCloningErrorMargin<0))return false;if(!document.getElementById)return false;this.targetElement=document.getElementById(this.targetId);if(!this.targetElement)return false;if(!document.createElement||!this.targetElement.appendChild||((typeof this.targetElement.offsetHeight)!='number')||((typeof this.targetElement.offsetTop)!='number'))return false;if(!this.targetElement.cloneNode||!this.targetElement.removeChild)this.debugDisableSmartCloning=true;if(!this.createScrollerElements())return false;this.setEvents();var a=this;this.intervalTimer=window.setInterval(function(){if(a)a.doScroll()},this.scrollerInterval);return true};b3rtScroller.prototype.createScrollerElements=function(){this.contentElement=document.createElement('div');this.containerElement=document.createElement('div');this.contentCloneElement=document.createElement('div');if(!this.containerElement||!this.contentElement||!this.contentCloneElement)return false;this.containerElement.style.position='relative';this.containerElement.style.left='0';this.containerElement.style.top='0';this.containerElement.style.width=this.scrollerWidth+'px';this.containerElement.style.height=this.scrollerHeight+'px';if(!this.debugShowOverflow)this.containerElement.style.overflow='hidden';if(((typeof this.scrollerId)=='string')||(this.scrollerId!=''))this.containerElement.id=this.scrollerId;this.contentElement.style.position='absolute';this.contentElement.style.left='0';this.contentElement.style.top='0';if(!this.debugDisableClipping)this.contentElement.style.clip='rect(0,'+this.scrollerWidth+'px,'+this.scrollerHeight+'px,0)';while(this.targetElement.childNodes.length)this.contentElement.appendChild(this.targetElement.childNodes[0]);this.contentCloneElement.style.position='absolute';this.contentCloneElement.style.left='0';this.contentCloneElement.style.top='0';if(!this.debugDisableClipping)this.contentCloneElement.style.clip='rect(0,'+this.scrollerWidth+'px,'+(this.scrollerHeight+this.scrollerSteps)+'px,0)';this.targetElement.appendChild(this.containerElement);this.containerElement.appendChild(this.contentElement);this.containerElement.appendChild(this.contentCloneElement);return true};b3rtScroller.prototype.setEvents=function(){var a=this;if(this.pauseOnMouseOver){this.addEventHandler(this.containerElement,'mouseover',function(){if(a)a.pauseScrolling=true});this.addEventHandler(this.containerElement,'mouseout',function(){if(a)a.pauseScrolling=false})}};b3rtScroller.prototype.doScroll=function(){if(this.pauseScrolling)return;var a=this.contentElement.offsetWidth;var b=this.contentElement.offsetHeight;if(b<=this.scrollerHeight)return;if((this.currentPosition+this.scrollerHeight)>(b-this.scrollerSteps)){if(this.contentCloneElement.childNodes.length==0){if(!this.debugDisableSmartCloning){for(var i=0;i<this.contentElement.childNodes.length;i++){this.contentCloneElement.appendChild(this.contentElement.childNodes[i].cloneNode(true));if(this.contentCloneElement.offsetHeight>(this.scrollerHeight+this.scrollerSteps+this.debugSmartCloningErrorMargin))break}}else this.contentCloneElement.innerHTML=this.contentElement.innerHTML}}if((this.currentPosition+this.scrollerSteps)>b){this.currentPosition=(0-this.contentCloneElement.offsetTop);if(this.contentCloneElement.childNodes.length>0){if(!this.debugDisableSmartCloning){var c=this.contentCloneElement.childNodes.length;for(var i=(c-1);i>=0;i--)this.contentCloneElement.removeChild(this.contentCloneElement.childNodes[i])}else this.contentCloneElement.innerHTML=''}}this.currentPosition+=this.scrollerSteps;this.contentElement.style.top=(0-this.currentPosition)+'px';this.contentCloneElement.style.top=(b-this.currentPosition)+'px';if(!this.debugDisableClipping)this.contentElement.style.clip='rect('+this.currentPosition+'px,'+this.scrollerWidth+'px,'+(this.scrollerHeight+this.currentPosition)+'px,0)'};b3rtScroller.prototype.addEventHandler=function(a,b,c,d){if(!a||!b||!c)return;var e=((typeof d)=='undefined'?false:d);if(a.addEventListener)a.addEventListener(b,c,e);else if(a.attachEvent)a.attachEvent('on'+b,c);else a['on'+b]=c};
