// JavaScript Document

function loadAccordions() {
	/* menu général */
	var tailleMax = $('menu').getWidth();
	var tailleBtn = 0;
	$$('#menu a').each(function(n, index) {
		tailleBtn += n.getWidth() + 1;
	});
	var pourDefaultSize = tailleMax - tailleBtn;
	var navAccordion = new accordion('menu_menuprincipal', {
		classNames : {
			toggle : 'horizontal_accordion_toggle',
			toggleActive : 'horizontal_accordion_toggle_active',
			content : 'horizontal_accordion_content'
		},
		onEvent : 'mouseover',
		direction : 'horizontal',
		defaultSize : {
    	    width : pourDefaultSize
	    }
	});
	// Open first one
	navAccordion.activate($$('#menu_menuprincipal .horizontal_accordion_toggle')[ouvrirPage]);
	
	/* permettre le lien sur le content */
	$$('#menu span.horizontal_accordion_content').each(function(n, index) {
		n.onclick = function() {
			window.location.href = n.parentNode.firstChild.href;
		}
	});
	// TODO: réafficher le menu de la page lorsque l'on sort du survol de celui-ci
	/*
	$('contenu').onmouseover = function() {
		var nbactif = -1;
		$$('#menu a').each(function(n, index) {
			if(n.className.search('horizontal_accordion_toggle_active') != -1) nbactif = index;
		});
		
		//$$('#menu a')[nbactif].removeClassName('horizontal_accordion_toggle_active');
		//new Effect.Scale($$('#menu a')[nbactif].showAccordion, 0);
		//alert($$('#menu a')[nbactif].firstChild.nodeValue);
		//$$('#menu a')[nbactif].deactivate();
		//navAccordion.activate($$('#menu_menuprincipal .horizontal_accordion_toggle')[0]);
	}
	*/
}

function iniPage() {
	var mapage = $('global');
	switch(mapage.className) {
		case "accueil" :
			$$('#menu_accueil li').each(function(s) {
				// création des flash sur la liste de liens
				createUFO(s.id.split("menu_accueil_")[1], s.firstChild.firstChild.firstChild.nodeValue, s.firstChild.href.split("/")[s.firstChild.href.split("/").length - 1]);
			});
			break;
			
		case "services" :
			$$('#menu_services a').each(function(s) {
				var elt = s.href.split("#")[1];
				s.href = "javascript:void(0)";
				s.onclick = function() {
					$$('#menu_services a').each(function(n) {
						n.className  = n.className.replace(new RegExp("actif\\b"), "");
					});
					this.className += " actif";
					afficheBloc(elt);
				}
			});
			// activation du premier lien
			afficheBloc("texte1");
			$('menu_services_1').firstChild.className += " actif";
			break;
			
		case "atouts" :
			$$('#menu_atouts span').each(function(s) {
				s.onclick = function() {
					$$('#menu_atouts li.blocConteneur').each(function(n) {
						n.className  = n.className.replace(new RegExp("actif\\b"), "");
					});
					this.parentNode.className += " actif";
					afficheBloc(this.nextSibling);
				}
			});
			break;
			
		case "references" :
			$$('#menu_references a').each(function(s) {
				var gotoUrl = s.href.split("#")[0];
				var elt = s.href.split("#")[1];
				s.href = "javascript:void(0)";
				s.onmouseover = function() {
					afficheBloc(elt);
				}
				s.onclick = function() {
					if(gotoUrl != 'http:///') window.open(gotoUrl, "_blank");
				}
			});
			break;
			
		case "production" :
			prepareZoom();
			break;
			
		default :
			break;
	}
}

function createUFO(num, texte, urlPage) {
	//alert("num : "+num+", texte : "+texte+", urlPage : "+urlPage);
	var FU = {     	movie :			"images/accueil-btn.swf",
					width :			"183",
					height :		"137",
					majorversion :	"9",
					build :			"0",
					wmode :			"transparent",
					flashvars :		"texte="+texte+"&urlPage="+urlPage
			};
	UFO.create(     FU, "menu_accueil_"+num);
}

function afficheBloc(elt) {
	// recherche du div déjà affiché pour masquage
	$$('.yo').each(function(s) {
		masqueBlocs(s);
	});
	new Effect.Appear(elt, {duration:0.5, fps:25, from:0.0, to:1.0, afterFinishInternal: function(effect) {$(elt).className += " yo"}
	});
}

function masqueBlocs(elt) {
	new Effect.Fade(elt.id, {duration:0.5, fps:25, afterFinishInternal: function(effect) {
		elt.className = elt.className.replace(new RegExp(" yo\\b"), "");
	}});
}

/****************************************************/
/*					GESTION DU ZOOM					*/
/****************************************************/
var offsetfrommouse=[5,-25]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var currentimageheight = 0;//150;
var currentimagewidth = 0;//200;
var hScreenSecurity = 0;//10;
var vScreenSecurity = 0;//260;
var trailer = '';

function prepareZoom() {
	$$('#bureaux a', '#sitesprod a').each(function(s) {
		s.onmouseover = function() {
			hideAll();
			showtrail(s.href.split("#")[1]);
		}
		s.onmouseout = function() { hidetrail(); }
	});
}

function gettrailobj(){
	return $(trailer).style;
}

function gettrailobjnostyle(){
	return $(trailer);
}

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

function showtrail(elt){
	$(elt).style.left = "auto";
	trailer = elt;
	document.onmousemove=followmouse;
}

function hidetrail(){
	document.onmousemove=killfollowmouse;
	hideAll();
}

function hideAll() {
	$$('.bloctexte').each(function(s) {
		s.style.left = "-999em";
		s.style.top = "0";
	});
}

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.max(truebody().scrollHeight, truebody().clientHeight) : window.innerHeight;
	
	var decalX = (docwidth - 980)/2 + 30 - offsetfrommouse[0];
	var decalY = (docheight - 560)/2 + 130 - offsetfrommouse[1];

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

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


/****************************************************/
/*				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() {
		loadAccordions();
		iniPage();
	});
}