

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, max){
	
    var length = max ? max : 550;
    var wp_link = '';
//	if (max) {
//		length = max;
//	}
	    

//    var blurb_id = document.getElementById(div_id_name);
    var blurburl = ''; // + "http://api.freebase.com/api/trans/blurb/";
	if (typeof topic == 'string') {
		blurburl += topic
	}
	else if (typeof topic == 'object') {
		blurburl += topic.id;
	    if ('wp_key' in topic) {
	    	wp_link =  '<a class="wp_more" target="_blank"';
	    	wp_link += ' href="http://en.wikipedia.org/wiki/index.html?curid=';
	    	wp_link += topic['wp_key']+'"> ... more</a>';
		}
		// 	    var blurbs = null;
		// 	    if ('/common/topic/article' in topic) {
		// 	    	blurbs = topic['/common/topic/article'];
		// 	    }
		// 	    if (blurbs != null) {
		// 	if (blurbs[0] != null) {
		// 		blurburl += '%23' + blurbs[0].guid.replace(/#/, "");
		// 	}
		// }
	}

	if (blurburl) {
		blurburl = 'http://api.freebase.com/api/trans/blurb/' + blurburl;
	    $.ajax({
	        url: blurburl,
	        dataType: "jsonp",
	        cache: true,
	        success: function(response){
	            if (response.result != undefined) {
	                response.result.body = response.result.body.replace(/...$/g, '');
	                
	                $('#'+div_id_name).append(response.result.body); 
	                if (wp_link) {
	                	$('#'+div_id_name).append(wp_link);
	                }
	            }
	        },
	        data: {
	            maxlength: length
	        }
	    });		
	}
}

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



