﻿if (ACC == null) {
    var ACC = {};
}

ACC.Calendar = function(container, options) {
    //set the calendar
    this.Container = container;

    this.Options = options;

    //create our info object to hold non critical info
    this.Info = {};

    //set our months
    this.Info.Months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

    //set the date container
    this.DateContainer = $(container).find(".cal-dates");

    //find the calendar title
    this.Container.Title = $(container).find(".cal-title h3");

    //find the prev control
    this.Container.Prev = $(container).find(".cal-prev");

    var Calendar = this;

    //set our calendar obj
    this.Container.Prev.each(function(index, element) {
        element.Calendar = Calendar;
        $(element).bind("click", function() {
            this.Calendar.PrevMonth();
        });
    });

    //find the next control
    this.Container.Next = $(container).find(".cal-next");

    //set our calendar obj
    this.Container.Next.each(function(index, element) {
        element.Calendar = Calendar;
        $(element).bind("click", function() {
            this.Calendar.NextMonth();
        });
    });

    //set our calendar object
    $(this.Container).each(function(index, element) {
        element.Calendar = Calendar;
    });

    //render the current month
    this.RenderMonth(new Date());
};

ACC.Calendar.prototype.NextMonth = function() {
    //create a new date object based on our current caldate
    var NewDate = new Date(this.CalendarDate.getTime());

    //advance the month by one
    NewDate.setMonth(NewDate.getMonth() + 1);

    //render the new month
    this.RenderMonth(NewDate);
};

ACC.Calendar.prototype.PrevMonth = function() {
    //create a new date object based on our current caldate
    var NewDate = new Date(this.CalendarDate.getTime());

    //advance the month by one
    NewDate.setMonth(NewDate.getMonth() - 1);

    //render the new month
    this.RenderMonth(NewDate);
};

ACC.Calendar.prototype.RenderMonth = function(CalDate)
{
    //set our calendar date
    this.CalendarDate = CalDate;

    //set the calendar year/month title
    $(this.Container.Title).html(this.Info.Months[CalDate.getMonth()] + " " + CalDate.getFullYear());

    //remove all previous dates
    //$(this.DateContainer).empty();

    var CurrentRows = $(this.DateContainer).children();

    //get the calendar date
    var StartDate = new Date(CalDate.getTime());
    StartDate.setDate(1);

    if (StartDate.getDay() == 0)
    {
        StartDate.setDate(StartDate.getDate() - 1);
    }

    while (StartDate.getDay() != 0)
    {
        StartDate.setDate(StartDate.getDate() - 1);
    }

    StartDate.setHours(0);
    StartDate.setMinutes(0);
    StartDate.setSeconds(0, 0);

    //short hand for the events
    var Events = this.Options.events;

    //current day
    var CurrentDay = 0;

    for (var w = 0; w < 6; w++)
    {
        //create our calendar row
        var RowContainer = $(document.createElement('div'));

        //set the classes
        $(RowContainer).addClass("cal-row f-left");

        if (CurrentRows.length > 0)
        {
            $(CurrentRows[w]).replaceWith(RowContainer);
        }
        else
        {
            //add our row the the calendar
            $(this.DateContainer).append(RowContainer);
        }

        CurrentWeek = (w * 7);

        //max row height
        var MaxDayHeight = 48;

        //loop through and create our days
        for (var d = 0; d < 7; d++)
        {
            //set our current day
            CurrentDay = CurrentWeek + d;

            CurrentDate = new Date(StartDate.getTime());

            //set the current date from the start date
            CurrentDate.setDate(StartDate.getDate() + CurrentDay);

            //create our day element
            var DayContainer = $(document.createElement('div'));

            //add our day to the row
            $(RowContainer).append(DayContainer);

            //set the classes
            $(DayContainer).addClass("cal-space");

            if (CurrentDate.getMonth() != this.CalendarDate.getMonth())
            {
                $(DayContainer).addClass("inactive");
            }

            //set our day # element
            var DayNumber = $(document.createElement('div'));

            $(DayContainer).append(DayNumber);

            //set the classes
            $(DayNumber).addClass("cal-date");

            $(DayNumber).html(CurrentDate.getDate());

            //create the events for the day
            for (var e = 0; e < Events.length; e++)
            {
                try
                {
                    //get our event
                    var Event = Events[e];

                    if (Event.end == null)
                    {
                        Event.end = new Date(Event.start.getTime());
                        Event.end.setHours(23);
                        Event.end.setMinutes(59);
                        Event.end.setSeconds(59, 999);
                    }

                    var time_stamp = "";

                    var start_hours = Event.start.getHours();

                    if (Event.start.getHours() > 0)
                    {
                        time_stamp = ((start_hours > 12) ? start_hours - 12 : start_hours) + ((start_hours > 12) ? "p" : "a");
                    }

                    var show_event = false;

                    var start_date = new Date(Event.start.getTime());
                    start_date.setHours(0);
                    start_date.setMinutes(0);
                    start_date.setSeconds(0, 0);

                    var end_date = new Date(Event.end.getTime());
                    end_date.setHours(0);
                    end_date.setMinutes(0);
                    end_date.setSeconds(0, 0);

                    if (CurrentDate.getTime() >= start_date.getTime() && CurrentDate.getTime() <= end_date.getTime())
                    {
                        show_event = true;
                    }

                    if (show_event)
                    {
                        //create our event if it falls within today
                        var EventContainer = $(document.createElement('div'));
                        $(EventContainer).addClass("cal-event");
                        $(EventContainer).addClass(Event.className);

                        var EventUrl = "";

                        //decode the event title
                        var EventTitle = $.base64Decode(Event.title);

                        //event url placeholder
                        var EventURL = Event.url;

                        EventURL = $.trim(EventURL);

                        if (EventURL.length > 0)
                        {
                            //create our event link
                            var EventLink = $(document.createElement('a'));
                            EventLink.html(EventTitle + " " + time_stamp);
                            EventLink.attr("href", EventURL);
                            EventLink.attr("title", EventTitle.replace(/"/ig, ""));
                            EventLink.attr("target", "_blank");

                            //append the link to the event
                            $(EventContainer).append(EventLink);
                        }
                        else
                        {
                            //no anchor present, so skip the url
                            EventContainer.html(EventTitle + " " + time_stamp);
                        }

                        //append the event to the day
                        $(DayContainer).append(EventContainer);

                        this.Options.eventRender(Event, EventContainer);
                    }
                }
                catch (exc)
                {
                    alert(exc);
                }
            }

            //get the height of the day container
            var DayHeight = $(DayContainer).height();

            //if our current day is higher than the last, record it
            if (DayHeight > MaxDayHeight)
            {
                MaxDayHeight = DayHeight;
            }
        }

        //set the height of all days to the max day height
        $(RowContainer).children().height(MaxDayHeight);

        $(RowContainer).css("display", "none");
        $(RowContainer).stop(true, true);
        $(RowContainer).fadeIn(500);
    }
};
