/* 
necessite la librairie Prototype JS 
http://www.prototypejs.org/

*/

/* From v6 framework */
/*
function addEvent( obj, type, fn ) {
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}
*/

/**
 * Added on 20090211
 * @param MyObject
 */
function getLeft(MyObject){
	if (MyObject.offsetParent)
		return (MyObject.offsetLeft + getLeft(MyObject.offsetParent));
	else
		return (MyObject.offsetLeft);
}
function getTop(MyObject){
	if (MyObject.offsetParent)
		return (MyObject.offsetTop + getTop(MyObject.offsetParent));
	else
		return (MyObject.offsetTop);
}


var autoScroll = {
	init: function (pElm){
		
		this.elm = $('sky');
		this.ctn = $('sky_right');

        // Special cases for the page list and offre
        if (!this.elm) {this.elm = $('sky');}
        if (!this.ctn) {this.ctn = $('sky_right');}
		// pour la page offre il y a les 2 menus qui suivent
		if(pElm) this.elm2 = $(pElm);

		if(!this.ctn || !this.elm) return;
		
		this.ctnHeight = this.ctn.getDimensions().height;
		this.elmHeight = this.elm.getDimensions().height;
		//if(this.ctnHeight < this.elmHeight) return;
		
		if(this.elm2)
		{
			this.elm2Height = this.elm2.getDimensions().height;
			if(this.ctnHeight < this.elm2Height) return;
		}
		
		this.initialTop = getTop(this.elm);
		
		this.interval = setInterval(function(oThis){
			return function (){
				oThis.maxHeight = oThis.ctn.offsetHeight;
				oThis.maxScroll = oThis.maxHeight - oThis.elm.offsetHeight;
				if (this.elm2) oThis.maxScroll = oThis.maxHeight - oThis.elm2.offsetHeight;
				
				
				
				oThis.place();
			}
		}(this), 20)
	},

	place: function (){
		var top = document.documentElement.scrollTop - this.initialTop; //remplacer par document.body.scrollTop si marche pas...
		top = top < 0 ? 0 : top;
		top = top > this.maxScroll ? this.maxScroll : top;
		if(top < 0) top = 0;

		
		this.elm.style.marginTop = top +  "px";
		if (this.elm2) this.elm2.style.marginTop = top +  "px";
	}
}


Event.observe(window,"load", function(){ var oSky = autoScroll.init(); });
