/*
 *
 *	CCAR Member jQuery functions
 *		Stuart Swope
 */

/* showActions enables the print and share functionality if the user has javascript */ 
$.showActions = function () {
   var actions = '<ul id="actions">\n<li>\n<a class="print" href="#" onclick="window.print(); return false;">Print</a>\n</li>\n<li>\n<!-- AddThis Button BEGIN -->\n<a class="share addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;username=ccar">Share</a>\n</li>\n</ul>\n';
   $(actions).prependTo('#content-wrapper');
};

/* Gets the event list from Rapattoni */
var getEventList = function () {
   Rapattoni.Script('Events', 'Events', 'type=events', function (events) {
      // result is an events object
      events.GetUpcomingEvents("Events", 3, function (result) {
         var eventList = '<ul id="rap-list">\n';
         $.each(result.Events, function(i, item) {
            var startDate = GetDateOnly(item.EventStartDate);
            var endDate = GetDateOnly(item.EventEndDate);
            eventList += "<li>\n<h3 class=\"homeEventTitle\">" + item.EventName + "</h3>\n<h4><span class=\"homeEventStartDate\">" + startDate + "</span>";
                             
            if (startDate != endDate)
               eventList += "- <span class=\"homeEventEndDate\">" + endDate + "</span>\n";
               
            eventList += "</h4></li>\n";
         
         });
         
         eventList += '</ul>';
         eventList += '<p><a href="/calendar">View Full Calendar</a></p>';
      
         if ($('#today-at-ccar').length > 0) {
            $("#today-at-ccar").html(eventList);
         }
         
         if ($('#rap-news').length > 0) {
            $('.loading-side').hide();
            $("#today-header").after(eventList);
         }
      });
  });
};

function setCookie(name, value, expires) {
    document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires);
}

/* Sets the Rapattoni token cookie */
function mySecurityTokenChanged(token) {
    // expiration: 30 days.
    var futdate = new Date()		    // Get the current time and date
    var expdate = futdate.getTime()     // Get the milliseconds since Jan 1, 1970
    expdate +=  2592000 * 1000              // expires in 30 days(milliseconds)
    futdate.setTime(expdate)
    setCookie("rapattoni_ims_cookie", token, futdate.toGMTString());
}

/* Login sets the security cookie, calls the php login function and reloads login page */
var lkfLogin = function (token) {
   mySecurityTokenChanged(token);
   window.location = window.location.pathname; 
}

// Initialize when dom is ready
$(document).ready(function() {
   //$.showActions();

   Rapattoni.OnLogin = function (token) { 
      lkfLogin(token);
   };
   
   // Load for login page.
   if ($('#loginContainer').length > 0) {
      Rapattoni.Module('Login', 'div#loginContainer');
   } 
   
   //if ($('#today-at-ccar').length > 0 || $('#rap-news').length > 0) {
   //   getEventList();
   //}
});
