﻿(function($) {
	
	$.fn.pankakforumcomments = function(ACaption, AForumGuid, AForumType, AForumPostType, AFetchMessagesScriptUrl, APageSize, AMessageGuid, AFileProductionGuid, AFilePersonGuid) {
		var rootObj = this;
		var firstRun = true;
		
		if (AMessageGuid == undefined) { AMessageGuid = ""; }
		if (AFileProductionGuid == undefined) { AFileProductionGuid = ""; }
		if (AFilePersonGuid == undefined) { AFilePersonGuid = ""; }
		
		//Φέρνει με ajax το html που χρειάζεται για τη λειτουργία του control
		$.ajax({
			type: "GET",
			url: "js/jquery.pankak-forum-comments-html.asp",
			data: "caption=" + escape(ACaption) + "&forum_guid=" + escape(AForumGuid) + "&forum_type=" + AForumType + "&forum_post_type=" + escape(AForumPostType) + "&file_production_guid=" + escape(AFileProductionGuid) + "&file_person_guid=" + escape(AFilePersonGuid) + "&rnd=" + Math.round(Math.random() * 9999999999),
			dataType: "html",
			success: ParseHtmlForComments
		});	
		
		function ParseHtmlForComments(data, textStatus) {
			$(rootObj).empty();
			$(rootObj).html(data);
			var isAuthenticated = $("input[name=isAuthenticated]").val();
			refreshMessages(AForumGuid, AForumPostType, 0, APageSize, AMessageGuid);
			$("#forumComments .linkForPreviousPage").bind("click", function(e) {
				e.preventDefault();
				navigateToPreviousForumPage();
			});
			$("#forumComments .linkForNextPage").bind("click", function(e) {
				e.preventDefault();
				navigateToNextForumPage();
			});
			if (isAuthenticated == "false") {
				$(".newCommentForm").find(".buttonForCommentPost").click(function(e) {
					e.preventDefault();
					showPopupForLogin();
				});
				$(".newCommentForm .textBoxForCommentBody").focus(showPopupForLogin);
			}else{
				$(".newCommentForm").find(".buttonForCommentPost").click(function(e) {
					e.preventDefault();
					if (!validateMessage()) { return; }
					$(this).fadeOut("fast");
					$(".newCommentForm .formTitle h1").data("lastText", $(".newCommentForm .formTitle h1").text());
					$(".newCommentForm .formTitle h1").text("Αποστολή μηνύματος");
					$(".newCommentForm .textBoxForCommentBody").fadeOut("fast", function() {
						$(".newCommentForm .waitMessageForCommentPost").fadeIn("fast", function() {
							submitMessage();
						});
					});
				});
			}
		}	//function ParseHtmlForComments(data, textStatus)

		
		function refreshMessages(forum_guid, type, page, page_size, message_guid) {
			showForumWait();
			$.ajax({ 
				async: true,
				type: "GET",
				url: AFetchMessagesScriptUrl,
				data: "forum_guid=" + escape(forum_guid) + "&type=" + escape(type) + "&page=" + escape(page) + "&page_size=" + page_size + "&message_guid=" + escape(message_guid) + "&rnd=" + Math.round(Math.random() * 9999999999),
				dataType: "html",
				success: function(data, textStatus) {
					hideForumWait();
					if (data != "error") {
						$("#forumComments .commentsList").html(data);
						if ($("#hiddenFieldForPreviousPage").length > 0 && $("#hiddenFieldForPreviousPage").val().length <= 0) {
							$("#forumComments .linkForPreviousPage").hide("fast");
						}else{
							if ($("#hiddenFieldForPreviousPage").length <= 0) {
								$("#forumComments .linkForPreviousPage").hide("fast");
							}else{
								$("#forumComments .linkForPreviousPage").show("fast");
							}
						}
						if ($("#hiddenFieldForNextPage").length > 0 && $("#hiddenFieldForNextPage").val().length <= 0) {
							$("#forumComments .linkForNextPage").hide("fast");
						}else{
							if ($("#hiddenFieldForNextPage").length <= 0) {
								$("#forumComments .linkForNextPage").hide("fast");
							}else{
								$("#forumComments .linkForNextPage").show("fast");
							}
						}
						if (message_guid && message_guid.length > 0) { 
							location.href = location.href + '#message_' + message_guid;
						}					
					}
					AMessageGuid = "";
				},
				error: function() {
					hideForumWait();
				}
			});
		}	//function refreshMessages(forum_guid, type, page)		

		function validateMessage() {
			if (!$(".newCommentForm .textBoxForCommentBody").val().match(/.{10,}/i)) {
				alert("Παρακαλώ γράψτε το μήνυμα που θέλετε.\n\nΤο μήνυμα πρέπει να περιέχει τουλάχιστον 10 γράμματα.");
				return false;
			}else{
				return true;
			}
		}	//function validateMessage()
		
		function submitMessage() {
			$.ajax({
				async: true,
				type: "POST",
				url: "addPostToForum.asp",
				data: $(".newCommentForm .commentForm").serialize(),
				success: function(data, textStatus) {
					if (data == "ok") {
						refreshMessages(escape(AForumGuid), AForumPostType, 0, APageSize, AMessageGuid);
					}else{
						alert("Παρουσιάστηκε σφάλμα κατά την αποστολή του μηνύματος.\n\nΤο μήνυμα δε δημοσιευθηκε.\n\nΠαρακαλώ προσπαθείστε ξανά.");
					}
					$(".newCommentForm .textBoxForCommentBody").val("");
					$(".newCommentForm .waitMessageForCommentPost").fadeOut("fast", function() {
						$(".newCommentForm .textBoxForCommentBody").fadeIn("fast", function() {
							$(".newCommentForm .formTitle h1").text($(".newCommentForm .formTitle h1").data("lastText"));
							$(".newCommentForm .buttonForCommentPost").fadeIn("fast");
						});
					});
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					$(".newCommentForm .waitMessageForCommentPost").fadeOut("fast", function() {
						$(".newCommentForm .textBoxForCommentBody").fadeIn("fast", function() {
							$(".newCommentForm .formTitle h1").text($(".newCommentForm .formTitle h1").data("lastText"));
							$(".newCommentForm .buttonForCommentPost").fadeIn("fast");
							alert("Παρουσιάστηκε σφάλμα κατά την αποστολή του μηνύματος.\n\nΤο μήνυμα δε δημοσιευθηκε.\n\nΠαρακαλώ προσπαθείστε ξανά.");
						});
					});
				}
			});
		}	//function submitMessage()

		function navigateToPreviousForumPage() {
			var prevPage = $("#hiddenFieldForPreviousPage").val();
			refreshMessages(escape(AForumGuid), AForumPostType, prevPage, APageSize, AMessageGuid);
		}	//function navigateToPreviousForumPage()
		
		function navigateToNextForumPage() {
			var nextPage = $("#hiddenFieldForNextPage").val();
			refreshMessages(escape(AForumGuid), AForumPostType, nextPage, APageSize, AMessageGuid);
		}	//function navigateToNextForumPage()
		
		function showForumWait() {
			var offset = $("#forumComments .commentsList").offset();
			var height = $("#forumComments .commentsList").height();
			var width = $("#forumComments .commentsList").width();
			$("#forumComments #forumWaitPanel").width(width).height(height).css({
				"left": offset.left + "px",
				"top": offset.top + "px",
				"filter": "alpha(opacity=75)",
				"-moz-opacity": "0.75",
				"opacity": "0.75"
			});
			$("#forumComments #forumWaitPanel").fadeIn("fast");
		}	//function showForumWait()
		
		function hideForumWait() {
			$("#forumComments #forumWaitPanel").fadeOut("fast");
		}	//function hideForumWait()

		function showPopupForLogin() {
			tb_show("caption", "login_popup.asp?height=210&width=600&modal=true");
			return false;
		}

	}	//$.fn.pankakforumcomments
		
})(jQuery);					
