var ytPlayer;
function onYouTubePlayerReady(playerId) {
    ytPlayer = document.getElementById('ytplayer');
}

jQuery(document).ready(function($) {

    // don't send them off the page if they click on these
    $('body.videocover #video_top .leftcell h3.title a, body.videocover #video_top .videoitem h3.date_title a').click(function() { return false; });
    
    // only the "watch video" link and teaser image works top right
    $('#video_top .rightcell .videoitem p.readmore a, #video_top .videoitem .img a').click(function() {
        $('body.videocover #video_top .leftcell h3.title a').html(
            $(this).parent().parent().find('h3 span.title').text()
        );
        $('body.videocover #video_top .leftcell p.date').html(
            $(this).parent().parent().find('h3 span.date').text()
        );
        $('body.videocover #video_top .leftcell .teaser').html(
            $(this).parent().parent().find('.teaser').html()
        );
        
        var vid = $(this).parent().parent().find('.video_url').text();
        ytPlayer.loadVideoByUrl(vid);
        ytPlayer.playVideo();
        
        return false;
    });
    
    // carousel links
    $('#videocarousel h3.title a, #videocarousel .img a').click(function() {
        $('body.videocover #video_top .leftcell h3.title a').html(
            $(this).parent().parent().find('h3').text()
        );
        $('body.videocover #video_top .leftcell p.date').html(
            $(this).parent().parent().find('.date').text()
        );
        $('body.videocover #video_top .leftcell .teaser').html(
            $(this).parent().parent().find('.teaser').html()
        );
        
        var vid = $(this).parent().parent().find('.video_url').text();
        ytPlayer.loadVideoByUrl(vid);
        ytPlayer.playVideo();
        
        return false;
    });
    
    //jCarouselLite doesn't do max height, but we want this here.
    var maxHt = 0;
    $('#videocarousel ul li').each(function() {
        if ($(this).height() > maxHt) maxHt = $(this).height();
    });
    
    $('#videocarousel').jCarouselLite({
        btnPrev: 'button.prev',
        btnNext: 'button.next',
        speed: 2000,
        visible: 6,
        scroll: 3,
        circular: true
    });
    
    $('#videocarousel ul li').css('height', maxHt);
});
