
Haedus_ScrollFix_Watched_Elemtens = new Array;

Haedus.Fnc.ScrollFix = {
	
	setElement : function(container) {
		
		if	(null === Haedus.getDom(container)) {
			return false;
		}
		
		var action = this.action(container);
		
		Haedus_ScrollFix_Watched_Elemtens.push(action);
		
	},
	
	action : function(container) {
		
		var element = Haedus.getDom(container);
		
		var position = Haedus.Dom.getOffset(element);
		
		return {
			
			container : container,
			element : element,
			position : position
			
		};
		
	},
	
	watch : function() {
		
		currentTop = Haedus.Fnc.ScrollFix.getScroll().Y;
		
		if (document.height) {
			wHeight = document.height;
		} else if (document.body && document.body.scrollHeight) {
			wHeight = document.body.scrollHeight;
		}
		
		for (i = 0; i < Haedus_ScrollFix_Watched_Elemtens.length; i++) {
			
			with (Haedus_ScrollFix_Watched_Elemtens[i]) {
				
				offset = Haedus.Dom.getOffset(element);
				
				toLow = ((currentTop + position.H) > (wHeight - 230));
				
				if (position.Y > currentTop && 'relative' != element.style.position.toLowerCase()) {
					
					element.style['position'] = 'relative';
					element.style['top'] = '0px';
					
				} else if (position.Y < currentTop && 'fixed' != element.style.position.toLowerCase() && false === toLow) {
					
					element.style['position'] = 'fixed';
					element.style['top'] = '0px';
					
				} else if (position.Y < currentTop && 'fixed' == element.style.position.toLowerCase() && true === toLow) {
					
					element.style['position'] = 'relative';
					element.style['top'] = (wHeight - 230 - position.H - position.Y) + 'px';
					
				}
				
			}
			
		}
		
		
	},
	
	getScroll : function() {
		
		//Netscape compliant
		if( typeof( window.pageYOffset ) == 'number' ) {
			
			return {
				X : window.pageXOffset,
				Y : window.pageYOffset
			};
			
		//DOM compliant
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			
			return {
				X : document.body.scrollLeft,
				Y : document.body.scrollTop
			}
			
		//IE6 standards compliant mode
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			
			return {
				X : document.documentElement.scrollLeft,
				Y : document.documentElement.scrollTop
			}
			
		}
		
		return false;
		
	}
	
};

function initOnScroll() {
	
	if (!document.getElementsByTagName("body")[0]) {
		window.setTimeout("initOnScroll()", 200);
	} else {
		window.onscroll = function(){Haedus.Fnc.ScrollFix.watch();};
	}
	
}

initOnScroll();

