// coding: utf-8
function addEvent(obj,event,fct) {
    if( obj.attachEvent) obj.attachEvent('on' + event,fct);
    else obj.addEventListener(event,fct,true);
}
/* window actual size and scroll pos */
var winW = 0;
var winH = 0;
if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    winW = window.innerWidth;
    winH = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    winW = document.documentElement.clientWidth;
    winH = document.documentElement.clientHeight;
}
//~ alert(winW+' x '+winH);
var scrW = screen.availWidth;
var scrH = screen.availHeight;
//~ alert(scrW+' x '+scrH);

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function Plop() {
    var myAs = document.getElementsByTagName('a');
    var myH3 = document.getElementsByTagName('h3');
    var myLi = document.getElementsByTagName('li');
    if(!myAs) return;
    
    for (var b = 0; b < myH3.length; ++b) {
	// backgroundColor du body
	if(myH3[b].id.substring(0,6) == 'color-') {
	    myH3[b].onmouseover = function() {
		document['body'].style.backgroundColor = '#'+this.id.substring(6, 12);
	    };
	    myH3[b].onmouseout = function() {
		document['body'].style.backgroundColor = '#fff';
	    };
	}
    }
    for (var c = 0; c < myLi.length; ++c) {
	// backgroundColor du body
	if(myLi[c].id.substring(0,6) == 'color-') {
	    myLi[c].onmouseover = function() {
		document['body'].style.backgroundColor = '#'+this.id.substring(6, 12);
		//rend noir le li a.current qd on pointe sur un lien d'une section différente (autre bgcolor du body)
		var nexta = this.firstChild;
		var par = this.parentNode.childNodes;
		if(nexta.className != 'current') {
		    for(var x = 0; x < par.length; ++x) {
			if(par[x].firstChild && par[x].firstChild.className == 'current') par[x].firstChild.className = 'noir';
		    }
		}
	    };
	    myLi[c].onmouseout = function() {
		document['body'].style.backgroundColor = '#fff';
		var nexta = this.firstChild;
		var par = this.parentNode.childNodes;
		if(nexta.className != 'noir') {
		    for(var x = 0; x < par.length; ++x) {
			if(par[x].firstChild && par[x].firstChild.className == 'noir') par[x].firstChild.className = 'current';
		    }
		}
	    };
	}
    }
    for (var a = 0; a < myAs.length; ++a) {
	
	// ouvre une nouvelle fenêtre en plein écran
	if(myAs[a].className == 'splash') {
	    myAs[a].onclick = function() {
		this.blur();
		//~ window.open(this.href, 'splash','fullscreen=yes, scrollbars=yes, location=0, status=no, resizable=yes, menubar=0');
		var splash = window.open(this.href, 'splash','width='+scrW+', height='+scrH+', scrollbars=yes, location=no, status=no, resizable=yes, menubar=no, top=0, left=0');
		return false;
	    };
	    myAs[a].onmouseover = function() {
		document['body'].style.backgroundColor = '#FF7E00';
		var sketch = document.getElementById('sketch');
		sketch.style.display = 'block';
	    };
	    myAs[a].onfocus = function() {
		document['body'].style.backgroundColor = '#FF7E00';
		var sketch = document.getElementById('sketch');
		sketch.style.display = 'block';
	    };
	    myAs[a].onmouseout = function() {
		document['body'].style.backgroundColor = '#fff';
		var sketch = document.getElementById('sketch');
		sketch.style.display = 'none';
	    };
	    myAs[a].onblur = function() {
		document['body'].style.backgroundColor = '#fff';
		var sketch = document.getElementById('sketch');
		sketch.style.display = 'none';
	    };
	}
	
	// backgroundColor du body
	else if(myAs[a].id.substring(0,6) == 'color-') {
	    myAs[a].onmouseover = function() {
		document['body'].style.backgroundColor = '#'+this.id.substring(6, 12);
	    };
	    myAs[a].onfocus = function() {
		document['body'].style.backgroundColor = '#'+this.id.substring(6, 12);
	    };
	    myAs[a].onmouseout = function() {
		document['body'].style.backgroundColor = '#fff';
	    };
	    myAs[a].onblur = function() {
		document['body'].style.backgroundColor = '#fff';
	    };
	}
	
	// popup de la description des titres (paragraphe suivant)
        else if (myAs[a].className == 'popdesc' || myAs[a].className == 'popdescin') {
	    var TopPos = (myAs[a].className == 'popdesc') ? -20 : 195;
	    //~ var LeftPos = (myAs[a].className == 'popdesc') ? 5 : (winW - 1000) / 2 + 5;
	    var LeftPos = (winW - 1000) / 2 + 10;
	    // on localise le paragraphe de texte
	    var descP = (myAs[a].parentNode.nextSibling.firstChild) ? myAs[a].parentNode.nextSibling : myAs[a].parentNode.nextSibling.nextSibling;
	    //~ alert(descP.firstChild.data);
	    if(descP) {
		descP.style.display = 'none';
		myAs[a].onmouseover = function() {
		    var showP = (this.parentNode.nextSibling.firstChild) ? this.parentNode.nextSibling : this.parentNode.nextSibling.nextSibling;
		    showP.style.display = 'block';
		};
		myAs[a].onmousemove = function(event) {
		    //~ event = (window.event) ? window.event : event;
		    event = event || window.event;
		    var moveP = (this.parentNode.nextSibling.firstChild) ? this.parentNode.nextSibling : this.parentNode.nextSibling.nextSibling;
		    moveP.style.top = event.clientY + (document.body.scrollTop || document.documentElement.scrollTop) - TopPos+'px';
		    moveP.style.left = event.clientX - LeftPos+'px';
		};
		myAs[a].onmouseout = function() {
		    var hideP = (this.parentNode.nextSibling.firstChild) ? this.parentNode.nextSibling : this.parentNode.nextSibling.nextSibling;
		    hideP.style.display = 'none';
		};
		myAs[a].onclick = function() {
		    var hideP = (this.parentNode.nextSibling.firstChild) ? this.parentNode.nextSibling : this.parentNode.nextSibling.nextSibling;
		    hideP.style.display = 'none';
		    this.blur();
		};
	    }
	}
	
	// popup de la fiche technique des projets dans liste
        else if (myAs[a].className.substring(0,5) == 'litem') {
	    var DaClass = myAs[a].className;
	    // on localise le <dd>
	    var FichTech = document.getElementById(DaClass);
	    if(FichTech) {
		FichTech.style.display = 'none';
		myAs[a].onmouseover = function() {
		    var ShowFich = document.getElementById(this.className);
		    ShowFich.style.display = 'block';
		};
		myAs[a].onmouseout = function() {
		    var HideFich = document.getElementById(this.className);
		    HideFich.style.display = 'none';
		};
	    }
	}
	
	// popup de la fiche technique sur 1 projet
        else if (myAs[a].className == 'popfich') {
	    var DaID = myAs[a].className;
	    // on localise le <div>
	    var FichTech = document.getElementById(DaID);
	    if(FichTech) {
		var nb = a;
		myAs[a].onclick = function() {
		    var ShowFich = document.getElementById(this.className);
		    ShowFich.style.display = 'block';
		    ShowFich.style.top = (document.all) ? (document.documentElement.scrollTop+ 100 +'px') : (window.pageYOffset+ 100 +'px');;
		    this.style.backgroundPosition = '0 0';
		    this.blur();
		    return false;
		};
		FichTech.onclick = function() {
		    this.style.display = 'none';
		    var ZeLink = myAs[nb];
		    ZeLink.style.backgroundPosition = '0 -63px';
		    return false;
		};
	    }
	}
	
	// faux popups pour afficher les images des news en grand
	else if(myAs[a].className.substring(0,5) == 'thumb') {
	    var Popup = document.createElement('div');
	    var divID = document.createAttribute('id');
	    divID.nodeValue = 'big_div';
	    Popup.setAttributeNode(divID);
	    var legend = document.createElement('p');
	    //var bold = document.createElement('b');
	    //var txt = document.createTextNode('Legende: ');
	    //bold.appendChild(txt);
	    var Img = document.createElement('img');
	    var imgsrc = document.createAttribute('src');
	    Img.setAttributeNode(imgsrc);
	    Popup.appendChild(Img);
	    Popup.appendChild(legend);
	    
	    myAs[a].onclick = function() {
		Popup.style.display = 'block';
		Popup.style.top = (document.all) ? (document.documentElement.scrollTop+5+'px') : (window.pageYOffset+5+'px');
		imgsrc.nodeValue = this.href;
		
		var tit = document.createTextNode(this.title);
		if(legend.firstChild == null) legend.appendChild(tit);
		else legend.replaceChild(tit, legend.firstChild);
		
		document.body.appendChild(Popup);
		return false;
	    };
	    Popup.onclick = function() {
		imgsrc.nodeValue = './img/loading.gif';
		this.style.display = 'none';
		return false;
	    };
	}
    }
}

window.onload = function() {
    Plop();
};
