/*
	handles the scrolling of the siteLoaderObj site
	
	author: luegs.ch
	for: networking.ch
*/

siteScroller=function() { //constructor
	this.siteLoaderObj=null; //information about the loaded site i.e browser type/version
	this.action=null; //the interval action
	this.doAutoScroll=false; //we do an automatic scroll, somehow an auto scroll is always a manual scroll @ the end (hash jump)
	this.userHitEnd=false; //user was already on bottom of the page
	this.dBoarder=500; //boarder value (manual scroll distance from link)
	this.fBoarder=998500; //no bottom content over this boarder! > display:none
	this.bottlePosition=null; //initial bottle position
	this.cBottlePosition=null; //current bottle position
	//this.playDrop=false; //mineralienkunde drop plays or not
}

/*
	saves the site loader object
*/
siteScroller.prototype.create=function(obj) {
	this.siteLoaderObj=obj;
	//alert("in scroller object, siteLoader navpicname: "+this.siteLoaderObj.navPicName);	
}

/*
	move the site to the end, where link is positioned
	specific speedfactors for specific distances
*/
siteScroller.prototype.scrollSite=function(link,end) { //scroll the site from currentypos to the end position (up/down) with speedup/speedown
	//alert("auto scroll to link: "+link+" on pos: "+end);
	//clear action if running!
	clearInterval(this.action);
	this.doAutoScroll=true;
	//correct the bottlepos shower position if not the end
	if(link.indexOf("b") == -1) {
		var bottomcontent=document.getElementById('bottomdiv');
		bottomcontent.style.display='none';
	}
	if(end!=0) {
		this.positionCorrection(10);
	}
	var start=this.getCurrentYPos(); //the start pos
	var self=this; //a link to ourself
	var delta=end-start; //the delta between the pos
	
	if(delta<0) {
		if(delta>-100) {
			//alert("jump direct in delta >-100");
			location.hash=link;
			this.hitContent(link); //do we have to place content?
			//this.dropFall();
			return;
		}
	} else {
		if(delta<100) {
			//alert("jump direct in delta <100");
			location.hash=link;
			this.hitContent(link); //do we have to place content?
			//this.dropFall();
			return;
		}
	}
	var middle=0; //the middle > before middle, speedup, after middle, speeddown
	var pos=0; //the pos to scrollto
	if(delta < 0) {
		middle=start-parseInt((0-delta)/2);
	} else {
		middle=start+parseInt(delta/2);
	}
	//alert("start: "+this.getCurrentYPos()+" end: "+end+" delta: "+delta+" middle: "+middle);
	var sFrame=1;
	var wasYPos=0;
	this.action=setInterval("scrolling()",20);
	var isYPos=0;
	var isAboveNow=null;
	var isAbove=null;
	var middlePassed=false;
	var speedfac=8;
	if(delta<0) { //select a speed
		if((delta<-100000)&&(delta>-400000)) {
			speedfac=8*speedfac;
			//alert("speedfac: "+speedfac);
		}
		if((delta<-400000)&&(delta>-800000)) {
			speedfac=12*speedfac;
			//alert("speedfac: "+speedfac);
		}
		if(delta<-800000) {
			speedfac=15*speedfac;
			//alert("speedfac: "+speedfac);
		}
	} else {
		if((delta>100000)&&(delta<400000)) {
			speedfac=4*speedfac;
			//alert("speedfac: "+speedfac);
		}
		if((delta>400000)&&(delta<800000)) {
			speedfac=8*speedfac;
			//alert("speedfac: "+speedfac);
		}
		if(delta>800000) {
			speedfac=15*speedfac;
			//alert("speedfac: "+speedfac);
		}
	}
	//alert("pos: "+pos);
	scrolling=function() {
		//alert("scrolling!");
		var step=0; //step
		wasYPos=self.getCurrentYPos(); //save before scroll!
		if(delta<0) { //scroll from bottom to top
			isAbove = (wasYPos > end);
			//alert("scrolling to aiiigggghhhh: "+pos);
			if(pos!=0) {
				window.scrollTo(0,pos); //eff scrolling
				isYPos=self.getCurrentYPos();
			}
			isAboveNow = (self.getCurrentYPos() > end);
			if ((sFrame>1)&&((isAbove != isAboveNow) || (wasYPos == isYPos))) { //1 wait-frame-hack to begin, ugly?
				//alert("clear action!");
				sFrame=1;
				window.scrollTo(0,end);
				clearInterval(self.action);
				self.doAutoScroll=false;
				location.hash=link;
				//self.dropFall();
				self.hitContent(link);
			} else {
				if(self.getCurrentYPos()>middle) { //speedup
					//alert("speedup, pos: "+self.getCurrentYPos());
					step=speedfac*sFrame;
					sFrame++;
				} else { //speeddown
					if(wasYPos>middle) { //we passed middle right now! > speed correction
						//alert("sFrame: "+sFrame);
						sFrame=Math.floor(sFrame/middle*(0-(self.getCurrentYPos()-end-start)));
						//alert("sFrame: "+sFrame);
					}
					//alert("speeddown, pos: "+self.getCurrentYPos());
					step=0-(-speedfac*sFrame);
					sFrame--;
				}
				//pos=Math.ceil(end+self.getCurrentYPos()-step);
				if(sFrame==1) {
					pos=Math.ceil(start+self.getCurrentYPos()-step);
				} else {
					pos=Math.ceil(self.getCurrentYPos()-step);
				}
				//alert("pos < delta: "+pos+" start @ "+start);
			}
		} else { //scroll from top to bottom
			isAbove = (wasYPos < end);
			//alert("scrolling to aiiigggghhhh: "+pos);
			if(pos!=0) {
				window.scrollTo(0,pos); //eff scrolling
				isYPos=self.getCurrentYPos();
			}
			isAboveNow = (self.getCurrentYPos() < end);
			if ((sFrame>1)&&((isAbove != isAboveNow) || (wasYPos == isYPos))) { //1 wait-frame-hack to get the ass up from the seat, ugly?
				//alert("clear action!");
				sFrame=1;
				window.scrollTo(0,end);
				clearInterval(self.action);
				self.doAutoScroll=false;
				location.hash=link;
				//self.dropFall();
				self.hitContent(link);
			} else {
				if(self.getCurrentYPos()<middle) { //speedup
					//alert("speedup, pos: "+self.getCurrentYPos());
					step=speedfac*sFrame;
					sFrame++;
				} else { //speeddown
					if(wasYPos<middle) { //we passed middle right now! > speed correction
						//alert("sFrame: "+sFrame);
						sFrame=Math.ceil(sFrame/middle*(end-self.getCurrentYPos()+start));
						//alert("sFrame: "+sFrame);
					}
					//alert("speeddown, pos: "+self.getCurrentYPos());
					step=0-(-speedfac*sFrame);
					sFrame--;
				}
				
				//pos=Math.ceil(start+self.getCurrentYPos()+step);
				if(sFrame==1) {
					pos=Math.ceil(start+self.getCurrentYPos()+step);
				} else {
					pos=Math.ceil(self.getCurrentYPos()+step);
				}
				//alert("step > delta: "+step+" start @ "+start);
			}
		}
		//window.document.showpos.SetVariable("pos",self.getCurrentYPos().toString()); //update flash movie!
		set_position(pos); //call in the main javascript section!
	}
}

/*siteScroller.prototype.dropFall=function() {
	//alert("drop fall screen innerheight: "+window.innerHeight);
	var element=document.getElementById("tropfen");
	var self=this;
	element.style.display='';
	var sFrame=1;
	var pos=1;
	var action=setInterval("dropMove()",20) //move the drop every n ms
	dropMove=function() {
		var speed=2;
		var mstep=0;
		if(pos>window.innerHeight+5) { //+5 security marge
			clearInterval(action);
			element.style.top="0px";
			element.style.display='none';
		}
		mstep=sFrame*speed;
		pos+=Math.ceil(mstep);
		//alert("new pos: "+pos);
		element.style.top=pos.toString()+"px";
		sFrame+=0.5;
	}
}*/

/*
	fits menu positions when we hit the end! Manual or with autoscroll
*/

siteScroller.prototype.positionCorrection=function(position) { 
	var el=document.getElementById('bottlepos');
	if(position!=this.cBottlePosition) {
		this.cBottlePosition=position;
		el.style.top=this.cBottlePosition+"px";
		//alert("fit menu position to: "+position+"px");
	}
}

siteScroller.prototype.showIntroTitle=function() {
	var el=document.getElementById('bottomdiv');
	var del=document.createElement('div');
	del.style.cssText='margin-left:80px';
	del.className='valsertaketime';
	del.innerHTML="<img src='../_img/intro_"+this.siteLoaderObj.lang+".gif' />";
	el.appendChild(del);
	el.style.display='';
	if(!this.siteLoaderObj.tux) {
		mov=(navigator.appName.indexOf ("Microsoft") !=-1)?window["drpb"]:document["drpb"];
		mov.playMovie('ende_cropped.flv');
	}
}

siteScroller.prototype.removeIntroTitle=function(link) {
	//alert("remove intro title");
	var el=document.getElementById('bottomdiv');
	el.innerHTML='';
}

siteScroller.prototype.placeContent=function(link,self) {
	//alert("we would place content for:"+link)
	if(link!=null) {
		var el=document.getElementById('bottomdiv');
		var str=self.siteLoaderObj.getContent(link);
		el.innerHTML=str;
		//el.style.display='';
		//flash movie handling
		//alert("link is: "+link+" collapsedSortiment: "+self.siteLoaderObj.collapsedSortiment);
		if((link=='b1')||(link=='b2')||(link=='b3')||(link=='b15')) {
			expand();
			getMovie().selectCat(link);
		} else {
			collapse();
		}
		//alert("we would place content for:"+link)
		self.siteLoaderObj.setBottomContentHeight(link);
		el.style.display='';
		self.siteLoaderObj.setBottomScrollHandler()
	}
}

/*
	check what content we hit if autoscroll is finished
*/

siteScroller.prototype.hitContent=function(link) {
	if(this.siteLoaderObj.ie) { //ie6 hack for not fixed positions!
		//alert("ie in version: "+this.siteLoaderObj.version);
		this.ieContentCorrection();
		
	}
	if(link!=null) {
		if(link.indexOf('m5') != -1) { //play the drop flv
			//alert("play movie");
			if(!this.siteLoaderObj.tux) {
				mov=(navigator.appName.indexOf ("Microsoft") !=-1)?window["mdrpb"]:document["mdrpb"];
				mov.playMovie('mineralienkunde.flv');
				this.playDrop=true;
			}
		}
		if((link.indexOf("b") != -1)&&!this.userHitEnd) { //hit the end first time?
			//alert("auto scroll, hit end first time!");
			this.showIntroTitle();
			this.userHitEnd=true;
			this.timer(1000,2,link,this.placeContent,this.removeIntroTitle,endIntro); //play endIntro after 1000*2 milliseconds
			//this.positionCorrection(10);
		} else if(link.indexOf("b") != -1){ //not the first time!
			//alert("hit the end again!");
			this.placeContent(link,this);
			//debug
		}
	}
}

/*
	check if we hit content during this manual scroll
*/
/*siteScroller.prototype.hitContentManual=function() {
	var cpos=this.getCurrentYPos();
	alert("cpos: "+cpos);
	if(cpos<=this.fBoarder){ //not bottom content over this boarder!
		var el=document.getElementById('bottomdiv')
		if(el.style.display=='') {
			alert("cpos<=998000 remove the content! key: "+key);
			el.style.display='none';
		}
	}
	var self=this;
	for(key in this.siteLoaderObj.subMenuLinkPos) {
		pos=parseInt(this.siteLoaderObj.subMenuLinkPos[key]);
		delta=pos-cpos;
		if(delta<0) { //make positive!
			delta=0-delta;
		}
		if((delta<this.dBoarder)&&(key.indexOf("bottom") != -1)&&!this.userHitEnd) { //scroll to the end first time
				this.userHitEnd=true;
				self.showIntroTitle()
				this.timer(1000,2,null,self.placeContent,self.removeIntroTitle,endIntro); //play endIntro after 1000*2 milliseconds
				//alert("we hit the end, key was: "+key);
				//this.positionCorrection(10);
		} else  if((delta<this.dBoarder)&&(key.indexOf("bottom") != -1)){ //show it, if scrolling down to bottom
			var el=document.getElementById('bottomdiv')
			if(el.style.display=='none') {
				//alert("show the content again!");
				el.style.display='';
			}
		} else {
			this.positionCorrection(10);
		}
	}
}*/

siteScroller.prototype.hitContentManual=function() {
	var self=this;
	var cpos=this.getCurrentYPos();
	var posb=this.siteLoaderObj.findPosition('bottom');
	//var posmdrop=this.siteLoaderObj.findPosition('divm5');
	//var drop_delta=cpos-posmdrop;
	var delta=cpos-posb;
	if(delta<0) {
		delta=0-delta;
	}
	/*if(drop_delta<0) {
		drop_delta=0-drop_delta;
	}
	if((drop_delta<this.dBoarder)&&(this.playDrop==false)) {
		if(!this.siteLoaderObj.tux) {
			mov=(navigator.appName.indexOf ("Microsoft") !=-1)?window["mdrpb"]:document["mdrpb"];
			mov.playMovie('mineralienkunde.flv');
		}
		this.playDrop=true;
	}
	if((drop_delta>this.dBoarder)&&(this.playDrop==true)) {
		this.playDrop=false;
	}*/
	if((delta<this.dBoarder)&&(!this.userHitEnd)) {
		this.userHitEnd=true;
		this.showIntroTitle()
		this.timer(1000,2,null,self.placeContent,self.removeIntroTitle,endIntro); //play endIntro after 1000*2 milliseconds
	}
	/*if(delta>this.dBoarder) {
		var el=document.getElementById('bottomdiv')
		if(el.style.display=='') {
			//alert("show the content again!");
			el.style.display='none';
		}
	}*/
	//Debug
	/*if(delta<this.dBoarder) {
		mov=(navigator.appName.indexOf ("Microsoft") !=-1)?window["drpb"]:document["drpb"];
		mov.playMovie('ende_cropped.flv');
	}*/
	//debug
	if(cpos>this.dBoarder) {
		this.positionCorrection(10);
	}
	if(cpos<this.dBoarder) {
		this.positionCorrection(this.bottlePosition);
	}
	if(this.siteLoaderObj.ie) { //ie6 hack for not fixed positions!
		//alert("ie in version: "+this.siteLoaderObj.version);
		this.ieContentCorrection();
		
	}
	if(this.siteLoaderObj.infoBox.showing) {
		this.siteLoaderObj.infoBox.boxPosCorrection();
	}
}

siteScroller.prototype.ieContentCorrection=function() {
	if ((this.siteLoaderObj.version >= 5.5) && (this.siteLoaderObj.version < 7)) {
		var cpos=this.getCurrentYPos();
		mpos = (cpos+10) + 'px';
		if(cpos<this.dBoarder) {
			bpos = (cpos+388) + 'px';
		} else {
			bpos = (cpos+20) + 'px';
		}
		dbpos= (cpos+10) + 'px';
		//alert(mpos);
		document.getElementById('navContainer').style.top=mpos;
		document.getElementById('bottlepos').style.top=bpos;
		document.getElementById('bottomdiv').style.top=bpos;
	}
}

/*
	adds an event handler for scrolling detection
	scroll with: wheel, bars, arrow keys, pgup/down
*/

siteScroller.prototype.userBarScrollHandling=function() {
	var self=this;
		manualScroll=function(e) {
			if(!self.doAutoScroll) { //if it isn't an automatic scroll
				//alert("scrolling manual, position: "+self.getCurrentYPos());
				set_position(self.getCurrentYPos());
				self.hitContentManual();
				e = e ? e : window.event;
				if(e.stopPropagation)
					e.stopPropagation();
				if(e.preventDefault)
				   	e.preventDefault();
				e.cancelBubble = true;
				e.cancel = true;
				e.returnValue = false;
				return false;
		    }
		}
		window.onscroll=manualScroll;
}

/*simple debug method*/
/*siteScroller.prototype.scrollSite=function(end, speed, interval) { //-speed = up; +speed = down
	var action=setInterval("scrolling()",interval);
	var self=this;
	scrolling=function() {
		if(window.pageYOffset<end) { //the real end is somewhere else
			window.scrollBy(0,speed);
		} else {
			clearInterval(action);
			alert("clear interval");
		}
		window.document.showpos.SetVariable("pos",window.pageYOffset.toString());
	}
}*/

/*
	get the current position on the site in a cross browser fassion
*/
siteScroller.prototype.getCurrentYPos=function() { 
	if (document.body && document.body.scrollTop)
    	return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
    	return document.documentElement.scrollTop;
    if (window.pageYOffset)
    	return window.pageYOffset;
    return 0;
}

/*
	call functions (fncTitle > title removing, fncFla > play endintro in flash) after multi*base time in milliseconds
*/
siteScroller.prototype.timer=function(base,multi,lnk,fncContent,fncTitle,fncFla) {
	var self=this;
	var ctr=0;
	var action=setInterval("timecount()",base);
	timecount=function() {
		if(ctr==multi) {
			clearInterval(action);
			//alert("clear timer action");
			fncTitle();
			fncContent(lnk,self);
			fncFla();
			return;
		}
		ctr++;
	}
}
