//--------------------------------------------------------------
// Class Scrollbox	(c) BRAINWORXX GmbH 2006-10-31
//					   added some fixes 2007-03-01
//					Author: gabriel.kaufmann@brainworxx.de
//--------------------------------------------------------------

function scrollbox() {
	this.boxobj=false;
	this.flipboxes=false;
	this.bannerOffsets=Array();
	this.effect=false;
	this.effectParam=false;
	this.autostop=false;
	this.timer=40;
	this.timerInterrupt=false;
	this.status=0;
	this.scrollByPx=1;
	
	this.init=function(boxname) {
		document.scrollbox=this;
	}
	
	this.start=function(boxref) {
		if(!this.boxobj) {
			this.boxobj=document.getElementById(boxref);
		}
		if(!this.boxobj) return false;
		
		if(!this.flipboxes) {
			this.flipboxes=this.boxobj.getElementsByTagName("div");
		}
		
		// check if enough elements for rotation
		if(!this.flipboxes[0].hasChildNodes()) {
			return false;	// skip module! no rotation suggestive
		}

		// clone content-box for rotation
		this.flipboxes[0].appendChild(this.flipboxes[0].cloneNode(true));
	
		// indicate banner-offsets of all mirrors
		// added fix for IE/FF in different childNodes indices
		var scanTag=false;
		var flipboxChilds=this.flipboxes[0].childNodes.length;
		for(var i=0;i<flipboxChilds;i++) {
			scanTag=this.flipboxes[0].childNodes[i].tagName;
			if(scanTag!=null)	break;
		}

		this.bannerOffsets=Array();
		if(!document.all) {			// Fix FF
			this.bannerOffsets.push('0');
		}
		var bannerObj=false;
		for(var f=0;f<this.flipboxes.length;f++) {
			bannerObj=this.flipboxes[f].getElementsByTagName(scanTag);
			for(var i=0;i<bannerObj.length;i++) {
				var browserFix=0;
				if(document.all) {	// Fix IE
					browserFix=this.boxobj.offsetTop*-1;
				} else {			// Fix FF
					browserFix=(this.boxobj.offsetTop-2)
				}
				this.bannerOffsets.push(bannerObj[i].offsetTop+browserFix);
			}
		}

		// minimize parent-box to desired box-height
		this.boxobj.style.height=this.boxobj.parentNode.offsetHeight+'px';
		this.boxobj.style.top='1px';
		this.status=1;
		this.scroller();
		return true;
	}
	
	this.pause=function(flag) {
		if(flag) { flag=true; }
		switch(flag) {
			case true: this.status=2;
					break;
			case false: this.status=1;
					this.scroller();
					break;
		}
		return true;
	}
	
	this.autostop=function(flag) {	
		// Event handling
		try {
			if(flag) {
				this.boxobj.onmouseover=function() {
					document.scrollbox.pause(true);
					return false;
				}
				
				this.boxobj.onmouseout=function() {
					document.scrollbox.pause(false);
					return false;
				}
			} else {
				this.boxobj.onmouseover=null;
				this.boxobj.onmouseout=null;
			}
			
			this.autostop=flag;
			return true;
		} catch(e) {
			return false;
		}
		return false;
	}
	
	this.setEffect=function(opt,param) {
		switch(opt) {
			default:			break;	// Apply effect
			case 'singleView':	this.effect=opt;
								this.effectParam=(param>0?param*600:600*10);	// default: 6 sek.
								return true;
			case 'continous':	this.effect=false;
								this.effectParam=false;
								return true;
		}
		
		// Apply effect
		switch(this.effect) {
			case 'singleView':
				 if(inArray(this.bannerOffsets,(parseInt(this.boxobj.style.top)*-1))) {
					this.pause(true);
				 	window.clearTimeout(this.timerInterrupt);
					this.timerInterrupt=window.setTimeout("document.scrollbox.pause(false)", this.effectParam);
  				 }
				return true;
		}
		return false;
	}
	
	this.setSpeed=function(speedInt) {
		this.timer=parseInt(speedInt);
		return true;
	}
	
	this.scroller=function() {
		if(this.status==1) {
		 	window.clearTimeout(this.timerInterrupt);
			this.boxobj.style.top=(parseInt(this.boxobj.style.top)-this.scrollByPx)+'px';
			this.setEffect();
			this.rotateAds();
			this.timerInterrupt=window.setTimeout("document.scrollbox.scroller()", this.timer);
			return true;
		}
		return false;
	}
	
	this.rotateAds=function() {
		if(this.isBoxHidden(this.boxobj,this.flipboxes[1],'top')) {
			// reset banner-loop for endless-looping
			this.boxobj.style.top=-15+'px'
		}
		return true;
	}
	
	this.isBoxHidden=function(parentObj,childObj,inAlign) {
		if(!parentObj) return false;
		try {
			if(inAlign=='top') {
				return Math.ceil(parentObj.offsetTop<(childObj.offsetHeight*-1));
			} else if(inAlign=='bottom') {
				return Math.ceil((childObj.offsetTop)>parentObj.offsetHeight);
			} else {
				return Math.ceil(parentObj.offsetTop<=((childObj.offsetHeight+2)*-1)) || (childObj.offsetTop>parentObj.offsetHeight);
			}
		} catch(e) {
			return false;
		}
		return false;
	}
	
	// self-init
	this.init();
}

//--------------------------------------------
// Functions
//--------------------------------------------
function inArray(arr,match) {
	for(i in arr) {
		if(arr[i]==match) {
			return true;
		}
	}
	return false;
}
// EOF
