////////////////////////////////////////
// Utilitaires
////////////////////////////////////////
var strRand = "";
function getUniqueRand(min,max)
{
	var num = getRand(min,max);
	if(strRand.length < 5)
	{
		while (strRand.indexOf(num) != -1) 
		{
			num = getRand(min,max);
		}
		strRand += ""+num;
		return num;
	}
	else
		return 0;
}

function getRand(min,max,dec)
{
	var dec = dec || false;
	var rand = Math.random() * (max-min);
	if(dec)
		var random = parseFloat(rand + min);
	else
		var random = parseInt((Math.round(rand)) + min);
	return random;
}

// A supprimer si pas de changement
function getOffsetSite()
{
	return 0;
	var oB = new BrowserDetectLite();
	var osite = $('corps_site').getDimensions();
	if(oB.isOpera)
	{
		return 0;
	}
	else
	{
		var ovp = document.viewport.getDimensions();
		var offsetPadding = parseInt($('corps_site').getStyle("padding-left").replace("px",""));
		return (ovp.width-osite.width+offsetPadding)/2;
	}
}

////////////////////////////////////////
// Gestion de la frise
////////////////////////////////////////
//param communs
var frise_vitesse_max = 20;
var frise_vitesse_min = 18;
var posmin = 0;
var posmax = (3000-1024);
var masque_vitesse_min = 25;
var masque_vitesse_max = 35;

var anim_param = Array();
function move_frise(frise_id)
{
	var frise = $(frise_id);
	
	var pos_max= 3000-1024;
	var current_pos = frise.style.left.replace("px","");

	if(anim_param[frise_id]['sens']==1)
	{
		var dest = pos_max*-1;
		var time= ((pos_max - (current_pos*-1))*  anim_param[frise_id]['vitesse']) / pos_max;
		anim_param[frise_id]['sens']=0;
	}
	else
	{
		var dest = 0;
		var time= ((current_pos*-1) * anim_param[frise_id]['vitesse']) / pos_max;
		anim_param[frise_id]['sens']=1;
	}
		
	new Effect.Move(frise, { 
			x: dest, 
			y: 0, mode: 'absolute' ,
			duration:time,
			transition: Effect.Transitions.linear,
			queue:{ position: 'front', scope: frise_id }
	});
}

function init_animation(bandeau, toleft)
{
	var pos_max= 3000-1024;

	anim_param[bandeau.id] = new Array();
	anim_param[bandeau.id]['sens']=1;
	anim_param[bandeau.id]['vitesse']=getRand(frise_vitesse_min,frise_vitesse_max);
	move_frise(bandeau.id);
	setInterval("move_frise('" + bandeau.id + "')",(anim_param[bandeau.id]['vitesse']*1000));
	//position des masques
	var masques = $$('.frise_masque');
	for(var i=0;i<masques.length;i++)
	{

		//set param masque
		anim_param[masques[i].id] = new Array();
		anim_param[masques[i].id]['sens']=getRand(0,1);
		//set start position
		if(anim_param[masques[i].id]['sens']==0)
		{
			masques[i].style.left=(pos_max*-1)+"px";
		}
		else
		{
			masques[i].style.left="0px";
		}
		anim_param[masques[i].id]['vitesse']=getRand(masque_vitesse_min,masque_vitesse_max);

		//set param masque
		move_frise(masques[i].id);
		setInterval("move_frise('" + masques[i].id+ "')",(anim_param[masques[i].id]['vitesse']*1000));
	}
}

////////////////////////////////////////
// Gestion des photos masquées
////////////////////////////////////////
var hc_tab = new Array();
var hc_time = 0.2;

function hideContainer(hczone,hcx,hcy,hcwidth,hcheight) {
	this.hczone = hczone;
    this.hcx = hcx;
    this.hcy = hcy;
    this.hcwidth= hcwidth;
    this.hcheight= hcheight;
    this.ishidden=false;
  }
  
function hide_hc(obj_hc)
{
    if(obj_hc.hczone.style.display == "block" || obj_hc.hczone.style.display == "")
	{
		var oB = new BrowserDetectLite();
		if(oB.isIE)
		{
			obj_hc.hczone.style.display="none";
		}
		else
		{
			new Effect.Fade(obj_hc.hczone, {
			    afterFinish: function(){if(obj_hc.ishidden==false)new Effect.Appear(obj_hc.hczone, {duration: hc_time});},
				duration: hc_time
			});
		}
	}
	obj_hc.ishidden=true;
}

function show_hc(obj)
{
	if(obj.hczone.style.display == "none")
	{
		var oB = new BrowserDetectLite();
		if(oB.isIE)
		{
			obj.hczone.style.display="block";
		}
		else
		{
			new Effect.Appear(obj.hczone, {
				duration: hc_time,
			    afterFinish: function(){if(obj.ishidden==true)new Effect.Fade(obj.hczone, {duration: hc_time});}
			});
		}
	}
	obj.ishidden=false;
}


function init_photo_smooth()
{
	hcZones = $$('.hc_zone');
	offsetWidth = getOffsetSite();
	strRand = "";
	for(var i = 0; i<hcZones.length; i++)
	{
		var zone_photo = hcZones[i];
		/* change class name */
		zone_photo.className += " masque_photo_" + getUniqueRand(1,hcZones.length);
		/*get dimension*/
		var opos = zone_photo.cumulativeOffset();
		var odim = zone_photo.getDimensions();
		/*create hc object*/
		var hc = new hideContainer(zone_photo,(opos[0]+offsetWidth),opos[1],odim.width,odim.height);
		hc_tab.push(hc);
	}
}

function init_photo_masque()
{
	hcZones = $$('.mos_masque');
	for(var i = 0; i<hcZones.length; i++)
	{
		var masque_photo = hcZones[i];
		masque_photo.className += " masque_photo_" + getUniqueRand(1,hcZones.length);
	}
}

function check_photo_smooth(event)
{
	for(var i=0 ; i < hc_tab.length ; i++)
	{
		tabMouse = getMousePosition(event);
		if(tabMouse[0]>=hc_tab[i].hcx && tabMouse[0]<(hc_tab[i].hcx+hc_tab[i].hcwidth) && tabMouse[1]>=hc_tab[i].hcy && tabMouse[1]<(hc_tab[i].hcy +hc_tab[i].hcheight))
		{
			hide_hc(hc_tab[i]);
		}
		else
			show_hc(hc_tab[i]);
	}
}

function hide_photos()
{
	for(var i=0 ; i < hc_tab.length ; i++)
	{
		show_hc(hc_tab[i]);
	}
}

////////////////////////////////////////
// Gestion du survol du menu
////////////////////////////////////////

var current_hover ="";
var current_toclose ="";
var menu_vitesse=0.2;
var menu_masque_vitesse=0.1;
function hide_menu(menu_id)
{
	var fond  =  $("fond_menu_" + menu_id);
	var masque1 = $("masque1_menu_" + menu_id);
	var masque2 = $("masque2_menu_" + menu_id);
	var masque3 = $("masque3_menu_" + menu_id);
	var sousmenu = $('sous_menu_haut_'+menu_id);
	var lien = $('lien_'+menu_id);
	if(lien.className.indexOf("lien_current")==-1)
	{
		lien.style.backgroundPosition ="0 0";
	}
	if(sousmenu)
		sousmenu.style.display="none";
	masque3.style.display="none";
	masque2.style.display="none";
	masque1.style.display="none";
	fond.style.left="-233px";
}

function anim_menu_hover(menu_id)
{
	if(menu_id!=current_hover)
	{
		current_hover = menu_id;
		hide_menus(menu_id);
		var sousmenu = $('sous_menu_haut_'+menu_id);
		var fond = $("fond_menu_" + menu_id);
		var lien = $('lien_'+menu_id);
		if(sousmenu)
			sousmenu.style.display="block";
		lien.style.backgroundPosition ="0 -30px";
		var masque1 = $("masque1_menu_" + menu_id);
		var masque2 = $("masque2_menu_" + menu_id);
		var masque3 = $("masque3_menu_" + menu_id);
		new Effect.Move(fond, { 
			x: 0, 
			y: 0, mode: 'absolute' ,
			duration:menu_vitesse,
			queue:{ position: 'front', scope: 'menu' },
			afterFinish: function()
			{
				fond.style.display="";
				new Effect.Appear(masque1, { 
				duration:menu_masque_vitesse,
				queue:{ position: 'front', scope: 'menu' },
				afterFinish: function()
				{
					masque1.style.display="";
					new Effect.Appear(masque2, { 
					duration:menu_masque_vitesse,
					queue:{ position: 'front', scope: 'menu' },
					afterFinish: function()
					{
						masque2.style.display="";
						new Effect.Appear(masque3, { 
						duration:menu_masque_vitesse,
						queue:{ position: 'front', scope: 'menu' },
						afterFinish: function()
						{
							masque3.style.display="";
							if(menu_id!=current_hover)
							{
								hide_menu(menu_id);
							}
						}});
					}});
					
				}});
				
			}});
	}
	
}

function check_menu_close(event)
{
	var res =  true;
	if(current_hover!="")
	{
		offsetWidth = getOffsetSite();
		odim = $("menu_" + current_hover).getDimensions();
		opos = $("menu_" + current_hover).cumulativeOffset();
		
		tabMouse = getMousePosition(event);
		if(!(tabMouse[0]>=opos[0]+offsetWidth && tabMouse[0]<(opos[0]+offsetWidth+odim.width) && tabMouse[1]>=opos[1] && tabMouse[1]<(opos[1]+odim.height)))
		{
			hide_menu(current_hover);
			current_hover="";
		}
		else
		{
			hide_photos();
			hide_menus(current_hover);
			res = false;
		}
	}
	return res;
}
function hide_menus(other)
{
	var other = other||0;
	for(var i=1 ; i<5; i++)
	{
		if(i!=other)
			hide_menu(i);
	}
}

////////////////////////////////////////
// Gestion des mosaiques d'images
////////////////////////////////////////
var mos_maxzone;
var mos_currentzone;
var mos_current = Array();
var mos_interval_time = 2000;
var mos_start_time_min = 1;
var mos_start_time_max = 2;
var mos_duration_time = 2;
var mos_init=false;

function anim_mosaisque()
{
	strRand = ""+mos_current[mos_currentzone];
	var mos_nb_photo = $$('.mos_zone_'+ mos_currentzone).length;
	if(mos_nb_photo > 1)
	{
	
		var mos_next = getUniqueRand(1,mos_nb_photo);
	
		var mos_zone = $("mos_image_" + mos_currentzone + "_" + mos_next);
		var mos_czone = $("mos_image_" + mos_currentzone + "_" + mos_current[mos_currentzone]);
	
		var start = getRand(mos_start_time_min,mos_start_time_max);
		if(mos_czone && mos_zone )
		{
			new Effect.Fade(mos_czone,
				{
					duration:mos_duration_time,
					delay:start,
					queue:{position: 'end', scope:'scopefade'+mos_czone.id, limit:1}
				});
			new Effect.Appear(mos_zone,
				{
					delay:(start*1.2),
					duration:mos_duration_time,
					queue:{position: 'end', scope:'scopefade'+mos_zone.id, limit:1}
				});
		}	
		mos_current[mos_currentzone] = mos_next;
	}
	mos_currentzone++;
	if(mos_currentzone>mos_maxzone)
		mos_currentzone=1;
}

function init_mosaique()
{
	mos_maxzone = $$('.mos_zone').length;
	mos_currentzone=1;
	//Init des photos
	for(var zone=1 ; zone<=mos_maxzone; zone++)
	{
		var mos_nb_photo = $$('.mos_zone_'+zone).length;
		if(mos_nb_photo>0)
		{
			var mos_index_photo = getRand(1,mos_nb_photo);
			mos_current[zone] = mos_index_photo;
			$("mos_image_" + zone + "_" + mos_index_photo).style.display="";
		}
	}

	setInterval("anim_mosaisque()",mos_interval_time);
}

//Gestion des images pour la page actu

function photo_actu()
{
	var sRand = getRand(0, 2);
	var lst_photo = $$('.actu_photo_'+sRand);
	var nb_photo = lst_photo.length;
	var tohide = "";
	var toshow = "";
	//On recherche la photo actuel
	for(var i = 0; i < nb_photo; i++)
	{
		if(lst_photo[i].style.display != "none")
		{
			tohide = lst_photo[i].id;
		}
	}
	//On en cherche une au hasard differente de celle en cours
	var toshow = tohide;
	if($(tohide))
	{
		while(tohide == toshow && nb_photo > 1)
		{
			sRand = getRand(0, (nb_photo - 1));
			toshow = lst_photo[sRand].id;
		}
	}
	if($(tohide) && $(toshow) && toshow != tohide)
	{
		new Effect.Fade($(tohide),
			{
				duration:mos_duration_time,
				queue:{position: 'end', scope:'scopefade'+tohide, limit:1}
			});
		new Effect.Appear($(toshow),
			{
				duration:mos_duration_time,
				queue:{position: 'end', scope:'scopefade'+toshow, limit:1}
			});
	}
}
////////////////////////////////////////
// Gestion de la navigation des projets
////////////////////////////////////////

var choix_projet_nbr;
var choix_projet_first;

function nav_projet(sens,type,offset)
{
	if(sens)
	{
		var show = choix_projet_first + offset;
		var hide = choix_projet_first;
		choix_projet_first++;
	}
	else
	{
		var show= (choix_projet_first -1);
		var hide = show+offset;
		choix_projet_first--;
	}
	
	//Change projet display
	$(type + '_' + hide).style.display="none";
	$(type + '_' + show).style.display="";
	
	//Change btn display
	if(choix_projet_first==1)
		$('btn_'+ type +'_prec').style.display="none";
	else
		$('btn_'+ type +'_prec').style.display="";

	if((choix_projet_first+offset-1)==choix_projet_nbr)
		$('btn_'+ type +'_suiv').style.display="none";
	else
		$('btn_'+ type +'_suiv').style.display="";
}

var choix_photo_nbr;
var choix_photo_current;
function nav_photo(sens,type,offset)
{
	choix_photo_current++;
	if(choix_photo_current>choix_photo_nbr)
	{
		choix_photo_current=1;
	}
	
	var show = choix_photo_current;
	var hide = choix_photo_current - offset;
	if(hide < 1)
	{
		hide += choix_photo_nbr;
	}
	
	//Change projet display
	$(type + '_' + hide).style.display="none";
	// On deplace le bloc suivant
	var element = $(type + '_' + hide).remove();
	$('choix_photo').insert(element, { position: 'after' });
	$(type + '_' + show).style.display="";
}

var current_conteneur = 1;
var photo_change_time = 0.5;
function change_photo(btn_id)
{
	if(current_conteneur==1)
	{
		current_conteneur=2;
		var current=$('lien_photo_1');
		var next=$('lien_photo_2');
	}
	else
	{
		current_conteneur=1;
		var current=$('lien_photo_2');
		var next=$('lien_photo_1');
	}
	$('photo_current').value = btn_id;
	$('conteneur_photo').style.backgroundPosition= "0px 0px"; 
	var photo_gd = $(btn_id + "_gd");

	if(photo_gd)
	{
		next.style.backgroundImage="url(' " + photo_gd.value + " ')";
		new Effect.Fade(current,
			{
				duration:photo_change_time,
				queue:{position: 'end', scope:current.id, limit:1}
			});
		new Effect.Appear(next,
			{
				duration:photo_change_time,
				queue:{position: 'end', scope:next.id, limit:1}
			});
	}
}

/* Zoom des photos */
var interval_popup_size;
var actu_timer;
var first_interval = true;

function refresh_popup_size()
{
	var active_nav = false;
	var site = $('corps_site');
	var popup_gris = $('site_grisage');
	var popup_content = $('popup_photo');
	var conteneur = $('conteneur_photo');
	var current = $('photo_current');
	
	if($(current.value + "_true"))
	{
		//On set l'image
		conteneur.style.backgroundImage="url('"+images_projet[current.value + "_true"].src+"')";
		//On recupere les dimensions
		var ovp = document.viewport.getDimensions();
		var w = images_projet[current.value + "_true"].width;
		var h = images_projet[current.value + "_true"].height;
		var offsetWidth = parseInt(popup_content.getStyle("padding-left").replace("px","")) +parseInt(popup_content.getStyle("padding-right").replace("px",""));
		var offsetHeight= parseInt(popup_content.getStyle("padding-top").replace("px","")) + parseInt(popup_content.getStyle("padding-bottom").replace("px",""));

		//Offset laissé pour que l'image ne touche pas les bord
		var offset_left_right = 20;
		var offset_up_down = 20;

		//On fixe les taille du popup du cadre et la position sur l'écran;
		if((w+offsetWidth+offset_left_right)>=ovp.width)
		{
			if(first_interval)
				$('btn_move_right').className=$('btn_move_right').className.replace(" btn_lr_hover","");
			active_nav=true;	
			var new_width = ovp.width - offsetWidth - offset_left_right;
			var new_left = offset_left_right/2;
		}
		else
		{
			var new_width = w;
			var new_left = Math.floor((ovp.width - w - offsetWidth) /2);
		}

		if((h+offsetHeight+offset_left_right)>=ovp.height)
		{
			if(first_interval)
				$('btn_move_bottom').className=$('btn_move_bottom').className.replace(" btn_tb_hover","");
			active_nav=true;	
			var new_height = ovp.height - offsetHeight - offset_up_down;
			var new_top = offset_up_down/2;
		}
		else
		{
			var new_height = h;
			var new_top = Math.floor((ovp.height - h - offsetHeight) /2);
		}
		
		//On retaille l'image et on active le module de slider
		popup_content.style.width = new_width + "px";
		popup_content.style.height = new_height + "px";
		popup_content.style.top = new_top + "px";
		popup_content.style.left = new_left + "px";

		conteneur.style.width = new_width + "px";
		conteneur.style.height = new_height + "px";	
		first_interval = false;
		
		//Maj de la taille du fond 100% ou site width
		var sdim  = site.getDimensions();
		if(ovp.height<sdim.height)
			popup_gris.style.height=sdim.height + "px";
		else
			popup_gris.style.height="100%";
		if(ovp.width<sdim.width)
			popup_gris.style.width=sdim.width + "px";
		else
			popup_gris.style.width = "100%";
		
		if(active_nav)	
			$('move_photo').style.display="block";
	}
}

function zoom_photo()
{
	var popup_gris = $('site_grisage');
	refresh_popup_size();
	interval_popup_size = setInterval('refresh_popup_size()',500);

	if(popup_gris)
		new Effect.Appear(popup_gris,{duration:0.5});
}
/* Zoom des videos */

function refresh_video_size()
{
	var site = $('corps_site');
	var popup_gris = $('site_grisage');
	var ovp = document.viewport.getDimensions();
	var popup_content = $('popup_video');
	var conteneur = $('conteneur_video');

	var oDim_video = $('conteneur_video').getDimensions();
	var w = oDim_video.width;
	var h = oDim_video.height;

	var offsetWidth = parseInt(popup_content.getStyle("padding-left").replace("px","")) +parseInt(popup_content.getStyle("padding-right").replace("px",""));
	var offsetHeight= parseInt(popup_content.getStyle("padding-top").replace("px","")) + parseInt(popup_content.getStyle("padding-bottom").replace("px",""));

	var new_left = Math.floor((ovp.width - w - offsetWidth) /2);
	var new_top = Math.floor((ovp.height - h - offsetHeight) /2);

	popup_content.style.top = new_top + "px";
	popup_content.style.left = new_left + "px";

	//Maj de la taille du fond 100% ou site width
	var sdim  = site.getDimensions();
	if(ovp.height<sdim.height)
		popup_gris.style.height=sdim.height + "px";
	else
		popup_gris.style.height="100%";
	if(ovp.width<sdim.width)
		popup_gris.style.width=sdim.width + "px";
	else
		popup_gris.style.width = "100%";

}
function zoom_video()
{
	var popup_gris = $('site_grisage');
	refresh_video_size();
	interval_popup_size = setInterval('refresh_video_size()',200);

	if(popup_gris)
		new Effect.Appear(popup_gris,{duration:0.5});
}

function preload_image(photo_class)
{
	is_preloaded=true;
	//Preload image
	var trues = $$(photo_class);
	for(i=0;i<trues.length;i++)
	{
		var image = new Image();
		if(trues[i].tagName=="IMG")
		{
			image.src = trues[i].src;
		}
		else
		{
			image.src = trues[i].value;
		}
		images_projet[trues[i].id] = image;
	}
}

var base_height; 
function refresh_height()
{
		var ovp = document.viewport.getDimensions();
		var offset_padding = $('corps_site').getStyle("padding-top").replace("px","");
		var new_height = (ovp.height-offset_padding);
		if(new_height<base_height)
		{
			new_height=base_height;
		}			
		$('corps_site').style.height= (new_height-1) + "px";
}

var images_projet = new Array();
var is_preloaded = false;

////////////////////////////////////////
// Gestion des detail de pays ou de site
////////////////////////////////////////
var current_span_id;

function set_current_map(map)
{
	current_span_id = "detail_" + map.id;
	if($(current_span_id))
		$(current_span_id).style.display="block";
	else
		current_span_id	= "";
}

function reset_current_map()
{
	if(current_span_id!="")
	{
		$(current_span_id).style.display="none";
		current_span_id = "";
	}
}

function move_span_pays(event)
{
	var span_to_move = $(current_span_id);
	if(current_span_id!="" && span_to_move)
	{
		var pos_mouse = getMousePosition(event);
		var pos_map = $("fond_map").cumulativeOffset();
		span_to_move.style.top = (pos_mouse[1] - pos_map[1]) + "px";
		span_to_move.style.left = (pos_mouse[0] - pos_map[0] +15) + "px";
	}
}

////////////////////////////////////////
// Navigation dans une photo
////////////////////////////////////////
var move_photo_interval;
var move_photo_active=false;
var move_photo_time = 0.5;
var move_photo_offset = 4;

function move_photo(sens)
{
	var tabpos = $('conteneur_photo').style.backgroundPosition.split(" ");
	var current = $('photo_current');
	var orig_x = parseInt(tabpos[0].replace("px",""));
	var orig_y = parseInt(tabpos[1].replace("px",""));
	var dim_conteneur = $('conteneur_photo').getDimensions();
	var dim_photo_w = images_projet[current.value + "_true"].width;
	var dim_photo_h = images_projet[current.value + "_true"].height;
	
	// Boutons
	var btn_top = $("btn_move_top");
	var btn_bottom = $("btn_move_bottom");
	var btn_left = $("btn_move_left");
	var btn_right = $("btn_move_right");
	
	var dest_x = 0;
	var dest_y = 0;
	
	var clear = false;
	if(sens=="left")
	{
		dest_x = orig_x + move_photo_offset;
		dest_y = orig_y;
		if(dest_x>0)
		{
			clear = true;
			dest_x = 0;
		}
	}
	else if(sens=="right")
	{
		dest_x = orig_x - move_photo_offset;
		dest_y = orig_y;
		if(dest_x<(dim_photo_w-dim_conteneur.width)*-1)
		{
			clear = true;
			dest_x = (dim_photo_w-dim_conteneur.width)*-1;
		}
	}
	else if(sens=="top")
	{
		dest_x = orig_x;
		dest_y = orig_y + move_photo_offset;
		if(dest_y>0)
		{
			clear = true;
			dest_y = 0;
		}
	}
	else if(sens=="bottom")
	{
		dest_x = orig_x;
		dest_y = orig_y - move_photo_offset;
		if(dest_y<(dim_photo_h-dim_conteneur.height)*-1)
		{
			clear = true;
			dest_y = (dim_photo_h-dim_conteneur.height)*-1;
		}		
	}

	$('conteneur_photo').style.backgroundPosition= dest_x + "px " + dest_y + "px"; 
	
	//Maj des boutons
	if(dest_x==0)
		btn_left.className=btn_left.className + " btn_lr_hover";
	else
		btn_left.className=btn_left.className.replace(" btn_lr_hover","");

	if(dest_x==(dim_photo_w-dim_conteneur.width)*-1)
		btn_right.className=btn_right.className + " btn_lr_hover";
	else
		btn_right.className=btn_right.className.replace(" btn_lr_hover","");

	if(dest_y==0)
		btn_top.className=btn_top.className + " btn_tb_hover";
	else
		btn_top.className=btn_top.className.replace(" btn_tb_hover","");

	if(dest_y==(dim_photo_h-dim_conteneur.height)*-1)
		btn_bottom.className=btn_bottom.className + " btn_tb_hover";
	else
		btn_bottom.className=btn_bottom.className.replace(" btn_tb_hover","");
		
	//Clear interval
	if(clear)
	{
		move_photo_active=false;
		clearInterval(move_photo_interval);
	}
}

function start_move_photo(btn_id)
{
	var tab = btn_id.split("_");
	var sens = tab[2];
	move_photo_interval = setInterval("move_photo('"+sens+"')",move_photo_time);
}

function stop_move_photo()
{
	if(move_photo_active)
	{
		clearInterval(move_photo_interval);
		move_photo_active=false;
	}
}
////////////////////////////////////////
// Animation page redac
////////////////////////////////////////
var animpage_currentseq;
var animpage_sequence = new Array();
var animpage_interval_time = 2000;
var animpage_start_time_min = 1;
var animpage_start_time_max = 2;
var animpage_init=false;

function anim_page_change()
{
	var seq = animpage_sequence[animpage_currentseq];
	var tabseq = seq.split("|");
	if(tabseq[1]!="none")
	{
		var tabhide = tabseq[2].split(";");
	}
	else
	{
		var tabhide = Array();
	}

	//Init delay
	var start = getRand(animpage_start_time_min,animpage_start_time_max,true);
	for(var i=0; i<tabhide.length;i++ )
	{
		new Effect.Fade($("agence_image" +tabhide[i]),
			{
				duration:mos_duration_time,
				delay:start,
				queue:{position: 'end', scope:'scopefade'+tabhide[i]}
			});
	}
	new Effect.Appear($("agence_image" +tabseq[0]),
		{
			delay:(start*1.2),
			duration:mos_duration_time,
			queue:{position: 'end', scope:'scopefade'+tabseq[0]}
		});
	
	animpage_currentseq++;
	if(animpage_currentseq>=animpage_sequence.length)
		animpage_currentseq=0;
}

function anim_page()
{
	var idpage = $("page_id").value;
	if(idpage==2)
	{
		animpage_sequence.push("3|hide|1");
		animpage_sequence.push("5|none");
		animpage_sequence.push("4|hide|2");
		animpage_sequence.push("1|hide|3");
		animpage_sequence.push("2|hide|4;5");
	}
	else if(idpage==3)
	{
		animpage_sequence.push("3|hide|1");
		animpage_sequence.push("4|hide|2");
		animpage_sequence.push("5|hide|3;4");
		animpage_sequence.push("1|none");
		animpage_sequence.push("2|hide|5");
	}
	
	animpage_currentseq = 0;
	setInterval("anim_page_change()",animpage_interval_time);
}
////////////////////////////////////////
// Animation de la scrollbar
////////////////////////////////////////
var sliders = new Array();
var wheel_current="";
var init_page = false;
var init_slider = false;

function init_scroller(scroller_id)
{
	if($('actu_move_' + scroller_id))
	{
		// mozilla
		Event.observe('actu_move_' + scroller_id, 'DOMMouseScroll', function(event){wheel_current = scroller_id; wheel(event);});
		// IE/Opera
		Event.observe('actu_move_' + scroller_id, 'mousewheel',  function(event){wheel_current = scroller_id; wheel(event);});

		var max_range = 10;
		if($('actu_move_'+scroller_id).offsetHeight > 0)
		{
			max_range = Math.round($('actu_move_'+scroller_id).scrollHeight / $('actu_move_'+scroller_id).offsetHeight) * 3;
		}
 		sliders[scroller_id] = new Control.Slider('scroller_handler_'+scroller_id, 'track_'+scroller_id, {
			axis: 'vertical',
			range: $R(0,max_range),
			onSlide: function(v) { scrollVertical(v, $('actu_move_'+scroller_id), sliders[scroller_id]);  },
			onChange: function(v) { scrollVertical(v, $('actu_move_'+scroller_id), sliders[scroller_id]);}
		});
		
		// disable vertical scrolling if text doesn't overflow the div
		if ($('actu_move_'+scroller_id).scrollHeight <= $('actu_move_'+scroller_id).offsetHeight) {
			sliders[scroller_id].setDisabled();
			$('c_defile_'+scroller_id).hide();
		}
	}	
}

// scroll the element vertically based on its width and the slider maximum value
function scrollVertical(value, element, slider) {
	element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
}

// mouse wheel code from http://adomas.org/javascript-mouse-wheel/
function handle(delta) {
	sliders[wheel_current].setValueBy(-delta);
}

function wheel(event){
	var delta = 0;
	if (!event) 
		event = window.event;
	if (event.wheelDelta) { 
		delta = event.wheelDelta/120;
		if (window.opera)
			delta = -delta;
	} else if (event.detail) {
		delta = -event.detail/3;
	}

	if (delta)
		handle(delta);

	if (event.preventDefault)
		event.preventDefault();
	
	event.returnValue = false;
}

var anim_btn_interval ="";

function move_btn(btn)
{
	new Effect.Move(btn,{y:0,x:15,duration:1,mode:'relative',transition:Effect.Transitions.pulse,queue:'end'});
	new Effect.Move(btn,{y:0,x:-15,duration:0.2,mode:'relative',transition:Effect.Transitions.linear,queue:'end'});
}

function stop_btn()
{
	clearInterval(anim_btn_interval);
}

var myrules = {
	
	'#corps_site': function(bodysite)
	{
		if(init_page == false)
		{
			var odim = $('corps_site').getDimensions();
			base_height = odim.height;
			refresh_height();
			setInterval('refresh_height()',500);
			init_page = true;
		}
	},
	'.corps_accueil': function(bodysite)
	{
		//Init mosaique
		if(mos_init==false)
		{
			mos_init=true;
			init_photo_masque();
			init_mosaique();
		}
		
		init_photo_smooth();
		
		//Animation du menu
		bodysite.onmousemove = function(event)
		{
			if(check_menu_close(event))
				check_photo_smooth(event);
			//check_menu_close(event);
		}
	},
	'.corps_secteur': function(bodysite)
	{
		//Init mosaique
		if(mos_init==false)
		{
			mos_init=true;
			init_photo_masque();
			init_mosaique();
		}
		//Animation du menu
		bodysite.onmousemove = function(event)
		{
			check_menu_close(event);
		}
	},
	'.corps_references': function(bodysite)
	{
		//Preload iamges
		if(!is_preloaded)
			preload_image('.photo_true');
			
		//Animation du menu
		bodysite.onmousemove = function(event)
		{
			check_menu_close(event);
		}
	},
	'.corps_agence': function(bodysite)
	{
		//Animation du menu
		bodysite.onmousemove = function(event)
		{
			check_menu_close(event);
		}
		
		//Animation des pages
		if(!animpage_init)
		{
			anim_page();
			animpage_init=true;
		}
	},
	'.corps_std': function(bodysite)
	{
		//Animation du menu
		bodysite.onmousemove = function(event)
		{
			check_menu_close(event);
		}
	},
	'.frise_color': function(frise)
	{
		init_animation(frise);
	},

	'.menu_hover': function(menu)
	{
		menu.onmouseover = function(event)
		{
			anim_menu_hover(menu.id.replace("menu_",""));
		}
	},
	'.btn_choix_prec': function(btn)
	{
		choix_projet_nbr = $$('.item_projet').length;	
		choix_projet_first = 1;
		var offset = 4;
		btn.onclick = function(event)
		{
			nav_projet(false,"projet",offset);
		}
	},
	
	'.btn_choix_suiv': function(btn)
	{
		var offset = 4;
		btn.onclick = function(event)
		{
			stop_btn();
			nav_projet(true,"projet",offset);
		}
	},

	'.btn_photo_suiv': function(btn)
	{
		choix_photo_nbr = $$('.item_photo').length;	
		choix_photo_current=3;
		var offset=3;
		btn.onclick = function(event)
		{
			stop_btn();
			nav_photo(true,"photo",offset);
		}
	},
	
	'.btn_photo': function(btn)
	{
		btn.onclick = function(event)
		{
			change_photo(btn.id.replace("btn_",""));
		}
	},
	'.close_popup' :function(btn)
	{
		btn.onclick = function()
		{
			popup = $(btn.id.replace("btn_close_",""));
			clearInterval(interval_popup_size);
			if(popup)
				new Effect.Fade(popup,{duration:1});
		}
	},
	'.view_photo' :function(btn)
	{
		btn.onclick = function()
		{
			zoom_photo();
		}
	},
	'.map_site' :function(map)
	{
		map.onmouseover = function()
		{
			set_current_map(map);
		},
		map.onmouseout = function()
		{
			reset_current_map();
		},
		map.onmousemove = function(event)
		{
			move_span_pays(event);
		}
		
	},
	'.btn_move_click':function(btn)
	{
		btn.onmousedown = function()
		{
			//Reinit de l'interval
			stop_move_photo();
			
			//Start de l'interval
			move_photo_active=true;
			start_move_photo(btn.id);
		}
		btn.onmouseup = function()
		{
			stop_move_photo();
		}
		btn.onmouseout = function()
		{
			stop_move_photo();
		}
	},
	
	'.view_video' :function(btn)
	{
		btn.onclick = function()
		{
			new Ajax.Request(urlaff, {
				asynchronous: 'true',
				postBody: 'tpl=front/actu/video&file='+$(btn.id + "_file").value,
				onSuccess: function(ahr)
				{
					$('conteneur_video').update(ahr.responseText);
					zoom_video();
					Behaviour.apply();
				}
			});
		}
	},
	'.c_defile' : function(scroller){
		if(init_slider == false)
		{
			var tab = scroller.id.split("_");
			var scroller_id = tab[2];
			init_scroller(scroller_id);
			init_slider = true;
		}
 	},
 	
 	'.scroller_btn_top' : function(s_top) {
 		var tab = s_top.id.split("_");
 		var scroller_id = tab[3];
 		s_top.onclick = function() {
 			sliders[scroller_id].setValueBy(-1);
 		}
 	},
 	
 	'.scroller_btn_bottom' : function(s_bottom) {
 		var tab = s_bottom.id.split("_");
 		var scroller_id = tab[3];
 		s_bottom.onclick = function() {
 			sliders[scroller_id].setValueBy(1);
 		}
 	},
 	
 	'.actu_colg' : function() {
 		actu_timer = setInterval("photo_actu()", 2000);
 	},
 	
 	'.title_actu' : function(title) {
 		title.onmouseover = function() {
 			clearInterval(actu_timer);
 			var tabID = title.id.split('_');
 			var idactu = tabID[(tabID.length - 1)];
 			if(title.className.indexOf("actu_photosup")>-1)
	 			var issup = 1;
	 		else
	 			var issup = 0;
 			new Ajax.Request(urlaff, {
				asynchronous: 'true',
				postBody: 'tpl=front/actu/liste_photo&actualite='+idactu+"&chantier="+issup,
				onSuccess: function(ahr)
				{
					$('actu_image').update(ahr.responseText);
					Behaviour.apply();
				}
			});
 		}
 	},
 	'.anim_btn' : function(btn){
 		move_btn(btn);
 		anim_btn_interval = setInterval(function(){move_btn(btn);},3000);
 	}
};

// Enregistrement des regles definies ci dessus dans Behaviour
Behaviour.register(myrules);
