function getElem(name){
	if (document.getElementById){
		return document.getElementById(name);
	} else if (document.all) {
		return document.all[name];
	} else if (document.layers) {
		return document.layers[name];
	} else {
		return null;
	}
}
function convoSetImage(id,url) {
	var image = new Image();
	image.src = url;
	image.onload = function()
	{
		if(/Konqueror|Safari|KHTML/.test(navigator.userAgent) && image.width == 0){ return; }
		elem = getElem(id)
		elem.setAttribute('src',url);
	}
}
function init_convo() {
	var convo_msgCount = 0;
	var convo_obj = [];
	var convo_images = [];
	var convo_talent = {}
	/*
	var convo_talent = {
		"username":{
			"name":"First Last",
			"img":"http://espn.go.com/i/profile/35/b/image.png"
		}
	};
	*/
	var convo_linkText = 'Be the first to comment';
	var convo_result = '';
	var convoLinkTextNode = getElem('convo-linktext')
	try {
		convo_msgCount = ProsperoResponse.ContentElement.Forum[0].Folder[0].Discussion[0]['@messageCount'];
		convo_tid = ProsperoResponse.ContentElement.Forum[0].Folder[0].Discussion[0]['@tid'];
		if (convo_tid == 1) {
			convo_msgCount = 0;
		}
	} catch(e) {
		convo_msgCount = 0;
	}
	if (convo_msgCount > 0) {

		if (convoLinkTextNode) {
			var convo_s = '';
			if (convo_msgCount > 1) { convo_s = 's'; }
			convoLinkTextNode.innerHTML = convo_msgCount + ' comment' + convo_s;
		}
		try{ convo_obj = ProsperoResponse.ContentElement.Forum[0].Folder[0].Discussion[0].Message; } catch(e){ convo_obj = []; }
		if (convo_obj != null && convo_obj.length > 0) {
			/* reverse array */
			convo_obj.reverse();
			for (i=0;i<convo_obj.length;i++) {
				var item = convo_obj[i];
				/*
				var convo_template = '<div class="convo-comment${postCount}">\
				<a href="http://myespn.espn.go.com/${username}"><img id="${id}" src="http://myespn.go.com/i/profile/default/silo1_guy_35x48.jpg" /></a>\
				<div class="comment-right">\
				<p class="convo-user"><a href="http://myespn.go.com/${username}">${username}</a> (${postDate})</p>\
				<p>${postText}</p>\
				</div>\
				<div style="clear:both"></div>\
					</div>';
				*/
				var convo_template = '<div class="convo-comment${postCount}">${mugshot}\
						<div class="comment-right" style="margin-left:0;border-left:none">\
						<p class="convo-user">${userlink} (${postDate})</p>\
						<p>${postText}</p>\
						</div>\
						<div style="clear:both"></div>\
					</div>';
				var date = item['@datePosted'];
				/* BEGIN ADJUSTING DATE TO USER TIMEZONE */
				var dTime = date.getTime();
				var userDate = new Date();
				var zto = (((userDate.getTimezoneOffset() * 60) * 1000) * -1)
				date.setTime(dTime + zto);
				/* END TIME ADJUST */
				var month = date.getMonth() + 1;
				var day = date.getDate();
				var year = date.getFullYear();
				var hour = date.getHours();
				var displayHour = (hour == 0) ? '12' : (hour >= 13) ? hour - 12 : hour;
				var amPm = (hour < 12) ? 'AM' : 'PM';
				var minute = date.getMinutes();
				minute = (minute < 10) ? '0' + minute : minute;
				if (convo_talent[item['@fromDisplayName']] != null) {
					var mugshot = '<div style="width:35px: height: 38px; float: left;"><img alt="' + item['@fromDisplayName'] + '" src="' + convo_talent[item['@fromDisplayName']]['img'] + '" alt="" style="padding-right:10px" /></div>';
					var userlink = '<a href="http://boards.espn.go.com/boards/mb/mb?username=' + item['@fromDisplayName'] + '">' + convo_talent[item['@fromDisplayName']]['name'] + '</a>'
				} else {
					var mugshot = '';
					var userlink = '<span style="color: #000; font-weight:bold">' + item['@fromDisplayName'] + '</span>'
				}
				var displayDate = month + '/' + day + '/' + year + ' at ' + displayHour + ':' + minute + ' ' + amPm;
				var props = {
					id: item['@fromDisplayName'] + '_' + i,
					username: item['@fromDisplayName'],
					postDate: displayDate,
					mugshot: mugshot,
					userlink: userlink,
					postText: item['@messageText'].replace(/\n\n/g,'</p>\n<p>')
				};
				var propValue, regex;
				convo_images.push([props.id,props.imgSrc]);
				props.postCount = ((i % 2 == 0) ? ' odd' : '');
				for(var n in props)
				{
					regex = new RegExp('\\$\\{' + n + '\\}','g');
					propValue = props[n].toString();
					convo_template = convo_template.replace(regex,propValue.toString());
				}
				convo_result += convo_template;
			}
			if (convo_result != '') {
				var commentNode = getElem('post-comments')
				if (commentNode) {
					commentNode.innerHTML = convo_result;
				}
			}
		}
	} else if (convoLinkTextNode) {
		convoLinkTextNode.innerHTML = convo_linkText;
	}
	convo_threadArray = undefined;
	delete(convo_result);
}
function addEvent( obj, type, fn ) {
	if ( obj.attachEvent ) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		obj.attachEvent( 'on'+type, obj[type+fn] );
	} else {
		obj.addEventListener( type, fn, false );
	}
}
function removeEvent( obj, type, fn ) {
	if ( obj.detachEvent ) {
		obj.detachEvent( 'on'+type, obj[type+fn] );
		obj[type+fn] = null;
	} else {
		obj.removeEventListener( type, fn, false );
	}
}
addEvent(window, 'load', init_convo);