function MM_preloadImages() { //v3.0
  var d=document;if(d.images){if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments;for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){d.MM_p[j]=new Image;d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr;for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;if(!d) d=document;if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document;n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n];for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n);return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments;document.MM_sr=new Array;for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x;if(!x.oSrc) x.oSrc=x.src;x.src=a[i+2];}
}

 function print_r(obj) {
  win_print_r = window.open('about:blank', 'win_print_r');
  win_print_r.document.write('<html><body>');
  r_print_r(obj, win_print_r);
  win_print_r.document.write('</body></html>');
 }

 function r_print_r(theObj, win_print_r) {
  if(theObj.constructor == Array ||
   theObj.constructor == Object){
   if (win_print_r == null)
    win_print_r = window.open('about:blank', 'win_print_r');
   }
   for(var p in theObj){
    if(theObj[p].constructor == Array||
     theObj[p].constructor == Object){
     win_print_r.document.write("<li>["+p+"] =>"+typeof(theObj)+"</li>");
     win_print_r.document.write("<ul>")
     r_print_r(theObj[p], win_print_r);
     win_print_r.document.write("</ul>")
    } else {
     win_print_r.document.write("<li>["+p+"] =>"+theObj[p]+"</li>");
    }
   }
  win_print_r.document.write("</ul>")
 }

function right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function isArray(elt){
    var nb=elt.length;
    if(!nb || typeof(elt)=='string'){
        return false;
    }
    else{
        return true;
    }
}

function ajaxAction(url,params,block){
    var parameters={};
    parameters['ajaxAction'] = true;
    var k=0;
    for(var i=0;i<params.length;i++){
        if(isArray(params[i])){
            var t=params[i];
            for(var j=0;j<t.length;j++){
                if(t[j].getAttribute('type') == 'radio'){
                    if(t[j].checked){
                        parameters[t[j].name]=t[j].value;
                    }
                }
                else{
                    parameters[t[j].name]=t[j].value;
                }
            }
        }
        else{
            parameters['param'+k]=params[i];
            k++;
        }
    }
    $.ajax({
        type: "POST",
        url: url,
        data: parameters,
        dataType:"html",
        beforeSend: function(data){
            $('#'+block).fadeOut('fast');
        },
        success: function(data){
            $('#'+block).html(data);
        },
        complete: function(data){
            $('#'+block).fadeIn('slow');
            $(document).ready( function(){
                documentReady();
            });
        }
    });
}

function rotate(img){
    var sens;
    if($('#page-photo img#back').css('width')=='0px'){
        if($('#page-photo img#front').attr('src')==web_root+'images/photos/'+img+'.png'){return false;}
        if(right($('#page-photo img#front').attr('src'),6)=='-g.png'){sens='-d';}else{sens='-g';}
        $('#page-photo img#back').attr('src',web_root+'images/photos/'+img+sens+'.png');

        $('#page-photo img#front').stop().animate({width:'0px',height:'370px',marginLeft:'191px',opacity:'0.5'},{duration:100});
        window.setTimeout(function() {
            if(img!='portfolio' && img!='portfolio-print' && img!='portfolio-web' && img!='portfolio-client' && img!='nous-contacter' && img!='l-equipe'){ $("#page-photo img#back").stop().animate({width:'382px',height:'370px',marginLeft:'-40px',opacity:'1'},{duration:300}); }
        },300);
    }
    else{
        if($('#page-photo img#back').attr('src')==web_root+'images/photos/'+img+'.png'){return false;}
        if(right($('#page-photo img#back').attr('src'),6)=='-g.png'){sens='-d';}else{sens='-g';}
        $('#page-photo img#front').attr('src',web_root+'images/photos/'+img+sens+'.png');

        $('#page-photo img#back').stop().animate({width:'0px',height:'370px',marginLeft:'191px',opacity:'0.5'},{duration:300});
        window.setTimeout(function() {
            if(img!='portfolio' && img!='portfolio-print' && img!='portfolio-web' && img!='portfolio-client' && img!='nous-contacter' && img!='l-equipe'){ $("#page-photo img#front").stop().animate({width:'382px',height:'370px',marginLeft:'-40px',opacity:'1'},{duration:300}); }
        },300);
    }
    return true;
}

function setMenuActiveLink(name){
    $('#menu a').removeClass('active');
    $('#menu a').removeClass('act');
    if($('#lien_'+name).hasClass('lvl0')){
        $('#lien_'+name).addClass('active');
    }
    else{
        $('#lien_'+name).parent().parent().parent().children('a').addClass('active');
    }
    $('#lien_'+name).addClass('act');
}

function updateBambou(name){
    $('#content-bambou img').fadeOut('fast',function(){ $('#content-bambou img').attr('src',web_root+'images/bambous/'+name+'.png');});
    $('#content-bambou img').fadeIn('slow');
}

function updateBambouBig(name){
    $('#content-bambou-big-g img').fadeOut('fast',function(){ $('#content-bambou-big-g img').attr('src',web_root+'images/bambous-big/'+name+'_g.png');});
    $('#content-bambou-big-g img').fadeIn('slow');
    $('#content-bambou-big-d img').fadeOut('fast',function(){ $('#content-bambou-big-d img').attr('src',web_root+'images/bambous-big/'+name+'_d.png');});
    $('#content-bambou-big-d img').fadeIn('slow');
}

function setPagination(){
    $('ul#paginator').parent().remove();
    var pages=$('#page-texte div.page');
    if(pages.length>1){
        $('<div><ul id="paginator"></ul></div>').appendTo('#page');
        $('<li class="last"><a href="#" class="lien-page-1" onclick="openPage(1); return false;"><img src="'+web_root+'/images/arrow-back.png" alt="Fleche" /><img src="'+web_root+'/images/arrow-back.png" alt="Fleche" /></a></li>').appendTo('ul#paginator');

        pages.each(function(i){
            if(i>0){
                $(this).hide();
            }
            $(this).addClass('page-'+(i+1));
            var classLast=(i+1)==pages.length?' class="last"':'';
            var classActive=(i+1)==1?' active':'';
            $('<li'+classLast+'><a href="#" class="lien-page-'+(i+1)+classActive+'" onclick="openPage('+(i+1)+'); return false;">'+(i+1)+'</a></li>').appendTo('ul#paginator');
        });
        $('<li class="last"><a href="#" class="lien-page-'+pages.length+'" onclick="openPage('+pages.length+'); return false;"><img src="'+web_root+'/images/arrow.png" alt="Fleche" /><img src="'+web_root+'/images/arrow.png" alt="Fleche" /></a></li>').appendTo('ul#paginator');
    }
}

function openPage(i){
    if($('#page-texte div.page-'+i).css('display')=='block'){return false;}
    $('ul#paginator li a').removeClass('active');
    $('ul#paginator li a.lien-page-'+i).addClass('active');
    $('#page-texte div.page').hide('slide', {opacity:0,direction: 'left'}, 100);
    $('#page-texte div.page-'+i).show('slide', {opacity:0,direction: 'right'}, 100);
    return true;
}

function preloadImages(){
    MM_preloadImages(web_root+'images/photos/l-agence-g.png');
    MM_preloadImages(web_root+'images/photos/l-identite-g.png');
    MM_preloadImages(web_root+'images/photos/principes-actifs-g.png');
    MM_preloadImages(web_root+'images/photos/le-bambou-g.png');
    MM_preloadImages(web_root+'images/photos/methode-g.png');
    MM_preloadImages(web_root+'images/photos/marquer-g.png');
    MM_preloadImages(web_root+'images/photos/se-demarquer-g.png');
    MM_preloadImages(web_root+'images/photos/se-faire-remarquer-g.png');
    MM_preloadImages(web_root+'images/photos/strategie-d-influence-g.png');
    MM_preloadImages(web_root+'images/photos/liens-g.png');

    MM_preloadImages(web_root+'images/photos/l-agence-d.png');
    MM_preloadImages(web_root+'images/photos/l-identite-d.png');
    MM_preloadImages(web_root+'images/photos/principes-actifs-d.png');
    MM_preloadImages(web_root+'images/photos/le-bambou-d.png');
    MM_preloadImages(web_root+'images/photos/methode-d.png');
    MM_preloadImages(web_root+'images/photos/marquer-d.png');
    MM_preloadImages(web_root+'images/photos/se-demarquer-d.png');
    MM_preloadImages(web_root+'images/photos/se-faire-remarquer-d.png');
    MM_preloadImages(web_root+'images/photos/strategie-d-influence-d.png');
    MM_preloadImages(web_root+'images/photos/liens-d.png');

    MM_preloadImages(web_root+'images/bambous/l-agence.png');
    MM_preloadImages(web_root+'images/bambous/l-identite.png');
    MM_preloadImages(web_root+'images/bambous/principes-actifs.png');
    MM_preloadImages(web_root+'images/bambous/le-bambou.png');
    MM_preloadImages(web_root+'images/bambous/methode.png');
    MM_preloadImages(web_root+'images/bambous/marquer.png');
    MM_preloadImages(web_root+'images/bambous/se-demarquer.png');
    MM_preloadImages(web_root+'images/bambous/se-faire-remarquer.png');
    MM_preloadImages(web_root+'images/bambous/strategie-d-influence.png');
    MM_preloadImages(web_root+'images/bambous/portfolio.png');
    MM_preloadImages(web_root+'images/bambous/portfolio-client.png');
    MM_preloadImages(web_root+'images/bambous/portfolio-print.png');
    MM_preloadImages(web_root+'images/bambous/portfolio-web.png');
    MM_preloadImages(web_root+'images/bambous/nous-contacter.png');
    MM_preloadImages(web_root+'images/bambous/l-equipe.png');
    MM_preloadImages(web_root+'images/bambous/liens.png');

    MM_preloadImages(web_root+'images/bambous-big/l-agence-g.png');
    MM_preloadImages(web_root+'images/bambous-big/l-identite-g.png');
    MM_preloadImages(web_root+'images/bambous-big/principes-actifs-g.png');
    MM_preloadImages(web_root+'images/bambous-big/le-bambou-g.png');
    MM_preloadImages(web_root+'images/bambous-big/methode-g.png');
    MM_preloadImages(web_root+'images/bambous-big/marquer-g.png');
    MM_preloadImages(web_root+'images/bambous-big/se-demarquer-g.png');
    MM_preloadImages(web_root+'images/bambous-big/se-faire-remarquer-g.png');
    MM_preloadImages(web_root+'images/bambous-big/strategie-d-influence-g.png');
    MM_preloadImages(web_root+'images/bambous-big/liens-g.png');

    MM_preloadImages(web_root+'images/bambous-big/l-agence-d.png');
    MM_preloadImages(web_root+'images/bambous-big/l-identite-d.png');
    MM_preloadImages(web_root+'images/bambous-big/principes-actifs-d.png');
    MM_preloadImages(web_root+'images/bambous-big/le-bambou-d.png');
    MM_preloadImages(web_root+'images/bambous-big/methode-d.png');
    MM_preloadImages(web_root+'images/bambous-big/marquer-d.png');
    MM_preloadImages(web_root+'images/bambous-big/se-demarquer-d.png');
    MM_preloadImages(web_root+'images/bambous-big/se-faire-remarquer-d.png');
    MM_preloadImages(web_root+'images/bambous-big/strategie-d-influence-d.png');
    MM_preloadImages(web_root+'images/bambous-big/liens-d.png');
}

function setLinkClick(el){
    var name=$(el).attr('name');
    var href=$(el).attr('href');
    //On test que le lien ne soit pas le lien actif
    if(!$('#lien_'+name).hasClass('act') && name!='facebook' && name!='index'){
        if($('#page-photo').css('display')=='none'){
            window.setTimeout(function() { $('#page-photo').show();}, 300);
        }
        //Gestion du lien actif
        setMenuActiveLink(name);
        //Update de la photo
        rotate(name);
        //Update du bambou
        updateBambou(name);
        updateBambouBig(name);
        if(name=='l-equipe'){
            ajaxAction(web_root+'index/l-equipe-bio',[],'l-equipe-bio');
        }
        //Update du slogan
        ajaxAction(web_root+'index/layout-slogan/slogan/'+name,[],'page-slogan');
        //Update du texte
        ajaxAction(href,[],'page-texte');
        if(name!='nous-contacter'){
            $('#map-container').fadeOut('fast',resize(name));
        }
        if(name!='l-equipe'){
            $('#l-equipe-bio').fadeOut('fast',resize(name));
        }
        if(name!='methode'){
            $('#page-methode').fadeOut('fast',resize(name));
        }
        else{
            $('#page-methode').fadeIn('slow',resize(name));
        }

        if(name=='se-demarquer' || name=='portfolio' || name=='portfolio-print' || name=='portfolio-web' || name=='portfolio-client'){
            if(name=='se-demarquer'){
                $('#page-photo').animate({left:'45px'},'100');
                $('#page-slogan').animate({left:'45px'},'100');
                $('#page-texte').animate({left: '465px'},'100');

            }
            else{
                //$('#page-texte').css('width','980px').css('height','auto').css('left','0');
                //$('#page-texte').css('width','980px').css('height','auto');

            }
        }
        else if($('#page-texte').css('left')!='45px'){
            $('#page-texte').css('height','380px');
            $('#page-texte').animate({left: '45px'},'100');
            $('#page-photo').animate({left:'395px'},'100');
            $('#page-slogan').animate({left:'355px'},'100');
        }
    }
    //Désactivation du href
    return false;
}

var resize=function(name){
    if(name!='portfolio' && name!='portfolio-print' && name!='portfolio-web' && name!='portfolio-client'){
        window.setTimeout(function() { $('#page-texte').css('width','250px');}, 300);
    }
};
var web_root;
jQuery.fn.exists = function(){return this.length>0;}

var portfolioCarousel=function(){
    //window.setTimeout(function() {
        $(".portfolio-carousel").smoothDivScroll({
            autoScroll: "onstart",
            autoScrollDirection: "endlessloopright",
            autoScrollStep: 1,
            autoScrollInterval: 10,
            visibleHotSpots: "",
            animation:'slow'
        });
    //}, 500);
        $("#portfolio-client a").colorbox({speed:"500",opacity:'0.85',width:'800px',height:'600px', current:'','transition':'elastic','loop':false});

        $("#portfolio-logos-scroll").simplyScroll({
            className: 'vert',
            horizontal: false,
            frameRate: 20,
            speed: 1,
            autoMode: 'loop'
        });

        $('#colorbox').mouseenter(function () {
            $('#cboxPrevious,#cboxNext').stop(true,true).fadeIn('fast');
        });
        $('#colorbox').mouseleave(function () {
            $('#cboxPrevious,#cboxNext').stop(true,true).fadeOut('fast');
        });
    };
var documentReady=function(){
    //Gestion des liens
    $('a:not(#portfolio-client a,#colorbox a,#l-equipe a,#l-equipe-bio a, a.lien-externe)').click(function(){
        return setLinkClick(this);
    });

    $('.portfolio-carousel').mouseenter(function () {
        $('.scrollingHotSpotLeft,.scrollingHotSpotRight').stop(true,true).fadeIn('fast');
        $(this).smoothDivScroll("stopAutoScroll");
    });
    $('.portfolio-carousel').mouseleave(function () {
        $('.scrollingHotSpotLeft,.scrollingHotSpotRight').stop(true,true).fadeOut('fast');
        $(this).smoothDivScroll("startAutoScroll");
    });
    window.setTimeout(function() {
        portfolioCarousel();
    }, 300);

    $('#l-equipe a:not(#l-equipe-vcard a)').click(function(){
        var name=$(this).attr('name');
        if(!$('#lien-equipe_'+name).hasClass('active')){
            $('#l-equipe a').removeClass('active');
            $(this).addClass('active');
            ajaxAction(web_root+'index/l-equipe-bio/e/'+name,[],'l-equipe-bio');
            ajaxAction(web_root+'index/l-equipe-vcard/e/'+name,[],'l-equipe-vcard');
        }
        return false;
    });

    $('#l-equipe-bio a:not(a.lien-externe)').click(function(){
        var name=$(this).attr('name');
        ajaxAction(web_root+'index/l-equipe-bio/e/'+name,[],'l-equipe-bio');
        ajaxAction(web_root+'index/l-equipe/e/'+name,[],'l-equipe');
        return false;
    });

    setPagination();
};

$(document).ready( function(){
    preloadImages();
    $(document).pngFix();
    //Init de l'effet de transition des photos
    $("#page-photo img#back").stop().css({width:'0px',height:'370px',marginLeft:'191px',opacity:'0.5'});
    documentReady();
});
