bebo.ui.LifestreamCommentHandler = new Class( {
	initialize : function(commentFormDivId, commentLinkClass, commentItemDivIdBegin) {
		this.commentFormDiv = $(commentFormDivId);
		this.allowJump = true;
		this.changesItemOwnerId = '';
		this.changesItemTime = '';
		this.changesItemType = '';
		this.commentLinkClass = commentLinkClass;
		this.lastWriteToggle = null;
		this.commentItemDivIdBegin = commentItemDivIdBegin;
		this.luvAdded = false;
		this.luvToggle = $('luv_toggle');
		this.luvLeftText = $('luv_left_text');
		this.luvLeft = $('luv_left');
		this.luvSpan = $('luv_span');
		this.numLuvs = 0;
		this.smackAdded = false;
		this.smackToggle = $('smack_toggle');
		this.smackSpan = $('smack_span');		
		if (this.commentFormDiv != null)
		{
			this.setEventHandlers(commentLinkClass);
		}
	},
	setEventHandlers : function(commentLinkClass) {
		this.commentForm = this.commentFormDiv.getElement('form');
		this.commentErrors = this.commentFormDiv.getElement('div.errors-wrapper');
		this.commentErrorsList = this.commentErrors.getElement("ul");
		var postCommentButton = this.commentFormDiv.getElement("input[type=submit]");
		this.commentText = this.commentFormDiv.getElement("textarea");
		this.countfield = this.commentFormDiv.getElement("span.char-counter em");

		if (postCommentButton != null && this.commentText != null && this.countfield != null)
		{
			this.commentFormDiv.setStyle('display','none');
			postCommentButton.addEvent('click', function(e) {
				e.preventDefault();
				this.postComment();
			}.bind(this));
			this.commentText.addEvent('keyup', function(e) {
				e.preventDefault();
				this.textCounter(this.commentText, this.countfield);
			}.bind(this));
			$$('a.'+commentLinkClass).each(function (myLink) {
				myLink.addEvent('click', function(e) {
					e.preventDefault();
					this.moveCommentForm(myLink);
				}.bind(this));
			}.bind(this));
			$$('a.more-comments-toggle').each(function (myLink) {
				myLink.addEvent('click', function(e) {
					e.preventDefault();
					this.showMoreComments(myLink);
				}.bind(this));
			}.bind(this));			
			$$('input.write-comment-toggle').each(function (myInput) {
				myInput.addEvent('focus', function(e) {
					e.preventDefault();
					this.toggleWriteComment(myInput);
				}.bind(this));				
			}.bind(this));	
			$$('span.delete-comment a').each(function (myLink) {
				myLink.addEvent('click', function(e) {
					e.preventDefault();
					this.deleteCommentConfirm(myLink);
				}.bind(this));				
			}.bind(this));					
			if (this.luvToggle)
			{
				this.numLuvs = parseInt(this.luvLeft.get('text'));
				this.luvToggle.addEvent('click', function(e) {
					e.preventDefault();
					this.toggleLuv();
				}.bind(this));
			}
			if (this.smackToggle)
			{
				this.smackToggle.addEvent('click', function(e) {
					e.preventDefault();
					this.toggleSmack();
				}.bind(this));
			}				
		}			
	},
	moveCommentForm : function(myLink) {
		if (this.allowJump)
		{
			var linkInfo = myLink.get('id').split('-');
			var commentDiv = $(this.commentItemDivIdBegin+'-'+linkInfo[2]+'-'+linkInfo[3]+'-'+linkInfo[4]);
			this.changesItemOwnerId = linkInfo[2];
			this.changesItemTime = linkInfo[3];
			this.changesItemType = linkInfo[4];
			commentDiv.adopt(this.commentFormDiv, 'bottom');
			this.commentFormDiv.setStyle('display', 'block');	
			if (this.lastWriteToggle && 
				this.lastWriteToggle.get('id') != writeToggleId)
		    {
				var parentLi = this.lastWriteToggle.getParent('li');
				if (!parentLi.hasClass("write-comment-off"))
				{
					parentLi.setStyle('display',null);	
				}
			}
			var writeToggleId = 'write-comment'+'-'+linkInfo[2]+'-'+linkInfo[3]+'-'+linkInfo[4];
			var writeToggleElem = $(writeToggleId);			
			if (writeToggleElem)
			{
				writeToggleElem.getParent('li').setStyle('display','none');	
				this.lastWriteToggle = writeToggleElem;
			}
			this.commentText.focus();
			this.reset();	
		}
	},
	showMoreComments : function(myLink, commentLinkClass) {
		var showMoreList = myLink.getParent('ul').getElements('li.more-comments');
		showMoreList.each(function (elem) {
			elem.setStyle('display',null);
		});
		myLink.getParent('li').setStyle('display','none');
	},	
	toggleWriteComment : function(myInput) {
		if (this.allowJump)
		{
			this.moveCommentForm(myInput);
		}
	},		
	postComment : function() {
	   var commentThrobber = new LifestreamCommentThrobber();
	   new Request.JSON( {
	        url: '/c/home/ajax_post_lifestream_comment',
	        data : {
	     	    Message: this.commentText.get('value'),
	     	    ChangesItemOwnerId: this.changesItemOwnerId,
	     	    ChangesItemTime: this.changesItemTime,
	     	    ChangesItemType: this.changesItemType,
	     	    LuvInd: this.luvAdded ? 'Y' : 'N',
	     	    SmackTalkInd: this.smackAdded ? 'Y' : 'N'
	        },
	        onComplete : function(json) {	
		   		  commentThrobber.dispose();
		   		  this.commentForm.setStyle('display','block');
		 	   	  if (json.message != 'error')
		 	   	  {
	   	   			  var newComment = new Element('li');
	   	   			  newComment.addClass('actual-comment')
	   	   			  newComment.set('html',json.data.comment_html);
	   	   			  var commentList = this.commentFormDiv.getPrevious('ul.comment-list');
	 	   		      newComment.inject(commentList, 'bottom');
	 	   		      var writeCommentElem = commentList.getLast('li.write-comment')
	 	   		      if (writeCommentElem)
	 	   		      {
	 	   		    	  commentList.adopt(writeCommentElem, 'bottom');
	 	   		    	  writeCommentElem.setStyle('display',null);
	 	   		    	  writeCommentElem.removeClass('write-comment-off');
	 	   		      }
	 	 			  this.commentFormDiv.setStyle('display', 'none');
	 	 			  var numLuvs = json.data.numLuvs;
	 	 			  if (numLuvs != null && this.luvToggle)
	 	 			  {
	 	 				  this.luvLeft.set('text', numLuvs);
	 	 				  this.numLuvs = parseInt(numLuvs);
	 	 				  this.showLuvIfLeft();
	 	 			  }
	 	 			  var commentSizeSpan = commentList.getElement('li span.comment-size');
	 	 			  if (commentSizeSpan)
	 	 			  {
	 	 				 var oldSize = parseInt(commentSizeSpan.get('text'));
	   	                 commentSizeSpan.set('text',oldSize+1);
	 	 			  }
	 	 			  var deleteLink = newComment.getElement('span.delete-comment a');
					  deleteLink.addEvent('click', function(e) {
							e.preventDefault();
							this.deleteCommentConfirm(deleteLink);
					  }.bind(this));				
		 	   	  }
		 	   	  else
		 	   	  {
					  this.addErrors(json.data.errors);
		 	   	  }
	        	  this.allowJump = true;
	        }.bind(this),
	        onRequest : function() {
	        	 this.allowJump = false;
		     	 this.reset();
		     	 this.commentForm.setStyle('display','none');
		     	 commentThrobber.display(this.commentFormDiv);	        	
	        }.bind(this)
	   	}).send();			
	},
	deleteCommentConfirm : function(myLink) {
    	var confirmBox = new bebo.ui.ConfirmationBox({ 
    		message : $I.transf('Are you sure you want to delete this comment?'),
    		position : 'relative',
    		title : $I.transf('Delete Comment'),
    		onOk : function(e) {
				this.deleteComment(myLink, confirmBox);
			}.bind(this)
    	});
		confirmBox.show();		
	},
	deleteComment : function(myLink, confirmBox) {
		   var linkInfo = myLink.get('id').split('-');
		   var commentThrobber = new LifestreamCommentThrobber();
		   new Request.JSON( {
		        url: '/c/home/ajax_delete_lifestream_comment',
		        data : {
		     	    ChangesItemOwnerId: linkInfo[2],
		     	    ChangesItemTime: linkInfo[3],
		     	    ChangesItemType: linkInfo[4],
		     	    CommentTime: linkInfo[5],
		     	    CommentMemberId : linkInfo[6]
		        },
		        onComplete : function(json) {	
			   		  commentThrobber.dispose();
			   		  this.commentForm.setStyle('display','block');
			 	   	  if (json.message != 'error')
			 	   	  {
			 	   		  confirmBox.close();
		   	   			  var commentList = myLink.getParent('ul.comment-list');  
			 	   		  myLink.getParent('li').dispose();
			 	   		  // reduce num comments
		 	 			  var commentSizeSpan = commentList.getElement('li span.comment-size');
		 	 			  if (commentSizeSpan)
		 	 			  {
		 	 				 var oldSize = parseInt(commentSizeSpan.get('text'));
		   	                 commentSizeSpan.set('text',oldSize-1);
		 	 			  }		
		 	 			  var actualComments = commentList.getElements('li.actual-comment');
		 	 			  if (actualComments != null && actualComments.length == 0)
		 	 			  {
		 	 				  commentList.set('html','');
		 	 			  }
			 	   	  }
			 	   	  else
			 	   	  {
			 	   		confirmBox.updateMessageCloseButton($I.transf("There was a problem with your request. Please try again later."));
			 	   	  }
		        	  this.allowJump = true;
		        }.bind(this),
		        onRequest : function() {
		        	confirmBox.updateMessageNoButtons($I.transf("Processing..."));     	
		        }.bind(this)
		   	}).send();			
		},	
	reset : function() {
		this.countfield.set('html',1000);
		this.commentText.set('value','');
		this.commentErrorsList.set('html','');
		this.commentErrors.setStyle('display', 'none');
		this.luvAdded = true;
		this.toggleLuv(); // will reset luvAdded to false
		this.smackAdded = true;
		this.toggleSmack();
	},
	addErrors : function(errors) {
		for (var i=0; i<errors.length; i++)
		{	
			new Element('li', {'html': errors[i]}).inject(this.commentErrorsList,'bottom');
		}
		this.commentErrors.setStyle('display', 'block');
	},
	textCounter : function (field,countfield) {
	   var value = field.get('value');
	   var maxlimit = 1000;
	   if (value.length >= maxlimit) 
	   {
	      field.set('value', value.substring(0,maxlimit));
	      alert($I.transf('You may only post up to 1000 characters.'));
	      countfield.set('html', 0);
	      return false;
	   } 
	   else 
	   {
	      countfield.set('html', maxlimit-value.length);
	   }
	},
	toggleLuv : function() {
		if (this.luvToggle)
		{
			if (this.luvAdded)
			{
				this.luvAdded = false;
				this.luvToggle.set('text', $I.transf('Add Luv'));
				this.luvLeftText.setStyle('display',null);		
				this.showSmackIfGame();
				this.showLuvIfLeft();
			}
			else
			{
				this.luvAdded = true;
				this.luvToggle.set('text', $I.transf('Remove Luv'));
				this.luvLeftText.setStyle('display', 'none');
				this.smackSpan.setStyle('display', 'none');
			}
		}
	},
	toggleSmack : function() {
		if (this.smackToggle)
		{
			if (this.smackAdded)
			{
				this.smackAdded = false;
				this.smackToggle.set('text', $I.transf('Add Smack Talk'));		
				this.showSmackIfGame();	
				this.showLuvIfLeft();			
			}
			else
			{
				this.smackAdded = true;
				this.smackToggle.set('text', $I.transf('Remove Smack Talk'));
				if (this.luvSpan)
				{
					this.luvSpan.setStyle('display', 'none');
				}
			}
		}
	}, 
	showSmackIfGame : function() {
		var parentDiv = this.commentFormDiv.getParent('div.'+this.commentItemDivIdBegin);
		if (parentDiv.hasClass('isgame-Y'))
		{
			this.smackSpan.setStyle('display', null);
		}
		else
		{
			this.smackSpan.setStyle('display', 'none');
		}
	},
	showLuvIfLeft : function() {
		  var parentDiv = this.commentFormDiv.getParent('div.'+this.commentItemDivIdBegin);
	      if (this.luvSpan)
	      {
			  if (this.numLuvs <= 0 || parentDiv.hasClass('isme-Y'))
		      {
				  this.luvSpan.setStyle('display','none');
			  }	
			  else
			  {
				  this.luvSpan.setStyle('display',null);			  
			  }
	      }
	}
});

var LifestreamCommentThrobber = new Class( {
	initialize: function() {
		this.throbber = new Element('img', {
			src :'/img/throbber_default.gif',
			id :'update-throbber'
	
		});		
    },
    display: function(target) {
		this.throbber.inject(target);
		this.throbber.position( {
			relativeTo : target, // centered over a target 
			position :"center"
		});    	
    },
	dispose: function () {
    	this.throbber.dispose();
    }
});