var widgetHtmlTemplate = "\
	<div id='scholarometerWidgetTop'\
        style='\
        padding:5px;\
        background-color:___hbc___;\
        -moz-border-radius-topleft: 5px;\
		-moz-border-radius-topright: 5px;\
		-webkit-border-top-left-radius: 5px;\
        -webkit-border-top-right-radius: 5px;'>\
        <table style='color:___htc___; text-indent:5px;'>\
            <tr>\
                <td width='45px'>\
                    <a href='http://scholarometer.indiana.edu'>\
						<img src='http://scholarometer.indiana.edu/html/images/favicon.ico'\
						border='0' height='35px'></a>\
                </td>\
                <td style='text-indent:0px'>\
                <span><a href='http://scholarometer.indiana.edu' style='text-decoration:none; color:___htc___'>Impact Analysis</a></span>\
                <br />\
                <span style='font-size:1.2em'>___authorName___</span>\
                </td>\
            </tr>\
        </table>\
    </div><!-- end of scholarometerWidgetTop -->\
    <div id='scholarometerWidgetMid'\
        style='\
            border:solid 5px ___hbc___;\
            background-color:___cbc___;\
            padding:5px;\
            vertical-align:middle'>\
    <table id='scholarometerWidgetTable' style='color:___ctc___; text-indent:5px;'>\
	___measureRows___\
    </table>\
    </div><!-- end of scholarometerWidgetInner -->\
    <div id='scholarometerWidgetBottom'\
    	align='right'\
        style='\
        background-color:___hbc___;\
        padding:5px;\
        -moz-border-radius-bottomleft: 5px;\
        -moz-border-radius-bottomright: 5px;\
        -webkit-border-bottom-left-radius: 5px;\
        -webkit-border-bottom-right-radius: 5px;'>\
        <a href='http://scholarometer.indiana.edu' style='text-decoration:none; color:___htc___'>by Scholarometer</a>\
    </div>\
";

var rowTemplate = "\
    <tr>\
        <td width='70%'>___name___</td>\
        <td width='30%' align='right' style='color:___mtc___'>___value___</td>\
    </tr>\
";

var hfHeader = "\
	<tr><td></td><td></td></tr>\
	<tr><td></td><td></td></tr>\
    <tr style='border-bottom: dotted thin ___ctc___;'>\
        <td width='70%'>discipline</td>\
        <td width='30%' align='right'>h<sub>f</sub> index</td>\
    </tr>\
";

var rowTemplateHf = "\
    <tr>\
        <td width='70%' style='padding-left:15px; text-indent:-10px; margin-left:0px'>___name___</td>\
        <td width='30%' align='right' style='color:___mtc___'>___value___</td>\
    </tr>\
";
 
function parseJsonData(jsonData){
	authorProfile = eval(jsonData);
}

function insertWidget(){
	authorProfile["name"] = authorProfile["name"].toUpperCase(); 
	
	var widgetHtml = widgetHtmlTemplate;
	widgetHtml = widgetHtml.replace(/___hbc___/g, scholarometerWidgetColors["headingBackground"]);
	widgetHtml = widgetHtml.replace(/___htc___/g, scholarometerWidgetColors["headingText"]);	
	widgetHtml = widgetHtml.replace(/___cbc___/g, scholarometerWidgetColors["contentBackground"]);
	widgetHtml = widgetHtml.replace(/___ctc___/g, scholarometerWidgetColors["contentText"]);
	hfHeader = hfHeader.replace(/___ctc___/g, scholarometerWidgetColors["contentText"]);
	widgetHtml = widgetHtml.replace(/___authorName___/g, authorProfile["name"]);
	
	var rowTemplateCopy = rowTemplate.replace(/___mtc___/g, scholarometerWidgetColors["measureText"]);
	var rowTemplateHfCopy = rowTemplateHf.replace(/___mtc___/g, scholarometerWidgetColors["measureText"]);
	
	var tableRows = "";
	var hfRows = "";
	var rowCopy;
	
	for(var attrib in scholarometerWidgetPreferences){
		rowCopy = rowTemplateCopy;
		if (attrib != 'h<sub>f</sub> index') {
			if(!scholarometerWidgetPreferences[attrib]) continue;
			rowCopy = rowCopy.replace("___name___", attrib);
			rowCopy = rowCopy.replace("___value___", authorProfile[attrib]);
			tableRows += rowCopy;
			continue;			
		}else{
			for(var discipline in authorProfile[attrib]){
				if(!(discipline in scholarometerWidgetPreferences[attrib])) continue;
				if(!scholarometerWidgetPreferences[attrib][discipline]) continue;
				rowCopy = rowTemplateHfCopy;
				rowCopy = rowCopy.replace("___name___", discipline);
				rowCopy = rowCopy.replace("___value___", authorProfile[attrib][discipline]);
				hfRows += rowCopy;
			}			
		}
	}	
	
	if(hfRows != "")
		tableRows += hfHeader + hfRows;
		
	widgetHtml = widgetHtml.replace("___measureRows___", tableRows);
	document.getElementById("scholarometerWidget").innerHTML = widgetHtml;
	document.getElementById("scholarometerWidget").style.width = scholarometerWidgetWidth;
	document.getElementById("scholarometerWidget").style.height = scholarometerWidgetHeight;
}

insertWidget();

