﻿/*

Script for notifications ctl.

*/

// Namespace
var Notifications = new Object();

// Notification counts
Notifications.CountCurrent = 0;
Notifications.CountArchive = 0;

// ID of first notice, if any
Notifications.First = 0;

//
// Init
//
jQuery(document).ready(function ()
{
   // Initial refresh
   Notifications.Refresh();

   // Click on the notification icon
   jQuery("#notification-link").click(function ()
   {
      if (Notifications.CountCurrent == 0)
      {
         window.location.href = "/Notifications/";
      }
      else if ((Notifications.CountCurrent == 1) && (Notifications.First > 0))
      {
         Notifications.Show(Notifications.First);
      }
      else
      {
         Notifications.Popup();
      }
      return false;
   });

   // Delete click
   jQuery("div.NotificationsSubjectOpen .delete").click(function ()
   {
      var id = jQuery(this).next().html();
      jQuery("#form-delete-id").val(id);
      jQuery(this).closest("form").submit();
      return false;
   });

   // end of init method
});

Notifications.Popup = function ()
{
   jQuery(".Notifications").dialog(
   {
      modal: true,
      title: "iTravex Notifications",
      width: 600,
      open: function ()
      {
         if (Notifications.CountArchive == 0)
         {
            jQuery(":button:contains('All Notices')").attr("disabled", "disabled").addClass('ui-state-disabled');
         }
      },
      buttons:
      {
         "All Notices": function ()
         {
            jQuery(this).dialog("close");
            window.location.href = "/Notifications/";
         },
         "Close": function ()
         {
            jQuery(".Notifications").dialog("close");
         }
      }
   });
}

Notifications.Refresh = function ()
{
   // Get notices for currently logged on member
   jQuery.ajax(
   {
      type: "GET",
      url: "/Notifications/Member/" + Notifications.MemberID,
      dataType: "json",
      "success": function (data)
      {
         Notifications.CountCurrent = data.current;
         Notifications.CountArchive = data.archived;

         if ((Notifications.CountCurrent > 0) || (Notifications.CountArchive > 0))
         {
            jQuery("#notification-link").show();

            if (data.current == 0)
            {
               jQuery(".Notifications").html("You have no new notices.");
               jQuery("#notification-counts").html(Notifications.CountArchive + " saved");
            }
            else
            {
               jQuery("#notifications-list").html(data.html);

               // Bind links in the new data
               jQuery(".notification .link").click(function ()
               {
                  var noticeID = jQuery(this).next().html();
                  Notifications.Show(noticeID);
               });

               jQuery("#notifications-new-count").html(Notifications.CountCurrent);

               Notifications.First = jQuery(".notification .link").next().html();

               jQuery("#notification-counts").html(Notifications.CountCurrent + " new");
            }
         }
         else
         {
            jQuery("#notification-link").hide();
         }
      },
      "error": function (xhr, status)
      {
         //alert('Could not retrieve notices' + noticeID);
      }
   });
}

//
// Shows a notice in the modal popup
//
Notifications.Show = function (noticeID)
{
   jQuery.ajax(
   {
      type: "GET",
      url: "/Notifications/Notice/" + noticeID,
      dataType: "json",
      "success": function (data)
      {
         jQuery(".notification-dialog").html(data.body);
         jQuery(".notification-dialog").dialog(
         {
            modal: true,
            title: data.subject,
            width: 600,
            open: function ()
            {
               jQuery(".Notifications").dialog("close");
            },
            buttons:
            {
               "Save": function ()
               {
                  jQuery.ajax(
                  {
                     type: "POST",
                     url: "/Notifications/Archive/" + noticeID,
                     "success": function (data)
                     {
                        Notifications.Refresh();
                        jQuery(".notification-dialog").dialog("close");
                     },
                     "error": function (xhr, status)
                     {
                        iTravex.Message('Could not archive notice #' + noticeID);
                     }
                  });
               },
               "Delete": function ()
               {
                  jQuery.ajax(
                  {
                     type: "POST",
                     url: "/Notifications/Delete/" + noticeID,
                     "success": function (data)
                     {
                        Notifications.Refresh();
                        jQuery(".notification-dialog").dialog("close");
                     },
                     "error": function (xhr, status)
                     {
                        iTravex.Message('Could not delete notice #' + noticeID);
                     }
                  });
               }
            }
         });
      },
      "error": function (xhr, status)
      {
         iTravex.Message('Could not retrieve notice #' + noticeID);
      }
   });
}

