// $Id: esource_login.js,v 1.0.1 2008/12/01 16:38:21 ChrisAlbrecht Exp $

$(document).ready(function() {
  if ($.esmodal) {
  
  /**
   * Bind events to member links
   * 20oct2008 - CEA
   */
 $(".non-member").click(function() {
    var thislink = $(this);
    var options = {width: 600};
    var data = {path: thislink.attr('href'), target: thislink.attr('target')};

    $.esmodal.Init(options);
    $.esmodal.LoadContent('body', '/non-member', data , function() {

      // set the form result destination
      //var destination = (thislink.hasClass('disknode-result') ? 'non-member-disknode-result' : (thislink.attr('href') ? thislink.attr('href') : location.pathname));
      var destination = (thislink.attr('href') ? thislink.attr('href') : location.pathname);
      $("#esmodal-body form").attr('action', '/user/login?destination='+ destination);
      
      if (thislink.attr('target')) {
        $("#esmodal-body form").attr('target', thislink.attr('target'));
      }

      $.esmodal.Show();
    });
    return false;
  });
  
  $(".non-subscriber").click(function() {
    var thislink = $(this);
    $.esmodal.Init();
    $.esmodal.LoadContent('body', '/non-subscriber', '', function(){
      // if 'this' is a link, set the destination to the HREF value
      var destination = (thislink.attr('href') ? thislink.attr('href') : location.pathname);
      $("#esmodal-body form").attr('action', '/user/login?destination='+ destination);

      $.esmodal.Show();
    });
    return false;
  });
  
  /**
   * Use a modal instead of user login form pages
   * 27/aug/2009 cea
   */
  $("a[href='/user/login']").click(function() {
    esource_login_non_member();
    /*$.esmodal.Init();
    $.esmodal.LoadContent('body', 'non-member', '', function() {
      $.esmodal.Show();
    });*/
    return false;
  });

  /**
   * Add a modal for logout confirmation
   * 20oct2008 - CEA
   */
  $("a[href='/logout']").click(function() {
    $.esmodal.Init();
    var content = "Are you sure you want to log out? If you do, you will not automatically be logged in on your next visit to this site.<br /><br />";
    content += '<input id="logout-button" class="esmClose form-submit" type="button" value="Log out" onclick="location.href=\'/eslogout\'" />';
    //content += '<a href="/eslogout" />Logout now</a>';
    $.esmodal.SetContent('body', content);
    $.esmodal.Show();
    return false;
  });
  
  /**
   * Modal for login help
   */
  $(".esModalLoginHelp").click(function() {
    $.esmodal.Init();
    $.esmodal.LoadContent("body", '/login-help', '', function() {
      $.esmodal.Show();
    });
    return false;
  });
  
  /**
   * Modal for non-member-info by clicking a link
   */
  $(".non-member-info").click(function() {
    $.esmodal.Init();
    $.esmodal.LoadContent("body", '/non-member-info', '', function() {
      $.esmodal.Show();
    });
    return false;
  });
  
  /**
   * Forgot Password form
   */
  $(".forgot-password").click(function() {
    $.esmodal.Init({width: 600});
    $.esmodal.LoadContent("body", '/forgot-password', '', function() {
      $("#esmodal form").attr('action', '/public/forgot_password');
      $.esmodal.Show();
    });
    return false;
  });

  /**
   * Logout button on logout modal
   */
  $("#logout-button").click(function() {
    $.esmodal.Hide();
    location.href = '/eslogout';
  });

  }
});

/**
 * Modal for non-members by page load
 */
function esource_login_non_member() {
  if ($.esmodal) {
    var options = {width: 600};
    $.esmodal.Init(options);
    $.esmodal.LoadContent("body", '/non-member', '', function() {
      var destination = (location.pathname.charAt(0) == '/' ? location.pathname.replace(/^\//,"") : location.pathname);
      
      // If we're on the home page, destination will be empty so leave it off
      if (destination == "" || destination == "user/register") {
        $("#esmodal form").attr('action', '/user/login');      }
      else {
        $("#esmodal form").attr('action', '/user/login?destination='+ destination);
      }
      
      $.esmodal.Show();
    });
  }
  else {
    $("#actual_content .node .content").append('<p class="non-member-notice"></p>');
    $("p.non-member-notice").hide().load('/non-member', null, function() {
      $('p.non-member-notice form').attr('action', '/user/login?destination=' + location.pathname);
      if ($(this).html() != '') {
        $(this).prepend("<h3>You must be logged in to view the full content.</h3>");
        $(this).slideDown('slow');
      }
    });
    //$("#actual_content .node .content").append('<p class="non-member-notice">Please login to view the full content.  <a href="/contact/E+Source+Subscription+Services">Not a member yet?</a></p>');
  }
}

/**
 * Modal for non-subscribers
 */
function esource_login_non_subscriber() {
  if ($.esmodal) {
    $.esmodal.Init();
    $.esmodal.LoadContent("body", '/non-subscriber', '', function() {
      $.esmodal.Show();
    });
  }
  else {
    $("#actual_content .node .content").append('<p class="non-member-notice"></p>');
    $("p.non-member-notice").hide().load('/non-subscriber', null, function() {
      //$(this).prepend("<h3>You must be logged in to view the full content.</h3>");
      $(this).slideDown('slow');
    });
    //$("#actual_content .node .content").append('<p class="non-member-notice">To view the full content you must be subscribed to this service.<br />Please <a href="/contact/E+Source+Subscription+Services">contact E Source Subscription Services</a> if you have any questions.</p>');
  }
}

