﻿$(document).ready(function(){
  $("#latest-happenings-items").html("");
  $().SPServices({
    operation: "GetListItems",
    async: true,
    listName: "Latest Happenings",
    CAMLQuery: "<Query><Where><Or><Geq><FieldRef Name='Expires'/><Value Type='DateTime'><Today/></Value></Geq><IsNull><FieldRef Name='Expires'/></IsNull></Or></Where><OrderBy><FieldRef Name='Date' Ascending='False' /></OrderBy></Query>",
    CAMLViewFields: "<ViewFields><FieldRef Name='ID' /><FieldRef Name='Date' /><FieldRef Name='Title' /><FieldRef Name='Body' /></ViewFields>",
    CAMLRowLimit: 2,
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("[nodeName=z:row]").each(function(i) {
        var title = $(this).attr("ows_Title");
        var body = $(this).attr("ows_Body");
        var date = $(this).attr("ows_Date");
        var nid = $(this).attr("ows_ID");

        body = $(body).text();
        
        var tempDate = date.split(' ');
        date = tempDate[0];
        tempDate = date.split('-');
        date = tempDate[1] + "." + tempDate[2] + "." + tempDate[0];
        
        var wordCount = body.length;
        var maxWordCount = 75;
        if(wordCount > maxWordCount)
        {
        	body = body.substring(0,maxWordCount);
        }
        body += "...";
        
        var strHtml = "<div class='latest-happenings-item' id='happenings-" + i + "'><div class='title'><a href='Pages/News.aspx#" + nid + "'>" + title + "</a></div><div class='date'>" + date + "</div><div class='body'>" + body + "</div></div>";
        $("#latest-happenings-items").append(strHtml);
      });
    }
  });

});
