/*************************************************************************
	Harrison & Wolf 2005
**************************************************************************
	Script d'ascenseur dynamique
	Date de création: 02/02
	Version: 6.0	
	Auteur: Laurent Gamiette
	Date de modification: 03/02/05
*************************************************************************/

document.onmousedown=catchEvent;
document.onmouseout=catchEvent;
document.onmouseup=catchEvent;
//document.onmousewheel=catchEvent; 

scrollManager = new scrollManagerClass();
	
function reloadDoc()
{
	location.reload();
}

function initScroll()
{
	if (clientIs.ns && !clientIs.dom)
	{
		window.captureEvents(Event.RESIZE );
		window.onresize = reloadDoc;
	}
	else
	{
		window.onresize = updateScroll;
	}

	scrollManager.initialize();

	var scrollObject01 = scrollManager.addScroll();
	
	scrollObject01.addReference("arrowUp","flechehaut","imageflechehaut");
	scrollObject01.setPositionReference("arrowUp","refHaut");
	
	scrollObject01.addReference("arrowDown","flechebas","imageflechebas");
	scrollObject01.setPositionReference("arrowDown","refBas");
	
	scrollObject01.addReference("cursor","curseur","imageCurseur");
	
	scrollObject01.addReference("contentHolder","conteneurFiche","");
	scrollObject01.setPositionReference("contentHolder","refContenu");
	
	scrollObject01.addReference("content","contenu","");
	
	scrollObject01.addReference("scrollBar","scrollBar","");
	scrollObject01.initialize();

}
function updateScroll()
{
	scrollManager.updateScroll();
}
function arrowScroll(scrollId,direction)
{
	//alert(scrollId+" "+direction);
	scrollManager.arrowScroll(scrollId,direction);
}
function cursorScroll(e)
{
	ev = (!e) ? window.event : e ;
	scrollManager.cursorScroll(ev);
}

function stopScroll()
{
	//alert("stop");
	//document.onmousemove=null;
	scrollManager.stopScroll();
	//clearTimeout(timer);
	return false;
}
function catchEvent(e)
{
	ev = (!e) ? window.event : e ;

	scrollManager.catchEvent(ev);
	return false;
}
function scrollManagerClass()
{
	this.className = "scrollManagerClass";
	this.scrollList = new Array();
	this.initialize = function()
	{
		if (clientIs.ns ) document.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP | Event.MOUSEDOWN | Event.MOUSEOVER | Event.MOUSEOUT);
	}
	this.updateScroll = function()
	{
		for(var i=0;i<this.scrollList.length;i++)
			this.scrollList[i].update();	
	}
	this.addScroll = function()
	{	

		this.scrollList[this.scrollList.length] = new scrollClass(this,this.addScroll.arguments);
		var obj= this.scrollList[this.scrollList.length-1];
		obj.setId(this.scrollList.length-1);
		return obj;
	}
	this.arrowScroll = function(scrollId,direction)
	{
		this.scrollList[scrollId].arrowScroll(direction);
	}
	this.stopScroll = function()
	{
		//document.onmousemove=null;
		for(var i=0;i<this.scrollList.length;i++)
			this.scrollList[i].stopScroll();
		//clearTimeout(timer);
	}
	this.cursorScroll = function(ev)
	{
		for(var i=0;i<this.scrollList.length;i++)
			this.scrollList[i].cursorScroll(ev);		
	}
	this.catchEvent = function(ev)
	{
		//ev = (!e) ? window.event : e ;
		for(var i=0;i<this.scrollList.length;i++)
			this.scrollList[i].catchEvent(ev);
			//this.scrollList[i].catchEvent(e);
			//if(clientIs.ns) document.captureEvents.routeEvent(e);
	}
}


function scrollClass(master)
{
	this.master = master;
	this.className ="scrollClass";
	this.cursor = null;
	this.arrowUp = null;
	this.arrowDown = null;
	this.vertical = true;
	this.scrollBar = null;
	this.contentHolder = null;
	this.content = null;
	this.percent = 0;
	this.id = -1;
	this.timer = null;
	this.percentIncrement = 0;
	
	this.lps = 50 //nombre de lignes par seconde (vitesse de défilement)
	this.lineHeight = 12 ; //taille de la typo utilisé dans le layer (valeur intuitive, aux choix de l'integrateur)
	this.ips = 75; // nombre d'images par seconde (taux de rafraichissement)
	this.wheelRatio = 3
	this.referenceList = new Array(); // Liste permettant de mémoriser les références des objects DOM(évite de faire des requêtes multiples)
	
	this.initialize = function()
	{
		this.update();
	}
	this.setId = function(id)
	{
		this.id = id;
	}
	this.getId = function()
	{
		return this.id;
	}
	this.addReference = function(componentType,refName,eventRefImageName)
	{
		switch(componentType)
		{
			case 'cursor':
				this.cursor = new cursorClass(refName,this);
//				this.referenceList.cursor = this.cursor.setEventReference(eventRefImageName);  // on mémorise la référence DOM
				this.referenceList.cursor = eventRefImageName;  // on mémorise la référence DOM
				break;
			case 'arrowUp':
				this.arrowUp = new buttonClass(refName,this);
				//this.referenceList.arrowUp = this.arrowUp.setEventReference(eventRefImageName);  // on mémorise la référence DOM
				this.referenceList.arrowUp = eventRefImageName;  // on mémorise la référence DOM
				break;
			case 'arrowDown':
				this.arrowDown = new buttonClass(refName,this);
				//this.referenceList.arrowDown = this.arrowDown.setEventReference(eventRefImageName);  // on mémorise la référence DOM
				this.referenceList.arrowDown = eventRefImageName;  // on mémorise la référence DOM
				break;
			case 'scrollBar':
				this.scrollBar = new scrollBarClass(refName,this);
				//this.referenceList.scrollBar = this.scrollBar.setEventReference(eventRefImageName);  // on mémorise la référence DOM
				break;
			case 'contentHolder':
				this.contentHolder = new clipAreaClass(refName,this);
				//this.referenceList.contentHolder = this.contentHolder.setEventReference(eventRefImageName);  // on mémorise la référence DOM
				break;
			case 'content':
				this.content = new areaClass(refName,this);
				//this.referenceList.content = this.content.setEventReference(eventRefImageName);  // on mémorise la référence DOM
				break;					
		}

	}
	this.setPositionReference = function(componentType,pointPosition)
	{
		switch(componentType)
		{
			case 'arrowUp':
				this.arrowUp.ref.setPositionReference(pointPosition);
				break;
			case 'arrowDown':
				this.arrowDown.ref.setPositionReference(pointPosition);
				break;
			case 'contentHolder':
				this.contentHolder.ref.setPositionReference(pointPosition);
				break;				
		}
	}
	this.update = function()
	{
		//this.scrollBar.boundary.set(this.arrowUp.boundary.right, this.arrowUp.boundary.bottom, this.arrowDown.boundary.left - this.cursor.ref.boundary.width,this.arrowDown.boundary.top - this.cursor.ref.boundary.height);
		//this.cursor.ref.setPosition(this.arrowUp.boundary.left+this.arrowUp.boundary.width/2 - this.cursor.ref.boundary.width/2,this.scrollBar.boundary.top)

		if(!this.arrowUp || !this.arrowDown)
		 return;

		this.arrowUp.update();
		this.arrowDown.update();
		this.cursor.update();
		this.contentHolder.update();
		this.content.update();
		this.scrollBar.update();
		
		/*
		var arrowUpPos = this.arrowUp.ref.position.getPosition();
		var arrowDownPos = this.arrowDown.ref.position.getPosition();
		
		this.arrowUp.ref.setPosition(arrowUpPos.x,arrowUpPos.y);
		this.arrowDown.ref.setPosition(arrowDownPos.x,arrowDownPos.y);
		*/
		//alert("hello ="+arrowUpPos.x+" "+arrowUpPos.y);
		//this.arrowUp.ref.setPosition(this.arrow)
	}
	
	this.catchEvent = function(ev)
	{	// L'evenment est envoyé par la fonction catchEvent de la classe scrolManagerClass
		//ev = (!e) ? window.event : e ;
		
		sourceObject = (window.event) ? ev.srcElement : ev.target ;
		//alert("catchEvent "+ev.type +" "+sourceObject.id);

		switch(ev.type)
		{
			case "mousewheel":
				
				if(ev.wheelDelta<0)
					this.arrowScroll(this.wheelRatio);
				else
					this.arrowScroll(-this.wheelRatio);
				this.stopScroll();
				break;
			case 'mousemove':
				this.cursorScroll(ev)
				break;
			case 'mouseover':
				break;
			case 'mouseout':
				stopScroll();
				break;
			case 'mouseup':
			
				this.stopScroll();
				document.onmousemove=null;				
				break;
			case 'mousedown':
				var x = (clientIs.ns) ? ev.pageX : ev.x;
				var y = (clientIs.ns) ? ev.pageY : ev.y;
				
				switch(sourceObject.id)
				{						
					case this.referenceList.arrowUp:
						this.arrowScroll(-1);	
						break;
					case this.referenceList.arrowDown:
						this.arrowScroll(1);
						break;
					case this.referenceList.cursor:
						document.onmousemove = catchEvent;
						this.cursor.setOffset(x,y)
						this.cursorScroll(ev);
						break;
					default:
						if(this.scrollBar.inside(x,y))
							this.scrollByPage(x,y);
						break;
				}
				break;
			default:
				break;									
		}
	}
	
	this.leCadreEstAssezGrand = function()
	{
		if(!this.contentHolder)
		{
			alert("!!!Erreur: L'objet Conteneur n\'existe pas. Il faut le déclarer a l\'aide de la fonction: addReference(\'contentHolder\',\'nomDeLaDivDeClipping\',\'\')");
			return true;
		}
		if(!this.content)
		{
			alert("!!!Erreur: L'objet de Contenu n\'existe pas. Il faut le déclarer a l\'aide de la fonction: addReference(\'content\',\'nomDeLaDivDeContenu\',\'\')");
			return true;
		}
		//alert(this.contentHolder.getHeight()+ " "+this.content.getHeight());
		if (this.contentHolder.getHeight() >= this.content.getHeight()) return true;
		return false;
	}
	this.setPercent = function(newValue)
	{
		this.percent = ((newValue<0) ? 0 : ((newValue>100) ? 100 : newValue )); 
		//window.status = this.percent;

		if(!this.cursor)
		{
			alert("!!!Erreur: L'objet Curseur n\'existe pas. Il faut le déclarer a l\'aide de la fonction: addReference(\'cursor\',\'nomDeLaDivCurseur\',\'\')");
			return true;
		}
		this.cursor.update();
		this.content.update();
				//LG050131
	}
	this.getPercent = function()
	{
		return this.percent;
	}
	this.stopScroll = function()
	{
		clearTimeout(this.timer);
		//document.mousemove=null;
	}
	this.arrowScroll = function(sens)
	{
		//si la hauteur du contenu est inférieur à la hauteur du cadre le contenant
		//alert(this.leCadreEstAssezGrand());
		if (this.leCadreEstAssezGrand()) return false;
		//on recalcul l'increment de pourcentage pour respecter la vitesse en prenant en compte les paramètres en cours
		if(!this.contentHolder) 
		{
			alert("Veuillez déclarer contenHolder");
			return ;
		}
		this.percentIncrement = 100*(((this.lps*this.lineHeight)/this.ips)/(this.content.getHeight()-this.contentHolder.getHeight()));
				
		// on calcul le nouveau pourcentage
		var tempPercent = this.getPercent();
		tempPercent += this.percentIncrement*sens;

		this.setPercent(tempPercent);
		this.timer = setTimeout("arrowScroll("+this.getId()+","+sens+")", (1000.0/this.ips));
	}
	this.cursorScroll = function(ev)
	{
		if (this.leCadreEstAssezGrand()) return false;

		var y = (clientIs.ns) ? ev.pageY : ev.y; // lecture de la position (y verticale) de la souris sur l'évènement onmousemove

		//var curseurTopPosition = y - (this.cursor.getHeight()/2.0);
		var curseurTopPosition = y - this.cursor.getOffset().y;
		var tempPercent = Math.max(0,Math.min(100,(100*(curseurTopPosition - this.scrollBar.getPosition().y) / this.scrollBar.getHeight() )));
		this.setPercent(tempPercent);
	}
	this.scrollByPage = function(x,y)
	{
		var cursorPos = this.cursor.ref.getPosition();
		offsetx = x - cursorPos.x;
		offsety = y - cursorPos.y;
		var sens;
		if(offsety<=0)
			sens =-1;
		else
			sens =1;
		
		
		//this.percentIncrement = 100*(((this.lps*this.lineHeight)/this.ips)/(this.content.getHeight()-this.contentHolder.getHeight()));
		this.percentIncrement = 100*((this.contentHolder.getHeight()-this.lineHeight)/this.content.getHeight())
		// on calcul le nouveau pourcentage
		var tempPercent = this.getPercent();
		tempPercent += this.percentIncrement*sens;

		this.setPercent(tempPercent);	
	}	
}

function referenceClass(divName,x,y)
{	
	this.object = MMNOAUTOLINK_findObj(divName); // Nom de la div HTML de reference(layer)

	this.eventReference = null; // Nom de l'image qui déclenche les évènement souris
	
	if(!this.object) return -1;
	// valeurs par defaut

	this.position = new point(x,y);   // a modifier pour une position relative

	this.getStyle = function(obj)
	{	
		return (clientIs.dom) ? this.object.style : this.object;
	}
	this.setPosition = function(x,y)
	{
		this.getStyle().left = x;
		this.getStyle().top = y;

		this.position.set(x,y);
	}
	this.getPosition = function()
	{
		//on remet à jour les positions 
		// en cas de centrage de la page ou autre
		//LG050201
		var tmpStyle = this.getStyle();
		this.setPosition(parseInt(tmpStyle.left),parseInt(tmpStyle.top));
		return this.position;
		
	}
	this.getRef = function()
	{
		return this.object;
	}
	this.setEventReference = function(imageName)
	{
		this.eventReference = MMNOAUTOLINK_findObj(imageName);
		return this.eventReference;
	}
	this.getEventReference = function()
	{
		return this.eventReference;
	}
	this.setPositionReference = function(imageName)
	{
		this.position.setRef(imageName);
	}
}


function buttonClass(divName,master,x,y)
{
	this.master = master;
	this.ref = new referenceClass(divName,x,y);

	this.getRef = function()
	{
		return this.ref.getRef();
	}	
	this.setEventReference = function(imageName)
	{
		return this.ref.setEventReference(imageName);
	}
	this.getEventReference = function()
	{
		return this.ref.getEventReference()
	}
	this.getHeight = function()
	{
		var buttonHeight;

		var tmpReference = this.getRef();
		if (clientIs.ie && !clientIs.dom)
			buttonHeight = parseInt(tmpReference.scrollHeight);

		if (clientIs.ns && !clientIs.dom)
			buttonHeight = parseInt(tmpReference.document.height);
			
		if (clientIs.dom)
			buttonHeight = parseInt(tmpReference.offsetHeight);
				
		return buttonHeight;			
	}
	this.update = function()
	{
		
		var position = this.ref.position.getPosition();
		//alert(position.x+" "+position.y);
		this.ref.setPosition(position.x,position.y);
		this.ref.getStyle().visibility = "visible";
	}	
	this.getPosition = function()
	{
		return this.ref.getPosition();
	}
}
function cursorClass(divName,master,x,y)
{
	this.master = master;
	this.ref = new referenceClass(divName,x,y);
	this.clickOffset = new point(0,0);
	this.ref.object.onmousewheel=catchEvent;
	
	this.getRef = function()
	{
		return this.ref.getRef();
	}	
	this.setEventReference = function(imageName)
	{
		return this.ref.setEventReference(imageName);
	}
	this.getEventReference = function()
	{
		//return this.ref.getEventReference()
	}
	this.update = function()
	{
		
		var position = this.ref.getPosition();
		if(!this.master.scrollBar)
		{
			alert("!!!Erreur: L'objet de scrollBar n\'existe pas. Il faut le déclarer a l\'aide de la fonction: addReference(\'scrollbar\',\'nomDeLaDivScrollbar\',\'\')");
			return true;
		}
		var tmpPosition = parseInt(this.master.scrollBar.getHeight());
		var tmp2position = this.master.scrollBar.getPosition();

		temp = tmp2position.y+ (this.master.percent*tmpPosition)/100.0;
		this.ref.setPosition(tmp2position.x,temp)
		this.ref.getStyle().visibility = "visible";
	}

	this.getPercent = function()
	{
		return this.ref.getPosition();
	}
	this.getHeight = function()
	{
		var cursorHeight;

		var tmpReference = this.getRef();
		if ( clientIs.ie && !clientIs.dom)
			cursorHeight = parseInt(tmpReference.scrollHeight);
		
		if (clientIs.ns && !clientIs.dom)
			cursorHeight = parseInt(tmpReference.document.height);
		
		if (clientIs.dom)
			cursorHeight = parseInt(tmpReference.offsetHeight);
				
		return cursorHeight;			
	}
	this.getOffset = function()
	{
		return this.clickOffset;
	}
	this.setOffset = function(x,y)
	{
		var position = this.ref.getPosition();
		this.clickOffset.x = x - position.x;
		this.clickOffset.y = y - position.y;
		return this.clickOffset;
	}
}
function clipAreaClass(divName,master,x,y)
{	this.master = master;
	this.ref = new referenceClass(divName,x,y);
	
	this.getRef = function()
	{
		return this.ref.getRef();
	}	
	this.setEventReference = function(imageName)
	{
	//	return this.ref.setEventReference(imageName);
	}
	this.getEventReference = function()
	{
	//	return this.ref.getEventReference()
	}
	this.getHeight = function()
	{
		var conteneurHeight;

		var tmpReference = this.getRef();
		if ( clientIs.ie && !clientIs.dom)
			conteneurHeight = parseInt(tmpReference.scrollHeight);
		
		if (clientIs.ns && !clientIs.dom)
			conteneurHeight = parseInt(tmpReference.clip.height);
		
		if (clientIs.dom)
			conteneurHeight = parseInt(tmpReference.offsetHeight);
						
		return conteneurHeight;		
	}
	this.update = function()
	{
			
		var position = this.ref.position.getPosition();
		this.ref.setPosition(position.x,position.y);
		this.ref.getStyle().visibility = "visible";
	}
	
}
function areaClass(divName,master,x,y)
{
	this.master = master;
	this.ref = new referenceClass(divName,x,y);
	//this.boundary = new rect();
	//alert(this.ref.object.id);
	this.ref.object.onmousewheel=catchEvent;
	this.getRef = function()
	{
		return this.ref.getRef();
	}	
	this.setEventReference = function(imageName)
	{	// pas d'evenment programmable sur ce type d'objet
	//	return this.ref.setEventReference(imageName);
	}
	this.getEventReference = function()
	{	// pas d'evenment programmable sur ce type d'objet
	//	return this.ref.getEventReference()
	}
	this.getHeight = function()
	{
		var contenuHeight;
		var tmpReference = this.getRef();
		//alert(tmpReference.clientHeight);
		//alert(this.master.contentHolder);
		if ( clientIs.ie && !clientIs.dom)
			contenuHeight = parseInt(tmpReference.scrollHeight) ;

		if (clientIs.ns && !clientIs.dom)
			contenuHeight = parseInt(tmpReference.document.height) ;

		if (clientIs.dom)
			//contenuHeight = parseInt(tmpReference.offsetHeight) ;
		contenuHeight = parseInt(tmpReference.clientHeight) ;

		return contenuHeight;	
	}
	this.update = function()
	{
		var position = this.ref.getPosition();
			
		var conteneurHeight = parseInt(this.master.contentHolder.getHeight());
		var tmpHeight = Math.max(0,this.getHeight() - conteneurHeight);

		if(!this.master.content)
		{
			alert("!!!Erreur: L'objet de Content n\'existe pas. Il faut le déclarer a l\'aide de la fonction: addReference(\'Content\',\'nomDeLaDivScrollbar\',\'\')");
			return true;
		}
		temp = -(this.master.percent*tmpHeight)/100.0;

		this.ref.setPosition(position.x,temp);
		this.ref.getStyle().visibility = "visible";
	}
	this.getPercent = function()
	{
	}
}

function scrollBarClass(divName,master,x,y)
{
	this.master = master;
	this.ref = new referenceClass(divName,x,y);
	this.pageHeight = 0;
	//this.boundary = new rect();
	this.getRef = function()
	{
		return this.ref.getRef();
	}	
	this.setEventReference = function(imageName)
	{	// pas d'evenment programmable sur ce type d'objet
	//	return this.ref.setEventReference(imageName);
	}
	this.getEventReference = function()
	{	// pas d'evenment programmable sur ce type d'objet
	//	return this.ref.getEventReference()
	}
	this.getHeight = function()
	{
		var scrollBarHeight;
		var arrowUpPos = this.master.arrowUp.getPosition();
		var arrowUpHeight = parseInt(this.master.arrowUp.getHeight());
		var cursorHeight = parseInt(this.master.cursor.getHeight());
		var arrowDownPos = this.master.arrowDown.getPosition();
		var arrowDownHeight = parseInt(this.master.arrowDown.getHeight());

		scrollBarHeight = parseInt(arrowDownPos.y) - (parseInt(arrowUpPos.y) + arrowUpHeight) - cursorHeight ;

		return scrollBarHeight;	
	}
	this.getPosition = function()
	{
		var scrollbarPosition;
		var arrowUpPos = this.master.arrowUp.getPosition();
		var arrowUpHeight = parseInt(this.master.arrowUp.getHeight());
		
		scrollbarPosition = new point(parseInt(arrowUpPos.x),parseInt(arrowUpPos.y)+arrowUpHeight);
		return scrollbarPosition;

	}

	this.update = function()
	{
	   // var percentPerPage = 
		var nlinesPerPage = this.master.contentHolder.getHeight()/this.master.lineHeight;
		this.pageHeight = this.master.content.getHeight()/this.master.lineHeight;
		
		
	}
	this.inside = function(x,y)
	{
		var position =this.getPosition();
		var lheight = this.getHeight();
		var offsetx = x - position.x;
		var offsety = y - position.y;
		//alert(offsetx+ " "+offsety+" " +lheight)
		if(offsetx<0 || offsetx>20) return false;
		if(offsety<0 || offsety>lheight+this.master.cursor.getHeight()) return false;
		return true;
	}
}
