var OwComments = function( contextId, formName ){
	this.formName = formName;
	this.$cmpContext = $('#' + contextId);
}

OwComments.prototype = {
	repaintCommentsList: function( data ){
		owForms[this.formName].getElement('commentText').resetValue();
                
		if(data.error){
			OW.error(data.error);
			return;
		}
		$('.comments_list_cont', this.$cmpContext).empty().append($(data.commentList));
		OW.addScript(data.onloadScript);
	}
};

var OwCommentsList = function( params ){
	this.$context = $('#' + params.contextId);
	$.extend(this, params);
}

OwCommentsList.prototype = {
	init: function(){
		var self = this;

        if( this.pagesCount > 0 )
        {
            for( var i = 1; i <= this.pagesCount; i++ )
            {
                $('a.page-'+i, self.$context).bind( 'click', {i:i},
                    function(event){
                        self.reload(event.data.i);
                    }
                );
            }
        }
       
        for( var i = 0; i <= this.commentIds.length; i++ )
        {   
            $('#del-'+this.commentIds[i]).bind( 'click', {i:i},
                function(e){
                    if( confirm(self.delConfirmMsg) )
                    {
                        $.ajax({
                            type: 'POST',
                            url: self.delUrl,
                            data: 'commentCountOnPage='+self.commentCountOnPage+'&ownerId='+self.ownerId+'&pluginKey='+self.pluginKey+'&displayType='+self.displayType+'&entityType='+self.entityType+'&entityId='+self.entityId+'&page='+self.page + '&commentId=' + self.commentIds[e.data.i],
                            dataType: 'json',
                            success : function(data){
                                if(data.error){
                                        OW.error(data.error);
                                        return;
                                }

                                self.$context.replaceWith(data.commentList);
                                OW.addScript(data.onloadScript);
                            },
                            error : function( XMLHttpRequest, textStatus, errorThrown ){
                                alert('Ajax Error: '+textStatus+'!');
                                throw textStatus;
                            }
                        });
                    }
                }
             );

            $('#flag-'+this.commentIds[i]).bind( 'click', {i:i},
                function(e){
                    alert(self.commentIds[e.data.i]);
                }
            );
        }
	},

	reload:function( page ){
		var self = this;
		$.ajax({
            type: 'POST',
            url: self.respondUrl,
            data: 'commentCountOnPage='+self.commentCountOnPage+'&ownerId='+self.ownerId+'&pluginKey='+self.pluginKey+'&displayType='+self.displayType+'&entityType='+self.entityType+'&entityId='+self.entityId+'&page='+page,
            dataType: 'json',
            success : function(data){
               if(data.error){
                        OW.error(data.error);
                        return;
                }


                self.$context.replaceWith(data.commentList);
                OW.addScript(data.onloadScript);
            },
            error : function( XMLHttpRequest, textStatus, errorThrown ){
                OW.error('Ajax Error: '+textStatus+'!');
                throw textStatus;
            }
        });
	}
}
