/***********************************************
* Dock Content script- Created by and © Dynamicdrive.com
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full script
***********************************************/

//modified by Oldes (this is just a quick hack - animations is working only for one element)

var offsetfromedge=0      //offset from window edge when content is "docked". Change if desired.
var dockarray=new Array() //array to cache dockit instances
var dkclear=new Array()   //array to cache corresponding clearinterval pointers

function dockit(el, duration,yofs){
this.source=document.all? document.all[el] : document.getElementById(el);
	if(this.source) {
		
		this.source.height=this.source.offsetHeight;
		this.docheight=truebody().clientHeight;
		this.duration=duration;
		this.pagetop=0;
		this.elementoffset=this.getOffsetY();
		this.source.targetY = 0;
		this.source.moving = false;
		dockarray[dockarray.length]=this;
		var pointer=eval(dockarray.length-1);
		var dynexpress='dkclear['+pointer+']=setInterval("dockornot(dockarray['+pointer+'],'+yofs+')",100);';
		dynexpress=(this.duration>0)? dynexpress+'setTimeout("clearInterval(dkclear['+pointer+']); dockarray['+pointer+'].source.style.top=0", duration*1000)' : dynexpress;
		eval(dynexpress);
	}
}

dockit.prototype.getOffsetY=function(){
var totaloffset=parseInt(this.source.offsetTop);
var parentEl=this.source.offsetParent;
while (parentEl!=null){
totaloffset+=parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

dockit.prototype.updatePos=function(){
	var obj = dockarray[0].source;
	var ctop = parseInt(obj.style.top);
	var smooth = 0.1 * (obj.targetY - ctop);
	if(smooth > 0) smooth = Math.ceil(smooth);
	else smooth = Math.floor(smooth);
	
	if(smooth == 0) {
		clearInterval(obj.moving);
		obj.moving = false;
	} else {
		obj.style.top=ctop+smooth+"px";
	}
}


function dockornot(obj,yofs){
	obj.pagetop=truebody().scrollTop;
	var objHeight = parseInt(obj.source.height);
	var ctop = parseInt(obj.source.style.top);

	if (obj.pagetop>(obj.elementoffset + yofs)) { //detect upper offset
		var targetY = 20+obj.pagetop-obj.elementoffset+offsetfromedge;
		
	} else if (obj.pagetop+obj.docheight<obj.elementoffset+objHeight) {//lower offset
		var targetY =  obj.pagetop+obj.docheight-objHeight-obj.elementoffset-offsetfromedge
	} else {
		var targetY = 0;
	}

	obj.source.targetY = targetY
	if((targetY != ctop) && (obj.source.moving==false)) {
		obj.source.moving = setInterval("dockit.prototype.updatePos()",1);	
	}
}


function truebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}


