function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

function getQueryStringArgs() {
	var h = window.location.search.substring(1);
	var ha = h.split('&');
	var out = {};
	for(var i = 0; i < ha.length; i++) {
		var hai = ha[i].split('=');
		out[hai[0]] = hai[1];
	}
	return out;
}

addEvent(window,'load',placeAlbum1);

var api_key = 'bac8ab480c5aa15de8595f04f2c3f86a';
var user_id = '93723338@N00'

function placeAlbum1() {
	window.setTimeout(placeAlbum2,10);
}

function placeAlbum2() {
	if(window.location.href.indexOf('localhost') == -1 && window.location.href.indexOf('getperson.php') > -1 && document.body.innerHTML.indexOf('At least one living individual') == -1) {
		var myargs = getQueryStringArgs();
		var infoBlock = document.getElementById('info');
		var baseDiv = null;
		if(!document.getElementById('media')) {
			baseDiv = document.createElement('LI');
			baseDiv.id = 'media';
			baseDiv.style.listStyleType = 'none';
			if(document.getElementById('citations')) infoBlock.parentNode.insertBefore(baseDiv,document.getElementById('citations'))
			else infoBlock.parentNode.appendChild(baseDiv);
			baseDiv.innerHTML = '<table border="0" cellpadding="4" cellspacing="1" width="100%"><tbody></tbody></table>';
		} else baseDiv = document.getElementById('media');

		var url = '/flickrbadge.php?title='+myargs.personID+'&cachecontrol='+(new Date()).getTime()+'&api_key='+api_key+'&user_id='+user_id;
		if (typeof(xmlhttp) == 'undefined' && typeof(XMLHttpRequest) != 'undefined') xmlhttp = new XMLHttpRequest();
		if(xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/html');
		xmlhttp.open("GET", url, false);
		xmlhttp.send(null);
	
		var rt = xmlhttp.responseText;
		if(rt == '') return;
		
		var tbl = baseDiv.getElementsByTagName("table")[0];
		var row = null;

		var anum = -1;
		for(var i = 0; i < tbl.childNodes[tbl.childNodes.length-1].rows.length; i++) if(tbl.childNodes[tbl.childNodes.length-1].rows[i].cells[0].innerHTML.indexOf('Albums') > -1) { anum = i; break; }
			
		if(tbl.childNodes[tbl.childNodes.length-1].rows.length == 0 || anum == -1) {
			var row = document.createElement("tr");
			tbl.childNodes[tbl.childNodes.length-1].appendChild(row);
			var cell = document.createElement("td");
			cell.className = "fieldnameback indleftcol";
			cell.id = "info1";
			cell.setAttribute("rowspan","1");
			cell.setAttribute("valign","top");
			cell.setAttribute("width","90");
			cell.innerHTML = '<span class="fieldname">Albums</span>';
			row.appendChild(cell);
			var cell = document.createElement("td");
			cell.className = "databack";
			cell.innerHTML = "";
			cell.setAttribute("colspan","2");
			row.appendChild(cell);
		} else {
			tbl.childNodes[tbl.childNodes.length-1].deleteRow(tbl.childNodes[tbl.childNodes.length-1].rows.length-1);

			tbl.childNodes[tbl.childNodes.length-1].rows[anum].cells[0].setAttribute("rowspan", parseInt(tbl.childNodes[tbl.childNodes.length-1].rows[0].cells[0].getAttribute("rowspan"),10));

			var row = document.createElement("tr");
			tbl.childNodes[tbl.childNodes.length-1].appendChild(row);
			cell = document.createElement("td");
			cell.className = "databack";
			cell.innerHTML = "";
			cell.setAttribute("colspan","2");
			row.appendChild(cell);
		}
		var rr = document.createElement("tr");
		var cc = document.createElement("td");
		cc.setAttribute("colspan","3");
		cc.setAttribute("height","3");
		cc.innerHTML = "<font size=1>&nbsp;</font>";
		rr.appendChild(cc);
		tbl.childNodes[tbl.childNodes.length-1].appendChild(rr);

		row.cells[row.cells.length-1].innerHTML = "<span class=normal>" + rt + "</span>";

	}
}