  window.fbAsyncInit = function() {
	    FB.init({appId: '182446851822231',
            status: true,
            cookie: true,
            channelUrl  : 'http://www.teachade.com/code/facebookChannel.jsp',
            xfbml: true
           });
        //FB.XFBML.parse();
  };

//    var _gaq = _gaq || [];
//   _gaq.push(['_setAccount', 'UA-12081738-1']);
//   _gaq.push(['_setDomainName', 'none']);
//   _gaq.push(['_setAllowLinker', true]);
//   _gaq.push(['_trackPageview']);
//
//   jQuery(document).ready(function() {
//        
//        //Load google analytics
//        var ga = document.createElement('script');
//        ga.type = 'text/javascript';
//        ga.async = true;
//        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
//        var s = document.getElementsByTagName('script')[0];
//        s.parentNode.insertBefore(ga, s);
//   });
   
   
  var lastLiveActionTime = 0;
  var lastPing = 0;
  //Tomcat sessions are configured for 30 minutes. Setting is: session-timeout in conf/web.xml
  var keepAliveWait = 20 * 60000;//Notify the user when they've been inactive for 20 minutes
  var dialogWaitTime = 9 * 60000;//Keep the 'want to keep your session alive?' dialog up for 9 minutes.
  //var dialogWaitTime = 4000;//Test values
  //var keepAliveWait = 5000;//Test values
  
  //This will ping the server every 20 minutes as
  //long as the user has pressed a key at least 20 minutes ago.
  //That way, the user will remain logged in if she is typing or moving around the screen.
  function pingServer() {
      var rightNow = new Date().getTime();
      var pingIsDue = (rightNow - lastPing) >= keepAliveWait;
      var userIsActive = (rightNow - lastLiveActionTime) < keepAliveWait;
      var refreshTimeout;
      var suppressWarningDialog = false; //If this is true the dialog will never be shown.  Pinging will continue forever it the page is up and use is logged in.
      if(pingIsDue) {
          //console.log("Ping is due.");
          if(userIsActive || suppressWarningDialog) {
              //console.log("User is active.  Pinging.");
              lastPing = rightNow;
              jQuery.ajax({
                  url:"/tc/ping",
                  success: function(data){
                      //console.log("Ping complete");
                  },
                  xhrFields: {
                  }
              });
    
              setTimeout("pingServer()", keepAliveWait);
          } else {
              //console.log("Setting timeout to 4 seconds before refreshing window.");
              //refreshTimeout = setTimeout("refreshWindow()", dialogWaitTime);//Keep the dialog up for 9 minutes.
              refreshTimeout = setTimeout("refreshWindow()", dialogWaitTime);
              $("#session-dialog-confirm").dialog({
                  resizable: false,
                  height:140,
                  modal: true,
                  buttons: {
                      "Keep me logged in": function() {
                          clearTimeout(refreshTimeout);
                          userIsAlive();
                          $(this).dialog("close");
                      },
                      "Cancel my session" : function() {
                          $(this).dialog("close");
                          window.location.href = "/";
                      }
                  }
              });
              //console.log("Opening the dialog");
              $("#session-dialog-confirm").dialog("open");
          }
      }
  }
  
  function refreshWindow() {
      //console.log("Refreshing window");
      window.location.href = "/";
  }

  function userIsAlive() {
      //console.log("User is alive");
      lastLiveActionTime = new Date().getTime();
      pingServer();
  }

  
  jQuery(document).ready(function() {
      var forms = jQuery('form');
      var textareas = jQuery(':input[type="textarea"]') 
      if(textareas != null && textareas.length > 0) {
          suppressWarningDialog = true;
      }
      if(window.currentViewerId && forms && forms.length > 0) {//if there are forms on the page keep the page alive.
          
          //console.log("Adding onchange to " + forms.length + " forms");
          jQuery(document).bind('keypress', userIsAlive);
          userIsAlive();//The user starts out active.
      } else {
          //console.log("There are no forms to protect for viewer " + window.currentViewerId);
      }
   });

