//------------------------------------------------------------------------
// desactivation de la selection
//------------------------------------------------------------------------
// fonction pour désactiver la selection
function selection_off(e) { return false; }
// fonction pour réactiver la selection
function selection_on() { return true; }
// initialise le systeme de desactivation de la selection
document.onselectstart=new Function ("return false");

if (window.sidebar) {
  document.onmousedown=selection_off;
  document.onclick=selection_on;
}
//------------------------------------------------------------------------
// determine le navigateur et sa version (IE, Netscape4.78, Netscape6.1, Opera, Mozilla, Firefox)
//------------------------------------------------------------------------
function detnav() {
  var p,nav,ver; // variables locales
  result= new Array();
   with(navigator.userAgent){
     switch(navigator.appName){
      case "Microsoft Internet Explorer" :
        nav='IE'; p=indexOf("MSIE"); ver= parseFloat(substring(p+5,p+9));
        if (indexOf("Opera")>-1){nav='Opera'; p=indexOf("Opera"); ver= parseFloat(substring(p+6,p+10));}
        break;
      case "Netscape" :
        nav='Netscape'; ver= parseFloat(substring(8,12));
        if (indexOf("Gecko")>-1){nav='Mozilla'; p=indexOf("rv:"); ver= parseFloat(substring(p+3,p+7));}
        if (indexOf("Safari")>-1){nav='Safari'; p=indexOf("Safari"); ver= substring(p-3,p-6);}
        if (indexOf("Chrome")>-1){nav='Chrome'; p=indexOf("Chrome"); ver= substring(p+7,p+10);}
        if (indexOf("Opera")>-1){nav='Opera'; p=indexOf("Opera"); ver= substring(p+6,p+10);}
        if (indexOf("Netscape6")>-1){nav='Netscape6'; p=indexOf("Netscape6"); ver= parseFloat(substring(p+10,p+14));}
        if (indexOf("Firefox")>-1){nav='Firefox'; p=indexOf("Firefox"); ver= parseFloat(substring(p+8,p+12));}
        break;
      case "Opera" :
        nav='Opera'; p=indexOf("/"); ver= parseFloat(substring(p+1,p+5));
        break;
      default :
        nav='inconnu'; ver='';
     }
  }
  result[0]=nav; result[1]=ver;
  return result;
}
//------------------------------------------------------------------------
// gere la roulette de la souris
//------------------------------------------------------------------------
function scroll_roulette (event){
	var pas = 0;
	var nav = detnav();
	id_scroll = this.id.substr(7);
	var e = event || window.event;
	
	
	switch (nav[0]) {
		case 'Opera':
			pas = e.wheelDelta/20;break;
		case 'IE':
			pas = e.wheelDelta/20;break;
		case 'Chrome':
			pas = e.wheelDelta/40;break;
		case 'Safari':
			pas = e.wheelDelta/20;break;
		default:
			pas = 4*(-e.detail);break;
	}
	
//	alert(e.wheelDelta);

	if (pas) {move_scroll(pas, false);}
	/** Prevent default actions caused by mouse wheel.
	 * That might be ugly, but we handle scrolls somehow
	 * anyway, so don't bother here..
	 */
	if (e.preventDefault) e.preventDefault();
	e.returnValue = false;
}
    
    



//------------------------------------------------------------------------
// initialisation des scrollbars
//------------------------------------------------------------------------
var timer_scroll;
var is_scroll = false;

function initScrollBar() {
	var div=document.getElementsByTagName("div");
	for (id = 0; id < div.length; id++)	{
		if (div[id].className == "scroll_bar") {
			var texte = div[id].innerHTML;
			var lChild  = parseInt(div[id].clientWidth);
			var hChild  = parseInt(div[id].clientHeight);
			var old_hChild = hChild;
			var hScroll = parseInt(div[id].scrollHeight);
			// ajuste la hauteur enfant hChild
			var tmp = parseInt(getCssValue ('style.css','#' + div[id].id, 'paddingLeft'));
			if (!isNaN(tmp))lChild = lChild - tmp;
			var tmp = parseInt(getCssValue ('style.css','#' + div[id].id, 'paddingRight'));
			if (!isNaN(tmp))lChild = lChild - tmp;
			
			if (hScroll > old_hChild && !document.getElementById('scroll_' + id )) {
				texte  = '<div id="scroll_' + id + '" style="width:' + (lChild-20) + 'px; margin-top:0px" class="scroll_left">' + texte + '</div>';
				div[id].innerHTML = texte;
				var div_left = document.getElementById('scroll_' + id);
				hScroll = parseInt(div_left.scrollHeight);
				
				var large = 15;
				var haut  = Math.round((hChild-50) * (hChild/hScroll));
				var fleH = '<img src="../include/scrollbar/scrollhaut.png" alt="Fleche haut scrollbar" width="' + large + 'px" />';
				var fleB = '<img src="../include/scrollbar/scrollbas.png"  alt="Fleche bas  scrollbar" width="' + large + 'px" />';
				
				texte += '<div class="scroll_fleche_haut" onmousedown="scrollBar_on('+ id + ',2);" onmouseup="scrollBar_off();">' + fleH + '</div>';
				texte += '<div id="barre_' + id + '_haut" class="barre_scroll" style="height:5px" onclick="move_barre(' + id + ',1);"></div>';
				texte += '<div class="barre_haut"></div>';
				texte += '<div id="barre_' + id + '" onmousedown="bar_on('+ id + ',event);" style="height:' + (haut) + 'px;" class="barre_centre"></div>';
				texte += '<div class="barre_bas" ></div>';
				texte += '<div id="barre_' + id + '_bas" class="barre_scroll" style="height:' + (hChild-haut-47) + 'px;" onclick="move_barre(' + id + ',-1);"></div>';
				texte += '<div class="scroll_fleche_bas" onmousedown="scrollBar_on('+ id + ',-2);" onmouseup="scrollBar_off();" >' + fleB + '</div>';
				div[id].innerHTML = texte;

				var div_scroll = document.getElementById('scroll_' + id);
				if (window.addEventListener) div_scroll.addEventListener('DOMMouseScroll', scroll_roulette, false);
				div_scroll.onmousewheel = scroll_roulette;
			}
		}
	}
}
//-------------------------------------------------------------------------------------------------------
// deplace le scroll de "pas" pixel
//-------------------------------------------------------------------------------------------------------
var id_scroll;
var old_y;

function move_scroll(pas, is_fleche) {
	// recupere les valeurs du scrollbar
	var div          = document.getElementById('scroll_' + id_scroll);
	var gap_scroll   = div.clientHeight-div.parentNode.clientHeight;
	var barre_haut   = document.getElementById('barre_' + id_scroll + '_haut');
	var barre_bas    = document.getElementById('barre_' + id_scroll + '_bas');
	// recupere les differentes hauteurs 
	var haut 		= parseInt(barre_haut.style.height) + parseInt(barre_bas.style.height);
	var height_haut = ((parseInt(barre_haut.style.height) - pas) < 0 ? 0 : (parseInt(barre_haut.style.height) - pas));
	var height_bas  = ((parseInt(barre_bas.style.height) + pas)  < 0 ? 0 : (parseInt(barre_bas.style.height) + pas));
	
	if (height_bas > haut-5) {
		if (is_fleche) clearInterval (timer_scroll);
		barre_bas.style.height  = (haut-5) + "px";
		barre_haut.style.height = '5px';
		div.style.marginTop = '0px';}
	else if (height_haut > haut-5) {
		if (is_fleche) clearInterval (timer_scroll);
		barre_haut.style.height = (haut-5) + "px";
		barre_bas.style.height  = '5px';
		div.style.marginTop = -gap_scroll + 'px';}
	else {
		barre_haut.style.height = height_haut + "px";
		barre_bas.style.height  = height_bas + "px";
		div.style.marginTop = parseInt(-parseInt(barre_haut.style.height) * gap_scroll / haut) + 'px';}
}
//-------------------------------------------------------------------------------------------------------
// mouvement de la barre par la barre
//-------------------------------------------------------------------------------------------------------
function bar_off(){
	document.body.onmousemove = 'void(0)';
	document.body.onmouseup = 'void(0)';
}
function bar_on(id,e){
	var y = (navigator.appName.substring(0,3) == "Net") ? e.pageY : e.y+document.body.scrollTop;
	
	id_scroll = id;
	old_y = y;
	document.body.onmousemove = bar;
	document.body.onmouseup = bar_off;
}
function select_off () {
	return false;
}

// deplacement de la barre sur accroche de la barre
function bar (event) {
	// e evenement suivant firefox ou IE
	var e = event || window.event;
	// hauteur du deplacement suivant firefox ou IE
	var y = (navigator.appName.substring(0,3) == "Net") ? e.pageY : e.y+document.body.scrollTop;
	move_scroll(old_y - y,false);
	old_y = y;
}
// deplacement de la barre sur click dans la zone de scroll
function move_barre (id, sens, gap) {
	id_scroll = id;
	var barre_centre = document.getElementById('barre_' + id);
	var pas = sens * parseInt(barre_centre.style.height);
	move_scroll(pas,false);
}
//-------------------------------------------------------------------------------------------------------
// mouvement de la barre par les fleches
//-------------------------------------------------------------------------------------------------------
function scrollBar_on(id, pas){
	id_scroll = id;
	
	if (is_scroll) {
		is_scroll=false;
		clearInterval (timer_scroll);}
	else {
		is_scroll=true;
		timer_scroll = setInterval ('move_scroll(' + pas + ',true);', 20);}
}

function scrollBar_off(){
	is_scroll=false;
	clearInterval (timer_scroll);
}
//-------------------------------------------------------------------------------------------------------
// scroll du diaporama
//-------------------------------------------------------------------------------------------------------
function scroll_photo(pas, nbElt, gap) {
	var base = 220;
	var haut = base *(nbElt-2);
	var div1 	 = document.getElementById("bloc1");
	var div2 	 = document.getElementById("bloc2");
	
	var marge = parseInt(div1.style.top) + pas;
	var old_gap = gap;
	gap = gap - Math.abs(pas);
	
	if (marge > 0) {
		div1.style.top  = "0px";
		div2.style.top  = (-div2.style.height) + "px";
		clearTimeout (timer_scroll);}
	else if (Math.abs(marge) > haut) {
		div1.style.top  = (haut * -1) + "px";
		div2.style.top  = ((haut-div2.style.height) * -1) + "px";
		clearTimeout (timer_scroll);}
	else if (gap < 0) {
		div1.style.top  = (parseInt(div1.style.top) + (pas>0 ? old_gap : -1 *old_gap)) + "px";
		div2.style.top  = (parseInt(div2.style.top) + (pas>0 ? old_gap : -1 *old_gap)) + "px";
		clearTimeout (timer_scroll);}
	else {
		div1.style.top  = (parseInt(div1.style.top) + pas) + "px";
		div2.style.top  = (parseInt(div2.style.top) + pas) + "px";
		timer_scroll = setTimeout("scroll_photo(" + pas + "," + nbElt + "," + gap + ");", 10);}
}


function getCssValue(cssFile,tagRef,element) {
	// point sur la feuille de style
	var files = document.styleSheets;
	var reg=new RegExp("[/]+", "g");
	
	for (i = 0; i < files.length; i++) {
		var elts=files[i].href.split(reg);
		if (elts[(elts.length-1)] == cssFile) {
			var idFile = i; 
		}
	}
	if (document.styleSheets[idFile].cssRules) {
		var css = document.styleSheets[idFile].cssRules;}
	else {
		var css = document.styleSheets[idFile].rules;}
	for (i = 0; i < css.length; i++) {
		if (css[i].selectorText == tagRef) {
			var value = css[i].style[element]; 
		}
	}
	return value;
}