

function make_freebase_link(topic){

    var topic_link = "" + '<' + 'a title=\"Learn more about this topic at Freebase.com\"' + 'href=' + '"' +
    'http://www.freebase.com/view?id=' +
    encodeURIComponent(topic.id) +
    '"' +
    '>' +
    topic.name +
    '<' +
    '/a' +
    '>';
    return topic_link;
}


function get_mw_blurb(topic, div_id_name){
    var blurb_id = document.getElementById(div_id_name);
    var blurburl = "" + "http://api.freebase.com/api/trans/blurb/%23";
    var blurbs = topic['/common/topic/article'];
    if (blurbs != null) {
        if (blurbs[0] != null) {
            blurburl += blurbs[0].guid.replace(/#/, "");
        }
        
        $.ajax({
            url: blurburl,
            dataType: "jsonp",
            cache: true,
            success: function(response){
                if (response.result != undefined) {
                    $(blurb_id).append(response.result.body);
                };
                            },
            data: {
                maxlength: 550
            }
        });
        
    }
}

//<!-- FUNCTION: showTopicImage -->
function showTopicImage(t, maxheight, maxwidth){
    if (maxheight == undefined) {
        maxheight = 100
    }
    if (maxwidth == undefined) {
        maxwidth = 200
    }
    var imageurl = "";
    imageurl = "" + '<' + 'img alt=' + '"' + t.name + '"' + ' src=' + '"' +
    "http://api.freebase.com/api/trans/image_thumb" +
    t.id +
    '?maxheight=' +
    maxheight +
    '&maxwidth=' +
    maxwidth +
    '"' +
    '>';
    return (imageurl);
    
} //<!-- END FUNCTION: showTopicImage -->


function best_topic_types(topic){
    var typelist = "";
    var card_type_max_chars = 70; // does not include space for ...;
    var dotdotdot = " ...";
    var add_dots = false;
    var n = 0;
    while ((n < topic['type'].length) &&
    (typelist.length < card_type_max_chars)) {
        var t = topic['type'][n];
        switch (t.name) {
            case 'Topic':
            case "Person":
            case "Deceased Person":
            case "Influence Node":
            case "NNDB Person":
                break;
            default:
                
                //                        var i = 0;
                // add the type only if it won't be truncated
                if ((typelist.length + t.name.length) < card_type_max_chars) {
                    if ((typelist.length > 0)) { // add comma if it isn't the first type
                        typelist += ", ";
                    }
                    typelist += t.name;
                //    						for (i=0; i < t.name.length; i++){typelist += t.name.charAt(i);}
                }
                else {
                    add_dots = true;
                }
                break;
        } // end switch
        n++;
    } // end for topics
    if (add_dots) {
        typelist += " ...";
    }
    return typelist;
} // end best_topic_types


