(function(){
	var userIsPosting = false;
	
	function verifyComment(text) {
		if(text.length) {
			text = text.replace(/^\s+/g,'').replace(/\s+$/g,'');

			if(text.length <= 0) {
				return false;
			} else {
				return true;
			}
		} else {
			return false;
		}
	}
	
	function showAnswerCommentBox(id, imgid, options) {
		options = jQuery.extend({
			formAction: "replyImageComment",
			formId:   "replyImageCommentForm_"+id,
			holderId: "commentReplies_"+id,
			type: "reply"
		}, options);

		var replyDiv = $("#replyInputHolder_"+id);
		var commentDiv = $("#comment_"+id);
		var commentFooter = $(".commentFooter", commentDiv);
		var textInput = $(".commentText", replyDiv);
		
		if(replyDiv.length == 0) {
			replyDiv = $("#commentReplyDummy").clone();
			replyDiv.attr("id", "replyInputHolder_"+id);
			$("form", replyDiv).attr("id", options.formId);;
			$(".commentid", replyDiv).attr("value", id);
			$(".imgid", replyDiv).attr("value", imgid);
			$(".diaryname", replyDiv).attr("value", options.diaryname);
			
			commentDiv.append(replyDiv);
			
			replyDiv.initPostComment(options);
		
			textInput = $(".commentText", replyDiv);
			
			replyDiv.bind('replydivremove.bdb', function() {
				commentFooter.show();
				replyDiv.hide();
				textInput.blur();
				textInput.attr("value", "");
			});
		}
		
		commentFooter.hide();
		replyDiv.show();
		textInput.focus();
		
		return replyDiv;
	}
	
	var showContentMethods = {	
		
		initPostComment: function(options) {
			options = jQuery.extend({
				formAction: "addImageComment",
				formId: "addImageCommentForm",
				holderId: "imageComments",
				textClass: "commentText",
				submitClass: "submitComment",
				type: "comment",
				mustCaptcha: false,
				bindEnter: false
			}, options);

			return this.each(function() {
				var comment = $(this);
				var form = $("#"+options.formId, this);
				var submit = $("."+options.submitClass, this);
				var text = $("."+options.textClass, this);
				var holder = $("#"+options.holderId);
				
				var submitSuccess = function(data) {
    				$("span", submit).text("Skicka");
    				userIsPosting = false;
    				if(data.success == 1) {
	    				var div = $(data.div);
	    				
	    				if(options.type == "reply") {
	    					comment.trigger('replydivremove.bdb');
	    					holder.append(div);
		    				div.initReply({mustCaptcha: options.mustCaptcha, diaryname: options.diaryname});
	    				} else if(options.type == "comment") {
	    					holder.prepend(div);
		    				div.initComment({mustCaptcha: options.mustCaptcha, diaryname: options.diaryname});
	    				}
	    				
	    				fixLongComments(div);
	    				text.attr("value", "");
	    			} else if(data.mustcaptcha == 1) {
	    				showCaptcha();
	    			} else {
	    				text.attr("value", "");
	    			}
				};
				
				var submitComment = function() {
					
					
					$("span", submit).text("Skickar...");
					form.ajaxSubmitJSON({
			    		success: submitSuccess
					});
					
					$("#commentTextarea").blur();
				};
				
				var showCaptcha = function() {
					$("body").bind('captchaok.bdb', function(event, data) {
						$("body").unbind('captchaok.bdb');
						submitSuccess(data);
					});
					
					bdb.misc.showPopup("Ordverifiering","captcha.html?a="+options.action+"&form="+options.formId+"&height=210&width=320&type=GB&KeepThis=true&TB_iframe=true",null);
				};
				
				var submitClick = function() {
					if(userIsPosting) {
						return false;
					}
					userIsPosting = true;
					
					if(!verifyComment(text.attr("value"))) {
						userIsPosting = false;
						alert("Du får inte skicka en tom kommentar")
						return false;
					}
					
					if(options.mustCaptcha) {
						showCaptcha();
					} else {
						submitComment();
					}
					return false;
				};
				
				submit.bind("click", submitClick);
				
				text.keydown(function (e) {
					if (e.ctrlKey && e.keyCode == 13) {
					    submitClick();
					    return false;
					}
				});
				
			});
		},
		
		initComment: function(options) {
			options = jQuery.extend({
				commentIdPrefix: "comment_",
				replyClass: "replyComment",
				deleteClass: "deleteComment",
				hoverClass: "baseCommentDiv",
				imageIdClass: "commentImgId",
				diarynameClass: "commentDiaryName",
				optionsDivIdPrefix: "showHideComment_",
				mustCaptcha: false,
				diaryname: ""
			}, options);
			
			return this.each(function() {
				var replyButton = $("."+options.replyClass, this);
				var deleteButton = $("."+options.deleteClass, this);
				var hoverDiv = $("."+options.hoverClass, this);
				var imgidDiv = $("."+options.imageIdClass, this);
				var diarynameDiv = $("."+options.diarynameClass, this);

				deleteButton.click(function(){
					var id = $(this).attr("id").replace(options.deleteClass + "_", "");

					$.getJSON("/p/ajax.tpl", { 
						action: "deleteImageComment",
						diaryname: options.diaryname,
						commentid: id
					}, function(data) {
						$("#"+options.commentIdPrefix+id).unbind();
						$("#"+options.commentIdPrefix+id).remove();
					});

					return false;
				});
				
				replyButton.click(function(){
					var imgid = imgidDiv.attr("value");
					var diaryname = diarynameDiv.attr("value");
					var id = $(this).attr("id").replace(options.replyClass + "_", "");
					showAnswerCommentBox(id, imgid, {mustCaptcha: options.mustCaptcha, diaryname: diaryname});
					return false;
				});
				
				hoverDiv.hover(function() {
					var id = $(this).attr("id");
					id = id.substr(id.indexOf("_")+1);
					$("#" + options.optionsDivIdPrefix + id).show();
				},function() {
					var id = $(this).attr("id");
					id = id.substr(id.indexOf("_")+1);
					$("#" + options.optionsDivIdPrefix + id).hide();
				});
			});
		},
		
		initReply: function(options) {
			options = jQuery.extend({
				hoverClass: "commentDiscussionReply",
				optionsDivIdPrefix: "showHideComment_",
				deleteClass: "deleteReplyComment",
				commentIdPrefix: "commentReplyId_",
				mustCaptcha: false,
				diaryname: ""
			}, options);

			return this.each(function() {
				var deleteButton = $("."+options.deleteClass, this);
				
				$(this).hover(function() {
					var id = $(this).attr("id");
					id = id.substr(id.indexOf("_")+1);
					$("#"+options.optionsDivIdPrefix+id).show();
				},function() {
					var id = $(this).attr("id");
					id = id.substr(id.indexOf("_")+1);
					$("#"+options.optionsDivIdPrefix+id).hide();
				});
				
				deleteButton.click(function(){
					var id = $(this).attr("id").replace(options.deleteClass + "_", "");

					$.getJSON("/p/ajax.tpl", { 
						action: "deleteImageComment",
						diaryname: options.diaryname,
						commentid: id
					}, function(data) {
						$("#"+options.commentIdPrefix+id).unbind();
						$("#"+options.commentIdPrefix+id).remove();
					});

					return false;
				});
			});
		},
		
		unbindComments: function() {
			$("*", this).unbind();
		}
	};
	
	jQuery.each(showContentMethods, function(i) {
		jQuery.fn[i] = this;
	});	
})();
