/**
* 
* Screen Resize (pour les écrans trop petits)
* 
*/
$(window).bind('resize load', function(){    
    if (($(document).height() <= 620) || ($(document).width() <= 985)){
        if(!$('body').hasClass('small'))
            $('body').addClass('small');       
    }
    else
        $('body').removeClass('small');                               
});

/**
* 
* Document Ready !
* 
*/

$(document).ready(function(){ 

 
    /* Menu */
    var timeout         = 0;
    var closetimer      = 0;
    var ddmenuitem      = 0;

    function jsddm_open(){    
        jsddm_canceltimer();
        jsddm_close();
        ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
    }

    function jsddm_close(){if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

    function jsddm_timer(){closetimer = window.setTimeout(jsddm_close, timeout);}

    function jsddm_canceltimer(){  
        if(closetimer){   
            window.clearTimeout(closetimer);
            closetimer = null;}
    }

    document.onclick = jsddm_close;   

    $('#jsddm > li').bind('mouseover', jsddm_open);
    $('#jsddm > li').bind('mouseout',  jsddm_timer);

    /* Titre */
    $('#content .title .title_logo').hover(
    function(){
        $(this).children('.transparent').fadeOut("slow");   
        $(this).children('.opaque').fadeIn("slow");   
    },
    function(){
        $(this).children('.opaque').fadeOut("slow");   
        $(this).children('.transparent').fadeIn("slow");     
    }
    );


    /* miniature */
    $(function() {  
        $("body .carousel div").jMyCarousel({  
            visible: '100%',
            circular: false,  
            eltByElt: false,
            btnPrev: (($('body:not(#news) .carousel li').length > 6)||($('body#news .carousel li').length > 3))?$('.previous'):'',
            btnNext: (($('body:not(#news) .carousel li').length > 6)||($('body#news .carousel li').length > 3))?$('.next'):''  
        }); 
        
       
    }); 


    $(".carousel ul li .mask").hide();
    $(".carousel ul li a").hover(function(){$(this).children('.mask').fadeIn("fast");}, function(){$(this).children('.mask').fadeOut("fast");})  

    var ecart;
    if (navigator.appVersion.indexOf("Mac")!=-1)        ecart = 2;
    else                                                ecart = 0;
    
    var empty_place_width = $('body #wrapper').width() - ($('a#project_cmd').outerWidth() + $('a#scene_cmd').outerWidth() + $('a#details_cmd').outerWidth() + $('a#movie_cmd').outerWidth() + $('a#download_cmd').outerWidth()) - 1; //-1 car bordure qui tiens 1px, non comptabilisé dans le .width();   
    $('body#projet #content #commands > a#emptyplace').css('width', empty_place_width - ecart);          
    
    
    var menu_padding_right = 0;
    $('div#menu ul#jsddm > li').each(function(){ menu_padding_right += $(this).outerWidth();}); 
    //console.log("Total des menus : "+menu_padding_right)
    menu_padding_right = (984-menu_padding_right-ecart);
    
    if(menu_padding_right < 0){
        $('#menu').css('fontSize', "10px");
        var menu_padding_right = 0;
        $('div#menu ul#jsddm > li').each(function(){ menu_padding_right += $(this).outerWidth();}); 
        menu_padding_right = (984-menu_padding_right-ecart);
    }
    
    $('#menu > ul > li:last-child').css('padding-right', menu_padding_right); 
    
    
           
    /* Rendu différent de la typo sur mac 
    if (navigator.appVersion.indexOf("Mac")!=-1){
        if( navigator.userAgent.match(/iPad/i) != null )
            {
            $('#menu > ul > li:last-child').css('padding-right', '4px'); 
            

        }else{
            $('#menu > ul > li:last-child').css('padding-right', '21px');    
            $('body#projet #content #commands > a#emptyplace').css('padding-right', '25px');     
        }

    }
    */ 

});






/*
* Home ready ! 
*/  

$('body#home').ready(function(){
    var effect;
    
    if($.browser.msie && (($.browser.version) == "7.0") || ($.browser.version == "8.0")){
        effect='horizontal';    
    }else{
        effect='fade';    
    }
   
    /* Petit slide */
    var minibx = $("#minibx").bxSlider({
        auto: true,
        speed: 3000,
        pause: 5000,
        mode:effect,
        controls: false,
        onNextSlide: function(currentSlideNumber, totalSlideQty, currentSlideHtmlObject){maxibx.goToSlide(currentSlideNumber);},
        onPrevSlide: function(currentSlideNumber, totalSlideQty, currentSlideHtmlObject){maxibx.goToSlide(currentSlideNumber);}
    });

    /* Grand slide */
    var maxibx = $("#maxibx").bxSlider({
        mode:effect,
        speed: 3000,
        controls: false
    });

});

/*
* Project ready !
*/

$('body#project').ready(function(){

    $('body#projet #menu').css('padding-top', ((($('body').height() / 2) - 315))+'px');
    $(window).bind('resize load', function(){   $('body#liens #menu').css('padding-top', ((($('body').height() / 2) - 315))+'px');  });


    /* Onglet */
    
    $('a#project_cmd, a#details_cmd, a#scene_cmd').not('.disabled').click(function(){

        if($(this).is('.selected')){
            $('#'+$(this).attr('rel')).slideUp(700, function(){
                $('#commands .selected').removeClass('selected');     
            });      
        }else{
            $('#commands .selected').removeClass('selected');
            $(this).addClass('selected');

            if($('.sub:visible').size()){            
                $('.sub:visible').slideUp(700, function(){ 
                    $('#'+$('.selected').attr('rel')).slideDown(700)
                });
            }else{
                $('#'+$('.selected').attr('rel')).slideDown(700)       
            }

        }

        return false;
    });
    $('#main_illustration').css('background-image','url('+$("#main_illustration img").attr('src')+')');
    $('#project_subcontent div.close').click(function(){$('a#project_cmd').click();})
    $('#scene_subcontent div.close').click(function(){$('a#scene_cmd').click();})
    $('#details_subcontent div.close').click(function(){$('a#details_cmd').click();})

    /* scenographie */
    $('#main_illustration').css('background-image', 'url('+$('#main_illustration img').attr('src')+')')
    $('body#projet ul#options_list li a').click(function(){
        if($(this).parent().is('.selected')) return false;
        
        $('ul#options_list li.selected').removeClass('selected');
        $(this).parent().addClass('selected');

        var newSrc = $(this).attr('href');

        $('.loader').show();

        $('#main_illustration img').fadeOut('slow', function(){

            $(this).attr('src',newSrc).bind('onreadystatechange load', function(){                    
                if (this.complete) { 
                    $('.loader').hide();
                    
                    $(this).fadeIn(300, function(){$('#main_illustration').css('background-image', 'url('+$(this).attr('src')+')') });    
                }
            });

        });

        return false;
    })

});

/*
* Category ready !
*/
$('body#category').ready(function(){
    $(function() {  
        if($('#carousel_u li').length > 0){
            $("#carousel_u div").jMyCarousel({  
                visible: '100%',
                circular: false,  
                eltByElt: false,
                btnPrev: ($('#carousel_u li').length > 6)?$('#previous_u'):'', 
                btnNext: ($('#carousel_u li').length > 6)?$('#next_u'):''  
            });
        }
    });

    $(function() {  
        if($('#carousel_p li').length > 0){
            $("#carousel_p div").jMyCarousel({  
                visible: '100%',
                circular: false,  
                eltByElt: false,
                btnPrev: ($('#carousel_p li').length > 6)?$('#previous_p'):'',
                btnNext: ($('#carousel_p li').length > 6)?$('#next_p'):''  
            });    
        }
    });

    $(function() {  
        if($('#carousel_a li').length > 0){
            $("#carousel_a div").jMyCarousel({  
                visible: '100%',
                circular: false,  
                eltByElt: false,
                btnPrev: ($('#carousel_a li').length > 6)?$('#previous_a'):'',
                btnNext: ($('#carousel_a li').length > 6)?$('#next_a'):''    
            }); 
        }
    });

    $(".carousel_multiple ul li .mask").hide();
    $(".carousel_multiple ul li a").hover(function(){$(this).children('.mask').fadeIn("fast");}, function(){$(this).children('.mask').fadeOut("fast");})  

});

/**
* Page agence ready !
*/
$('body#agence').ready(function(){
    $('body#agence #menu').css('padding-top', ((($('body').height() / 2) - 291))+'px'); 
    $(window).bind('resize load', function(){   $('body#liens #menu').css('padding-top', ((($('body').height() / 2) - 291))+'px');  });

});

/**
* Page presse ready !
*/
$('body#presse').ready(function(){
    $('body#presse #menu').css('padding-top', ((($('body').height() / 2) - 291))+'px');
    $(window).bind('resize load', function(){   $('body#liens #menu').css('padding-top', ((($('body').height() / 2) - 291))+'px');  });

});

/**
* Page news ready !
*/
$('body#news').ready(function(){
    $('body#news #menu').css('padding-top', ((($('body').height() / 2) - 291))+'px');
    $(window).bind('resize load', function(){   $('body#news #menu').css('padding-top', ((($('body').height() / 2) - 291))+'px');  });
    
    $(function() {  
        $("#news_carousel .carousel div").jMyCarousel({  
            visible: '100%',
            circular: false,  
            eltByElt: false,
            btnPrev: $('#news_carousel .previous'),
            btnNext: $('#news_carousel .next')  
        }); 
    });
   $("#news_carousel .carousel div").hide(); 

});

/**
* Page contact ready !
*/
$('body#contact').ready(function(){
    $('#contact_form').validate({
        messages: {
            name:    e_name,
            email:   e_mail,
            subject: e_sujet,
            msg:     e_msg
        }
    });
    $('body#liens #menu').css('padding-top', ((($('body').height() / 2) - 291))+'px');  
    $(window).bind('resize load', function(){   $('body#liens #menu').css('padding-top', ((($('body').height() / 2) - 291))+'px');  });
});

$('body#partenaires').ready(function(){
    $('body#partenaires #menu').css('padding-top', ((($('body').height() / 2) - 315))+'px');
    $(window).bind('resize load', function(){   $('body#partenaires #menu').css('padding-top', ((($('body').height() / 2) - 315))+'px');  });
});
