﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EzineLink.js" company="Wellpoint">
//   Copyright (c) Wellpoint Inc.  All rights reserved.
// </copyright>
// <summary>
//   Client script to be used for handling a link or button that is intended to open an Ezine
//   document in a modal dialog.
//   May be used with any page provided that the Ezine link that has the CSS class "EzineLink".
//   The server-side code may set the DocId of the Ezine document to be opened by setting it as the
//   value of a hidden input field with the CSS class
//   "inputDocId". If that hidden input field is not present or has no value, a default DocId
//   (defined in this script file as a constant) is used.
//   EzineLink.js is dependent on Ezine.js and on jQuery UI.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

(function($) {

  // Declares the message types that this control will accept
  // This variable is used to setup the control with the observer
  var acceptedMessagesTypes = 'BrowserNav;PlanSummaryLoaded';
  var controlIdentifier = 'ezinelink';
  var browserNavPrefix = "ezinepopup";

  $.EzineLinkConstants = {
    defaultDocId: "medicare-plans-state",
    dialogWidth: 780
  };

  // Handles the event that is raised when the page is loaded
  $(document).ready(function() {
    // Subscribe to page observer events
    pageobserver.subscribe(handleObserverNotification, acceptedMessagesTypes, controlIdentifier);

    // Bind the ezine link
    $.bindEzineLink();
  });

  // Handle messages from other controls
  var handleObserverNotification = function(message) {
    var messagedata = JSON.parse(message);
    switch (messagedata.messagetype) {
      case 'BrowserNav':
        if (messagedata.prefix == browserNavPrefix) {
          // bookmark is irrlevant here
          if ($.ezineDialog) {
            $.ezineDialog.dialog("open");
          }
        } else if (messagedata.prefix == "ezine") {
          // navigating within the ezine - don't close it
        } else {
          // navigating outside the ezine - close it
          if ($.ezineDialog) {
            $.ezineDialog.dialog("close");
          }
        }
        break;
      case 'PlanSummaryLoaded':
        // Bind the ezine link
        $.bindEzineLink();
    }
  };

  // Set up Click event handler for Ezine link or button
  $.bindEzineLink = function() {
    $("a.EzineLink").unbind();
    $("a.EzineLink").click(function() {
      var ezineMode = null;
      var $inputEzineMode = $(".EzineMode", $(this).parents(".EzineLinkControl"));
      if ($inputEzineMode) {
        ezineMode = $inputEzineMode.eq(0).val();
      }
      if (ezineMode == "Popup") {
        $.browserNavigationAddBookmark("ezinepopup", ""); // Save the page with the closed ezine in the browser back history
      }
      $.displayEzine($(this));
      return false;
    });

    $(".EzineRightNavBodyLink").unbind();
    $(".EzineRightNavBodyLink").click(function() {
      var ezineMode = null;
      var $inputEzineMode = $(".EzineMode", $(this).parents(".EzineLinkControl"));
      if ($inputEzineMode) {
        ezineMode = $inputEzineMode.eq(0).val();
      }
      if (ezineMode == "Popup") {
        $.browserNavigationAddBookmark("ezinepopup", ""); // Save the page with the closed ezine in the browser back history
      }
      $.displayEzine($(this));
      return false;
    });
  };

  $.displayEzine = function(link, docToDisplay) {
    // Get the ID of the document to display
    var docId = null;
    if (link != null) {
      var $inputDocId = $(".EzineDocId", link.parents(".EzineLinkControl"));
      if ($inputDocId) {
        docId = $inputDocId.eq(0).val();
      }
      if (!docId || !(/\S/).test(docId)) {
        docId = $.EzineLinkConstants.defaultDocId;
      }
    } else {
      docId = docToDisplay;
    }

    var ePage = null;
    var $inputEpage = $(".EzineEpage", link.parents(".EzineLinkControl"));
    if ($inputEpage) {
      ePage = $inputEpage.eq(0).val();
    }

    var ezineMode = null;
    var $inputEzineMode = $(".EzineMode", link.parents(".EzineLinkControl"));
    if ($inputEzineMode) {
      ezineMode = $inputEzineMode.eq(0).val();
    }

    if (ezineMode == "NewWindow") {
      var url = "/Learn/" + docId + "&mode=blank";
      window.open(url, '', 'width=810,height=690,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');
    }
    else if (ezineMode == "SameWindow") {
      var url = "/Learn/" + docId;
      NavigateWithReferrer(url);
    }
    else if (ezineMode == "Popup") {
      loadCssFile("/ols/css/Ezine/Ezine.css");
      // Open the Ezine display control in a modal dialog
      var url = "/ols/nav.cds?site=olspublic&cat=ezine&page=modal&edoc=" + docId;
      if (ePage != null && ePage != "") {
        url = url + "&epage=" + ePage;
      }
      $.ezineDialog = $("<div></div>");
      $.ezineDialog.dialog({
        width: $.EzineLinkConstants.dialogWidth,
        draggable: false,
        modal: true,
        dialogClass: 'olsmodal',
        position: "top",
        autoOpen: false,
        title: "Learn more",
        buttons: { "Close": function() {
          $(this).dialog("close");
          if (typeof $.ezineDialog != 'undefined') {
            $.ezineDialog.remove();
          }
        }
        }
      });
      $.ezineDialog.dialog("open");
      AjaxWorkingStart($.ezineDialog, "");
      $.ezineDialog.load(url, function() {
        AjaxWorkingDone($.ezineDialog);
        loadJsFile('/ols/js/CustomJS/Ezine.js', function() {
          $.EzineReady();
          $.EzineSelection.Init();
        });
      });
    }
  };

})(jQuery);

