/**
 * @author iexx
 */
function comments(){

	this.text_align   	 = 'top';
	this.commentsForms   = new Array();
	this.commentsFormsIt = 0;
    this.controller      = 'posts';
	
	
	this.process_createComment = function(formObject) {
		
		try {
		
			var currentClass = this;

			var responseBlock   = $('response_' + formObject.id);

			responseBlock.update('<img src="/images/ajax-loader.gif" />');
					
			responseBlock.show();
			
			var myAjax = new Ajax.Request('/' + this.controller + '/addcomment', {method: 'post', parameters: $(formObject).serialize(),
	      
		      onSuccess: function(transport) {
			  	
		        var response = transport.responseText.evalJSON() || false;
				
				if(response.error) {
					responseBlock.update('<div class="innerError">' + response.error + '</div>');
				}else {
					
	                responseBlock.update('<div class="innerDone">Комментарий сохранён</div>');
	                
	                // Edit
	                if(response.is_edit) {
	                    $('comment_content_' + response.done).update(response.HTML_Content);
	                // Create    
	                }else {

	                    // Show Comment and hide form
	                    var MainObj = $(currentClass.controller + '_CommentsBlock');
	                    var eeDiv = new Element('div').update('this is a test');
	                   
	                    eeDiv.update(response.HTML_Content);
	                    
	                    if(formObject.id == 'comments_form_0') {
	                    
	                        try {
	                            MainObj.insertBefore(eeDiv.childNodes[1], MainObj.lastChild);
	                        }catch(e) {alert(e)}
	                        
	                    }else {
	                        try {
	                            MainObj.insertBefore(eeDiv.childNodes[1], formObject.parentNode);
	                        }catch(e) {alert(e)}   
	                    }
	                }
	                    
	                currentClass.destroyOpenedForms();
	                $('comments_form_0').hide();

	                setTimeout(function(){
							responseBlock.update();
						}, 2000);
				}
				
				// Update captcha
				$$('div.captcha_image').each(function(item){
						item.update('');
						
						var idHTML    = new Element('input', {type: 'hidden', name: 'cappptcha[id]', value: response.captcha.id});
						var imageHTML = new Element('img', {src: response.captcha.src});
						
						item.appendChild(idHTML);
						item.appendChild(imageHTML);
					}
				);
				
		      }
			});
        }catch(e) {alert('final' + e)}
	}
	
	this.destroyOpenedForms = function() {
    
        this.commentsForms.each(function(formIntID){
			$('comments_form_' + formIntID).parentNode.parentNode.removeChild($('comments_form_' + formIntID).parentNode);
		});
        
        this.commentsForms = new Array();
    }

	this.answer = function(commentID, is_quote) {
		
        this.destroyOpenedForms();
        
		//$('comments_form_0').hide();
        
		this.commentsFormsIt++;
		
		var CommentFormID = 'comments_form_' + this.commentsFormsIt;
        
        this.commentsForms.push(this.commentsFormsIt);
        
		var block4update  = this.controller + '_CommentsBlock';
		
		var copyForm = Object.clone($('comments_form_0'));
		
		copyForm.innerHTML += '<input type="hidden" name="parent_id"    value="' + commentID + '"/>';
		copyForm.innerHTML += '<input type="hidden" name="parent_level" value="' + $('comment_' + commentID + '_level').innerHTML + '"/>';
		
		var eForm = new Element('form', {method: 'POST', id: CommentFormID});
		eForm.onsubmit = function() {try {commentsObj.process_createComment(this)}catch(e){alert(e)}return false;};
		
		eForm.update(copyForm.innerHTML);
		
		var eDivName = new Element('div', {class: 'commentCreateForm'});
		eDivName.appendChild(eForm);
        
		$(block4update).insertBefore(eDivName, $('comment_' + commentID).nextSibling);
		
		$(CommentFormID).childNodes[1].id = 'response_' + CommentFormID;
		$(CommentFormID).childNodes[1].innerHTML = '';
		
		if(is_quote) {
			eForm.vcontent.innerHTML = '<blockquote>' + $('comment_content_' + commentID).innerHTML + '</blockquote>'
		}
        if(copyForm.name) {
            //copyForm.name.focus();
        }else {
            //copyForm.vcontent.focus();
        }
		
		return;
	}
	
	
	this.edit = function(commentID) {
		
        this.destroyOpenedForms();
        
		//$('comments_form_0').hide();
        
		this.commentsFormsIt++;
		
		var CommentFormID = 'comments_form_' + this.commentsFormsIt;
        
        this.commentsForms.push(this.commentsFormsIt);
        
		var block4update  = this.controller + '_CommentsBlock';
		
		var copyForm = Object.clone($('comments_form_0'));
		
		copyForm.innerHTML += '<input type="hidden" name="comment_id" value="' + commentID + '"/>';
		
		var eForm = new Element('form', {method: 'POST', id: CommentFormID});
		eForm.onsubmit = function() {try {commentsObj.process_createComment(this)}catch(e){alert(e)}return false;};
		
		eForm.update(copyForm.innerHTML);
		
		var eDivName = new Element('div', {class: 'commentCreateForm'});
		eDivName.appendChild(eForm);
		
		$(block4update).insertBefore(eDivName, $('comment_' + commentID).nextSibling);
		
		$(CommentFormID).childNodes[1].id = 'response_' + CommentFormID;
		$(CommentFormID).childNodes[1].innerHTML = '';
		
		eForm.vcontent.innerHTML = $('comment_content_' + commentID).innerHTML;
		
		$(block4update).show();
		
        if(copyForm.name) {
            //copyForm.name.focus();
        }else {
            //copyForm.vcontent.focus();
        }
		
		return;
	}
	
	
	this.remove = function(commentID) {
		
		var commentBlock = $('comment_' + commentID);
		
		var myAjax = new Ajax.Request('/' + this.controller + '/removecomment/commentID/' + commentID, {method: 'get',
      
	      onSuccess: function(transport) {
		  	
	        var response = transport.responseText.evalJSON() || false;
			
			if(response.error) {
				alert(response.error);
			}else {
				commentBlock.parentNode.removeChild(commentBlock);
			}
	      }
		});
	}
	
	
	
	this.rate = function(commentID, buttonObject) {
		
		var rateValue = $F(buttonObject);
		
		if(rateValue == '+') {
			rateValue = 1;
		}else {
			rateValue = -1;
		}
		
		buttonObject.value = 'please wait';
		
		var myAjax = new Ajax.Request('/' + this.controller + '/ratecomment/commentID/' + commentID + '/rate/' + rateValue, {method: 'get',
      
	      onSuccess: function(transport) {
		  	
	        var response = transport.responseText.evalJSON() || false;
			
			if(response.error) {
				buttonObject.value = 'failed';
			}else {
				
				var CommentRateValue = 0;
				
				if(rateValue > 0) {
					CommentRateValue = parseInt($('comment_rate_' + commentID).innerHTML) + 1;
				}else{
					CommentRateValue = parseInt($('comment_rate_' + commentID).innerHTML) - 1;					
				}
				
				$('comment_rate_' + commentID).update(CommentRateValue);
				
				if(CommentRateValue > 0) {
					$('comment_rate_' + commentID).className = 'green';
				}else if(CommentRateValue < 0) {
					$('comment_rate_' + commentID).className = 'red';					
				}else {
					$('comment_rate_' + commentID).className = 'gray';					
				}
				buttonObject.value = 'Готово';
			}
	      }
		});
	}
}