// FUNCTIONS
// init: when the page is loaded (must be called on dom ready)
// closedToPreview: from closed pane to preview pane
// previewToFull: from preview pane to full pane
// fullToClosed: from full pane to closed pane

var rightPanel = (function() {
    var s = {
        openingPreview: false,
		closingPreview: false,
		mouseOverPreview: false,
		loadingNav: false,
		loadingFull: false
        },
        state = 'closed', // closed, preview or full
        isPreviewLoaded = false,
        speed = {
            closed: 600,
            previewExpand: 400,
            previewHeaderIn: 200,//300,
            previewHeaderOut: 200,
            previewBoxesIn: 100,//600,
            previewBoxesOut: 300,
            fullExpand: 600,
            fullHeaderIn: 300,
            fullHeaderOut: 300,
            fullHeaderDown: 300,
            fullHeaderUp: 300,
            fullCcontentIn: 600,
            fullContentOut: 600,
            fullChannelBoxesIn: 100,
            fullChannelBoxesOut: 300,
            thumbIn: 200,
            thumbOut: 200,
            navIn: 200,
            navOut: 200
        },
        delay = {
            beforeClosingOnLoadAnim: 600, // during initial animation, there is a delay before closing
            previewHeaderIn: 200,//300,
            previewHeaderOut: 0,
            previewBoxesIn: 150,//600,
            previewBoxesOut: 100,//100,
            fullHeaderIn: 200,
            fullHeaderOut: 0,
            fullContentIn: 0,
            fullContentOut: 100,
            fullChannelBoxesIn: 150,
            fullChannelBoxesOut: 100
        };
        
    function changeState(newState) {
        if(newState === 'player' || newState === "channels") {
            s.dom.full.removeClass().addClass(newState+' rightPanelFull');
        } else {
            s.dom.full.removeClass();
            s.dom.main.removeClass().addClass(newState);
            state = newState;
        }
    }
    
    function resetDOM() {
        s.dom = {
            main: $('#rightPanel'),
            closed: $('#rightPanelClosed'),
            preview: $('#rightPanelPreview'),
            previewHeader: $('#rightPanelPreview').find('.rightPanelHeader'),
            previewBoxes: $('#rightPanelPreview').find('.allBoxrightPanel'),
            previewThumbs: $('#rightPanelPreview .thumb'),
            tooltips: $('.tooltip'),
            full: $('#rightPanelFull'),
            fullHeader: $('#rightPanelFull').find('.rightPanelHeader'),
            fullChannels: $('#rightPanelFull').find('.rightPanelChannels'),
            fullChannelBoxes: $('.rightPanelChannels .channel'),
            fullPlayer: $('#rightPanelFull').find('.rightPanelPlayer'),
            fullPlayerVideo: $('#rightPanelVideo'),
            fullPlayerNav: $('.rightPanelPlayer .channelNav'),
            fullPlayerTitle: $('.rightPanelPlayer .videoTitle'),
            fullThumbs: $('#rightPanelFull .thumb')
        }
    }
    
    // Get the information in the dom aboout the video
    function getThumbInfo(o) {
		var info = {
			dom: o,
            title: o.find('.tooltip').find('h5').html(),
            desc: o.find('.tooltip').children('.inner').children('p').html()
		};
		
		if( o.find('a.html').length || o.find('a.mp4').length ) {
			info.ogg = o.find('a.html').attr('href') || '';
			info.mp4 = o.find('a.mp4').attr('href') || '';
			info.type = 'video'
		}
		
		if( o.find('a.preview').length || o.find('a-2.html').length ) {
			info.preview = o.find('a.preview').attr('href') || '';
			info.pdf = o.find('a-2.html').attr('href') || '';
			info.type = 'pdf'
		}
		
		if( o.find('a.images').length ) {
			info.images = o.find('a.images').attr('href') || '';
			info.type = 'gallery'
		}
		
		console.log(info);
		
        return info;
    }
    
    // Animate the thumbnails
    function thumbAnim(dom, direction, state) {
        var animations,
            tooltip = dom.next('.tooltip');
        
        if(direction === 'in') {
            $('.tooltip').hide();
            
            if(state === 'full') animations = { marginTop: "0px", marginLeft: "0px", height: "54px", width: "114px" };
            else animations = { marginTop: "0px", marginLeft: "0px", height: "44px", width: "83px" };
            
            dom.find('.overlay').animate(animations, speed.thumbIn, function() {
                var top,right,left;
                
                if(state === 'preview') {
                    top = dom.offset().top - Math.floor(dom.height()/3);
                    right = s.dom.preview.width() - ( dom.offset().left - s.dom.preview.offset().left ) - 2;
                    
                    tooltip.css({'top': top+'px', 'right': right+'px'});
                }
                tooltip.show();
            });
        } else {
            if(state === 'full') animations = { marginTop: "-12px", marginLeft: "-12px", height: "78px", width: "138px" };
            else animations = { marginTop: "-8px", marginLeft: "-8px", height: "60px", width: "99px" };
            
            dom.find('.overlay').stop();
            $('.tooltip').hide();
            dom.find('.overlay').animate(animations, speed.thumbOut);
        }
    }
    
    // Animate the closed box on load
    function closedOpeningAnim() {
        changeState('closed');
        s.dom.closed.css('opacity', '0').show();
        s.dom.closed.animate({opacity: '1'}, speed.closed, function() {
            s.closingPreview = false;
        });
    }
    
    // Animate the preview box on load
    function previewOpeningAnim(callback) {
        var numberOfPreviewBoxes = s.dom.previewBoxes.children().length;
        
        if(s.closingPreview) return false;
        if(typeof callback === "function") {
            s.dom.preview.hover(function() {s.mouseOverPreview = true;}, function() {s.mouseOverPreview = false;});
        }
        
        changeState('preview');
        s.dom.preview.show();
        s.dom.previewHeader.delay(delay.previewHeaderIn).fadeIn(speed.previewHeaderIn, function() {
            s.dom.previewBoxes.show().children().each(function(index,elm) {
        		$(this).delay(delay.previewBoxesIn*index).animate({marginLeft: "10px"}, speed.previewBoxesIn, 'easeOutExpo', function() {
        		    var numberOfPreviewBoxesProcessed = index + 1;
        		    if(numberOfPreviewBoxesProcessed === numberOfPreviewBoxes) {
                        s.openingPreview = false;
        		        if(typeof callback === "function" && !s.mouseOverPreview) setTimeout(callback, delay.beforeClosingOnLoadAnim);
        		    }
        		});
        	});
        });
    }
    
    // Animate the preview box on quit
    function previewClosingAnim(callback) {
        var numberOfPreviewBoxes = s.dom.previewBoxes.children().length;
        
        if(s.openingPreview) return false;
        s.dom.tooltips.hide();
        $(s.dom.previewBoxes.children().get().reverse()).each(function(index,elm){
			$(this).delay(delay.previewBoxesOut*index).animate({marginLeft: "260px"}, speed.previewBoxesOut, 'easeOutExpo', function() {
			    var numberOfPreviewBoxesProcessed = index + 1;
    		    if(numberOfPreviewBoxesProcessed === numberOfPreviewBoxes) {
			        s.dom.previewHeader.delay(delay.previewHeaderOut).fadeOut(speed.previewHeaderOut, function() {
                        s.dom.preview.hide();
                        if(typeof callback === "function") callback();
                        s.closingPreview = false;
                    });
                }
			});	
		});
    }
    
    // Animate the full box on load
    function fullOpeningAnim(callback) {
		s.dom.full.show();
        s.dom.fullHeader.delay(delay.fullHeaderIn).css('marginTop', '-60px').fadeIn(speed.fullHeaderIn).animate({marginTop: '0px'}, speed.fullHeaderDown, function() {
            if(typeof callback === 'function') callback();
            s.loadingFull = false;
        });
    }
    
    function channelsOpeningAnim() {
        var numberOfPreviewBoxes = s.dom.fullChannelBoxes.length;
        
        changeState('channels');
        
        s.dom.fullChannels.fadeIn(speed.fullContentIn, function() {

		if (isIPhone()) {
			$('#ipadScroller').height(1200);
			rightPanelIPadScroller = new iScroll('ipadScroller');
		}

            s.dom.fullChannelBoxes.css('marginLeft', $(document).width()).show().each(function(index,elm) {
    	    	$(this).delay(delay.fullChannelBoxesIn*index).animate({marginLeft: '0px'}, speed.fullChannelBoxesIn, 'easeOutExpo');
                $('#rightPanel .videos').width( $('.rightPanelChannels .channel').width() - $('.rightPanelChannels .description').outerWidth() -40 );
                $('.rightPanelChannels').height( $(document).height() - $('.rightPanelFull .rightPanelHeader').height() );
    	    });
    	});
    }
    
    function playerOpeningAnim(content) {
        changeState('player');
		s.dom.full.show();
        s.dom.fullPlayer.fadeIn(speed.fullContentIn, function() {
            $(this).children('.channelNav').css('bottom', '-40px').animate({bottom: '0px'}, 'fast', function() {
                s.dom.fullPlayerVideo.show();
                loadContent(content);
            });
        });
    }
    
    // Animate the full box on quit
    function fullClosingAnim() {
		if (isIPhone()) rightPanelIPadScroller.destroy();
		$('#rightPanelVideo').empty();
        s.dom.fullHeader.delay(delay.fullHeaderOut).animate({marginTop: '-60px'}, speed.fullHeaderUp).fadeOut(speed.fullHeaderOut, function() {
            $(this).hide().css('marginTop', '0px');
            s.dom.full.hide();
            collapserightPanelFull(speed.fullExpand, closedOpeningAnim);
        });
    }
    
    function channelsClosingAnim(callback) {
        var numberOfPreviewBoxes = s.dom.fullChannelBoxes.length;
        
    	$(s.dom.fullChannelBoxes.get().reverse()).each(function(index,elm){
			$(this).delay(delay.fullChannelBoxesOut*index).animate({marginLeft: $(document).width()}, speed.fullChannelBoxesOut, 'easeOutExpo', function() {
			    var numberOfPreviewBoxesProcessed = index + 1;
			    $(this).hide();
                if(numberOfPreviewBoxesProcessed === numberOfPreviewBoxes)
			        s.dom.fullChannels.delay(delay.fullContentOut).fadeOut(speed.fullContentOut, callback);
			});	
		});
    }
    
    function playerClosingAnim(callback) {
		$('#rightPanelVideo').empty();
        s.dom.fullPlayer.children('.videoTitle').fadeOut('fast', function() {
            s.dom.fullPlayer.children('.channelNav').animate({bottom: '-40px'}, 'fast', function() {
                s.dom.fullPlayerVideo.fadeOut(speed.fullContentOut, function() {
                    s.dom.fullPlayer.hide();
                    if(typeof callback === 'function') callback();
                });
            });
        });
    }

    function loadContent(content) {
		var html = '<h3>'+content.title+'</h3><p>'+content.desc+'</p>',
			dl = ($('body').hasClass('fr')) ? 'Télécharger le pdf' : 'Download the pdf';
		
		if( content.type === 'pdf' && content.pdf ) html = '<h3>'+content.title+'</h3><p><a href="'+content.pdf+'" target="_blank">'+dl+'</a></p>';
        s.dom.fullPlayerTitle.html(html).fadeIn('fast');
	
		if(content.type === 'video') {

			// Create a fresh new div for the video player.
			var playerHolder = $('<div id="rightPanelPlayer" class="playerHolder"></div>');
			$('#rightPanelVideo').empty().append(playerHolder);

			// Create player.
			createVideoPlayer(playerHolder, 'tvPlayer', content.mp4, 640, 480, function(e) {
				if (! e.success) return;
				playerHolder.show();
				playerHolder.height(480);
				centerContentVertically();
				startCenterElementVertically(playerHolder);
			}, true);

			// Center player in window.
//			var videoFreeSpace = $(window).height() - $('.rightPanelFull .rightPanelHeader').height() - $('.rightPanelPlayer .videoTitle').height() - 60;
//	        var videoHeight = $('#rightPanelPlayer .playerHolder').height();
//	        var margin = Math.floor( ( videoFreeSpace - videoHeight ) / 2 );
//			if (margin < 0) margin = 0;
//	        $('#rightPanelVideo').css('marginTop', margin);
		}
		
		if(content.type === 'pdf') {
			$('<img src="'+content.preview+'" alt="" />').load(function() {
				var videoFreeSpace = $(document).height() - $('.rightPanelFull .rightPanelHeader').height() - $('.rightPanelPlayer .videoTitle').height() - 60,
			        videoHeight = $('#rightPanelVideo').height(),
			        margin;

			    if (videoFreeSpace < videoHeight ) margin = 0;
			    else margin = Math.floor( ( videoFreeSpace - videoHeight ) / 2 );
			    $('#rightPanelVideo').css('marginTop', margin );
			}).appendTo('#rightPanelVideo');
		}
			
		if(content.type === 'gallery') {
			var images = content.images.split(';'),
				i, html,
				htmlFull = '<ul class="galleryFull">',
				htmlThumb = '<div class="galleryThumbs">';
				htmlThumb += '<div class="scrollArrow leftScroll"><span>&lt;</span></div>';
				htmlThumb += '<div style="width:5000px; text-align:left;"><ul>';
				
			for( i=0; i<images.length; i++ ){
				htmlThumb += '<li><a href="img'+i+'"><img src="/'+images[i]+'" alt="" /></a></li>';
				htmlFull += '<li class="img'+i+'"><img src="/'+images[i]+'" alt="" /></li>';
			}
			

			htmlFull += '</ul>';
			htmlThumb += '<li style="width:0px"></li></ul></div>';
			htmlThumb += '<div class="scrollArrow rightScroll"><span>&gt;</span></div>';
			htmlThumb += '</div>';
			html = '<div class="gallery">'+htmlFull+htmlThumb+'</div>';
			
			$('#rightPanelVideo').empty().append( html );
			
			$(document).ready(function() {
				$('.galleryThumbs .leftScroll').hover(function() {
					var currentMargin = $('.galleryThumbs ul').css('margin-left').replace(/px$/i, '');
					if (currentMargin >= 0) return;
					var offset = -currentMargin;
					$('.galleryThumbs .leftScroll span').fadeIn(500);
					$('.galleryThumbs ul').animate({'margin-left':'+=' + offset}, offset * 5);
				}, function() {
					$('.galleryThumbs .leftScroll span').fadeOut(500);
					$('.galleryThumbs ul').stop(true, false);
				});
				$('.galleryThumbs .rightScroll').hover(function() {
					var currentMargin = $('.galleryThumbs ul').css('margin-left').replace(/px$/i, '');
					var overflow = - $('.galleryThumbs').width() + $('.galleryThumbs ul').width();
					if (overflow < 0) return;
					$('.galleryThumbs .rightScroll span').fadeIn(500);
					$('.galleryThumbs ul').animate({'margin-left':'-=' + overflow}, overflow * 5);
				}, function() {
					$('.galleryThumbs .rightScroll span').fadeOut(500);
					$('.galleryThumbs ul').stop(true, false);
				});

				$('.galleryThumbs li img').each(function() {
					var h = $(this).height(),
						w = $(this).width(),
						sh = Math.floor(98*h/w),
						sw = Math.floor(73*w/h);

					if((w/h) > (98/73)) {
						$(this).height('73px').css('marginLeft', '-'+Math.floor((sw-98)/2)+'px' );
					} else {
						$(this).width('98px').css('marginTop', '-'+Math.floor((sh-73)/2)+'px' );
					}
				});
			});
			
			var freeSpace = $(document).height() - $('.rightPanelFull .rightPanelHeader').height() - $('.rightPanelPlayer .videoTitle').height() - 62,
				fullSpace = freeSpace - 155;
			$('#rightPanelVideo').css('marginTop', '0px');
			$('.gallery').css('height', freeSpace );
			$('.galleryFull img').css('maxHeight', fullSpace);
			$('.galleryFull li').css('marginTop', '30px');
			
			// Set first image
			$('.galleryFull li:not(:first-child)').hide();
			$('.galleryThumbs li').eq(0).addClass('active');
			$('.galleryFull li:first-child').addClass('active');
		}
    }
    
    function createPlayerNode(content) {
    	var video = $('<video class="video-js" width="720" height="505" controls="controls" xpreload="preload"></video>');
    	var srcMp4 = video.append('<source class="mp4" type="video/mp4; codecs=\'avc1.42E01E, mp4a.40.2\'"');
    	srcMp4.attr('src', content.mp4);

    	var srcOgg = video.append('<source class="ogg" type="video/ogg; codecs=\'theora, vorbis\'"');
    	srcOgg.attr('src', content.ogg);

    	return video;
    }

    s.closedToPreview = function(callback) {
        if(s.openingPreview || currentlyScrolling) return false;
        s.openingPreview = true;
        s.dom.closed.fadeOut(200, function() {
            expandrightPanel(speed.previewExpand, function() {
                previewOpeningAnim(callback);
            });
        });
    };
    
	s.closedToFull = function() {
		s.loadingFull = true;

		var f = function() {
			s.dom.closed.fadeOut(200, function() {
				changeState('full');
				expandrightPanel(speed.previewExpand, function() {
					expandrightPanelFull(speed.fullExpand, function() {
						fullOpeningAnim(function() {
							channelsOpeningAnim(content);
						});
					});
				});
			});
		};
		// Preview sometimes can stay opened, make sure it is closed before openening full.
		if (s.dom.preview.is(':visible')) {
			previewClosingAnim(f);
		} else {
			f();
		}
	}

	s.closedToPlayer = function(content) {
		s.loadingFull = true;
		
        s.dom.fullPlayerNav.children('.channel').remove();
        s.dom.fullPlayerNav.append( $('.channel.'+content.channel).clone() ).children('.channel').hide();
        s.dom.fullPlayerNav.find('.overlay').removeAttr('style');
        s.dom.fullPlayerNav.find('.tooltip').removeAttr('style');
        
        //$('.channelNav .'+$(this).attr('class')).addClass('active');
		var f = function() {
			s.dom.closed.fadeOut(200, function() {
				changeState('full');
				expandrightPanel(speed.previewExpand, function() {
					expandrightPanelFull(speed.fullExpand, function() {
						fullOpeningAnim(function() {
							playerOpeningAnim(content);
						});
					});
				});
			});
		}

		// Preview sometimes can stay opened, make sure it is closed before openening full.
		if (s.dom.preview.is(':visible')) {
			previewClosingAnim(f);
		} else {
			f();
		}
	};

    s.previewToClosed = function() {
        if(s.openingPreview || s.loadingFull) return false;
        s.closingPreview = true;
        previewClosingAnim(function() {
            collapserightPanel(speed.previewExpand, closedOpeningAnim);
        });
    };
    
    s.previewToFull = function() {
		s.loadingFull = true;
        previewClosingAnim(function() {
            changeState('full');
            expandrightPanelFull(speed.fullExpand, function() {
                fullOpeningAnim( channelsOpeningAnim );
            });
        });
    };

	s.previewToPlayer = function(content) {
		s.loadingFull = true;
        previewClosingAnim(function() {
            changeState('full');
            expandrightPanelFull(speed.fullExpand, function() {
                fullOpeningAnim(function() {
					playerOpeningAnim(content);
				});
            });
        });
    };
    
    s.fullToClosed = function() {
        if(s.dom.full.hasClass('channels')) channelsClosingAnim(fullClosingAnim);
        else playerClosingAnim(fullClosingAnim);
    };
    
    s.channelsToPlayer = function(content) {
        channelsClosingAnim(function() {
            playerOpeningAnim(content);
        });
    };
    
    s.playerToPlayer = function(content) {
        s.loadingNav = true;
                
        s.dom.fullPlayerNav.animate({height: s.dom.fullPlayerNav.children('.openNav').height() }, speed.navOut, function() {
            $(this).children('.channel').hide();
            $(this).children('.openNav').fadeIn('fast', function() {
                s.addEvent('playerNav');
                s.loadingNav = false;
$('#rightPanelVideo').empty();
//                s.dom.fullPlayerVideo.children().get(0).empty();
                s.dom.fullPlayerTitle.fadeOut('fast', function() {
                    loadContent(content);
                });
            });
        });
        
        $('.rightPanelPlayer .videos li').removeClass('active');
//        video.dom.addClass('active');
    };
    
    s.playerToChannels = function() {
        playerClosingAnim(channelsOpeningAnim);
    };
    
    s.addEvent = function(event) {
        var all = false;
        if(event === 'all') all = true;
        
        $(window).resize( function() {
            $('#rightPanel .videos').width( $('.rightPanelChannels .channel').width() - $('.rightPanelChannels .description').outerWidth() -40 );
            $('.rightPanelChannels').height( $(document).height() - $('.rightPanelFull .rightPanelHeader').height()  );
            
            var videoFreeSpace = $(document).height() - $('.rightPanelFull .rightPanelHeader').height() - $('.rightPanelPlayer .videoTitle').height() - 40,
                videoHeight = $('#rightPanelVideo').height(),
                margin;
            if (videoFreeSpace < videoHeight ) margin = 0;
            else margin = Math.floor( ( videoFreeSpace - videoHeight ) / 2 );
            $('#rightPanelVideo').css('marginTop', margin );
        });
        
        // Open the Preview
        if(all || event === 'preview') {
            s.dom.closed.unbind('mouseenter')
            s.dom.closed.bind('mouseenter', s.closedToPreview);
            
            s.dom.preview.unbind('mouseleave');
            s.dom.preview.bind('mouseleave', function() {
                if(state !== 'full') s.previewToClosed();
            });
        }
        
        // Thumbnails
        if(all || event === 'openPlayer' || event === 'thumb') {
            
            // over effect
            if(! $('.thumb .overlay').length) $('.thumb').append('<span class="overlay"></span>');

            s.dom.previewThumbs.live('mouseenter', function() {
                thumbAnim($(this), 'in', 'preview');
            });
            s.dom.previewThumbs.live('mouseleave', function() {
                thumbAnim($(this), 'out', 'preview');
            });
            
            s.dom.fullThumbs.live('mouseenter', function() {
                thumbAnim($(this), 'in', 'full');
            });
            s.dom.fullThumbs.live('mouseleave', function() {
                thumbAnim($(this), 'out', 'full');
            });
            
            // load video
            $('.rightPanelChannels .videos li').live('click', function() {
                var videoInfo = getThumbInfo( $(this) );
                
                s.dom.fullPlayerNav.children('.channel').remove();
                s.dom.fullPlayerNav.append( $(this).parent().parent().clone() ).children('.channel').hide();
                s.dom.fullPlayerNav.find('.overlay').removeAttr('style');
                s.dom.fullPlayerNav.find('.tooltip').removeAttr('style');
                
                $('.channelNav .'+$(this).attr('class')).addClass('active');
                
                // Modify the tooltip
                s.dom.fullPlayerNav.find('.inner').each(function() {
                    var inner = $(this).clone();
                    $(this).parent().prepend(inner);
                    $(this).remove();
                });
                
                s.channelsToPlayer(videoInfo);
            });
            $('.rightPanelPlayer .videos li').live('click', function() {
                var videoInfo = getThumbInfo( $(this) );
                s.playerToPlayer(videoInfo);
            });
            $('.box .thumb').live('click', function() {
                var videoInfo = getThumbInfo( $(this).parent() ),
                    id = $(this).parent().parent().attr('id');
                
                s.dom.fullPlayerNav.children('.channel').remove();
                s.dom.fullPlayerNav.append( $('.rightPanelChannels .'+id ).clone() ).children('.channel').hide();
                s.dom.fullPlayerNav.find('.overlay').removeAttr('style');
                s.dom.fullPlayerNav.find('.tooltip').removeAttr('style');
                
                $('.channelNav .'+id).addClass('active');
                
                // Modify the tooltip
                s.dom.fullPlayerNav.find('.inner').each(function() {
                    var inner = $(this).clone();
                    $(this).parent().prepend(inner);
                    $(this).remove();
                });
                
                s.previewToPlayer(videoInfo);
            });
        }

		// Gallery
		$('.galleryThumbs a').live('click', function() {
			if($(this).parent().hasClass('active')) return false;
			
			var img = $(this).attr('href');
			$('.galleryFull .active').fadeOut('slow', function() {
				$('.galleryFull .'+img).fadeIn('slow').addClass('active');
			}).removeClass('active');
			$('.galleryThumbs li').removeClass('active');
			$(this).parent().addClass('active');
			
			return false;
		});
        
        // Link to open the full rightPanel
        if(all || event === 'openFull'){
            $('.allVideos').click( s.previewToFull );
        }
        
        // Link to close the full rightPanel
        if(all || event === 'closeFull'){
            $('#rightPanelClose').unbind('click');
            $('#rightPanelClose').click(s.fullToClosed);
        }
        // Link to go back to channels
        if(all || event === 'closePlayer'){
            $('#rightPanelBack').unbind('click');
            $('#rightPanelBack').click(s.playerToChannels);
        }
        // Player nav open/close
        if(all || event === 'openPlayer' || event === 'playerNav') {
            s.dom.fullPlayerNav.unbind('mouseenter mouseleave');
            s.dom.fullPlayerNav.hover(
                function() {
                    if(s.loadingNav) return false;
                    s.loadingNav = true;
                    $(this).children('.openNav').hide();
                    $(this).children('.channel').show();
                    $(this).animate({height: $(this).children('.channel').height() }, speed.navIn, function() {
                        s.loadingNav = false;
                    })
                },
                function() {
                    if(s.loadingNav) return false;
                    s.loadingNav = true;
                    $(this).animate({height: $(this).children('.openNav').height() }, speed.navOut, function() {
                        $(this).children('.openNav').fadeIn('fast');
                        s.loadingNav = false;
                    });
                    $(this).children('.channel').fadeOut('fast');
                }
            );
        }
    }
    
    s.init = function() {
        s.dom = {
            main: $('#rightPanel'),
            closed: $('#rightPanelClosed'),
            preview: $('#rightPanelPreview'),
            previewHeader: $('#rightPanelPreview').find('.rightPanelHeader'),
            previewBoxes: $('#rightPanelPreview').find('.allBoxrightPanel'),
            previewThumbs: $('#rightPanelPreview .thumb'),
            tooltips: $('.tooltip'),
            full: $('#rightPanelFull'),
            fullHeader: $('#rightPanelFull').find('.rightPanelHeader'),
            fullChannels: $('#rightPanelFull').find('.rightPanelChannels'),
            fullChannelBoxes: $('.rightPanelChannels .channel'),
            fullPlayer: $('#rightPanelFull').find('.rightPanelPlayer'),
            fullPlayerVideo: $('#rightPanelVideo'),
            fullPlayerNav: $('.rightPanelPlayer .channelNav'),
            fullPlayerTitle: $('.rightPanelPlayer .videoTitle'),
            fullThumbs: $('#rightPanelFull .thumb')
        };
        
        closedOpeningAnim();
        
        // add the event listeners
        s.addEvent('all');
    };

    return s;
}());
function expandrightPanel(slideSpeed, callback) {
    if (currentlyScrolling) return false;
    $('#contentPane').queue(function (next) {
        if (secondaryMenuOpened) {
            $('#mainMenu').css('width', 40);
            $('#mainMenu').css('z-index', 5);
        }
        next();
    });
    $('#contentPane').animate({ 'margin-left': -200 }, slideSpeed, function () {
        if (typeof callback === 'function') callback();
    });
}

function collapserightPanel(slideSpeed, callback) {
    $('#contentPane').animate({ 'margin-left': 0 }, slideSpeed, function () {
        $('#contentPane').queue(function (next) {
            if (secondaryMenuOpened) {
                $('#mainMenu').css('width', 240);
                $('#mainMenu').css('z-index', '');
            }
            next();
        });
        if (typeof callback === 'function') callback();
    });
}
$(document).ready(function() {    
    rightPanel.init();
});

