﻿/*
Title: Advent site - spotlight javascript
Author: josh@josgherdes.com
*/

$(document).ready(function() {
    // Hide the spotlight links
    $('#spotlightImageLink').hide();
    $('#spotlightTextLink').hide();

    // Call youtube API to get most recent
    $.getJSON('http://gdata.youtube.com/feeds/api/users/adventmd/uploads?alt=json-in-script&format=5&max-results=1&orderby=published&callback=?', function(data){
        $.each(data.feed.entry, function(id, item){
            // Get title, if available
            var title = '';
            if(item.media$group.media$title.$t != null)
                title = item.media$group.media$title.$t;
            
            // Get description, if available
            var description = '';
            if(item.media$group.media$description.$t != null)
                description = item.media$group.media$description.$t;   
            
            // Get video url, if available
            var playerUrl = item.media$group.media$content[0].url;

            // Add copy to page
            $('#spotlightTitle').html(title);
            $('#spotlightDescription').html(description);   

            // Add click functionality to link
            $('#spotlightTextLink').click(function() {
                showVideo(playerUrl, false);
                return false;
            });
            
            // Add click functionality to image
            $('#spotlightImageLink').click(function() {
                showVideo(playerUrl, false);
                return false;
            });
            
            // Show the links now that the spotlight is loaded
            $('#spotlightImageLink').show();
            $('#spotlightTextLink').show();
        });
        
        data = null; // Destroys the reference to the data
    });
});