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);
	},

    updateCommentsCountOnPage: function( count ){
        if( count == 0 )
        {
            count = parseInt($('input[name=commentCountOnPage]', this.$cmpContext).val()) + 1;
        }
        $('input[name=commentCountOnPage]', this.$cmpContext).val(count);
    }
};

var OwCommentsList = function( params ){
	this.$context = $('#' + params.contextId);
	$.extend(this, params);
}

OwCommentsList.prototype = {
	init: function(){
		var self = this;

		//Js event trigger
		OW.trigger('base.comments_list_init', {entityType: this.entityType, entityId: this.entityId}, 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: 'cid='+self.cid+'&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]);
                }
            );
        }

        if( this.displayType == 3 || this.displayType == 4 )
        {
            $('.comments_view_all a', this.$context).one('click',
                function(){
                    $(this).replaceWith('<img src="'+self.preloaderImgUrl+'" alt="" />');
                    self.commentCountOnPage = 1000;
                    if( window.commentCmps && window.commentCmps[self.cid]  )
                    {
                        window.commentCmps[self.cid].updateCommentsCountOnPage(1000);
                    }
                    self.reload(1);
                }
            );

        }
	},

	reload:function( page ){
		var self = this;
		$.ajax({
            type: 'POST',
            url: self.respondUrl,
            data: 'cid='+self.cid+'&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;
            }
        });
	}
}
