

	Message = 
	{
		compose : function(memberid,name)
		{
			resetForm('compose');
			$('recipient_name').value = name;
			$('recip_autocomplete_choices').value = memberid;
			showDialog('compose'); 
			return false;
		},
		
		addRecipient : function(name, id)
		{
			var existingRecipients = $('recips').childElements('input');
			var alreadyAdded = false;
			existingRecipients.each(function(re) {
				if(re.id == id)
				{
					new Effect.Highlight($(re));
					alreadyAdded = true;
				}
			});
			if(!alreadyAdded)
			{
				var recip_button = document.createElement("span");
				recip_button.id = id;
				recip_button.className="button";
				recip_button.style.float = "left";
				
				var html = name;
				html+= '<input type="hidden" value="'+id+'" name="recipient[]"/>';
				html+= '<span onclick="Message.removeRecipient(this)" style="padding-left:10px;">x</span>';

				recip_button.innerHTML = html;
				$("recips").insert(recip_button);
				new Effect.Highlight($(recip_button));
			}
		},
		
		removeRecipient : function(obj)
		{
			new Effect.Fade($(obj).parentNode, {
				afterFinish : function() {
					$((obj).parentNode).remove();
				}
			});
		}	
		
	}
	
	member = 
	{
		clearStatusValueText : "What I'm doing right now...",
		
		editStatusValueText : "is ",
		
		editStatus : function(obj)
		{
			if ($('status_text') != null)
			{
				$('status_text').focus();
				$('status_text').value = obj.innerHTML;
			}
		},
	
		clearStatus : function(obj, member_username)
		{
			memberAjaxActions.updateMemberStatus(obj, member_username, "");
			return false;
		},
		
		clearStatusValue : function(obj)
		{
			/*if(obj.value == member.clearStatusValueText)
				$(obj).value = member.editStatusValueText;
			else
				$(obj).value = $('status_display').innerHTML;	*/
			
			$(obj).value = member.editStatusValueText;
			
			$('submit_status').style.display = "inline";
			obj.className = 'what-are-you-doing';
			
			// Microsoft bug fix
			//var range = $(obj).createTextRange();
			//range.collapse(false);
			//range.select();

			return false;
		},
		
		clearMoodValue : function()
		{
			
			
			
			
			$('mood_icon').style.display = "inline";
			
			return false;
		},
		
		checkStatus : function(obj)
		{
			if(obj.value == member.editStatusValueText || $(obj).value == $('status_display').innerHTML)
			{
				//obj.value = member.clearStatusValueText;
				$('submit_status').style.display = "none";
				obj.className = 'what-are-you-doing';
			}
			return false;
		},
				
		keyDetect : function(obj, e)
		{
			var keynum;
			var keychar;
			var numcheck;
			
			if(window.event) // IE
			  keynum = e.keyCode;
			else if(e.which) // Netscape/Firefox/Opera
				keynum = e.which;
			if(parseInt(keynum) == 13)
				member.addStatus(obj);
			else if(parseInt(keynum) == 27 || keynum == null)
				memberUIActions.hideStatus(obj);
		},
		
		removeMemberComment : function(obj,comment_public_id)
		{
			if (confirm('Are you sure you wish to delete the comment?'))
				memberAjaxActions.removeMemberComment(obj, comment_public_id);
		},
		
		removeGroupComment : function(obj)
		{
            if (confirm('Are you sure you wish to delete this comment?'))
            {
                var group_public_id = $('current_group').getValue();
                memberAjaxActions.removeGroupComment(obj, group_public_id);
            }
		},
		
		removeDiscussionComment : function(obj)
		{
            if (confirm('Are you sure you wish to delete this post?'))
            {
                var discussion_public_id = $('current_discussion').getValue();
                memberAjaxActions.removeDiscussionComment(obj, discussion_public_id);
            }
		},
		
		removePhotoComment : function(obj)
		{
            if (confirm('Are you sure you wish to delete this comment?'))
            {
                var photo_public_id = $('current_photo').getValue();
                memberAjaxActions.removePhotoComment(obj, photo_public_id);
            }
		},
		
		removeFeedItem : function(obj,comment_public_id)
		{
			//if (confirm('Are you sure you wish to delete the comment?'))
				memberAjaxActions.removeFeedItem(obj, comment_public_id);
		},
		
		postMessage : function(obj)
		{
			Effect.FadeOut(obj.parentNode.parentNode, {
				afterFinish: function() {
					Effect.Appear('#message_holder');
					$('#message_type').getValue('message');
				}
			});
		},
		
		editComment : function(obj)
		{
			memberAjaxActions.getDiscussionComment(obj);
		},
		
		quoteComment : function(obj)
		{
			memberAjaxActions.getDiscussionCommentForQuote(obj);
		},
		
		saveComment : function(obj)
		{
			//var text = $('edit_comment').getValue(); // find the textarea
			var text = tinyMCE.get('edit_comment').getContent(); // find the textarea
			var publicid = $('comment_public_id').getValue(); // get the public id
			memberAjaxActions.updateDiscussionComment(publicid, text); // make the ajax request
		},
		
		cancelComment : function(obj)
		{
			memberUIActions.cancelEditComment(obj);
		},
		
		
		reportPhoto : function(obj, pid)
		{
			var message = window.prompt('Brief description of objection...');
			if(message != null && message != "")
				memberAjaxActions.reportPhoto(obj, pid, message);
		},
		
		reportPhotoComment : function(obj, pid)
		{
			var message = window.prompt('Brief description of objection...');
			if(message != null && message != "")
				memberAjaxActions.reportPhotoComment(obj, pid, message);
		},
		
		reportMemberComment : function(obj, pid)
		{
			var message = window.prompt('Brief description of objection...');
			if(message != null && message != "")
				memberAjaxActions.reportMemberComment(obj, pid, message);
		}
		
	}
	

	
	memberAjaxActions = 
	{
		updateMemberStatus : function(obj, member_username, txt)
		{
			new Ajax.Request('/member/'+member_username+'/status/update?'+(Math.round(Math.random()*10000)),
				{
					method : 'post',
					parameters: {'status_text' :  txt},
					onSuccess: function(data)
					{
						if(data.responseText.match('success'))
							Effect.Fade(obj.parentNode);
						else
							memberUIActions.showError(data.responseText);
					}
				}
			);
			return false;
		},
		
		removeMemberComment : function(obj, comment_publicid)
		{
			new Ajax.Request('/member/comment/remove',
				{
					method : 'get',
					parameters: {'public_id' : comment_publicid },
					onSuccess: function(data)
					{
						if(data.responseText.match('success'))
							new Effect.Parallel([new Effect.BlindUp(obj.parentNode.parentNode.parentNode), new Effect.Fade(obj.parentNode.parentNode.parentNode)],{duration: 1});
						else
							memberUIActions.showError(data.responseText);
					}
				}
			);
			return false;
		},
		
		removeGroupComment : function(obj, group_publicid)
		{
			new Ajax.Request("/group/"+group_publicid+"/comment/remove",
				{
					method : 'get',
					parameters: {'public_id' : memberStringActions.getPublicId(obj.id)},
					onSuccess: function(data){

						if(data.responseText.match('success'))
							new Effect.Parallel([new Effect.BlindUp(obj), new Effect.Fade(obj)],{duration: 1});
						else
						{
							memberUIActions.showError(data.responseText);
						}
					}
				}
			);
			return false;
		},
		
		removeDiscussionComment : function(obj, discussion_publicid)
		{	
			new Ajax.Request("/discussion/"+discussion_publicid+"/comment/remove",
				{
					method : 'get',
					parameters: {'public_id' : memberStringActions.getPublicId(obj.id)},
					onSuccess: function(data){

						if(data.responseText.match('success'))
							new Effect.Parallel([new Effect.BlindUp(obj.parentNode.parentNode), new Effect.Fade(obj.parentNode.parentNode)],{duration: 1});
						else
							memberUIActions.showError(data.responseText);
					}
				}
			);
			return false;
		},
		
		removePhotoComment : function(obj, photo_public_id)
		{
			new Ajax.Request("/photo/"+photo_public_id+"/comment/remove",
				{
					method : 'get',
					parameters: {'public_id' : memberStringActions.getPublicId(obj.id)},
					onSuccess: function(data){

						if(data.responseText.match('success'))
							new Effect.Parallel([new Effect.BlindUp(obj.parentNode), new Effect.Fade(obj.parentNode)],{duration: 1});
						else
							memberUIActions.showError(data.responseText);
					}
				}
			);
			return false;
		},
		
		getDiscussionComment : function(obj)
		{
			var s = "/spapi/discussion/comment/get?"+(Math.round(Math.random()*10000));
			new Ajax.Request(
				s,
				{
					method : 'get',
					parameters: {'public_id' : memberStringActions.getPublicId(obj.id)},
					onSuccess: function(data){
						var j = data.responseJSON;
						if(j.error)
							alert(j.error);
						else
							memberUIActions.showEditComment(j);
					}
				}
			);
			return false;
		},
		
		getDiscussionCommentForQuote : function(obj)
		{
			var s = "/spapi/discussion/comment/get?"+(Math.round(Math.random()*10000));
			new Ajax.Request(
				s,
				{
					method : 'get',
					parameters: {'public_id' : memberStringActions.getPublicId(obj.id)},
					onSuccess: function(data){
						var j = data.responseJSON;
						if(j.error)
							alert(j.error);
						else
							memberUIActions.showAddComment(j);
					}
				}
			);
			return false;
		},
		
		updateDiscussionComment : function(public_id, text)
		{
			var s = "/spapi/discussion/comment/update?"+(Math.round(Math.random()*10000));
            hideDialogs();
			new Ajax.Request(
				s,
				{
					method : 'get',
					parameters: {'public_id' : public_id, 'message' : text},
					onSuccess: function(data){
						var j = data.responseJSON;
						if(j.error)
							alert(j.error);
						else
							memberUIActions.showUpdatedComment(j);
					}
				}
			);
			return false;
		},
		
		
		removeFeedItem : function(obj, comment_publicid)
		{
			new Ajax.Request('/member/feeditemremove',
				{
					method : 'get',
					parameters: {'t' : comment_publicid },
					onSuccess: function(data)
					{
						if(data.responseText.match('success'))
							new Effect.Parallel([new Effect.BlindUp(obj.parentNode.parentNode.parentNode), new Effect.Fade(obj.parentNode.parentNode.parentNode)],{duration: 1});
						else
							memberUIActions.showError(data.responseText);
					}
				}
			);
			return false;
		},
		
		
		reportPhoto : function(obj, public_id, message)
		{
			new Ajax.Request("/photo/report/?"+(Math.round(Math.random()/0.0001)),
				{
					method : 'post',
					parameters: {'public_id' : public_id, 'message' : message},
					onSuccess: function(data){
						var j = data.responseJSON;
						if(j.error)
							alert(j.error);
						else
							memberUIActions.reportPostHandle(obj);
					}
				}
			);
		},
		
		reportPhotoComment : function(obj, public_id, message)
		{
			new Ajax.Request("/photocomment/report/?"+(Math.round(Math.random()/0.0001)),
				{
					method : 'post',
					parameters: {'public_id' : public_id, 'message' : message},
					onSuccess: function(data){
						var j = data.responseJSON;
						if(j.error)
							alert(j.error);
						else
							memberUIActions.reportPostHandle(obj);
					}
				}
			);
		},
		
		reportMemberComment : function(obj, public_id, message)
		{
			new Ajax.Request("/membercomment/report/?"+(Math.round(Math.random()/0.0001)),
				{
					method : 'post',
					parameters: {'public_id' : public_id, 'message' : message},
					onSuccess: function(data){
						var j = data.responseJSON;
						if(j.error)
							alert(j.error);
						else
							memberUIActions.reportPostHandle(obj);
					}
				}
			);
		}
	}
	
	memberUIActions = 
	{		
		hideStatus : function(obj)
		{
			var par = $($(obj).parentNode).parentNode;
			Effect.Fade(par);
		},
		
		showError : function(msg)
		{
			alert("Error: " + msg);
		},
		
		showEditComment : function(json)
		{
			//var my_textarea = $('edit_comment');
			//my_textarea.innerHTML = '<!--'+json.message+'-->';
			//my_textarea.value = json.message;
			
			tinyMCE.get('edit_comment').setContent(json.message);

			var hid = $('comment_public_id');
			hid.value = json.publicid;

			/*
			var publicid = json.publicid;
			var com = 'comment_'+publicid;
			
			var my_div = document.createElement('div'); // create the div
			
			var com_edit = 'commentedit_'+publicid; // create the id
			my_div.setAttribute('id', com_edit); // set the id			
			my_div.style.display = "none";
			
			var my_textarea = document.createElement('textarea'); // create the textarea
			my_textarea.innerHTML = json.message;
			my_textarea.value = json.message;
			$(my_textarea).className = 'editable';
			
			var my_savebutton = document.createElement('span');
			my_savebutton.className = "save";
			$(my_savebutton).observe('click', function(){member.saveComment(this)});
			my_savebutton.innerHTML = "save";
			
			var my_cancelbutton = document.createElement('span');
			my_cancelbutton.className = "cancel";
			$(my_cancelbutton).observe('click', function(){member.cancelComment(this)});
			my_cancelbutton.innerHTML = "cancel";
			
			var my_spc = document.createElement('div'); // create the div
			my_spc.className = "spc";
			
			my_div.appendChild(my_savebutton); // add the submit button to the div
			my_div.appendChild(my_cancelbutton); // add the submit button to the div
			my_div.appendChild(my_textarea); // add the textarea to the div
			my_div.appendChild(my_spc); // add the spc to the div
			
			
			new Insertion.After(com, my_div);
			
			Effect.BlindUp(com, {
				afterFinish : function(){
					Effect.BlindDown(my_div);
					//$(my_div).blindDown();
				}
			}); // ie fix
			*/
		},
		
		showAddComment : function(json)
		{
			//var my_textarea = $('add_comment');
			//my_textarea.innerHTML = '<!--'+json.message+'-->';
			//my_textarea.value = json.message;
			
			tinyMCE.get('add_comment').setContent(json.author + ' said:<blockquote>' + json.message + '</blockquote><p></p>');
		},
		
		showUpdatedComment : function(json)
		{
			//var par_id = "comment_"+json.publicid;
			var par_id = "comment_"+json.publicid;
			
			var par = $(par_id);
			
			var arr = $(par).getElementsBySelector('span');
			arr[0].innerHTML = json.message;		
            new Effect.Highlight($(par).parentNode.parentNode);
        },
		
		cancelEditComment : function(obj)
		{
			var par = $(obj).parentNode;
			var publicid = 'comment_'+memberStringActions.getPublicId(par.id);
			var com = $(publicid);
			
			Effect.BlindUp(par, {
				afterFinish: function() {
					$(par).remove;
					Effect.BlindDown(com);
				}
			}); // ie fix
		},
		
		removeItem : function(obj)
		{
			Effect.BlindUp(obj, {
				afterFinish: function() {
					$(obj).remove;
				}
			}); // ie fix
		},
		
		reportPostHandle : function(obj)
		{
			alert("your report has been successfully submitted");
			Effect.Fade(obj);
		}
	}
	
	memberStringActions = 
	{
		getPublicId : function(str)
		{
			var i = str.lastIndexOf("_");
			return str.substr((i+1), str.length);
		}
	}
	
	
	ServiceHistory = 
	{
		deleteItem : function(obj, username)
		{
			var public_id = $(obj.parentNode).getInputs('hidden','publicid')[0].value;
			if(confirm('Are you sure you wish to delete this service item?'))
			{
				new Ajax.Request("/member/"+username+"/servicehistory/remove?"+(Math.round(Math.random()/0.0001)),
					{
						method : 'post',
						parameters: {'public_id' : public_id},
						onSuccess: function(data){
							var j = data.responseJSON;
							if(data.responseText.match('success'))
							{
								var theBlock = obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
								new Effect.Parallel([new Effect.BlindUp(theBlock), new Effect.Fade(theBlock)],{duration: 1});
							}
							else
								memberUIActions.showError(data.responseText);
								
						}
					}
				);
			}
		},
		
		editItem : function(obj, username)
		{	
			resetForm('career');
			var src = $(obj.parentNode).getInputs('hidden');
			$('group_row').style.display='block';
			for(var i=0; i<src.length;i++)
			{
				switch (src[i].name)
				{
					case 'publicid' :
						$('entry_publicid').value = src[i].value;
					break;
					case 'type' :
						$$('#entry_type option[value='+src[i].value+']')[0].selected=true;
						$('entry_text').innerHTML = $$('#entry_type option[value='+src[i].value+']')[0].innerHTML + ":";
					break;
					case 'group_publicid' :
						$('group_publicid_editform').value = src[i].value;
					break;
					case 'group_name' :
						$('group_autocomplete').value = src[i].value;
					break;
					case 'date_from' :
						$$('#entry_from_date option[value='+src[i].value+']')[0].selected=true;
					break;
					case 'date_to' :
						$$('#entry_to_date option[value='+src[i].value+']')[0].selected=true;
					break;
					case 'job' :
						$('entry_job').value = src[i].value;
					break;
				}
			}
			showDialog('career');
		},
		
		showEntry : function(obj) 
		{
			if ($('entry_type').selectedIndex == 0)
				new Effect.Parallel([new Effect.BlindUp('group_row'),new Effect.Fade('group_row')],{duration:0.3});
			else
			{
				$('group_autocomplete').value = '';
				$('entry_text').innerHTML = $('entry_type').options[$('entry_type').selectedIndex].innerHTML+":";
				if ($('group_row').style.display == 'none')
					new Effect.Parallel([new Effect.BlindDown('group_row'),new Effect.Appear('group_row')],{duration:0.3});
			}
		}
	
	}
	
	
		
function resetForm(theDialog)
{
	if ($('entry_errors'))
		$('entry_errors').style.display = 'none';
	if ($('group_row'))
		$('group_row').style.display = 'none';	
		
		
	var src = $('dialog_'+theDialog).getElementsByTagName('input');
	for(var i=0; i<src.length;i++)
	{ 
		if (src[i].type == 'text' || src[i].type == 'hidden' )
			src[i].value = '';
	}	
	var src = $('dialog_'+theDialog).getElementsByTagName('textarea');
	for(var i=0; i<src.length;i++)
		src[i].value = '';

	var src = $('dialog_'+theDialog).getElementsByTagName('select');
	for(var i=0; i<src.length;i++)	
		src[i].options[0].selected = true;	
}
	
function showDialog(theDialog)
{
	if ($('dialog_'+theDialog).style.display == 'none')
	{
		new Effect.Appear('screen',{duration:0.6});
		new Effect.Appear('dialog_'+theDialog,{duration:0.2});
	}
	return false;
}

function hideDialogs()
{
	if ($('screen').style.display != 'none')
	{
		new Effect.Fade('screen',{duration:0.6});
		var ddls = $$('div.dialog');
		for(var i=0;i<ddls.length;i++)
			new Effect.Fade(ddls[i],{duration:0.2});
	}
	return false;
}



