// JavaScript Document

var ExtraMove_Mouse_X; 		// Position X de la Mouse
var ExtraMove_Mouse_Y; 		// Position Y de la Mouse
var ExtraMove_Out = false;	// Souris En-Dehors de l'Ecran

var intervalID = null;
var intervalCatID = null;
var intervalLgd = null;
var tagID = null;
var ApercuLeft = 0;
var ApercuTop = 0;
var CellID = null;
var CellWay = "R";
var CellNum = 1;
var CellImg = false;

var eBottom = -20;
var eLgdPas = 2;

var bInWork = false;

var objLoadImg = null;

var tmpId = null;
var tmpLgdLf = null;
var tmpLgdRg = null;

function OverLink(pId,pSize,pPrt)
// pId : Id de l'element
{
	if(tagID != pId){ 
		idLink(pId,true); 
		if(intervalID != null)
			clearInterval(intervalID); 
		intervalID = setTimeout("ActiveLink('"+ pId +"','"+ pSize +"','"+ pPrt +"')",500);
	} else {
		if(intervalCatID != null)
			clearInterval(intervalCatID); 
	}
}

function OutLink(pId)
{
	if(intervalID != null)
		clearInterval(intervalID);
	intervalID = null;
	
	if(tagID != pId) {
		idLink(pId,false);
	} else {
		intervalCatID = setTimeout("UnactiveLink('"+ pId +"',false)",500);
	}
}

function OverCat(pId){
	if(intervalCatID != null)
		clearInterval(intervalCatID);
}

function OutCat(pId){
	intervalCatID = setTimeout("UnactiveLink('"+ pId +"',false)",500);
}

function ActiveLink(pId,pSize,pPrt)
// pName : Name de l'element
// pType : Active ou Non
{
	eCoef = 75; // "Hauteur d'une Ligne"
	eMrgTop = 250;	// Marge Haut
	eMrgLeft = 100;	// Marge Gauche
	eMrgRight = 0;	// Marge Droite
	ePadding = 25;//17.5;	// Padding des Images
	eWidth = document.documentElement.clientWidth - eMrgLeft - eMrgRight;
	eDeb = 0;
	eLem = 0;
	ePos = 1;
	
	if(document.documentElement.clientWidth < 996)
		eWidth = 996 - eMrgLeft - eMrgRight;
	else
		eWidth = document.documentElement.clientWidth - eMrgLeft - eMrgRight;
		
	intervalID = null;
	if(tagID != null )
		idLink(tagID,false);
	tagID = pId;
	
	UnactiveAllCat();
	
	for(i=0; i < taWork.length; i++){
		eDeb = ePos;
		eLem = Math.ceil(ePos / eWidth);
		eTmp = ePos + (taWork[i][1] + ePadding);
		eLig = Math.ceil(eTmp / eWidth);
		if(eLem != eLig){
				ePos = (eWidth * eLem) + (taWork[i][1] + ePadding);
				eDeb = 0;
			} else {
				ePos += (taWork[i][1] + ePadding);
			}
		if(pId == taWork[i][0]){ 
			//alert("eLem="+eLem+" // eLig="+eLig);
			eLem = Math.ceil(ePos / eWidth);
			//alert("eLem="+eLem);
			break;
		}
	}
		
	document.getElementById("Mask").style.width = eWidth +"px";
	document.getElementById("Mask").style.top = (eMrgTop + (eCoef * eLem)) +"px";
	show("Mask");
	
	if(eDeb != 0)
		eTmp = eDeb - ((eLig-1) * eWidth);
	else
		eTmp = 0;
		
	ApercuLeft = eMrgLeft + eTmp + 4;
	ApercuTop = (eMrgTop + (eCoef * eLig)) + (eCoef + 8);
	
	document.getElementById("cat"+ pId).style.left = (eMrgLeft + eTmp) +"px";
	document.getElementById("cat"+ pId).style.top = ((eMrgTop + (eCoef * eLig)) + 4) +"px";
	document.getElementById("cat"+ pId).style.minWidth = (eWidth - ApercuLeft) +"px";
	show("cat"+ pId);
	
	if((eWidth - ApercuLeft) < pSize){
		document.getElementById("Mask").style.height = (Math.ceil(pSize / (eWidth - ApercuLeft)) * 78) +"px";
	} else {
		document.getElementById("Mask").style.height = 78 + "px";
	}
	
	document.getElementById("imgextrafine").src = "img/"+ pPrt;
}

function UnactiveLink(pId,pWork)
{
	intervalCatID = null;
	if(!bInWork){
		if(pWork){
			idLink("cat"+ CellID,false);
			idLink(tagID,false);
			hide("cat"+ tagID);
		} else {
			idLink(pId,false);
			hide("cat"+ pId);
		}
		hide("Mask");
		tagID = null;
		document.getElementById("imgextrafine").src = "img/extrafine.png";
	}
}

function ActiveCat(pId,pType,pSrc,pPos,pFin){
	
/*	pId = tmpId;
	tmpId = null;
	pType = tmpType;
	tmpType = null;
	pSrc = tmpSrc;
	tmpSrc = null;
	pPos = tmpPos;
	tmpPos = null;
	pFin = tmpFin;
	tmpFin = null;*/
	
	eMrgLeft = 100;	// Marge Gauche
	eMrgRight = 0;	// Marge Droite
	
	if(document.documentElement.clientWidth < 996)
		Wdt = 996 - 50;
	else
		Wdt = document.documentElement.clientWidth - 50;
	
	document.getElementById("Apercu").src = "apercu/"+ pSrc;
	
	
	
	// ######################################################################################################################
	// ##### Positionnement de l'apercu sur la bonne ligne #################################################################
	// ######################################################################################################################
	
	//alert((eWidth - ApercuLeft));
	
	eLigDeb = Math.ceil(pPos / (Wdt - ApercuLeft));
	eLigFin = Math.ceil(pFin / (Wdt - ApercuLeft));
	//alert("eLigDeb = "+ eLigDeb +" // eLigFin = "+ eLigFin);
	if(eLigFin > 1){
		if(eLigDeb == eLigFin){
			eLigFin--;
			document.getElementById("Apercu").style.left = ApercuLeft + (pPos - (Wdt - ApercuLeft)) +"px";
			document.getElementById("Apercu").style.top = ApercuTop + (eLigFin * 75) +"px";
		} else {
			eLigFin--;
			document.getElementById("Apercu").style.left = ApercuLeft +"px";
			document.getElementById("Apercu").style.top = ApercuTop + (eLigFin * 75) +"px";			
		}
	} else {
		document.getElementById("Apercu").style.left = (ApercuLeft + pPos) +"px";
		document.getElementById("Apercu").style.top = ApercuTop +"px";
	}
		
	// ######################################################################################################################
	// ######################################################################################################################
	// ######################################################################################################################
	
	
	if(pType){
		show("Apercu");
	} else {
		hide("Apercu");
	}
	
	idLink(pId,pType);
}

function UnactiveCat(pId,pType)
{
	hide("Apercu");
	if(!bInWork)
		idLink(pId,pType);
}

function UnactiveAllCat() 
{
	if(navigator.appName.substring(0,9) == "Microsoft") {
		var allElement = document.getElementsByTagName("div");
		for(i = 0; i < allElement.length; i++) {
			if(allElement[i].getAttribute("name") == "SousMenu") {
				allElement[i].style.visibility = 'hidden';
				allElement[i].style.display = 'none';	
			}
		}
	} else {
		for(i = 0; i < document.getElementsByName("SousMenu").length; i++) {
			var object = document.getElementsByName("SousMenu")[i];
			object.style.visibility = 'hidden';
			object.style.display = 'none';
		}
	}
}

function LoadCell(pId,pLgdLf,pLgdRg) 
{
	tmpId = pId;
	tmpLgdLf = pLgdLf;
	tmpLgdRg = pLgdRg;
	
	bInWork = true;
	
	if(pId == "tatataaff") {
		document.getElementById("Voile").style.backgroundColor = "#1A1F1C";
		document.getElementById("Voile").style.opacity = 1;
		document.getElementById("Voile").style.filter = "alpha(opacity=100)";
		document.getElementById("LgdLf").style.color = "black";
		myColorVoile.Loop = false;
		myColorVoile.ColorBy("1A1F1C","FFFFFF");
	} else {
		document.getElementById("Voile").style.backgroundColor = "#1A1F1C";
		document.getElementById("Voile").style.opacity = 0.9;
		document.getElementById("Voile").style.filter = "alpha(opacity=90)";
		document.getElementById("LgdLf").style.color = "white";
		myColorVoile.Loop = false;
		myColorVoile.ColorBy("1A1F1C","1A1F1C");
	}
	show("vl");
	
	document.getElementById("LgdLf").innerHTML = "Chargement en cours...";
	//LoadLegende(true);
	
	// Prechargement de la Serie ---------------------------------------
	objLoadImg = new mdPreload_ImgPreload(taWrkImg[pId],ActiveCell);
	// -----------------------------------------------------------------
}

function ActiveCell(pId,pLgdLf,pLgdRg)
{
	pId = tmpId;
	pLgdLf = tmpLgdLf;
	pLgdRg = tmpLgdRg;
	
	pLgdLf = ChangeChar(pLgdLf);
	pLgdRg = ChangeChar(pLgdRg);
	
	cPath = "work/";
	cTag = "";
	for(i = 0; i < taWrkImg[pId].length; i++) {
		cTag += '<table id="'+ pId + (i+1) +'" class="Work"><tr><td align="center" valign="middle"><img src="'+ cPath + taWrkImg[pId][i] +'" id="img'+ pId + (i+1) +'" onmouseover="ImageCell(true);" onmouseout="ImageCell(false);" /></td></tr></table>';
	}
	document.getElementById(pId).innerHTML = cTag;
	
	for(i = 1; i <= taCell[pId]; i++) {
		document.getElementById(pId + i).style.visibility = 'hidden';
	}
	document.getElementById(pId +"1").style.visibility = 'visible';
	CellID = pId;
	CellNum = 1;
	
	if(taCell[pId] == 1) {
		document.getElementById(pId).style.cursor = "url(img/barre.cur),default";
	} else {
		document.getElementById(pId).style.cursor = "url(img/close.cur),default";
	}
	
//	myColorVoile.ColorBy("1A1F1C","3A3F3C");
//	show("vl");
	
	document.getElementById("LgdLf").innerHTML = pLgdLf;
	document.getElementById("LgdRg").innerHTML = pLgdRg;
//	show("Legende");
	
	show(pId);
	WhichPart();
	ChangeCursor();
	
//	setTimeout('LoadLegende(true)',1000);
}

function UnactiveCell(pId)
{
	myColorVoile.ColorBreak();
	
	bInWork = false;
	UnactiveLink(pId,true);
	
	CellID = null;
	document.getElementById("LgdLf").innerHTML = "";
	document.getElementById("LgdRg").innerHTML = "";
	//LoadLegende(false);
	hide(pId);
	hide("vl");
}

function ChangeCell()
{
	if(CellImg) {
		if(taCell[CellID] != 1) {
			document.getElementById(CellID + CellNum).style.visibility = 'hidden';
			if(CellWay == "R") {
				if(CellNum == taCell[CellID])
					CellNum = 1;
				else
					CellNum++;
			} else {
				if(CellNum == 1)
					CellNum = taCell[CellID];
				else
					CellNum--;
			}
			document.getElementById(CellID + CellNum).style.visibility = 'visible';
		}
	} else {
		UnactiveCell(CellID);
	}
}

function ImageCell(pType) {
	if(pType) {
		//window.status = "Mange tes m&egrave;res";
		CellImg = true;
	} else {
		//window.status = "";
		CellImg = false;
	}
}

function ChangeChar(pTxt) {
	
	var tbReg = new Array();
	tbReg[0] = new Array("#guillement#","&acute;");
	tbReg[1] = new Array("#copy#","&copy;");
	tbReg[2] = new Array("#br#","<br />");
	
	for(i=0; i<tbReg.length; i++) {
		var cReg = new RegExp(tbReg[i][0],"g");
		while(cReg.test(pTxt)) {
			pTxt = pTxt.replace(cReg,tbReg[i][1]);
		}
	}
	return pTxt;	
}

function show(pId)
// pId : Id de l'element
{
	var object = document.getElementById(pId).style;
	object.visibility = 'visible';
	object.display = 'block';
}

function hide(pId)
// pId : Id de l'element
{
	var object = document.getElementById(pId).style;
	object.visibility = 'hidden';
	object.display = 'none';
}

function idLink(pId,pType)
// pId : Id de l'element
// x   : Valeur d'opacite de l'element
{
	var element = document.getElementById(pId);
	if(pType){
		if(element.src.search(/A.png/) == -1)
			element.src = element.src.replace(".png","A.png");	
	} else {
		if(element.src.search(/A.png/) != -1)
			element.src = element.src.replace("A.png",".png");
	}
}

function ChangeCursor() {
	switch(CellWay) {
		case "L" :
			document.getElementById(CellID).style.cursor = "url(img/flecheleft.cur),url(img/flecheleft.png),default";
			break;
		case "R" :
			document.getElementById(CellID).style.cursor = "url(img/flecheright.cur),url(img/flecheright.png),default";
			break;
		case "I" :
			document.getElementById(CellID).style.cursor = "url(img/barre.cur),url(img/barre.png),default";
			break;
		case "X" :
			document.getElementById(CellID).style.cursor = "url(img/close.cur),url(img/close.png),default";
			break;
		default :
			document.getElementById(CellID).style.cursor = "default";
	}
}

function Credit(pType){
	if(pType)
		document.getElementById("Legende").innerHTML = "Samuel Diakit&eacute;, Tony Sim&otilde;es Relvas, Samuel Rambaud";
	else	
		document.getElementById("Legende").innerHTML = "";
		
	LoadLegende(pType);
}

function LoadLegende(pType){
	if(intervalLgd != null){
		clearInterval(intervalLgd);
		intervalLgd = null;
	}
	if(pType)
		intervalLgd = setInterval("ActiveLegende()",30);
	else
		intervalLgd = setInterval("UnactiveLegende()",50);
}

function ActiveLegende(){
	eBottom += eLgdPas;
	if(eBottom >= 0){
		eBottom = 0;
		clearInterval(intervalLgd);
		intervalLgd = null;
	}
	document.getElementById("Legende").style.bottom = eBottom +"px";
}

function UnactiveLegende() {
	eBottom -= eLgdPas;
	if(eBottom <= -20){
		eBottom = -20;
		clearInterval(intervalLgd);
		intervalLgd = null;
	}
	document.getElementById("Legende").style.bottom = eBottom +"px";
}

function ActiveContact() {
	myColorVoile2.ColorBy("1A1F1C","3A3F3C");
	show("vl2");
	show("dvContact");
}

function UnactiveContact() {
	myColorVoile2.ColorBreak();
	hide("vl2");
	hide("dvContact");
}

function ChangeEmpty() {
	var eHeight = document.documentElement.clientHeight - 330;
	document.getElementById("Empty").style.height = eHeight +"px";
}

function EnvoiMail(form) {
	
	var cMail = form.AdressMail.value;
	var cTexte = form.MessageMail.value;
	var prm = 'exp='+ cMail +'&msg='+ cTexte ;

	var xhr = getXhr();
	
	xhr.onreadystatechange = function()
    {
        if(xhr.readyState == 4 && xhr.status == 200)
        {
			var docXML = xhr.responseXML;
			if(docXML == null) { 
				var strResult = "Le message n'a pu &ecirc;tre envoy&eacute;. Veuillez r&eacute;essayer ult&eacute;rieurement.";
			} else {
				var nodeParent = docXML.getElementsByTagName('result')[0];
				if(nodeParent == null) { 
					var strResult = "Le message n'a pu &ecirc;tre envoy&eacute;. Veuillez r&eacute;essayer ult&eacute;rieurement.";
				} else {
            		var strResult = nodeParent.getAttribute('txt');
					var cMail = form.AdressMail.value = "";
					var cTexte = form.MessageMail.value = "";
				}
			} 
			UnactiveContact();
			document.getElementById("Legende").innerHTML = strResult;
			LoadLegende(true);
			setTimeout('LoadLegende(false)',10000);
    	}
	}

	xhr.open('POST', 'scr/mail.php', true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", prm.length);
	xhr.setRequestHeader("Connection", "close")
	xhr.send(prm);	
}

function getXhr()
{
	if(window.XMLHttpRequest) // Firefox et autres
		{xhr = new XMLHttpRequest();} 
	else if(window.ActiveXObject){ // Internet Explorer 
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else { // XMLHttpRequest non supporte par le navigateur 
		alert("Votre navigateur ne supporte pas la technologie AJAX"); 
		xhr = false; 
	} 
	return xhr;
}

function WhichPart() {
	var tmpCellWay;
	if(CellID != null){
		if(CellImg) {
			if(taCell[CellID] == 1) {
				tmpCellWay = "I";
			} else {
				if(ExtraMove_Mouse_X > document.documentElement.clientWidth / 2 )
					tmpCellWay = "R";
				else
					tmpCellWay = "L";
			}
		} else {
			tmpCellWay = "X";
		}
		if(tmpCellWay != CellWay) {
			CellWay = tmpCellWay;
			ChangeCursor();
		}
	}
}

function WhereMouse(e){
	
	var DocRef;  	// Variable pour IE uniquement
  
	if(e){
		ExtraMove_Mouse_X = e.pageX;
		ExtraMove_Mouse_Y = e.pageY;
	}
	else{                      
		ExtraMove_Mouse_X = event.clientX;
		ExtraMove_Mouse_Y = event.clientY;
	
		if( document.documentElement && document.documentElement.clientWidth) 
	  		DocRef = document.documentElement;
		else
	  		DocRef = document.body;
	
		ExtraMove_Mouse_X += DocRef.scrollLeft;
		ExtraMove_Mouse_Y += DocRef.scrollTop;
	}

	WhichPart();

}

document.onmousemove = WhereMouse;

