function activity() {
	// id of the node we are working with
	var id;
	var type;
	var opening;
	var timeout;
	var timer;
}

activity.prototype.attach = function(method) {
	act.opening=true;
	sys.connect(null, 'common.attaches.'+method, 'attach_engine');
	$('#attach_engine').fadeIn('slow');
}

activity.prototype.exit = function() {
	$('#myactivity').focus();
	$('#attach_engine').fadeOut('fast');
	act.opening=false;
}

activity.prototype.post = function() {
	var addon = '';
	if (actref.length > 0) {
		addon = '&ref=['+actref.join(',')+']';
	}
	sys.connect(null, 'common.get.js&activity='+document.getElementById('myactivity').innerHTML+addon, 'nowrite');
	document.getElementById('myactivity').innerHTML='';
	document.getElementById('myactivity').style.height='25px';
	$('#shareButton').attr('disabled', 'disabled');
	$('#attachto').fadeOut('fast');
	actref.splice(0,actref.length);
}

activity.prototype.save = function() {
	var ref;
	switch(document.getElementById('attach_type').value) {
		case "link":
			ref = '<a href="'+document.getElementById('attach_link').value+'" target="_new" class="activitylink">#link</a>&nbsp;';
			//actref[actref.length] = '{"pclass":"link", "pid":"'+document.getElementById('attach_link').value+'"}';
		break;
	}
	$("#myactivity").append(ref);
	$('#attach_engine').fadeOut('fast');
	act.timer=false;
	act.opening=false;
	$("#myactivity").focus();
}

activity.prototype.close = function() {
	if (this.timer) {
		if (document.getElementById('myactivity').innerHTML.length == 0 && !act.opening) {
			document.getElementById('myactivity').style.height='25px';$('#attachto').fadeOut('fast');
			$('#shareButton').attr('disabled', 'disabled');
			clearTimeout(this.timeout);
			this.timer=false;
		}
	}
	else {
		this.timeout = setTimeout("act.close()", 600);
		this.timer=true;
	}
}

activity.prototype.get = function(lf) {
	// grab activity
	var html;
	var id;
	var pclass;
	var pid;
	var url;
	if (lf) {
		lastfetch = lf;
	}
	$.getJSON(_siteurl+'/data.php', {"class":"json", "feed":"activity", "lastfetch":lastfetch}, 
		function(data){
          $.each(data.items, function(i,item){
          	id = 'comments_'+item.history;
	        html = '<div id="'+item.history+'_container" style="border-bottom:1px solid #9FBEE8;margin-bottom:5px;padding:5px;">';
		        html+= '<table border="0" cellpadding="3" cellspacing="3" style="width:100%;"><tr>';
		        	html+= '<td style="width:45px;"><div style="padding:2px;border:1px solid #ccc;"><img src="'+_siteurl+'/pix.php?user='+item.image+'" height="40" width="40"/></div></td>';
		        	html+= '<td style="width:95%;font-size:13px;padding-left:3px;">';
		        	html+= '<div style="padding:3px;">';
		       	if (item.url) {
		        	url = item.action + '&nbsp;<a href="#" onclick="act.openItem(\''+item.pclass+'\', \''+item.pid+'\');">' + item.pclasslabel+'</a>';
		       		}
		       	else {
		       		url = item.pclasslabel;
		       	}
		        if (item.data) {
		         	html+= '<span style="font-weight:bold;color:#000053;">'+item.actorlabel+'</span>&nbsp;'+' '+item.data.label+'&nbsp;<span class="'+item.data.cls+'">&nbsp;</span>'+item.data.name+' <a href="#">'+item.data.item+'</a> <span style="font-weight:bold;color:#000053;">references</span> '+url;
		        	pclass = item.data.cclass;
		        	pid = item.data.cid;
		        }
		        else {
		        	html+= '<span style="font-weight:bold;color:#003162;">'+item.actorlabel+'</span>&nbsp;'+ url;
		        	pclass = item.pclass;
		        	pid = item.pid;
		        }
		        	html+='</div>';
		        	html+='<div style="padding:2px;background-color:#F4F8FD;margin-bottom:3px;font-size:11px;color:#003264;">&nbsp;-&nbsp;'+item.howlongago+'&nbsp;|&nbsp;<a style="color:#003264;" href="#" onclick="$(\'#'+id+'_dialog\').fadeIn(\'fast\');$(\'#'+id+'_textarea\').focus();">Comment</a>&nbsp;|&nbsp;<span id="'+item.history+'_likelist"></span></div>';
		        	html+='</td>';
		        	html+= '<td style="width:25px;text-align:center;">';
		        		if (!item.lock)
		        			html+= '<div style="cursor:pointer;padding-top:10px;width:25px;height:25px;" onclick="sys.connect(null, \'common.get.js&remove=1&rmactivity='+item.pid+'&history='+item.history+'\', \'nowrite\');"><img src="images/reset.gif"/></div>';
		        	html+= '</td></tr>';
		        html+= '</table>';
	        	html+= '<div style="background-color:#F8F8F8" id="'+id+'"></div>';
	  		html+= '</div>';
	        if (lastfetch != 0)
	        	$("#site_activity_content").prepend(html);
	        else
	            $("#site_activity_content").append(html);
	        if (i+1 == data.items.length) {
	        	lastfetch = data.topitem;
	        }
	        activity.prototype.comment('comments_'+item.history, pclass, pid, item.history);
	        activity.prototype.like(item.history,pclass,pid);
          });
		});
}

activity.prototype.like = function(history,pclass,pid) {
	var html='';
	$.getJSON(_siteurl+'/data.php', {"class":"json", "feed":"likelist", "pclass":pclass, "pid":pid, "history":history}, 
		function(data){
			html+= '<a href="#" style="color:#003264;" onclick="sys.connect(null, \'common.get.js&like=1&pclass='+pclass+'&pid='+pid+'&history='+history+'\', \'nowrite\');">';
			if (data.like) {
				html+='UnLike';
			}
			else {
				html+='Like';
			}
			html+='</a>&nbsp;&nbsp;';
				
			if (data.items.length>0) {
				html+='&nbsp;(';
				$.each(data.items, function(k,litem) {
					html+= '<a href="#" style="color:#003264;" ';
					html+=' onclick="act.openItem(\'company.contact\', \''+litem.contact+'\');">'+litem.person+'</a>';
					if (k+2 == data.items.length) {
						html+='&nbsp;and&nbsp;';
					}
					else if (k+1 != data.items.length){
						html+= ',&nbsp;';
					}
				});
				html+=' like this)';
			}
			$("#"+history+'_likelist').html(html);
		}
	);
}

activity.prototype.comment = function(id,pclass,pid,history) {
	var html='';
	var del='';
	var bc;
	$.getJSON(_siteurl+'/data.php', {"class":"json", "feed":"comments", "pclass":pclass, "pid":pid, "history":history}, 
		function(data){
          $.each(data.items, function(i,item){
          	if (!item.locked) {
          		del = '[&nbsp;<a href="#" onclick="sys.connect(null, \'common.get.js&badcomment='+item.comment+'&remove=1\', \'nowrite\');">delete</a>&nbsp;]';
          	}
          	else {
     			del = '';
          	}
			html+= '<div style="border-top:1px dotted #efefef;margin-bottom:1px;padding:3px;" id="comment_'+item.comment+'">';
			html+= '<div style="margin-left:40px;float:left;width:30px;background-color:transparent;"><img src="'+_siteurl+'/pix.php?user='+item.image+'" height="25" width="25" align="center"/></div>';
				html+= '<div style="float:left;">';
					html+= '<span style="font-weight:bold;">'+item.author+'</span>&nbsp;-&nbsp;'+item.posted+'&nbsp;'+del+'<br>'+item.message;
				html+= '</div>';
				html+= '<div class="clear"><!-- --></div>';
			html+= '</div>';
          });
          html+= '<div id="'+id+'_dialog" style="display:none;border-top:1px dotted #efefef;margin-bottom:1px;padding:3px;">';
          html+= '<div style="float:left;width:40px;padding-right:5px;margin-top:3px;background-color:transparent;">&nbsp;</div>';
          html+= '<div style="float:left;width:90%;">';
			    html+= '<textarea id="'+id+'_textarea" style="width:70%;height:40px;" onblur="$(\'#'+id+'_dialog\').fadeOut(\'fast\');"></textarea><br/>';
			    html+= '<input type="button" id="'+id+'_button" value="Save Comment" onclick="sys.connect(null, \'common.get.js&pclass='+pclass+'&pid='+pid+'&history='+history+'&comment=\'+document.getElementById(\''+id+'_textarea\').value, \'nowrite\');"/>';
			    html+= '</div>';
		  html+= '<div class="clear"><!-- --></div>';
		  html+= '</div>';
		  $("#"+id).html(html);
		});
}

activity.prototype.openItem = function(pclass,pid) {
	Ext.getCmp('main-panel').getUpdater().update({url:'service.php', callback: cnfjs, params:'&request=common.get.remote&subrequest='+pclass+'.space&pclass='+pclass+'&pid='+pid});
}

activity.prototype.openUrl = function(req) {
	Ext.getCmp('main-panel').getUpdater().update({url:'service.php', callback: cnfjs, params:'&request=common.get.remote&subrequest='+req});
}

act = new activity();