
function Friends(mymid,inst,friendDIV,recipDIV,pc,pp,pg) {
	this.mymid=mymid;
	this.inst=inst;
	this.friendDIV=friendDIV;
	this.recipDIV=recipDIV;
	this.iPC=(1*pc);
	this.iPP=(1*pp);
	this.iPg=(1*pg);
	this.PG=null;
	this.mid=-1;
	this.un='';
	
	this.asGroups=null;
	this.iGroup=-1;
	this.recipients='';
	this.showgroups=true;
	this.memberlink=false;
	this.contact=false;
	this.passive=false;
	this.asCtrl=null;
	
	this.mid=function(mid) { this.mid=mid; }
	this.username=function(un) { this.un=un; }
	this.initRecipients=function(recipients) { this.recipients=recipients; }
	this.setMemberLink=function(memberlink) { this.memberlink=memberlink; }
	this.setContact=function(contact) { this.contact=contact; }
	this.setPassive=function(passive) { this.passive=passive; }
	this.setShowGroups=function(showgroups) { this.showgroups=showgroups; }
	this.setGroupID=function(grpid) { this.iGroup=grpid; }
	
	this.addCtrl=function(ctrl) {
		if (this.asCtrl==null) this.asCtrl=new Array();
		this.asCtrl[this.asCtrl.length]=ctrl;
	}

	this.drawFriends=function() {
		if (this.PG==null || this.asGroups==null) {
			var fetch=new Fetcher(true);
			var sResp=fetch.now('jsp/feed/friends.jsp?cnt='+(this.PG==null ? 1 : 0)+'&mid='+this.mid+'&grps='+(this.asGroups==null ? 1 : 0)+"&grpid="+this.iGroup);

			if (this.PG==null) this.PG=new PG(this.iPP,this.iPg,extractXML(sResp,"cnt"));
			if (this.showgroups && this.asGroups==null) this.asGroups=extractXML(sResp,"grps").split("\|");
		}

		var oDiv=getElement(this.friendDIV);
		if (oDiv) {
			var sGrpHTML=this.getGroups();
			var sPgHTML=this.getPaging();
			var sFriendHTML=this.getFriends();
			var sMsg='';
			if (this.PG.max()>0) {
				if (this.mymid==this.mid)
					sMsg='<p>You currently have ';
				else
					sMsg='<p>They currently have ';
				sMsg+=(this.PG.max()==1 ? '1 friend' : this.PG.max()+' friends');
				if (sGrpHTML!='') {
					if (this.iGroup!=-1) 
						sMsg+=' in this group. To search for more friends, click <a href="people">here</a>.';
					else				
						sMsg+='. To view friends in a particular group, use the drop down menu below. And to search for more friends, click <a href="people">here</a>.';
				}
				else
					sMsg+='. To search for more friends, click <a href="people">here</a>.';
				sMsg+='<\/p><p>&nbsp;<\/p>';
			}

			oDiv.innerHTML='<div class="friendtop">'
							+sMsg
							+sGrpHTML
							+sPgHTML
							+'<br clear="all" \/>'
							+'<\/div>'
							+sFriendHTML;
		}
	}
	this.getGroups=function() {
		var sHTML='';
		if (this.PG.max()>0 && this.asGroups!=null && this.asGroups.length>1) {
			var asGrp=null;
			for (var i=0 ; i<this.asGroups.length-1 ; i++) {
				asGrp=this.asGroups[i].split("~");
				sHTML+='<option value="'+asGrp[0]+'"'+(asGrp[0]==this.iGroup ? ' selected' : '')+'>'+asGrp[1]+'<\/option>';
			}
			sHTML='<form name="frmGroup" style="float:left"><select name="group" onchange="javascript:'+this.inst+'.goGroup()" style="width:150px">'
						+'<option value="-1">All Groups<\/option>'
						+sHTML
					+'<\/select><\/form>';
		}
		return sHTML;
	}
	this.getPaging=function() {
		if (this.PG.max()>1)
			return '<p class="pgflick" style="float:right"><a href="javascript:'+this.inst+'.pg(-1)"><img src="img/btn/prev.gif" width="18" height="15" alt="<" \/><\/a>'
					+'<span>'+this.PG.from()+' to '+this.PG.to()+' of '+this.PG.max()+'<\/span>'
					+'<a href="javascript:'+this.inst+'.pg(1)"><img src="img/btn/next.gif" width="18" height="15" alt=">" \/><\/a><\/p>';
		else
			return '<p class="pgflick">&nbsp;<\/p>';
	}
	this.getFriends=function() {
		var sWho=this.recipients;
		var fetch=new Fetcher(true);
		var sResp=fetch.now('jsp/feed/friends.jsp?frnds=1&mid='+this.mid+'&pg='+this.PG.current()+'&pp='+this.PG.perpage()+'&grpid='+this.iGroup);

		var asAll=extractXML(sResp,"frnds").split("\|");

		var sHTML='';

		var as=null;
		var bAdded=false;
		var iCol=this.iPC;
		var sClass="";
		var url=null;
		for (var i=0 ; i<asAll.length-1 ; i++) {
			if (iCol--==0) {
				iCol=this.iPC-1;
				sHTML+='<\/tr><tr valign="bottom">';
			}

			as=asAll[i].split("~");

			bAdded=(sWho.indexOf('|'+as[0]+'~')!=-1);
			sClass=(iCol==0 ? 'r' : '')+(bAdded ? 'b' : '');
			sHTML+='<td'+(sClass.length>0 ? ' class="'+sClass+'"' : '')+' id="t'+as[0]+'">';
			
			if (this.memberlink) {
				url='people/'+as[2];
				sHTML+='<a href="'+url+'"><img src="'+getThumbPicPath(as[0])+'" align="top" \/><\/a><p><a href="'+url+'">'+as[1]+'<\/a><\/p>'
			}
			else
				sHTML+='<img src="'+getThumbPicPath(as[0])+'" align="top" \/><p>'+as[1]+'<\/p>'
			
			if (!this.passive) sHTML+='<p id="b'+as[0]+'">'+(bAdded ? this.removeBtn(as[0],as[1]) : this.addBtn(as[0],as[1]))+'<\/p>';
			
			if (this.contact && as[5]==1) {
				if (as[4]==1)
					sHTML+='<p><a href="people/'+this.un+'/message/contact/'+as[2]+'"><img src="img/btn_s/contact.gif" width="53" height="14" alt="Say Hi" \/><\/a><\/p>';
				else
					sHTML+='<p><a href="people/'+this.un+'/message/befriend/'+as[2]+'"><img src="img/btn_s/make-friends.gif" width="86" height="14" alt="Make Friends" \/><\/a><\/p>';
			}

			for (var j=0 ; this.asCtrl!=null && j<this.asCtrl.length ; j++) {
				sHTML+='<p>'+this.asCtrl[j].replace(/#MID/g,as[0])+'</p>';
			}
				
			sHTML+='<div class="hr"><\/div><\/td>';
		}
		if (sHTML.length>0)
			return '<table class="recip"><tr valign="bottom">'+sHTML+'<\/table>';
		else if (this.mymid==this.mid)
			return '<div class="inset"><h4>Search <a href="people">here<\/a> to find out if any of your friends are already Doosport members.<\/h4><\/div>'; 
		else
			return '<div class="inset"><h4>No friends.<\/h4><\/div>';
	}
	this.pg=function(iDelta) {
		this.PG.move(iDelta);
		this.drawFriends();
	}
	this.goGroup=function() {
		var o=document.frmGroup.group;
		this.iGroup=o[o.selectedIndex].value;
		this.PG=null;
		this.drawFriends();
	}
	this.addBtn=function(id,aka) { return (this.passive ? '' : '<a href="javascript:'+this.inst+'.addRecip('+id+',\''+aka+'\')"><img src="img/btn_s/add-recipient.gif" width="46" height="16" alt="add" align="top" \/><\/a>'); }
	this.removeBtn=function(id,aka) { return (this.passive ? '' : '<a href="javascript:'+this.inst+'.removeRecip('+id+',\''+aka+'\')"><img src="img/btn_s/remove-recipient.gif" width="60" height="16" alt="remove" align="top" \/><\/a>'); }
	this.removeRecip=function(iID,sAKA) {
		var o=getElement('t'+iID);
		if (o) o.style.backgroundColor='transparent';
		o=getElement('b'+iID);
		if (o) o.innerHTML=this.addBtn(iID,sAKA);

		this.recipients=this.recipients.replace('\|'+iID+'~'+sAKA,'');

		this.setRecipients();
	}
	this.addRecip=function(iID,sAKA) {
		var o=getElement('t'+iID);
		if (o) o.style.backgroundColor='#def1f7';
		o=getElement('b'+iID);
		if (o) o.innerHTML=this.removeBtn(iID,sAKA);

		this.recipients+='|'+iID+'~'+sAKA;

		this.setRecipients();
	}
	this.setRecipients=function() {
		var sList='';
		if (this.recipients.length>0) {
			var ar=this.recipients.split('\|');
			var sAKA=null;
			for (var i=0 ; i<ar.length ; i++) {
				if (ar[i].length>0) {
					sAKA=ar[i].split('~');
					sList+=sAKA[1]+', ';
				}
			}
			if (sList.length>0)	sList=sList.substring(0,sList.length-2);
		}
		var o=getElement(this.recipDIV);
		o.innerHTML='<p>'+(sList.length>0 ? sList : '')+'</p>';
	}
	this.getRecipients=function() {
		var sList='';
		if (this.recipients.length>0) {
			var ar=this.recipients.split('\|');
			var sID=null;
			for (var i=0 ; i<ar.length ; i++) {
				if (ar[i].length>0) {
					sID=ar[i].split('~');
					sList+=sID[0]+',';
				}
			}
			sList=sList.substring(0,sList.length-1);
		}
		return sList;
	}
}
