/*
---------------------------------------------------------
*
*	Module "Event/Place"
*	Author: Alexander Zinchuk ( Ajaxy ) 
*	Contact e-mail: hey@ajaxy.ru
*
---------------------------------------------------------
*/

window.addEvent('domready', function() {
  lsCmtTree.responseNewComment = lsCmtTreeBill.responseNewComment;
  lsCmtTree.addComment = lsCmtTreeBill.addComment;
  lsCmtTree.toggleComment = lsCmtTreeBill.toggleComment;
});

lsCmtTreeBill = {

	responseNewComment: function(actId,objImg,selfIdComment,bNotFlushNew) {
		var thisObj=this;		
		
		if (!bNotFlushNew) {
			var aDivComments=$$('.comment');
			aDivComments.each(function(item,index){
				var divContent=item.getChildren('div.content')[0];
				if (divContent) {
					divContent.removeClass('new');
					divContent.removeClass('view');
				}
			});
		}
		
		var idCommentLast=this.idCommentLast;
		objImg=$(objImg);
		objImg.setProperty('src',DIR_STATIC_SKIN+'/images/update_act.gif');	
		(function(){		
		JsHttpRequest.query(
        	DIR_WEB_ROOT+'/include/ajax/bill_commentResponse.php',
        	{ idCommentLast: idCommentLast, act: thisObj.actionType, actId: actId },
        	function(result, errors) {        		
        		objImg.setProperty('src',DIR_STATIC_SKIN+'/images/update.gif'); 
            	if (!result) {
                	msgErrorBox.alert('Error','Please try again later');           
        		}      
        		if (result.bStateError) {
                	msgErrorBox.alert(result.sMsgTitle,result.sMsg);
        		} else {   
        			var aCmt=result.aComments;         			
        			if (aCmt.length>0 && result.iMaxIdComment) {
        				thisObj.setIdCommentLast(result.iMaxIdComment);
        				var countComments=$('count-comments');
        				countComments.set('text',parseInt(countComments.get('text'))+aCmt.length);
        				if ($('block_stream_comment') && lsBlockStream) {
        					lsBlockStream.toggle($('block_stream_comment'),'comment_stream');
        				}
        			}        	
        			var iCountOld=0;
        			if (bNotFlushNew) {		      	       			       			
        				iCountOld=thisObj.countNewComment;        				
        			} else {
        				thisObj.aCommentNew=[];
        			}
        			if (selfIdComment) {
        				thisObj.setCountNewComment(aCmt.length-1+iCountOld);
                $('form_comment_text').value = '';
        				thisObj.hideCommentForm(thisObj.iCurrentShowFormComment); 
        			} else {
        				thisObj.setCountNewComment(aCmt.length+iCountOld);
        			}        			
        			aCmt.each(function(item,index) {   
        				if (!(selfIdComment && selfIdComment==item.id)) {
        					thisObj.aCommentNew.extend([item.id]);
        				}        				 				
        				thisObj.injectComment(item.idParent,item.id,item.html);
        			}); 
        			
        			if (selfIdComment && $('comment_id_'+selfIdComment)) {
						thisObj.scrollToComment(selfIdComment);
					}
        		}                           
	        },
        	true
       );
       }).delay(1000);
	},
  
	addComment: function(formObj,actId) {
		var thisObj=this;
    
      		p = {};
          $(formObj).getElements('*').each(function(s){
            p[s.name] = s.value;
          });
          
		JsHttpRequest.query(
      DIR_WEB_ROOT+'/include/ajax/bill_commentAdd.php',
      (new Hash({ act: thisObj.actionType })).extend(p),
      function(result, errors) {
        if (!result) {
          thisObj.enableFormComment();
          msgErrorBox.alert('Error','Please try again later');   
        }    
        else if (result.bStateError) {        			
          thisObj.enableFormComment();        			
          msgErrorBox.alert(result.sMsgTitle,result.sMsg);
        } else {       
          $('form_comment_text').disabled=true;
          thisObj.responseNewComment(actId,$('update-comments'),result.sCommentId,true);        			   								
        }                           
      },
      true
    );
    $('form_comment_text').addClass('loader');		
	},
	
	toggleComment: function(obj,commentId) {
		var divContent=$('comment_content_id_'+commentId);
		if (!divContent) {
			return false;
		}
		
		var thisObj=this;			
		JsHttpRequest.query(
        	DIR_WEB_ROOT+'/include/ajax/bill_commentToggle.php',
        	{ idComment: commentId, act: thisObj.actionType },
        	function(result, errors) {         		 
            	if (!result) {
                	msgErrorBox.alert('Error','Please try again later');           
        		}      
        		if (result.bStateError) {        			
                	msgErrorBox.alert(result.sMsgTitle,result.sMsg);
        		} else {   
        			msgNoticeBox.alert(result.sMsgTitle,result.sMsg);     			
        			divContent.removeClass('old').removeClass('self').removeClass('new').removeClass('del');
        			obj.removeClass('delete').removeClass('repair');
        			if (result.bState) {
        				divContent.addClass('del');
        				obj.addClass('repair');
        			} else {
        				obj.addClass('delete');
        			}
					obj.set('text',result.sTextToggle);        			        								
        		}                           
	        },
        	true
       );
	}
};


