
$.fn.initializeEvent = function() {
    if(this.data('evntInitialized') === true)
        return this;
    //this.unbind();

    var eventObj = EventModel.ids[this.attr('ctrl-event-id')]; 
    
    // Preempt any bubbling. We NEED this for click n drag event drawing
    this.bind('mousedown', function(e) { e.stopPropagation(); } );

    this.children().attr('unselectable', 'on').css('-moz-user-select', 'none');

    this.css({
        'opacity' : 1
    });
    
    this.click(function(e){
        e.stopPropagation();

        if($(this).data('doNotOpenTheDialogPlease') === true)
            return;
        
        var event = EventModel.ids[$(this).attr('ctrl-event-id')];
        Dialog.html( new Ctrl_Calendar_Event_Decorator_DialogContents(event) ).dialog('open');

        Dialog.find('a.delete').click(function(){
            $.get(this.href, function(response) {
                Dialog.dialog('close');
                event.getNodes().remove();
                EventModel.unindex([event]);

                Ctrl.notify('Event Deleted', 'success');
            });
            
            return false;
        });
    });

    if(eventObj.editable) {
        if(eventObj.all_day) {
            
        } else {
            if(CalendarInstance.getView() != 'month') {
                this.draggable({
                    axis: 'y', // TODO: This is temporary!!!! Just until we make better sense of how to handle moving to a different day
                    zIndex: 111,
                    opacity: 0.7,
                    grid: [0, 10],
                    start: function(event, ui) {
                       event.stopPropagation();
                       event.preventDefault();
                       var xSnap = ui.helper.parent().width();
                       ui.helper.draggable('option', 'grid', [xSnap, 10]);
                       ui.helper.data('doNotOpenTheDialogPlease', true);
                    },
                    stop: function(event, ui) {
                        setTimeout(function(){ui.helper.data('doNotOpenTheDialogPlease', false);}, 100);
                        var eventObj = EventModel.ids[ui.helper.attr('wrk-event-id')]; 
                        var timeTxt = ui.helper.find('span.time').text().split(' - ');
                        var data = {
                            start_date: eventObj.timeStart.format('m/d/Y'),
                            start_time: timeTxt[0],
                            end_date: eventObj.timeStart.format('m/d/Y'),
                            end_time: timeTxt[1]
                        }

                        if(false === updateEvent(eventObj.ID, data))
                            CalendarInstance.view(null, CalendarInstance.getView()); // reset
                    },
                    drag: function (event, ui) { ui.helper.updateEventTime() }
                });
                
                this.resizable({
                    distance: 4,
                    grid: [10, 10],
                    handles: 's',
                    minHeight: 40,
                    start: function(event, ui) {
                        event.stopPropagation();
                        event.preventDefault();
                        ui.helper.data('doNotOpenTheDialogPlease', true);
                    },
                    stop: function(event, ui) {
                        setTimeout(function(){ui.helper.data('doNotOpenTheDialogPlease', false);}, 100);
                        var eventObj = EventModel.ids[ui.helper.attr('ctrl-event-id')]; 
                        var timeTxt = ui.helper.find('span.time').text().split(' - ');
                        var data = {
                            start_date: eventObj.start.format('m/d/Y'),
                            start_time: timeTxt[0],
                            end_date: eventObj.start.format('m/d/Y'),
                            end_time: timeTxt[1]
                        }

                        if(false === updateEvent(eventObj.ID, data))
                            CalendarInstance.view(null, CalendarInstance.getView());  // reset
                        
                    },
                    resize: function (event, ui) { ui.helper.updateEventTime() }
                });
            }
        }
    }
    
    this.data('evntInitialized', true);
    return this;
};





