if(typeof parkside == "undefined") var parkside = new Object();
if(typeof parkside.webuser == "undefined") parkside.webuser = new Object();

// Constructor
parkside.webuser = function(action, entry_type, form_name) {
	this.action = action;
	this.entry_type = entry_type;
	this.form_name = form_name;
	this.$form = $("form[name=" + form_name + "]");
	var self = this;
	this.timeout = "";
	this.timeout_message = "";
		
	this.request = function(params, target_link)  {		
		var url = params.url;
		var response = params.response;
		params.argumentCollection = $.toJSON(params);		
		
		window.clearTimeout(self.timeout_message);
		if(self.timeout == "")	
		{
			self.timeout = window.setTimeout(function()  { $.post(url, params, response, "json"); }, 250);
		}
		else
		{	
			window.clearTimeout(self.timeout);
			self.timeout = "";
		}
	};
	
	this.reporting_response = function(obj, status) { 	
		if(obj !== undefined)
		{
			window.clearTimeout(self.timeout);
			self.timeout = "";
			var params = {
				type : obj.ERROR ? "error" : "success",
				text : obj.MSG,
				output : false
			}
			
			$.get("custom/modules/text/text_renderer.cfc?method=showMessage&returnFormat=json", params, function(obj) {
				if(self.$form.parent().find(".message").length)
				{
					self.$form.parent().find(".message").remove();	
				}
				
				self.$form = $("form[name=" + self.form_name + "]");
				
				self.$form.before(obj);
				self.$form.parent().find(".message").hide().fadeIn(200, function() {
					self.timeout_message = window.setTimeout(function() { self.$form.parent().find(".message").fadeOut(200, function() { self.$form.parent().hide(); }); }, 15000);														 
				});
				
				self.$form.hide();
				self.$form.parent().find("h3").hide();
				
				if(self.entry_type != "blog")		
				{	
					self.$form.find("input[name=id]").val("");
				}
				self.$form.find("input[name=author]").val("");
				self.$form.find("input[name=author_email]").val("");
				self.$form.find("textarea[name=text]").val("");
			});
		}
	};
	
	this.comment_response = function(obj, status) { 	
		if(obj !== undefined)
		{	
			window.clearTimeout(self.timeout);
			self.timeout = "";
			var params = {
				type : obj.ERROR ? "error" : "success",
				text : obj.MSG,
				output : false
			}
			
			if(obj.CFC_COMPONENT !== undefined)
			{
				paging = eval(obj.CFC_COMPONENT + "_paging"); 
				paging.loadPage(obj.CURRENT_PAGE, true, function() { 
					self.$form = $("form[name=" + self.form_name + "]");
					if(self.$form.length > 1)
					{
						self.$form.slice(1).each(function() { $(this).parent().remove(); });
						self.$form=$("form[name=" + self.form_name + "]");
					}
	
					self.comment_response_standard(params, obj, status);
					if(obj.UNLOCKED)
					{
						$(document).scrollTop(($("#comment-" + obj.COMMENT_ID).offset().top));
						$("#comment-" + obj.COMMENT_ID).after(self.$form.parent());
					}
					else if(obj.PARENT_COMMENT_ID > 0)
					{
						$(document).scrollTop(($("#comment-" + obj.PARENT_COMMENT_ID).offset().top));
						$("#comment-" + obj.PARENT_COMMENT_ID).after(self.$form.parent());
					}
					
				});
				
				$(".blog-entry .community .actions .comments").html(obj.COMMENTS_TEXT);
			}			
			else
			{
				self.comment_response_standard(params, obj, status);	
			}
		}
	};
	
	this.comment_response_standard = function(params, obj, status)
	{
		$.get("custom/modules/text/text_renderer.cfc?method=showMessage&returnFormat=json", params, function(obj) {
			if(self.$form.parent().find(".message").length)
			{
				self.$form.parent().find(".message").remove();	
			}
			
			self.$form = $("form[name=" + self.form_name + "]");
			
			self.$form.before(obj);
			self.$form.parent().find(".message").hide().fadeIn(200, function() {
				self.timeout_message = window.setTimeout(function() { self.$form.parent().find(".message").fadeOut(200, function() { initCommentForm(0, self.entry_type); }); }, 15000);														 
			});
			self.$form.hide(); 
			self.$form.parent().find(".cancel-reply").hide();
			self.$form.parent().find(".comment-form-header").hide();
			
			self.$form.find("input[name=parent_id]").val("");
			self.$form.find("input[name=author]").val("");
			self.$form.find("input[name=author_email]").val("");
			self.$form.find("textarea[name=text]").val("");
		});	
	}
	
	this.getUserObj = function()
	{
		var user = {
			frontend_user_id : this.$form.find("input[name=frontend_user_id]").val(),
			author : this.$form.find("input[name=author]").val(),
			author_email : this.$form.find("input[name=author_email]").val(),
			loggedin : this.$form.find("input[name=loggedin]").val()
		};
		
		return user;
	}
	
	/* Reporting Init */
	this.initReporting = function(action, entry_type, form_name)
	{		
		var params = {
			id : this.$form.find("input[name=id]").val(),
			type : entry_type,
			user : self.getUserObj(),
			form_data : {
				text : this.$form.find("textarea[name=text]").val()
			},
			url : "custom/modules/reporting/reporting.cfc?method=reportEntry",
			response : self.reporting_response
		};

		self.request(params);
	};
	
	/* Comment Init */
	this.initComment = function(action, entry_type, form_name)
	{		
		var params = {
			id : this.$form.find("input[name=id]").val(),
			type : entry_type,
			user : self.getUserObj(),
			form_data : {
				text :  this.$form.find("textarea[name=text]").val(),
				subscribed : this.$form.find("input[name=subscribed]:checked").length,
				parent_id : this.$form.find("input[name=parent_id]").val()
			},
			url : "custom/modules/comment/comment.cfc?method=postEntry",
			response : self.comment_response
		};
		
		self.request(params);
	};
	
	if(this.action == "report")
	{
		this.initReporting(action, entry_type, form_name);
	}
	else if(this.action == "post_blog_comment" || this.action == "post_idea_comment")
	{ 
		this.initComment(action, entry_type, form_name);
	}
}
