

var pages = 0;

$j(document).ready(function() {
	var author = $j("#author").val();
	pages = parseInt($j("#number_of_pages").html());
	//page(0);
});

function populatePagerHolder(current_page){

var pages = parseInt($j("#number_of_pages").html());
var content = $j('#pager_holder').html();
var new_content = '';

var i,j;
var min = 0;
pages = parseInt(pages);

if (pages=="1") {
	$j('#pager_holder').html('');
	return false;
}

var on_class = '';

var max = 0;

current_page = current_page + 1;
min = current_page - 2;
if (min<=0) min = 1;

max = current_page + 2;
if (max > pages) max = pages;

if (max - min < 4) {
	if (max < pages) {
		max = min + 4;
	}
	else {
		min = max - 4;
	}

}

if (min<=0) min = 1;
if (max > pages) max = pages;


if (max > pages) max = pages;
if (min < 0) min = 1;

{
	new_content += '<li><a href="javascript:void(0);" id="previouspagelink" onclick="javascript:previouspage();" class="prev">'+citizen_friends_prev+'</a></li>';
}

for(i = min; i<=max; i++){
	j = i-1;
	if(i == current_page){
		on_class = 'class="on" ';
	}
	else {
		on_class = '';
	}

	new_content += '<li><a href="javascript:void(0);" '+on_class+'id="friend_page_id_'+j+'" onclick="javascript:page('+j+')">'+i+'</a></li>';
}

{
	new_content += '<li><a href="javascript:void(0);" id="nextpagelink" onclick="javascript:nextpage();" class="next">'+citizen_friends_next+'</a></li>';
}

$j('#pager_holder').html(new_content);

}

function remove_friend(link) {
	if (confirm('Remove friend ?')) {
		jQuery.get(link, '', function(nr_of_pages){
			$j("#number_of_pages").html(parseInt(nr_of_pages));
			pages = parseInt($j("#number_of_pages").html());
			max = pages;
			page(parseInt($j("#page").val()));
		});
	}
	return false;
}

function populateFriends(data) {
	$j('#fixed_tab_friends').css('background', '');

	if (data.length<12){
		//hidenext();
	}else{
		//shownext();
	}
	jQuery.each(data, function(idx, shout) {
		var content = $j('#friends_tab_content').html();
		var new_content = '';
		new_content += '<div class="citizen">';
		new_content += '	<div class="avatarholder">';
		new_content += shout["img"];
		if (shout["xp"]!=""){
			new_content += '		<div class="xprank">'+shout["xp"]+'</div>';
		}
		new_content += '	</div> ';
		new_content += '	<a href="'+shout["url"]+'" class="nameholder">'+shout["name"]+' ';
		if (shout['online'] != ''){
			new_content += '<img src="/images/parts/icon-online.gif" alt="online" />';
		}
		new_content += '</a>';
		new_content += shout["snd"]+"&nbsp;";
		new_content += "<a href=\"#\" onclick=\"return remove_friend('"+shout["rmv_url"]+"');\">";
		new_content += shout["rmv_img"];
		new_content += '</a>';

		new_content += '</div>';

		var content = $j('#friends_tab_content').html(content + new_content);
	});
}

function unpopulateFriends() {
	// unfortunately this commented line below, makes IE 6/7 hang up !
	//$j('#fixed_tab_friends').css('background', 'url(/images/parts/ajax-loader.gif) no-repeat center;');
	$j('#friends_tab_content').html('');
}

function hideprevious(){
	$j("#previouspagelink").css('display','none');
}

function showprevious(){
	$j("#previouspagelink").css('display','');
}

function hidenext(){
	$j("#nextpagelink").css('display','none');
}

function shownext(){
	$j("#nextpagelink").css('display','');
}

function page(id){
	if (id == 0) {
		hideprevious();
	}else{
		showprevious();
	}
	unpopulateFriends();
	var author = $j("#author").val();
	jQuery.getJSON('/citizen/friends/'+author+'/'+id+'&callback?', function(data) {
		populateFriends(data);
		populatePagerHolder(parseInt(id));
		setCurrentPage(parseInt(id));

	});
}

function previouspage(){
	var author = $j("#author").val();
	var id = $j("#page").val();
	if (id > 0) page(--id);
	else return false;
}

function nextpage(){
	var id = $j("#page").val();
	var author = $j("#author").val();
	if (id<pages-1) page(++id);
	else return false;
}

function setCurrentPage(id){
	$j("#friend_page_id_"+$j("#page").val()).removeAttr('class','');
	$j("#page").val(id);
	$j("#friend_page_id_"+id).attr('class','on');
}