// ----------------------------------------------------------------------
// Fetches information about a YouTube video using video ID from URL
// Displays title, author, duration, and thumbnail image
// Displays an estimated price breakdown for requested service
//
// Form element naming:
// id="youtube-url"         Input for YouTube URL
// id="youtube-data"        Div for video data
// id="youtube-thumbnail"   Div for thumbnail image
// id="youtube-estimate"    Div for cost estimate 
// ----------------------------------------------------------------------

function youtubeFetchData( )
{
    // valid YouTube video IDs are 11-character strings
    var videoid = '';
    var tempvar = $( '#youtube-url' ).attr( 'value' );
    if (tempvar == "") { return; }
    
    // if input is a URL
    if (/^https?\:\/\/.+/i.test(tempvar)) 
    {
        // URL is directly to the YouTube video (v=videoID)
        // example: http://www.youtube.com/watch?v=zj77LoraAY4
        if (/[\?\&]v=([^\?\&]{11})/.test(tempvar))
        {
            tempvar = /[\?\&]v=([^\?\&]{11})/.exec(tempvar);
        }
        
        // URL is part of a user's channel (video ID is the longer string near the end)
        // example: http://www.youtube.com/captionmax#p/u/7/nIGxc7-2r1w    
        else if (/([^\?\&\/=]{11}$)/i.test(tempvar))
        {   
            tempvar = /([^\?\&\/=]{11}$)/i.exec(tempvar);
        }
    
        else
        {
            clearData();
        }
        
        // save the video ID
        videoid = tempvar[1];
    }
    
    // if input is the video ID
    else
    {
        if (/([^\?\&\/=]{11}$)/i.test(tempvar))
        {
            tempvar = /([^\?\&\/=]{11}$)/i.exec(tempvar);
        }
        
        else
        {
            clearData();
        }
        
        // save the video ID
        videoid = tempvar[1];
    }
    
    // grab data from YouTube API
    // callback function is youtubeDisplayData()
    $.getScript( 'http://gdata.youtube.com/feeds/api/videos/' + 
                 encodeURIComponent(videoid) + 
                 '?v=2&alt=json-in-script&callback=youtubeDisplayData' );

}

function clearData()
{
    alert('Please input a valid YouTube URL or Video ID');
    $( '#youtube-url' ).val( '' );
    $( '#youtube-details' ).html( '' );
    $( '#youtube-thumbnail' ).html( '<img src="/structure/img/youtube-placeholder.jpg" ' +
       'width="120" height="90" alt="video thumbnail" />' );
    return;  
}

function youtubeDisplayData(data)
{
    // grab data
    var title    = data.entry[ "title" ].$t;
    var author   = data.entry[ "author" ][ 0 ].name.$t;
    var seconds  = data.entry[ "media$group" ][ "yt$duration" ].seconds;
    var thumburl = data.entry[ "media$group" ][ "media$thumbnail" ][ 0 ].url;
    
    // display details
    var details  = '<li class="title">Title: <span>'   + title  + '</span></li>';
        details += '<li class="author">Author: <span>' + author + '</span></li>';    
    var min = Math.floor(seconds / 60);
    var sec = (seconds % 60);
    if (sec < 10) {sec = '0' + sec+'';}
        details += '<li class="duration">Duration: <span>' + min + ':' + sec + '</span></li>';
    $( '#youtube-details' ).html( details );
    
    // display thumbnail
    var thumb = '<img src="' + thumburl + '" width="120px" height="90px" alt="' + title + '" />';
    $( '#youtube-thumbnail' ).html( thumb );
    
    // calculate estimate
    var ccBase  = 85;
    var ccExtra = 8;
    var trBase  = 50;
    var trExtra = 4;
    var service = 'CaptionMax Captions';
    if ($( '#captioning' ).val() == 'Transcript Only') {service = 'Transcript Only'};
    var estimate = 0;
    if (service == 'CaptionMax Captions')
    {
        estimate = 85;
        if ((seconds - 600) > 0)
        {
            estimate += (seconds - 600) * 0.1333333;
        }
    }
    if (service == 'Transcript Only')
    {
        estimate = 50;
        if ((seconds - 600) > 0)
        {
            estimate += (seconds - 600) * 0.0666666;
        }
    }
    estimate = (Math.floor(estimate * 100)) / 100;
      
    // display estimate
    $( '#youtube-estimate' ).html( 'Estimated Total: <span>$' + estimate + '</span>' );
    
    // Update form elements
    $( '#title' ).val( title );
    $( '#author' ).val( author );
    $( '#duration' ).val( min + ':' + sec + ' (' + seconds +  ' seconds)');
    $( '#estimate' ).val( '$' + estimate );
    
}
