//
// Created by ThisSideUp Software Inc.
//
// Copyright 2010 ThisSideUp Software Inc. http://www.thissideupsoftware.com
// All Rights Reserved. Permission to use, copy, modify, and distribute this
// software is hereby granted provided that the full text of this copyright
// notice appears in all copies.
//
// THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS
// MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR
// PURPOSE OR THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE
// ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
//
// COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR
// CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR
// DOCUMENTATION.
//

function logout()
{
   var cookie = getCookieValue(jsonData.sessionCookieName);
   if (cookie == null || cookie == "" || typeof cookie == "undefined") {
      logoutHandler();
      return;
   }

   $("#logout-err-msg").css({ visibility: "hidden" });
   var scheme = window.location.href.split(":")[0];
   var logoutUrl = scheme + "://" + jsonData.authority + jsonData.dir + "/assets/php/logout.php";
   $.ajax({
      type: "POST",
      url: logoutUrl,
      async: false,
      error: function() {
         $("#logout-err-msg")
            .fadeOut(0, function() {
               $(this).css({ visibility: "visible" })
               $(this).fadeIn(200, function() {
                  $(this).animate({opacity: 1.0}, 2000);
                  $(this).fadeOut(200);
               });
            });
      },
      success: function(data) {
         logoutHandler();
      }
   });
}

function logoutHandler() {
   if (jsonData.pageName == "profile" || jsonData.pageName == "enroll") {
      window.location.href = "account.html";
      if (window.event != null) window.event.returnValue=false;
      return;
   }
   $("#logout").fadeOut(200, function() {
      $(this).css({display: "none"});
      $("#profile").css({display: "none"});
      if (jsonData.pageName != "account") {
         $("#login").fadeOut(0, function() {
            $(this)
               .css({ visibility : "visible" })
               .fadeIn(200);
         });
      }
   });
}

function accountLinks() {
   var loggedIn = getCookieValue(jsonData.sessionCookieName);
   if (loggedIn) {
      $("#login").css({display : "none"});
      $("#logout").css({visibility : "visible"});
      $("#profile").css({visibility : "visible"});
   } else {
      $("#logout").css({display : "none"});
      $("#profile").css({display: "none"});
      if (jsonData.pageName != "account") {
         $("#login").css({visibility : "visible"});
      }
   }
   $("#logout").click(function() { logout(); });
   $("#login").click(function() {
      window.location.href="account.html"
      if (window.event != null) window.event.returnValue=false;
   });
   $("#profile").click(function() {
      window.location.href="profile.html";
      if (window.event != null) window.event.returnValue=false;
   });
}

