﻿$(document).ready(function () {
   //Display Admin Menu
   DisplayAdminMenu();
   $(".addNewComment").live("click", function (event) {
         AddComment(this, event);
      });

      $(".pageNumber").live("click", function (event) { GetPage(this, event, "#tblComments", "Page "); });

});
function DisplayAdminMenu() {
   $.get('/Home/IsUserAdmin', function (data) {
      //alert('Returned data ' + data)
      if (data == 'true') {
         $("#AdminMenu").removeClass("HideAdminMenu");
         $("#AdminMenu").addClass("ShowAdminMenu");
      }
      else {
         $("#AdminMenu").removeClass("ShowAdminMenu");
         $("#AdminMenu").addClass("HideAdminMenu");
      }
   });
};

function AddComment(clikedtag, event) {
   event.preventDefault();
   $.get("/Home/IsUserLoggedIn", function (data) {
      //alert('Returned data ' + data);
      if (data == 'true') {
         var $loading = $('<img src="../../Content/images/ajaxLoading.gif" alt="loading" class="ui-loading-icon">');
         var $url = $(clikedtag).attr('href');
         //var $title = $(clikedtag).attr('title');
         var $title = "Add Comment";
         var $dialog = $("#popDialog");
         //var $dialog = $("<div></div>");
         //alert("You have cliked me. Thank you");
         $dialog
      .empty()
      .append($loading)
      .load($url)
		.dialog({
		   autoOpen: true,
		   title: $title,
		   modal: true,
		   width: 500,
		   minWidth: 400,
		   minHeight: 200,
		   position: 'center',
		   show: 'fade',
		   hide: 'fade',
		   buttons: {
		      "Submit": function () {
		         var $frm = $("#frmAddComment");
		         $frm.validate();
		         var dlg = $(this);

		         if ($frm.valid()) {
		            //alert("Valid");
		            $.ajax({
		               url: $frm.attr('action'),
		               type: 'POST',
		               data: $frm.serialize(),
		               success: function (data, textStatus, xhr) {
		                  if (data != "") {
		                     $("#tblComments").html(data);
		                     dlg.dialog('close');
		                     dlg.empty();
		                     //twitter type notification
		                     var msg = "Your comment is added, thank you!";
		                     $('#commonMessage').html(msg);
		                     $('#commonMessage').delay(400).slideDown(400).delay(2000).slideUp(400);
		                  }
		                  else {
		                     alert("Comment is required. Please enter your comment before clicking the submit button");
		                  }
		               },
		               error: function (xhr, status) {
		                  displayError(xhr.responseText);
		               }
		            }); //End of ajax request
		         }

		      }, //End of Kaydet
		      "Close": function () {
		         $(this).empty();
		         $(this).dialog("close");
		      }
		   } //End of Button
		});  //End of .dialog
      } //End of if
      else {
         var pathname = window.location.pathname;
         //alert(pathname);
         document.location = "/Account/LogOn?ReturnUrl=" + pathname;
      }
   });    //End of .get
   //
};
function GetPage(clikedtag, event, target, msg) {
   event.preventDefault();

   var pagenumber = $(clikedtag).attr("data-page");
   if (pagenumber == 0) return false;

   var pathname = window.location.pathname;
   var array = pathname.split('/');
   //alert("pathname = " + pathname + " and length = " + array.length + " last element is : " + array[array.length - 1]);
   var categoryID = array[array.length - 1];
   $url = "/Forum/Comments";

   msg = msg + pagenumber
   $.ajax({
      url: $url,
      type: 'GET',
      data: { id: categoryID, page: pagenumber },
      success: function (data, textStatus, xhr) {
         $(target).html(data);

         $("#previousPage").show();
         $("#nextPage").show();
//         var prevPage = $("#previousPage").val();
//         var nextPage = $("#nextPage").val();
//         (prevPage == "" || prevPage == '') ? $("#previousPage").hide() : $("#previousPage").show();
//         (nextPage == "" || nextPage == '') ? $("#nextPage").hide() : $("#nextPage").show();

         $('#commonMessage').html(msg);
         $('#commonMessage').delay(400).slideDown(400).delay(1000).slideUp(400);
      },
      error: function (xhr, status) {
         displayError(xhr.responseText);
      }
   });
};
// Display Error
function displayError(message, status) {
    var $dialog = $(message);
    $dialog
                .dialog({
                    modal: true,
                    title: status + ' Error',
                    buttons: {
                        Ok: function () {
                            $(this).dialog("close");
                        }
                    }
                });
    return false;
};

