function get_scores(id){
	if($('popup')){
		$('popup').remove();	
	}
	var div = new Element("div", { id: "popup" });
	var h4 = new Element("h4").update("Please wait while we calculate your score<br />from your Twitter activity.");
	var loader = new Element("img", { id: "ajax_loader", src: "/images/ajax-loader.gif" });
	var li = new Element("li").update($$('#topic_'+id+' img')[0].cloneNode(true));
	var ad = new Element("div", { class: "advertisement" }).update('<h5>Low scores? Try reading more.</h5><iframe src="http://rcm.amazon.com/e/cm?t=140gadgets-20&o=1&p=12&l=bn1&mode=books&browse=1000&fc1=242424&lt1=_blank&lc1=3399CC&bg1=FFFFFF&f=ifr" marginwidth="0" marginheight="0" width="300" height="250" border="0" frameborder="0" style="border:none;" scrolling="no"></iframe>');
	var clear = new Element("div", { class: "clear" });
	var close_popup = new Element("a", { class: "close", onclick: "close_popup();return false;", href: "#" }).update("close");
	div.appendChild(close_popup);
	div.appendChild(ad);
	div.appendChild(h4);
	div.appendChild(li);
	li.appendChild(new Element("br"));
	li.appendChild(loader);
	div.appendChild(clear);
	var scroll_height = document.viewport.getScrollOffsets().top;
	div.setStyle({ top:scroll_height+200+"px" });
	
	$$('body')[0].appendChild(div);
	
	var scroll_height = document.viewport.getScrollOffsets().top;
	$('popup').setStyle({ top:scroll_height+200+"px" });
	
	new Ajax.Request('/topic_check.php?q='+id, { method: 'get', onSuccess: function(elem){ 
			var div = new Element("div").update(elem.responseText+"<p><a href='/t/"+id+"'>View Ranks</a> &middot <a href='/' onclick='add_comment("+id+");return false;'>Add Comment</a></p><p>Or<br /><a href='#' onclick='close_popup();return false;'><strong>close this</strong></a></a>");
			$('ajax_loader').remove();
			li.appendChild(div);
		}
	});
}

function add_comment(id){
	if($('popup')){
		$('popup').remove();	
	}
	var div = new Element("div", { id: "popup" });
	var h2 = new Element("h2").update("Loading comment form");
	var loader = new Element("img", { id: "ajax_loader", src: "/images/ajax-loader.gif" });
	var close_popup = new Element("a", { class: "close", onclick: "close_popup();return false;", href: "#" }).update("close");
	div.appendChild(close_popup);
	div.appendChild(h2);
	div.appendChild(loader);
	$$('body')[0].appendChild(div);
	
	var scroll_height = document.viewport.getScrollOffsets().top;
	$('popup').setStyle({ top:scroll_height+200+"px", width:"300px", marginLeft:"-160px" });
	
	new Ajax.Request('/comment_form.php?q='+id, { method: 'get', onSuccess: function(elem){
			$('ajax_loader').remove();
			$$('#popup h2')[0].remove();
			
			var div = new Element("div").update(elem.responseText);
			$('popup').appendChild(div);													
		}
	});															
}

function get_friends(){
	if($('popup')){
		$('popup').remove();	
	}
	var div = new Element("div", { id: "popup" });
	var close_popup = new Element("a", { class: "close", onclick: "close_popup();return false;", href: "#" }).update("close");
	div.appendChild(close_popup);
	$$('body')[0].appendChild(div);
	
	var scroll_height = document.viewport.getScrollOffsets().top;
	$('popup').setStyle({ top:scroll_height+200+"px", width:"300px", marginLeft:"-160px" });
	
	new Ajax.Request('/friends.php', { method: 'get', onSuccess: function(elem){
			var div = new Element("div").update(elem.responseText);
			$('popup').appendChild(div);													
		}
	});															
}

function submit_comment(id){	
	new Ajax.Request('/comment_submit.php', {
      method: 'post',
      parameters: {
        'topic_id': id,
        'comment': $('comment_box').getValue()
      },

      onComplete: function(transport) {
        $('popup').update("<h2>Success!!</h2><p>Thanks for the support</p>");
		$('popup').fade({ duration:2.5 });
      }
    });
}

function close_popup(){
	if($('popup')){
		$('popup').remove();	
	}	
}