/*
**  Utilisation de la librairie jQuery
**  http://docs.jquery.com/Downloading_jQuery#Download_jQuery
*/
    
jQuery.noConflict();
jQuery(function($) { 

    var $html         = $('html');
    var $header       = $('#header');
    var $main         = $('#main');
    var $footer       = $('#footer');

    var $audio_player = $header.find('.audio-player');

/*
 * Views and backgrounds adjustments
 */
 
    var mainNav      = $footer.find('.main-nav');
    var numberViews  = mainNav.find('li').length;
    var indexViews   = mainNav.find('li.current-menu-item').length > 0 ? mainNav.find('li').index(mainNav.find('li.current-menu-item')) : 0;
    var transition   = false;
   
    // Load new view
    function loadView(item) {
        var url = item.attr('href');
        $('#next-view').load(url + ' .view', function() {
                
            loadBg($('#next-view .view .background'), false);
                       
            setTimeout(function(){
            
                $('#view-video').find('iframe').css({ 'visibility': 'hidden' });    

                refreshScript();
            
                $main.find('.mask-view > .view').fadeOut(1500, function() {
                    $(this).replaceWith($('#next-view .view'));
                    refreshScript();
                    $('#view-video').find('iframe').css({ 'visibility': 'visible' });
                    transition = false;
                });
                
            }, 800);

        });
    }
   
    // Change nav arrows
    function showNavArrows(indexViews) {
        $main.find('.change-view a').addClass('active');
        if(indexViews == 0) {
            $main.find('.change-view a.prev.active').removeClass('active');
        } else if(indexViews >= numberViews-1) {
            $main.find('.change-view a.next.active').removeClass('active');
        }
    }    
 
    // Resize with ratio the background
    function resizeBg($bg) {

        var ratio = $bg.width() / $bg.height();
    
        if ( ($(window).width() / $(window).height()) < ratio ) {
            $bg.removeClass('width').addClass('height');
        } else {
            $bg.removeClass('height').addClass('width');
        }           
    } 
    
    // Resize with ratio all backgrounds
    function resizeAllBg() {
        $main.find('.background').each(function() {
            resizeBg($(this));
        });        
    } 
    
    // Init the background loading
    function loadBg(items, fade) {
        items.each(function() {  
        
            $this = $(this);
        
            setTimeout(function(){

                // Show image to get height/width, but not visible to the user
                $this.css({ 'margin-left': '9999px' }).show(0, function() {
                    resizeBg($(this));
                    // Show image to the user
                    fade ? $(this).hide().css({ 'margin-left': '0' }).fadeIn('1000') : $(this).css({ 'margin-left': '0' });
                });
                
            }, 1000);

        });            
    }
    
    // Show requested modal
    function showModal() {
           
        // Sub-navigation url type
        if($.url().segment(-2) == 'sub-navigation') {
        
            transition = true;
        
            var page = $.url().segment(-1);
            setTimeout(function(){
                $('#menu-sub-navigation').find('a[href$="/sub-navigation/'+ page +'/"]').trigger('click');
                transition = false;
            }, 2000);
            
        }
           
        // Opengraph type exist
        else if( $('head').find('meta[property="og:type"]').length ) {
                    
            switch( $('head').find('meta[property="og:type"]').attr('content') ) {
            
                case 'song':
                    var album_url = $('head').find('meta[property="og:url"]').attr('content');
                    var album     = $.url(album_url).segment(-2);
                    var view      = 'discographie';
                    var sim_click = true;
                    break;
                  
                case 'album':
                    var album_url = $('head').find('meta[property="og:url"]').attr('content');
                    var album     = $.url(album_url).segment(-1);
                    var view      = 'discographie';
                    var sim_click = true;
                    break;
                  
                case 'movie':
                    var album_url = $('head').find('meta[property="og:url"]').attr('content');
                    var album     = $.url(album_url).segment(-1);
                    var view      = 'dvdtheque'; 
                    var sim_click = true;
                    break;
                  
                default:
                    var sim_click = false;
            }
            
            // If action needed
            if(sim_click) {
                        
                $('#menu-navigation').find('a[href$="/navigation/'+ view +'/"]').trigger('click');
                
                setTimeout(function(){
                    $('#view-' + view).find('a.cboxElement[href$="/'+ album +'/"]').trigger('click');
                }, 5000);
            }
            
        } 
        
    }
    
    // Click on nav
    mainNav.delegate('a', 'click', function(e) {
        e.preventDefault();
        
        if(!transition) {
        
            transition = true;
        
            if( $(this).not('.active').length ) {
                mainNav.find('.active').removeClass('active');
                $(this).addClass('active');
                indexViews = mainNav.find('a').index( $(this) );
                showNavArrows( indexViews );
                loadView( $(this) );
            }
        
        }
        
    });  

    // Click on nav-arrow
    $main.delegate('.change-view a.active', 'click', function(e) {
        e.preventDefault();
        
        if(!transition) {
        
            $(this).hasClass('prev') ? indexViews-- : indexViews++;
            mainNav.find('a:eq('+indexViews+')').click();
        
        }
        
        
    });  

    
    // When browser resize
    $(window).resize(function() {
        resizeAllBg();
    });
    
    // Init
    mainNav.find('a:eq('+indexViews+')').addClass('active');
    showNavArrows( indexViews );
    loadBg($main.find('.background'), true);
    showModal();


/*
 * Ajax functions
 */
 
    $main.delegate('#view-actualite .pagination a, #view-actualite .back a, #view-actualite .more-link', 'click', function(e) {
        e.preventDefault();       
        var url = $(this).attr('href');
        $('#view-actualite .posts').load(url + ' .posts', function() {
            $(this).find('.posts').unwrap();
        });
    });


/*
 * Color-box
 */

    function refreshScript() {
    
        // Photos
        if( $('#view-photos').length ) {
            $('#view-photos').find('a').attr('rel', 'photos');
            $('a[rel=photos]').colorbox({ maxHeight:'600px', maxWidth:'800px', scrolling:false }).click(function() {
                $('#colorbox').removeClass('nobg');
                $('#cboxLoadedContent').append('<img alt="" src="/wp-content/themes/1theme/img/view/blank.png" class="fake">');
            }); 
        }

      
        // Discographie
        if( $('#view-discographie').length ) {
            $('#view-discographie').find('a').colorbox({ href: function(){ return $(this).attr('href')+' .modal-discographie'; }, maxHeight:'536px', maxWidth:'1200px', scrolling:false }).click(function() {
                $('#colorbox').addClass('nobg');
            });  
        }

        
        // DVDtheque
        if( $('#view-dvdtheque').length ) {
            $('#view-dvdtheque').find('a').colorbox({ href: function(){ return $(this).attr('href')+' .modal-dvdtheque'; }, maxHeight:'593px', maxWidth:'963px', scrolling:false }).click(function() {
                $('#colorbox').addClass('nobg');
            });     
        }
        
        var widget = new BIT.Widget({"artist": "The Puppini Sisters", "prefix": "wpjs", "div_id": "reservation"});
        widget.insert_events();
        
        if(!jQuery.browser.mobile) {
            jQuery('#main .content').jScrollPane();
        }
        
        setTimeout(function(){
            if(!jQuery.browser.mobile) {
                jQuery('#view-operas-concerts .content').jScrollPane();
            }
        }, 1000);
        
    }
    
    // Sub-navigation
    $('.second-nav').find('.menu-item-type-post_type a').colorbox({ href: function(){ return $(this).attr('href')+' .modal-sub-navigation'; }, maxHeight:'600px', maxWidth:'640px', scrolling:false  }).click(function() {
        $('#colorbox').addClass('nobg');
        close_player_tracklist();
    });
    
    // After load
    $(document).bind('cbox_complete', function(){
        makeScroll( $('#colorbox').find('.scroll') );
    });
    
    // Youtube
    $(document).bind('cbox_open', function(){ 
        $('#view-video').find('iframe').css({ 'visibility': 'hidden' });
    });
    $(document).bind('cbox_closed', function(){ 
        $('#view-video').find('iframe').css({ 'visibility': 'visible' });
    });
    

/*
 * Player MP3   
 */
  
    function open_player_tracklist() {
        $audio_player.find('.open-close').removeClass('closed').addClass('opened').siblings('.block').slideDown(function() {
            var scroll_active = $(this).find('.jspContainer').length;
            if( scroll_active == false ) {
                makeScroll( $(this).find('.content') );
            }
        });
    } 
    
    function close_player_tracklist() {
        $audio_player.find('.open-close').removeClass('opened').addClass('closed').siblings('.block').slideUp();
    }
 
    $audio_player.delegate('.open-close, a.close', 'click', function(e) {
        e.preventDefault();       
        if( $audio_player.find('.open-close').hasClass('opened') ) {
            close_player_tracklist();
        }
    }); 
    
    $audio_player.find('.open-close').mouseenter(function() {
        if( $(this).hasClass('closed') ) {
            open_player_tracklist();
        }
    });
    $audio_player.delegate('.others .share > a', 'click', function(e) {
        e.preventDefault();   
        var $box = $(this).siblings('.box');
        if($box.hasClass('opened')) {
            $box.removeClass('opened').animate({ 'left': 0 });
        } else {
            $box.animate({ 'left': '-' + $box.outerWidth(true) }, function() {
                $box.addClass('opened');
            });
        }
    });


/*
 *Share
 */
 
    $footer.find('.misc .share > a').click(function(e) {
        e.preventDefault();    
        $(this).parents('.share').find('.box').slideDown();
    });
    $footer.find('.misc .share').mouseleave(function() {
        $(this).find('.box').slideUp();
    });  


/*
 * Scroll
 */    
 
    function makeScroll($item, params) {
        if(!jQuery.browser.mobile) {
            $item.jScrollPane(params);
        }
    }
 
    makeScroll( $('.scroll') );
    
    setTimeout(function(){
        makeScroll( $('#view-operas-concerts').find('.content') );
        $('#view-operas-concerts').find('.content').find('a').click(function() {
            makeScroll( $(this).parents('.content') );
        });
    }, 3500);

    
/*
 * Chargement des albums suivant et précédent
 */

    $('#colorbox').delegate('.modal-discographie .navigation a', 'click', function(e){
        e.preventDefault();
        $('#album'+$(e.currentTarget).attr('albumId')).click();
    });
    
    $('#colorbox').delegate('.modal-dvdtheque .navigation a', 'click', function(e){
        e.preventDefault();
        $('#dvd'+$(e.currentTarget).attr('dvdId')).click();
    });

/*
 * Mobile
 */ 
 
    if(jQuery.browser.mobile) {
        $html.addClass('handheld');
    }
    
    
/*
 * IE6
 */  
 
    if($('html.ie6').length) {
        $('html.ie6').find('body').prepend('<div class="warning-obsolete">Attention, votre navigateur est obsolète, des problèmes d\'affichage peuvent survenir sur cette page. Pour y remédier, nous vous conseillons de <a href="http://windows.microsoft.com/fr-FR/internet-explorer/products/ie/home">le mettre à jour</a>.</div>');
    }    

    
}); /* jQuery end */
