/* ********************************
*	SCROLL JAVASCRIPT 
* 	AUTHOR : JP
*	APPLICATION : ISODIS
******************************** */
	
	var scroller = {
		
		active		: false,
		node 		: null,
		pixelMove	: 1,
		absXmin		: 0,
		absYmin		: 0,
		absXmax		: 0,
		absYmax		: 0,
		relYmin		: 0,
		relYmax		: 0,
		height		: 0,
		width		: 0,
		dir			: "down",
		refreshTime	: 100,
		
		
		// Initialize scroller
		ini : function (htmlElement, refreshTime, pixelMove, dir) {
			scroller.node = document.getElementById(htmlElement);
			scroller.refreshTime = refreshTime;
			scroller.pixelMove = pixelMove;
			dir ? scroller.dir = dir : '';
			scroller.setConstraints();
			scroller.setContentProperty();
			scroller.setXY();
			scroller.active="null";
			//scroller.setMinHeight();
		},
		
		// Start scroll
		start : function () {
			if (scroller.active=="null"){
				scroller.active = setInterval("scroller.scrollContent();", scroller.refreshTime);
			}
		},
		
		// End scroll
		stop : function () {
			clearInterval(scroller.active);
			scroller.active="null";
		},
		
		
		// Scroll content
		scrollContent : function () {
			children = scroller.getContent();
			var swap;
			min = scroller.getMinProperty(children, "top");
			// Which element has to be removed first ?
			max = scroller.getMaxProperty(children, "top");
			// Reset position of element
			for (i = 0; i < children.length; i++) {
					switch (scroller.dir) {
						case "down":
							children[i].style.top = parseFloat(children[i].style.top) + scroller.pixelMove +"px";
							if (children[max].visible == 0 && parseFloat(children[i].style.top) >= scroller.relYmax) {
								children[max].style.top = scroller.relYmin - children[i].height + parseFloat(children[min].style.top);
								children[i].visible = 1;
							}
							else
								children[i].visible = 0;
							break;
						case "up":
							children[i].style.top = parseFloat(children[i].style.top) - scroller.pixelMove +"px";
							if (children[min].visible == 0 && parseFloat(children[i].style.top) + children[i].height <= scroller.relYmin) {
								children[min].style.top = children[max].height + parseFloat(children[max].style.top);
								children[i].visible = 1;
							}
							else
								children[i].visible = 0;
							break;
					}
			}	
			
		
		},
		
		// Add scroller event
		onEvent : function (event, fnc) {
			if (document.all) 
				scroller.node.attachEvent("on"+event, fnc)
			else
				scroller.node.addEventListener(event, fnc, true);
		},
		
		// Set scroller constraints
		setConstraints : function () {
			height = scroller.node.offsetHeight;
			//height = scroller.getMinHeight();
			width = scroller.node.offsetWidth;
			Ymin = scroller.getProperty(scroller.node, "offsetTop");
			Xmin = scroller.getProperty(scroller.node, "offsetLeft");
			Ymax = Ymin + height;
			Xmax = Xmin + width;
			scroller.absXmin = Xmin;
			scroller.absYmin = Ymin;
			scroller.absXmax = Xmax;
			scroller.absYmax = Ymax;
			scroller.height = scroller.relYmax = height;
			scroller.width = width;
			scroller.relYmin = scroller.absYmax - scroller.absYmin - scroller.height;
			//window.status = Xmin +"<= X <=" + Xmax+ " ------ " + scroller.relYmin +"<= Y <=" + height;		
		},
		
		// Get any property  for a defined node which properties depend on parent node property
		getProperty : function (node, property) {
			value = 0;
			node = node;
			while (node.nodeName != "HTML") {
				value += node[property];
				node = node.parentNode;
			}
			return value;
		},
		
		
		// get minimun scroller height
		getMinHeight : function () {
			children = scroller.getContent();
			min = scroller.getMinProperty(children, "height");
			//window.status += "Min : "+children[min].offsetHeight;
			return children[min].offsetHeight;
		},
		
		// set minimun scroller height
		setMinHeight : function ()  {
			scroller.node.style.height = scroller.getMinHeight() + "px";
		},
		
		// Retrieve scroller content
		getContent : function () {
			return scroller.node.children;
		},
		
		// Retrieve minimum according to property 
		getMinProperty : function (array, property) {
			min = parseFloat(array[0].style[property]);
			index = 0;
			for (i = 0; i < array.length; i++) {
				if (min < parseFloat(array[i].style[property])) {
					min = min;
				}
				else {
					min = parseFloat(array[i].style[property]);
					index = i;
				}
			}
			return index;
		},
		
		// Retrieve maximum according to property 
		getMaxProperty : function (array, property) {
			max = parseFloat(array[0].style[property]);
			index = 0;
			for (i = 0; i < array.length; i++) {
				if (max > parseFloat(array[i].style[property])) {
					max = max;
					
				}
				else {
					max = parseFloat(array[i].style[property]);
					index = i;
				}
			}
			return index;
		},
		
		// Define properties of childrens to be scrolled
		setContentProperty : function () {		
			children = scroller.getContent();
			for (i = 0; i < children.length; i++) {
				document.all ? decay = 0 : decay = 1;
				children[i].y =  scroller.getProperty(children[i], "offsetTop");
				children[i].x =  scroller.getProperty(children[i], "offsetLeft");
				children[i].height = children[i].offsetHeight;
				children[i].width = children[i].offsetWidth;
				children[i].yrelative = children[i].y - scroller.absYmin;
				children[i].xrelative = children[i].x - scroller.absXmin;
						
				var posY = children[i].yrelative + decay;
				var posX = children[i].xrelative + children[i].width;
				if (posY >= 0 && posY <= scroller.absYmax - scroller.absYmin) {
					children[i].visible = 1;
				}
				else {
					children[i].visible = 0;
				}
				//window.status += children[i].visible+" / "; 
			}
				
		},
		
		
		setXY : function () {
			children = scroller.getContent();
			for (i = 0; i < children.length; i++) {
				children[i].style['position'] = "absolute";
				border = parseFloat(children[i].style['border']);
				isNaN(border) ? border = 0 : '';
				children[i].style['width'] = children[i].width - (2 * border) + "px";
				children[i].style['top'] = children[i].yrelative + border + "px";
				children[i].style['left'] = children[i].xrelative + border + "px";
				//window.status += children[i].style['top'] + " / ";
			}
		}
		
		
		
		

	}
	



	
