         'click .dropdown-toggle': 'hideColorPicker'
         'click .calendar-color': 'showColorPicker'
         'change .color-picker': 'setColor'
-
-
+        'blur input.calendar-name': 'onRenameValidation'
+        'keyup input.calendar-name': 'onRenameValidation'
 
     getRenderData: ->
         label: @model.get 'name'

        # focus the input and select its value
        input.focus()
        input[0].setSelectionRange 0, calendarName.length

        # Binds event to actually rename the calendar (enter key and blur)
        input.blur (event) =>
            console.log(calendarName)
            @onRenameValidation input, calendarName

        input.keyup (event) =>
            key = event.keyCode or event.charCode
            if key is 13 # enter key
                @onRenameValidation input, calendarName
            else
                @buildBadge colorhash input.val()

        # Close the form and restore original state when user presses "escape"
        $(document).keyup restore = (event) =>
            key = event.keyCode or event.charCode
            if key is 27 # escape key
                # removes the binding to prevent memory leak
                $(document).off 'keyup', 'document', restore
                input.off 'keyup'

                # re-appends text element
                input.remove()
                rawTextElement.insertAfter @$('.badge')

                # Restores the badge color
                @buildBadge calendarName

                # Shows the menu again
                @$('.dropdown-toggle').show()

    onRemoveCalendar: ->
        calendarName = @model.get 'name'

        # Gone after succefull color pick, put it back.
        @$('.dropdown-toggle').on 'click', @hideColorPicker

    #setting rename validation function to handle esc / return key, and blur event
    onRenameValidation: (event) ->

        input = $(event.target)
        calendarName = @model.get 'name'
        @startSpinner()

        # removes the binding to prevent memory leak
        key = event.keyCode or event.charCode
        if key is 27 # escape key
            input.remove()
            # re-appends text element
            @rawTextElement.insertAfter @$('.badge')
            # Restores the badge color
            @buildBadge calendarName
            # Shows the menu again
            @$('.dropdown-toggle').show()
            @stopSpinner()
        #blur and enter have the same effect
        else if (key is 13 or event.type is 'focusout')
            app.calendars.rename calendarName, input.val(), =>
                @stopSpinner()

    onRenameCalendar: ->
        calendarName = @model.get 'name'
