Module_Plugin_Comments = function(preset_id, cid, category) {
	this.init(preset_id, cid, category);
}

//static get main item
Module_Plugin_Comments.TIME_EFFECT = 300;
Module_Plugin_Comments.__dict = {};
Module_Plugin_Comments.gm = function(preset_id, cid, category) {
	if( typeof category !== 'undefined' && category !== false)
    	var key = cid + '_' + category;
	else
		var key = cid;
	if (Module_Plugin_Comments.__dict[key]) {
		return Module_Plugin_Comments.__dict[key];
	}
	
	var plugin = new Module_Plugin_Comments(preset_id, cid, category);
	
	Module_Plugin_Comments.__dict[cid] = plugin;
	return plugin;
}

Module_Plugin_Comments.prototype = {
	cid: null,
	preset_id: null,
	current_page: null,
	
	init: function(preset_id, cid, category ) {
		this.cid = cid;
		this.preset_id = preset_id;
		this.category = category;
		this.current_page = 1;
	},	
	getMain: function() {
		if(typeof this.category !== undefined && this.category !== false)
			return $('.m_plugin_comment_'+this.cid+'_'+this.category);
		else
			return $('.m_plugin_comment_'+this.cid);
	},
	
	showDetailComments: function() {
		this.getMain().find('.commentbox').hide();
		this.getMain().find('.commentbrief-link').hide();
		this.getMain().find('.commentbox-detail').show();
	},
	
	removeComment: function(event, element, comment_id) {
		var top = $(element).offset().top;
		
		Enjin_Core.stopBubble(event);
		Enjin_Core.showPopup({
			top: top,
			message: 'Are you sure to delete this comment?',
			button_continue: 'Delete',
			callback: this.removeCommentReal,
			scope: this,
			params: [comment_id]
		});
	},
	
	removeCommentReal: function(comment_id) {
		var content_area = this.getMain();
		var element = content_area.find('.comments .commentid_'+comment_id);
		var self = this;
		
		var data = { 
				ajax: 'true',  
				op: 'plugin_comment',
				opc: 'deletecomment',
				m: this.preset_id, 
				cid: this.cid,
				comment_id: comment_id
			};
		
		$.post(location.href, data, 
				function(response) {
					if (response.error.length == 0) { 
						var total_comments = response.total_comments;
						content_area.find('.dcommentbox .block-title .num').html(response.total_comments);
						
						element.fadeOut(Module_Plugin_Comments.TIME_EFFECT, function(){
							element.remove();
							
							//update all css
							content_area.find('.dcommentbox .comments .comment:first').removeClass('first');				
							content_area.find('.dcommentbox .comments .comment:first').removeClass('last');				
							
							content_area.find('.dcommentbox .comments .comment:last').removeClass('first');				
							content_area.find('.dcommentbox .comments .comment:last').removeClass('last');
							
							content_area.find('.dcommentbox .comments .comment:first').addClass('first');
							content_area.find('.dcommentbox .comments .comment:last').addClass('last');
							
							if (response.total_comments == 0) {
								//hide main area
								content_area.find('.dcommentbox').hide();
							}
							
							// look for an optional tab for this comment category, and update its count
							$('.comment-tabs .menu .category_' + self.category + ' .count').html(response.total_comments);
						});
					} else {
						alert(response.error.join("\n"));
					}
			}, 'json');		
	},
	
	postComment: function(element, category) {
		var self = this;
		var error = [];
		var main_area = $(element).closest('.form-body-content');
		var textarea = main_area.find('textarea[name=content]');
		var guest = main_area.find('input[name=guest_name]');
		
		var comment = jQuery.trim(textarea.val());
		var guest_name = '';
		
		if (guest.length) {
			guest_name = jQuery.trim(guest.val());
			
			if (guest_name == '')
				error.push('You must write your name');
		}
		
		if (comment == '') {
			error.push('You must write a comment');
		}
		
		if (error.length) {
			alert(error.join("\n"));
		} else {
			var data = { 
				ajax: 'true',  
				op: 'plugin_comment',
				opc: 'addcomment',
				m: this.preset_id, 
				cid: this.cid,
				comment: comment,
				guest: guest_name,
				page: this.current_page
			};
			
			if ( typeof category !== 'undefined' && category !== false )
			{
			    data.category = category;
			}
			
			$.post(location.href, data, 
				function(response) {
					if (response.error.length > 0) {
						alert(response.error.join("\n"));
					} else {
						if (response.new_page) {
							self.getPage(response.new_page);
						} else {
							//add comment
							var total_comments = response.total_comments;
							var content_area = self.getMain();
							var element = $(response.comment);
							
							content_area.find('.dcommentbox .comments .comment:last').removeClass('last');
							
							content_area.find('.dcommentbox .block-title').css('display', 'block');						
							
							// number of comments in the comment box label
							content_area.find('.dcommentbox .block-title .num').html(response.total_comments);						
							// look for an optional tab for this comment category, and update its count
							$('.comment-tabs .menu .category_' + self.category + ' .count').html(response.total_comments);
							content_area.find('.dcommentbox .comments').append(element);
							
							//just in case show the main box
							content_area.find('.dcommentbox').show();
							content_area.find('.dcommentbox').show();
							
							element.hide();
							element.slideDown(Module_Plugin_Comments.TIME_EFFECT);
						}
						//empty area
						guest.val('');
						textarea.val('');
					}
				}, 
			'json');
		}
	},
	
	goPage: function(element, total_pages) {
		var page = parseInt($(element).closest('.box').find('input').val());
		
		if (isNaN(page)) {
			alert('You need to write a valid number');
		} else if ( page < 1 || page > total_pages ) {
			alert('The page is not valid');
		} else {
			this.getPage(page);
		}
	},
	
	getPage: function(page) {
		var self = this;
		var data = { 
				ajax: 'true',  
				op: 'plugin_comment',
				opc: 'pager',
				m: this.preset_id, 
				cid: this.cid,
				category: this.category,
				page: page
			};
		
		$.post(location.href, data, 
				function(response) {
					//override content
					self.getMain().find('.commentbox-detail').html(response.output);
					
					// look for an optional tab for this comment category, and update its count
					var num = self.getMain().find('.commentbox-detail').find('span.num').text();
					$('.comment-tabs .menu .category_' + self.category + ' .count').html(num);
					
					self.current_page = page;
				}, 'json');
	}
}
