/*
 * Resource : /javascript/util/MiniProfil.class.js
 */


	VNS.util.MiniProfil = Class.create();
	VNS.util.MiniProfil.prototype = {
		
		params : {
			distance : 7,								// pixels
			delay : 0.0,								// seconds !! Please don't change this value without asking TL !!
			src : '/public/miniprofile/index.jsp'		// url
		},

		setAttributes : function(elt){
			var state = {
				exists		: false,
				position	: 'undefined',
				dimensions	: 'undefined',
				flownOver	: false,
				visible		: false,
				filledIn	: false
			};
			elt.state = state;
		},

		initialize : function(handler, memberId) {
		
			if (arguments[1]){
				this.options = arguments[1];
				if (this.options.src) 		this.params.src 		= this.options.src;
				if (this.options.distance) 	this.params.distance 	= this.options.distance;
				if (this.options.delay) 	this.params.delay 		= this.options.delay;
			}
		
			this.window				= (window.event) ? document.body : window; // IE Hack
			this.handler			= $(handler);
			this.memberId			= memberId;
			
			this.setAttributes(this.handler);
			
			this.getCoordinates(this.handler);
			this.getDimensions(this.handler);
			
			Event.observe(window, 'resize', function(){
				this.getCoordinates(this.handler);
				this.getDimensions(this.handler);
			}.bind(this));
			
			this.createcanvas();
			
			this.eltListenerSwitcher(this.window, 'on', this.updateflyOvers.bind(this));
			this.eltListenerSwitcher(this.handler, 'on', this.fillInCanvas.bind(this));
		
		},
		
		createcanvas : function(){
		
			var canvas = Builder.node('div', { className : 'mini-profile shadow' });
			document.body.appendChild(canvas);
			
			this.canvas = canvas;
			this.setAttributes(this.canvas);
			this._hide(this.canvas);
			this.canvas.exists = true;
		
		},
		
		canvasCss : function(){
			
			var windowDim	 		= this.getWindowDim();
			var windowScrollDepth 	= document.documentElement.scrollTop || document.body.scrollTop;
			var handlerPos 			= this.handler.state.position;
			var handlerWidth 		= this.handler.state.dimensions.width;
			var canvasWidth  		= this.canvas.state.dimensions.width;
			var canvasHeight  		= this.canvas.state.dimensions.height;

			var topOverflow		= parseInt((handlerPos[1] + canvasHeight - windowDim[1] - windowScrollDepth), 10);
			var rightOverflow	= parseInt((handlerPos[0] + handlerWidth + canvasWidth - windowDim[0]), 10);

			var style = {
				position : 'absolute',
				top : ( (topOverflow > 0) ? handlerPos[1] - topOverflow : handlerPos[1] ) + "px",
				left: ( (rightOverflow > 0) ? handlerPos[0] - canvasWidth - this.params.distance : handlerPos[0] + handlerWidth + this.params.distance ) + "px"
			};
			
			return style;
		},
		
		fillInCanvas : function(event){
			
			var updater = function(){
				
				if(this.handler.state.flownOver){
				
					new Ajax.Updater({ success: this.canvas }, this.params.src, {
						parameters: 'paramControl=valide&memberId='+this.memberId,
						onComplete : function(){
						
							this.canvas.state.filledIn = true;
							this.getDimensions(this.canvas);
							
							this.canvas.setStyle(this.canvasCss());
							this._show(this.canvas);
							
							Event.observe(window, 'resize', function(){
								this.getCoordinates(this.handler);
								this.getDimensions(this.handler);
								this.canvas.setStyle( this.canvasCss() );
							}.bind(this));
							
							this.eltListenerSwitcher(this.handler, 'off', this.fillInCanvas.bind(this));
							
							if(this.canvas.down('.close')) {
								this.closer = this.canvas.down('.close');
								this.closer.observe('click', function(){this._hide(this.canvas);}.bind(this));
							}
							
						}.bind(this)
					});
				
				}
			}.bind(this);
			
			if(!this.canvas.state.filledIn) updater.delay(this.params.delay);
			
		}, 

		updateflyOvers : function(event){
			var element = $( Event.element(event) );
			this.canvas.state.flownOver 	= (this.canvas		== element || element.descendantOf(this.canvas) );
			this.handler.state.flownOver 	= (this.handler		== element || element.descendantOf(this.handler) );
			this.initbehavior();
		},
		
		initbehavior : function(){
			if(this.canvas.state.filledIn) {
			
				if(this.canvas.state.visible){
					if( !this.handler.state.flownOver	&&	!this.canvas.state.flownOver	){ this._timedCanvasHide(); }
				}
				else if(!this.canvas.state.visible){
					if( this.handler.state.flownOver	&&	this.canvas.state.flownOver		){ this._timedCanvasShow(); }
					if( this.handler.state.flownOver	&&	!this.canvas.state.flownOver	){ this._timedCanvasShow(); }
					if( !this.handler.state.flownOver	&&	!this.canvas.state.flownOver	){ this._timedCanvasHide(); }
				}
				
			}
		},
		
		eltListenerSwitcher : function(element, choice, callback){
			if('off' == choice) {
				Event.stopObserving(element,'mouseover', callback);
			} else {
				Event.observe(element, 'mouseover', callback);
			}
		},

		getCoordinates : function(elt){
			elt.state.position 		= elt.cumulativeOffset();
		},

		getDimensions : function(elt){
			elt.state.dimensions	= elt.getDimensions();
		},
		
		_hide : function(elt){$_VNS(elt).cssHide(); elt.state.visible = false; },
		_show : function(elt){
			elt.hide();
			$_VNS(elt).cssShow();
			new Effect.Appear(elt, {duration: 0.1});
			elt.state.visible = true;
			this.canvas.setStyle(this.canvasCss());
		},
		
		_timedCanvasHide : function(){
			var hide = function(){ if( !this.handler.state.flownOver	&&	!this.canvas.state.flownOver ) this._hide(this.canvas); }.bind(this);
			hide.delay(0.25);
		},
		_timedCanvasShow : function(){
			var show = function(){ if( this.handler.state.flownOver || this.canvas.state.flownOver ) this._show(this.canvas); }.bind(this);
			show.delay(this.params.delay);
		},
		
		getWindowDim: function() { // dimensions fenetre navigateur
			var width 	= 0;
			var height 	= 0;
			if ( typeof(window.innerWidth) == 'number' ) {
				//Non-IE
				width 	= window.innerWidth;
				height 	= window.innerHeight;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				//IE 6+ in 'standards compliant mode'
				width 	= document.documentElement.clientWidth;
				height 	= document.documentElement.clientHeight;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				//IE 4 compatible
				width 	= document.body.clientWidth;
				height 	= document.body.clientHeight;
			};
			return [width, height];
		}
	 
	};
/*
 * Resource : /javascript/highlightcomments.js
 */

function highlightWord(node,word, niv) {	   	
	// On itère sur les fils
	var hi_cn;
	if (node.hasChildNodes && niv < 16) {		
		if (node.nodeType == 3) {traiteNoeud(node, word);}		
		else
			{
				niv++; 
				for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {			
					highlightWord(node.childNodes[hi_cn],word,niv);
				}
		}
	}
}


function traiteNoeud(node, word){
// on traite le noeud lui-meme
		
	tempNodeVal = node.nodeValue;	
	
	if (node.nodeValue.indexOf(word) != -1 && node.nodeValue.indexOf(word) < 6 ) {		
			pn = node.parentNode;						
			
			node.replaceData(tempNodeVal.indexOf(word),1,"  "); // Ajout de 2 espaces : 1 qui sera la petite barre de citation et l'autre pour un décalage

			nv = node.nodeValue;						
			ni = tempNodeVal.indexOf(word);
						
			before = document.createTextNode(nv.substr(0,ni));
			docWordVal = nv.substr(ni,tempNodeVal.length + 1 ); // +1 car on a ajouter 2 espaces avant !!!
			after = document.createTextNode(nv.substr(ni+tempNodeVal.length + 1 )); // +1 car on a ajouter 2 espaces avant !!!

			hiwordtext = document.createTextNode(docWordVal);				
			hiword = document.createElement("span");				
			hiword.className = "comments";				
							
			hiword.appendChild(hiwordtext);
			pn.insertBefore(before,node);
			pn.insertBefore(hiword,node);
			pn.insertBefore(after,node);
			pn.removeChild(node);

	}	
}

function highlightQuote() {
	var niv=0;
	if (!document.createElement) return;		                																																		
	var stop = true;
	for (j=0;stop;j++){								
		lesResultats = document.getElementById('result'+j);								
		if (lesResultats != null) {highlightWord(lesResultats,">",niv)}	else {stop = false;}
	}			

}



