var projetSelected=-1;
var nbProjet=0;
var chProjet=false;
var hImg=0;
var wImg=0;

function width_fenetre(){
    var largeur = 0;
    if(window.innerWidth)
    {
        largeur=window.innerWidth;
    }
    if(document.body.clientWidth)
    {
        largeur=document.body.clientWidth;
    }
    if(document.documentElement.clientWidth)
    {
        largeur=document.documentElement.clientWidth;
    }
    return largeur;
}

function height_fenetre(){
    var largeur = 0;
    if(window.innerHeight)
    {
        largeur=window.innerHeight;
    }
    if(document.body.clientHeight)
    {
        largeur=document.body.clientHeight;
    }
    if(document.documentElement.clientHeight)
    {
        largeur=document.documentElement.clientHeight;
    }
    return largeur;
}
/*
function window_onscroll(){
    alert(window.document.body.scrollTop);
    getId("blogDroite").style.top=window.document.body.scrollTop+'px';
}*/

 /* Trouver la version d'Internet Explorer */
function GetVersionIe(){
 var ua = window.navigator.userAgent;
 var msie = ua.indexOf("MSIE ");

 if(msie > 0) // Pour Internet Explorer
  return(parseInt(ua.substring(msie + 5,ua.indexOf(".",msie))));
 else // Pour les autres navigateurs
  return(0);
}

function getId(id){
    return document.getElementById(id);
}

window.onresize=function(){
    resize(0);
}

function resize(premierResize){
   /* var widthF=width_fenetre();
    var heightF=height_fenetre();
    var height_total = jQuery('.contentGeneral').innerHeight();
    var heightheader = jQuery('.header').height() + jQuery('#redBar').height() + 65;

    

    if(heightheader<heightF){
        getId('contenuPage').style.minHeight=(heightF - heightheader)+'px';
        //getId('Iframe').style.height=heightF+'px';
        // alert(heightF+' :: '+(jQuery('#contenuPage').outerHeight()+100));
        if(GetVersionIe() > 0 && GetVersionIe() < 7){
          if(widthF > 1280){
            $('.contentGeneral').width(widthF);
          }
          getId('blogDroite').style.height=(height_total)+'px';
          getId('contenuPage').style.height=(heightF - heightheader + 40)+'px';
        }else if(GetVersionIe() > 0 && GetVersionIe() < 8){
          getId('blogDroite').style.height=(height_total)+'px';
          getId('contenuPage').style.minHeight=(heightF - heightheader + 40)+'px';
        }else{
          getId('contenuPage').style.minHeight=(heightF - heightheader)+'px';
        }
    }
    */
    //else
        //getId('Iframe').style.height=heightPage+'px';
	placeCalque(premierResize);
}

function placeCalque(premierResize){
      var largeurFond = width_fenetre();
      var hauteurFond = height_fenetre();
       /*if(largeurFond < $('.contentGeneral').outerWidth()) largeurFond = $('.contentGeneral').outerWidth();

       //valeur de class='contPres'
       var hauteurFond=$('.contPres').position().top;
       if(hauteurFond<height_fenetre())
           hauteurFond = height_fenetre();
       if(hauteurFond < 750) hauteurFond = 750;
		
		$('#calque').css({'height': hauteurFond+'px', 'width': largeurFond+'px'});
       */
       //agrandissement de l'image
       var coef=largeurFond/wImg;
       var hTemp=coef*hImg;
       if(hTemp<hauteurFond){
           coef=hauteurFond/hImg;
           hTemp=hImg*coef;
       }
       var wTemp=coef*wImg;
		//$('#calque img').animate({'height': hTemp+'px', 'width': wTemp+'px'});
                $('#calque img').css({'height': hTemp+'px', 'width': wTemp+'px'});

       if(premierResize == 1)
       {
         /*
         placeCalque(2);
       }

       if(premierResize == 2)
       {*/
           $('#calque img').animate({'opacity': 1});
       }
}


// Fonction déclenchée à l'initialisation
function initSite()
{
    $('#boutonBackTop').click(goBackToTop);
    $(window).scroll(function() {

        if($(window).scrollTop() <= $('#contenuPage').offset().top)
        {
            $('#boutonBackTop').hide();
        }else{
            $('#boutonBackTop').show();
        }
    });
}



//function afficheProjet(id, url){
    /*if(jQuery('#conteneurPresentation').innerHeight()=='0')
    {
       // masque et donne de la hauteur à la page pour le scrollTop
       $('#conteneurPresentation').animate({opacity:1, height:'400px'},0, function(){
           gestionLoadingImage(id, url);
       });

    }
   else
    {
        // masque et donne de la hauteur à la page pour le scrollTop
       $('#conteneurPresentation').animate({opacity:1},0, function(){
           gestionLoadingImage(id, url);
       });
    }*/
/*	gestionLoadingImage(id, url);
}*/

function affichePress(id, url){
   // charge le contenu et affiche le
   $('#conteneurPresentation').load(url, function(responseText, textStatus, XMLHttpRequest){
      
       
	   getId('ferme').style.display='block';
       $('#zoneBlockDescProjet').css('opacity', '0');
	  
	  // définit la hauteur de la zone de Présentation
       $('#conteneurPresentation').css('height', jQuery('#contenuProjet').innerHeight()+'px');
	   $('#conteneurPresentation').css('padding-bottom', '20px');
	   $.scrollTo($('#conteneurPresentation').offset().top, 1000);
		
       $('#conteneurPresentation').animate({opacity:1}, 1000, function(){
            resize();
       });
      
   });
}

function gestionLoadingImage(id, url){
   // charge le contenu et affiche le
   $('#conteneurPresentation').load(url, function(responseText, textStatus, XMLHttpRequest){
      
       
	   getId('ferme').style.display='block';
       $('#zoneBlockDescProjet').css('opacity', '0');
	  
	  // définit la hauteur de la zone de Présentation
       $('#conteneurPresentation').css('height', jQuery('#contenuProjet').innerHeight()+'px');
	   $('#conteneurPresentation').css('padding-bottom', '20px');
	   $.scrollTo($('#conteneurPresentation').offset().top, 1000);
		
       $('#conteneurPresentation').animate({opacity:1}, 1000, function(){
           
           var img = new Image();
            $(img).load(function () {
                $(this).addClass('appProjet');
                $(this).css('opacity', '0'); // .hide() doesn't work in Safari when the element isn't on the DOM already
                //$(this).hide();
                $('#zoneBlockImageProjet').html('');$('#zoneBlockImageProjet').append(this);
                //$(this).fadeIn();
                $(this).animate({opacity:1}, 1000);
                $("#contenuPage").animate({opacity:1},500, function() {
                    $('#zoneBlockDescProjet').animate({opacity:1},1000);
                });
                $('#boutonBackTop').animate({bottom: '90px'},
                       {duration: 500,
                        easing: 'easeOutBack'
                    }, function(){});

            }).error(function () {
                // notify the user that the image could not be loaded
            }).attr('src', $('#imageToLoad').html()); 
            resize();
       });
      
   });
}

function afficheEq(id, url){
    //var scrollToPx=$('#conteneurPresentation').offset().top;
    if(jQuery('#conteneurPresentation').innerHeight()=='0'){
       jQuery('#conteneurPresentation').load(url, function(){
          jQuery('#conteneurPresentation').animate({height: (jQuery('#contenuProjetAgence').outerHeight()+25)+'px'}, 1000, function(){centreSurProjet();
            $.scrollTo($('#conteneurPresentation').offset().top, 1000);
            resize();
          });
           //$('html,body').animate({scrollTop: scrollToPx}, 1000);
       });
    }
   else
      jQuery('#conteneurPresentation').load(url, function(){
          jQuery('#conteneurPresentation').animate({height: (jQuery('#contenuProjetAgence').outerHeight()+20)+'px'}, 150, function(){centreSurProjet();
            $.scrollTo($('#conteneurPresentation').offset().top, 1000);
            resize();
          });
           //$('html,body').animate({scrollTop: scrollToPx}, 1000);
      });

}

function centreSurProjet(scrollToPx){
	//document.location='#conteneurPresentation';
}


function fermeProject(){
    getId('ferme').style.display='none';
    
		
	
	$.scrollTo($('#contenuPage').offset().top, 1000);
		
	jQuery('#conteneurPresentation').animate({height: '0px', opacity: 0}, 1000, function(){
		resize();
	   $('#conteneurPresentation').css('padding-bottom', '0px');
		jQuery('#conteneurPresentation').empty();
    });
    $('#boutonBackTop').animate({bottom: '-100px'},
		   {duration: 500,
			easing: 'easeOutBack'
		}, function(){});

}

/*****************************/
// Animation rollover/out
/*****************************/
    var ancienneCibleHover = null;
    var ancienneCibleLeft = 0;
    var ancienneCibleTop = 0;

    function initActiveHover()
    {
        idTimeOut= window.clearTimeout(idTimeOut);
        $('.bouton_vignette').hover(
        // over
        function()
        {
            var cible;
            var varleft;
            var vartop;

            var decoupage = $(this).attr('id').split("_");
            cible = decoupage[0];
            varleft = decoupage[1];
            vartop = decoupage[2];

            $(this).find('.contVignette').stop(true).animate(
            {
                'opacity': '1'
            }, 0, function(){
				//fade
                activeHover(cible, varleft, vartop);
            });
        },
        //out
        function()
        {
            var cible;
            var varleft;
            var vartop;

            var decoupage = $(this).attr('id').split("_");
            cible = decoupage[0];
            varleft = decoupage[1];
            vartop = decoupage[2];


            $(this).find('.contVignette').stop(true).animate(
            {
                'opacity': '1'
            }, 0, function(){
				//fade
                desactiveHoverFin(cible, varleft, vartop);
            });
        });

    }

    function activeHover(cible, varleft, vartop){
            if(document.getElementById(cible+'NB')) $("#" + cible+'NB').css('z-index', 101);
            $("#" + cible).css('z-index',100);

            var reg=new RegExp("(blocSlide)", "g");
            var zoneInfo = "#" +cible.replace(reg,"infoVignette");
            //var zoneInfo = $("#" + cible).parent().find(".infoVignette");
            $(zoneInfo).css('z-index',102);
            $(zoneInfo).css('opacity',0);

            $("#" + cible)
              .css({
                WebkitBorderTopLeftRadius: 5,
                WebkitBorderTopRightRadius: 5,
                WebkitBorderBottomLeftRadius: 5,
                WebkitBorderBottomRightRadius: 5,
                MozBorderRadius: 5,
                BorderRadius: 5});
            /*
              .animate({
                WebkitBorderTopLeftRadius: 30,
                WebkitBorderTopRightRadius: 30,
                WebkitBorderBottomLeftRadius: 30,
                WebkitBorderBottomRightRadius: 30,
                MozBorderRadius: 30,
                BorderRadius: 30}, 900);
            */


			$('#'+cible).stop().animate({
                          //'opacity':1,
						  borderTopColor: "#e62b36",borderBottomColor: "#e62b36",borderLeftColor: "#e62b36",borderRightColor: "#e62b36",
						  borderLeftWidth: "6px",borderTopWidth: "6px",borderRightWidth: "6px",borderBottomWidth: "40px"
                },{duration: 300,
                    easing: 'easeOut'
                });

            if(document.getElementById(cible+'NB'))
            {
                $('#'+cible+'NB').stop().animate({
                          'opacity':0
                },{duration: 500,
                    easing: 'easeOut'
                });
                $(zoneInfo).stop().animate({
                          'opacity':1
                },{duration: 500,
                    easing: 'easeOut'
                });
            }
            else{
                $(zoneInfo).stop().animate({
                          'opacity':1
                },{duration: 500,
                    easing: 'easeOut'
                });
            }
    }

    function desactiveHover(cible, varleft, vartop){
        if(document.getElementById(cible+'NB')){
            $('#'+cible+'NB').stop().animate({
                          'opacity':1
                },{duration: 500,
                    easing: 'easeOut'
                }, function () {desactiveHoverFin(cible, varleft, vartop);});
	}
	else
            desactiveHoverFin(cible, varleft, vartop);
    }

    function desactiveHoverFin(cible, varleft, vartop){

            if(document.getElementById(cible+'NB')) $("#" + cible+'NB').css('z-index', 11);
            $("#" + cible).css('z-index',10);
            var reg=new RegExp("(blocSlide)", "g");
            var zoneInfo = "#" +cible.replace(reg,"infoVignette");
            
			$(zoneInfo).css('z-index',9);
           
		   $('#'+cible).stop().animate({
                          //'opacity':1,
						  borderTopColor: "#ffffff",borderBottomColor: "#ffffff",borderLeftColor: "#ffffff",borderRightColor: "#ffffff",
						  borderLeftWidth: "6px",borderTopWidth: "6px",borderRightWidth: "6px",borderBottomWidth: "6px"
			},{duration: 300,
				easing: 'easeOut'
			});
			
			
            // remise à zéro de l'opacité en stopant l'éventuelle anime précédente
            $(zoneInfo).stop().animate({
                      'opacity':0
            },{duration: 0,
                easing: 'easeOut'
            });

    }

    function goBackToTop(){
	//$('html,body').animate({scrollTop: $('#contenuPage').offset().top}, 1000);
		$.scrollTo($('#contenuPage').offset().top, 1000);
        return false;
    }

    function verifForm(){
        if(getId('nom').value==''){
                alert('Votre nom est incorrect');
                return false;
        }
        else if(getId('prenom').value==''){
            alert('Votre prénom est incorrect');
            return false;
        }
        else if(getId('message').value=='' || getId('message').value=='Votre Message...'){
            alert('Votre message est incorrect');
            return false;
        }
        else if(getId('mail').value.indexOf('@', 0)==-1){
            alert('Votre e-mail est incorrect');
            return false;
        }
        else
            return true;
    }


var enCours = false;
function afficheProjet(bloc, id, url){
  if(enCours == false){
      enCours = true;
      if($(".blocAjout").hasClass("blocAjout")){
          $(".masonry-brick-cache").show();
          $(".masonry-brick-cache").addClass('masonry-brick');
          $(".masonry-brick-cache").removeClass('masonry-brick-cache');

          $('.blocAjout').animate(
            {height:0, width:0}, 200,function(){
            $('.blocAjout').remove();
            chargeProjet(bloc, id, url);
          });
      }else{
          //$(".conteneurVignetteHome").find('.vignetteCoul').fadeOut(200);
          //$(".conteneurVignetteHome").find('.vignetteNB').fadeIn(200);
          chargeProjet(bloc, id, url);
      }
  }
}
timer = null;
function chargeProjet(bloc, id, url){
    // charge le contenu et affiche le
    //$(bloc).before( "<div class='masonry-brick blocAjout'></div>" );
    $(".conteneurVignetteHome").prepend( "<div class='masonry-brick blocAjout'></div>" );

        $.post(url, function(leRetour){
            $('.blocAjout').html(leRetour);

            $(bloc).hide( 200,function(){
                $(bloc).removeClass('masonry-brick');
                $(bloc).addClass('masonry-brick-cache');
                $(".conteneurVignetteHome").masonry( 'reload', function(){
                  timer = setTimeout("repositionne()",400);
                  //repositionne();
                } );
            });

        });


}
function moveProjetG(url, urlLienProfond){
    if(!chProjet){
        chProjet=true
        projetSelected--;
        if(projetSelected<0)
          projetSelected=nbProjet-1;
        $.post(url+projets[projetSelected]+'/getInfo/desc', function(leRetour){
            jQuery('.contentProjetBox').before("<div class='contentProjetBox colGauche' style='margin-left: -707px'>"+ leRetour + "</div>");
            jQuery('.contentProjetBox').first().animate({marginLeft: '0'}, 500, function(){
              jQuery('.contentProjetBox').last().remove();
              //jQuery('.blocAjout').css('height', jQuery('.contentProjetBox').innerHeight());
              if(jQuery('.contentProjetBox').innerHeight() > 230){
                jQuery('.flecheImg').css('top', '180px');
              }else{
                jQuery('.flecheImg').css('top', (jQuery('.contentProjetBox').innerHeight() - 50) + "px");
              }
              //jQuery('#contenuProjet').animate({height: jQuery('.contentProjetBox').innerHeight()+'px'}, 150, function(){
              $(".conteneurVignetteHome").masonry( 'reload', function(){
                timer = setTimeout("repositionne()",400);
                //repositionne();
              } );
              //});
            });
            chProjet=false;
       });
        suivant = projetSelected + 1;
        if(suivant==nbProjet)
          suivant=0;
        $(".flecheDImage").find("a").attr("href", urlLienProfond+projets[suivant]);
        precedent = projetSelected - 1;
        if(precedent<0)
          precedent = nbProjet - 1;
        $(".flecheGImage").find("a").attr("href", urlLienProfond+projets[precedent]);
    }
}

function moveProjetD(url, urlLienProfond){
    if(!chProjet){
        chProjet=true;
        projetSelected++;
        if(projetSelected==nbProjet)
          projetSelected=0;
        $.post(url+projets[projetSelected]+'/getInfo/desc', function(leRetour){
        jQuery('.contentProjetBox').after("<div class='contentProjetBox colGauche'>"+ leRetour + "</div>");
            jQuery('.contentProjetBox').first().animate({marginLeft: '-707px'}, 500, function(){
              jQuery('.contentProjetBox').first().remove();
              //jQuery('.blocAjout').css('height', jQuery('.contentProjetBox').innerHeight());
              if(jQuery('.contentProjetBox').innerHeight() > 230){
                jQuery('.flecheImg').css('top', '180px');
              }else{
                jQuery('.flecheImg').css('top', (jQuery('.contentProjetBox').innerHeight() - 50) + "px");
              }
              //jQuery('#contenuProjet').animate({height: jQuery('.contentProjetBox').innerHeight()+'px'}, 150, function(){
              $(".conteneurVignetteHome").masonry( 'reload', function(){
                timer = setTimeout("repositionne()",400);
                //repositionne();
              } );
              //});
            });
            chProjet=false;
       });
        suivant = projetSelected + 1;
        if(suivant==nbProjet)
          suivant=0;
        $(".flecheDImage").find("a").attr("href", urlLienProfond+projets[suivant]);
        precedent = projetSelected - 1;
        if(precedent<0)
          precedent = nbProjet - 1;
        $(".flecheGImage").find("a").attr("href", urlLienProfond+projets[precedent]);
    }
}

function repositionne(){
    //resize();
    $.scrollTo($('.blocAjout').offset().top, 1000);
    enCours = false;
    clearTimeout(timer);

}
