// JavaScript Document

function trimNodes(elt) {
	for(i=0; i<elt.childNodes.length; i++) {
		if(elt.childNodes[i].nodeType == 3) {
			// remover le node
			elt.removeChild(elt.childNodes[i]);
		}
	}
}

/****************************************************/
/*					GESTION DU ZOOM					*/
/****************************************************/
var offsetfrommouse=[10,15]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var currentimageheight = 50;
var currentimagewidth = 250;
var hScreenSecurity = 20;
var vScreenSecurity = 150;

function prepareZoom() {
	if(document.getElementById('autressites')) {
		/*
		var tabPhotos = document.getElementById('autressites').getElementsByTagName('ul');
		
		for(iPZ=0; iPZ<tabPhotos.length; iPZ++) {
			tabPhotos[iPZ].previousSibling.onmouseover = function() {
				this.title = '';
				showtrail(this.nextSibling.firstChild.firstChild.nodeValue,150,'auto');
				//alert(this.nextSibling.firstChild.firstChild.nodeValue);
			}
			tabPhotos[iPZ].previousSibling.onmouseout = function() {
				hidetrail();
			}
		}
		*/
		$$('#autressites ul.detail').each( function(e) {
			var lelien = e.previousSibling;
			while(lelien.nodeType == 3) { lelien = lelien.previousSibling; }
			//lelien.detail = e.innerHTML.replace('<li>','').replace('</li>','').replace('<br>','');
			lelien.detail = e.firstChild;
			while(lelien.detail.nodeType == 3) { lelien.detail = lelien.detail.nextSibling; }
			lelien.detail = lelien.detail.firstChild.nodeValue;
			lelien.onmouseover = function() {
				this.title = '';
				showtrail(this.detail,150,'auto');
			}
			lelien.onmouseout = function() { hidetrail(); }
			
		});
	}
}

function gettrailobj(){
	return document.getElementById("trailimageid").style;
}

function gettrailobjnostyle(){
	return document.getElementById("trailimageid");
}

function truebody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function showtrail(description,zwidth,zheight){
	
	// cration du conteneur de zoom
	var conteneurZoom = document.createElement('div');
	conteneurZoom.id = 'trailimageid';
	document.body.appendChild(conteneurZoom);
	// cration du descriptif s'il est rempli
	if(description != '') {
		var descZoom = document.createElement('p');
		conteneurZoom.appendChild(descZoom);
		var txtDescZoom = document.createTextNode(description);
		descZoom.appendChild(txtDescZoom);
	}
	document.onmousemove=followmouse;
}

function hidetrail(){
	document.onmousemove=killfollowmouse;
	var eltTrail = document.getElementById('trailimageid');
	if(eltTrail) document.body.removeChild(eltTrail);
}

function killfollowmouse(e) {
}

function followmouse(e){
	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	if (typeof e != "undefined"){	// MOZILLA
		if (docwidth - e.pageX < hScreenSecurity){
			xcoord = e.pageX - xcoord - hScreenSecurity - 60; // Move to the left side of the cursor
		} else {
			xcoord += e.pageX;
		}
		if (docheight - e.pageY < (currentimageheight + vScreenSecurity)){
			ycoord += e.pageY - Math.max(0,(vScreenSecurity + currentimageheight + e.pageY - docheight - truebody().scrollTop));
		} else {
			ycoord += e.pageY;
		}
	} else if (typeof window.event != "undefined"){		// IE
		if (docwidth - event.clientX < hScreenSecurity){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - hScreenSecurity - 60; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (currentimageheight + vScreenSecurity)){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(vScreenSecurity + currentimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}

	if(ycoord < 0) { ycoord = ycoord*-1; }
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"
	
}

/****************************************************/
/*				INITIALISATION DE LA PAGE			*/
/****************************************************/
function addLoadListenerPage(func) {
	if (window.addEventListener) {
		window.addEventListener("load", func, false);
	} else if (document.addEventListener) {
		document.addEventListener("load", func, false);
	} else if (window.attachEvent) {
		window.attachEvent("onload", func);
	}
}

if (document.getElementById && document.createTextNode) {
	addLoadListenerPage(function() {
		prepareZoom();
	});
}