<script type='text/javascript'>
    selStart = "";
    selEnd = "";
    selAllDay = "";
    var calColorUsers = new Array();

    function addUser(userid) {
        var myuserid = <?php echo $this->userId; ?>;
        if(myuserid == userid) return "mycolorMe";
        if (userid == 0) return "";

        for (var x=0; x < calColorUsers.length; x++)
        {
            if (calColorUsers[x] != userid) continue;

            if(x > 7) x = 7; //we only have 8 colors
            return "mycolor"+ (x+1);
        }
        
        calColorUsers.push(userid);
        return "mycolor"+ [calColorUsers.length];
    }

    function saveEvent(btn,text) {
        if ((text) && (btn != "cancel")) {
            
            //check to see if time was passed in text
            data = ParseTimeFromTitle(text);
            allday = true;
            if(data[1]!="") {
                //add time to selStart
                selStart.setHours(data[1]);
                selStart.setMinutes(data[2]);
                allday = false;
            }
            updateEvent(0,data[0],selStart,selEnd,allday)
            /*
            $('#calendar').fullCalendar('renderEvent',
                {
                    title: data[0],
                    start: selStart,
                    end: selEnd,
                    allDay: selAllDay
                },
                false // make the event "stick"
            );
            */
           //Have to do this so that the time entered in title is properly displayed
           //so we have to switch away and back since I have lazyFetching turned off
           $('#calendar').fullCalendar( 'changeView', 'basicWeek' );
           $('#calendar').fullCalendar( 'changeView', 'month' );

        }
    }

    function updateEvent(id,text,start,end,allday) {
        Ext.Ajax.request({
           url: "index.php?fuse=admin&action=AdminSaveCalendarEvent",
           success: function(t) {
               json = ce.parseAjaxResponse(t);

           },
           params: {title: text,
                    id: id,
                    start: start,
                    end: end,
                    allday: allday
                  }
        });
    }

    $(document).ready(function() {
	
        var date = new Date();
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();

        var calendar = $('#calendar').fullCalendar({
            header: {
                    left: 'today',
                    center: 'prev,month,agendaWeek,agendaDay,next',
                    right: 'title'
            },
            weekMode : 'liquid',
            selectable: true,
            lazyFetching: false,
            selectHelper: true,
            eventResize: function(event, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view) {
                updateEvent(event.id,event.title,event.start,event.end,event.allDay)
            },
            eventDrop : function(event, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view) {
                updateEvent(event.id,event.title,event.start,event.end,event.allDay)
            },
            select: function(start, end, allDay) {
                selStart = start;
                selEnd = end;
                selAllDay = allDay;
                Ext.MessageBox.prompt('Event', 'Title:', saveEvent);
                calendar.fullCalendar('unselect');
            },
            editable: true,
            /*
            eventSources:
            [
                    $.fullCalendar.gcalFeed('http://www.google.com/calendar/feeds/usa__en%40holiday.calendar.google.com/public/basic')
            ],
            */
            events: function(start, end, callback) {

                Ext.Ajax.request({
                   url: "index.php?fuse=admin&action=AdminGetCalendarEvents",
                   success: function(t) {
                       json = ce.parseAjaxResponse(t);

                       var events = [];
                       for (var i=0; i < json.data.length; i++) {
                          events.push({
                            id: json.data[i].id,
                            title: json.data[i].title,
                            start: json.data[i].start,
                            end: json.data[i].end,
                            allDay : json.data[i].allDay,
                            url : json.data[i].url,
                            className: addUser(json.data[i].userid),
                            editable : json.data[i].editable,
                            description: json.data[i].descroption
                          });
                       }

                       callback(events);

                   },
                   params: {start: Math.round(start.getTime() / 1000),
                            end: Math.round(end.getTime() / 1000)
                          }
                });
            },
            eventClick: function(event) {
                 // opens events in a popup window
                 ShowEventForm(event.id);
            },
            eventRender: function(event, element) {
                //element.css("background-color","red");
                //element.css("border-color","red");
            }


	});

    <?php
        if ($this->mode != "month") {
    ?>
        $('#calendar').fullCalendar( 'changeView', '<?php echo $this->mode; ?>' );
    <?php
        }
    ?>
		
	});
</script>

<div style="padding-top:20px;width: 98%;margin: 0 auto;" id='calendar'></div>

<!--
[
				{
					title: 'All Day Event',
					start: new Date(y, m, 1)
				},
				{
					title: 'Long Event',
					start: new Date(y, m, d-5),
					end: new Date(y, m, d-2)
				},
				{
					id: 999,
					title: 'Repeating Event',
					start: new Date(y, m, d-3, 16, 0),
					allDay: false
				},
				{
					id: 999,
					title: 'Repeating Event',
					start: new Date(y, m, d+4, 16, 0),
					allDay: false
				},
				{
					title: 'Meeting',
					start: new Date(y, m, d, 10, 30),
					allDay: false
				},
				{
					title: 'Lunch',
					start: new Date(y, m, d, 12, 0),
					end: new Date(y, m, d, 14, 0),
					allDay: false
				},
				{
					title: 'Birthday Party',
					start: new Date(y, m, d+1, 19, 0),
					end: new Date(y, m, d+1, 22, 30),
					allDay: false
				},
				{
					title: 'Click for Google',
					start: new Date(y, m, 28),
					end: new Date(y, m, 29),
					url: 'http://google.com/'
				}
			]
-->