function updateResults () {
    $('#search-results').empty();
    var searchCriteria = [];
    if ($('#search-raga').val() != '') {
        searchCriteria.push({
            criterion : 'raga',
            value :    $('#search-raga').val()
        });
    }
    if ($('#search-instrument').val() != '') {
        searchCriteria.push({
            criterion : 'instrument',
            value :    $('#search-instrument').val()
        });
    }
    if ($('#search-artist').val() != '') {
        searchCriteria.push({
            criterion : 'artist',
            value :    $('#search-artist').val()
        });
    }
    if ($('#search-decade').val() != '') {
        searchCriteria.push({
            criterion : 'decade',
            value :    $('#search-decade').val()
        });
    }

    $.ajax({
        url : '/videos/search',
        data : { criteria : searchCriteria }
    }).done (function (data) {
        var matching_records = [];
        data.forEach(function (datum) {
            matching_records.push(datum.vid_ytid);
        });
        gapi.client.youtube.videos.list({
            part : 'snippet',
            id : matching_records.join(',')
        }).then(function (resp) {
            addResults(data, resp);
        });
    });
    
    function addResults (data, resp) {
        var i = 0;
        resp.result.items.forEach(function(viditem) {
            var newDiv = $('<div>').addClass('result-elem');
            $('<div>').addClass('result-elem-data').text("Decade: " + data[i].vid_decade + " | Instrument: " + data[i].vid_instrument + " | Raga: " + data[i].vid_raga).appendTo(newDiv);
            $('<div>').addClass('result-elem-title').text(viditem.snippet.title).appendTo(newDiv);
            var imgDiv = $('<div>').addClass('result-elem-img');
            $('<img>').attr({
                src : viditem.snippet.thumbnails.default.url,
                width : viditem.snippet.thumbnails.default.width,
                height : viditem.snippet.thumbnails.default.height
            }).appendTo(imgDiv);
            imgDiv.appendTo(newDiv);
            $('<div>').addClass('result-elem-description').text(viditem.snippet.description.substring(0,200) + " ...").appendTo(newDiv);
            $('<div>').addClass('spacer').appendTo(newDiv);
            var watchDiv = $('<div>');
            var newLink = $('<a>').addClass('result-elem-ytlink').attr({
                href : 'https://www.youtube.com/watch?v=' + viditem.id,
                target : '_blank'
            }).text('Watch on ');
            $('<img>').attr({
                src: 'images/youtube_social_icon_red.png',
                width: '32',
                height: '22'
            }).appendTo(newLink);
            newLink.appendTo(watchDiv);
            watchDiv.appendTo(newDiv);
            newDiv.appendTo('#search-results');
            i++;
        });
    }
}
/**
 * Does a search over the database and updates the
 * #search-results div with the results
*/
Does a search over the database and updates the
#search-results div with the results
Does a search over the database and updates the #search - results div with the results
function updateResults () {
    $('#search-results').empty();
    var searchCriteria = [];
    if ($('#search-raga').val() != '') {
        searchCriteria.push({
            criterion : 'raga',
            value :    $('#search-raga').val()
        });
    }
    if ($('#search-instrument').val() != '') {
        searchCriteria.push({
            criterion : 'instrument',
            value :    $('#search-instrument').val()
        });
    }
    if ($('#search-artist').val() != '') {
        searchCriteria.push({
            criterion : 'artist',
            value :    $('#search-artist').val()
        });
    }
    if ($('#search-decade').val() != '') {
        searchCriteria.push({
            criterion : 'decade',
            value :    $('#search-decade').val()
        });
    }
    $.ajax({
        url : '/videos/search',
        data : { criteria : searchCriteria }
    }).done (function (data) {
        var matching_records = [];
        data.forEach(function (datum) {
            matching_records.push(datum.vid_ytid);
        });
        gapi.client.youtube.videos.list({
            part : 'snippet',
            id : matching_records.join(',')
        }).then(function (resp) {
            addResults(data, resp);
        });
    });
    function addResults (data, resp) {
        var i = 0;
        resp.result.items.forEach(function(viditem) {
            var newDiv = $('<div>').addClass('result-elem');
            $('<div>').addClass('result-elem-data').text("Decade: " + data[i].vid_decade + " | Instrument: " + data[i].vid_instrument + " | Raga: " + data[i].vid_raga).appendTo(newDiv);
            $('<div>').addClass('result-elem-title').text(viditem.snippet.title).appendTo(newDiv);
            var imgDiv = $('<div>').addClass('result-elem-img');
            $('<img>').attr({
                src : viditem.snippet.thumbnails.default.url,
                width : viditem.snippet.thumbnails.default.width,
                height : viditem.snippet.thumbnails.default.height
            }).appendTo(imgDiv);
            imgDiv.appendTo(newDiv);
            $('<div>').addClass('result-elem-description').text(viditem.snippet.description.substring(0,200) + " ...").appendTo(newDiv);
            $('<div>').addClass('spacer').appendTo(newDiv);
            var watchDiv = $('<div>');
            var newLink = $('<a>').addClass('result-elem-ytlink').attr({
                href : 'https://www.youtube.com/watch?v=' + viditem.id,
                target : '_blank'
            }).text('Watch on ');
            $('<img>').attr({
                src: 'images/youtube_social_icon_red.png',
                width: '32',
                height: '22'
            }).appendTo(newLink);
            newLink.appendTo(watchDiv);
            watchDiv.appendTo(newDiv);
            newDiv.appendTo('#search-results');
            i++;
        });
    }
}
function updateResults ( ) { $ ( '#search-results' ) . empty ( ) ; var searchCriteria = [ ] ; if ( $ ( '#search-raga' ) . val ( ) != '' ) { searchCriteria . push ( { criterion : 'raga' , value : $ ( '#search-raga' ) . val ( ) } ) ; } if ( $ ( '#search-instrument' ) . val ( ) != '' ) { searchCriteria . push ( { criterion : 'instrument' , value : $ ( '#search-instrument' ) . val ( ) } ) ; } if ( $ ( '#search-artist' ) . val ( ) != '' ) { searchCriteria . push ( { criterion : 'artist' , value : $ ( '#search-artist' ) . val ( ) } ) ; } if ( $ ( '#search-decade' ) . val ( ) != '' ) { searchCriteria . push ( { criterion : 'decade' , value : $ ( '#search-decade' ) . val ( ) } ) ; } $ . ajax ( { url : '/videos/search' , data : { criteria : searchCriteria } } ) . done ( function ( data ) { var matching_records = [ ] ; data . forEach ( function ( datum ) { matching_records . push ( datum . vid_ytid ) ; } ) ; gapi . client . youtube . videos . list ( { part : 'snippet' , id : matching_records . join ( ',' ) } ) . then ( function ( resp ) { addResults ( data , resp ) ; } ) ; } ) ; function addResults ( data , resp ) { var i = 0 ; resp . result . items . forEach ( function ( viditem ) { var newDiv = $ ( '<div>' ) . addClass ( 'result-elem' ) ; $ ( '<div>' ) . addClass ( 'result-elem-data' ) . text ( "Decade: " + data [ i ] . vid_decade + " | Instrument: " + data [ i ] . vid_instrument + " | Raga: " + data [ i ] . vid_raga ) . appendTo ( newDiv ) ; $ ( '<div>' ) . addClass ( 'result-elem-title' ) . text ( viditem . snippet . title ) . appendTo ( newDiv ) ; var imgDiv = $ ( '<div>' ) . addClass ( 'result-elem-img' ) ; $ ( '<img>' ) . attr ( { src : viditem . snippet . thumbnails . default . url , width : viditem . snippet . thumbnails . default . width , height : viditem . snippet . thumbnails . default . height } ) . appendTo ( imgDiv ) ; imgDiv . appendTo ( newDiv ) ; $ ( '<div>' ) . addClass ( 'result-elem-description' ) . text ( viditem . snippet . description . substring ( 0 , 200 ) + " ..." ) . appendTo ( newDiv ) ; $ ( '<div>' ) . addClass ( 'spacer' ) . appendTo ( newDiv ) ; var watchDiv = $ ( '<div>' ) ; var newLink = $ ( '<a>' ) . addClass ( 'result-elem-ytlink' ) . attr ( { href : 'https://www.youtube.com/watch?v=' + viditem . id , target : '_blank' } ) . text ( 'Watch on ' ) ; $ ( '<img>' ) . attr ( { src : 'images/youtube_social_icon_red.png' , width : '32' , height : '22' } ) . appendTo ( newLink ) ; newLink . appendTo ( watchDiv ) ; watchDiv . appendTo ( newDiv ) ; newDiv . appendTo ( '#search-results' ) ; i ++ ; } ) ; } }
Does a search over the database and updates the
 #search-results div with the results
Does a search over the database and updates the #search - results div with the results