(function($){$.fn.EnetCal = function(options) {
    var defaults = {
        numCal: 1,
        dept: "ALL",
        boardId: "ALL",
        category: "ALL",
        showTodayEvents: false,
        changeAllowed: true
    };
    var options = $.extend(defaults, options);
    
    return this.each(function() {
        obj = $(this);
        
        var currentCalDate;
    
        obj.ready(function(){
            //obj.append("<div id=\"cal_day\">Select a day to see its events.</div>"); // each page needs this div now. This allows us to put other default content in the div, like upcoming 3 events
            obj.datepicker({
                enetDept: options.dept,
                enetBoardId: options.boardId,
                enetCategory: options.category,
                numberOfMonths: options.numCal,
                changeMonth: options.changeAllowed,
                changeYear: options.changeAllowed,
                prevText: 'prev',
                nextText: 'next',
                onSelect: function(date) {
                
                    showEvents(date);
                    
                }
            });
            
            if (options.showTodayEvents == true)
            {
                var currentTime = new Date();
                var month = currentTime.getMonth() + 1;
                var day = currentTime.getDate();
                var year = (currentTime.getFullYear() + '');
                
                var selectorDate = month + "/" + day + "/" + year;
                
                //$(selectorDate).click();
                //alert(selectorDate);
                showEvents(selectorDate);
            }
            
            function showEvents(date)
            {
                $.ajax({
                        type: "GET",
                        url: "http://www.elon.edu/e-net/webservices/WebServicePosts.asmx/GetCalendarDatePostsXML?date=" + date + "&category=" + options.category + "&dept=" + options.dept + "&boardId=" + options.boardId,
                        dataType: "xml",
                        success: function(xml) {
                            $("#cal_day",obj).text('');
                            var events = "";
                            $("posts > post", xml).each(function(){
                                events = events + "<li><span class=\"cal_time\">" + $(this).attr("startTime") + "</span> <a href=\"http://www.elon.edu/e-net/Note.aspx?id=" + $(this).attr("id") + "\">" + $(this).find("subject").text() + "<\/a><\/li>";
                            });
                            
                            if (events == "")
                            {
                                events = "<li>There are no events for " + date + "<\/li>";
                            }
                            
                            $("#cal_day",obj).html($(events).wrap("<ul><\/ul>"));
                        },
                        error: function(xhr, message, ex) {
                            //alert("Ajax Error: " + message + " " + ex);
                        }
                    });
            }
        });
    });
    
};})(jQuery);    