var DM_Widget_Notebox = {
    rules : {
        'textarea#new_note' : function(element)
        {
            element.onclick = function()
            {   
                if(DM_Widget_Notebox.defaultText == element.value)
                {
                    element.value = '';
                }
            };
        },
        'div.message': function(element)
        {
            element.onmouseover = function()
            {
                element.firstChild.nextSibling.nextSibling.style.display = 'block';  
            };
            element.onmouseout = function()
            {
                element.firstChild.nextSibling.nextSibling.style.display = 'none';  
            };
        },
        'a.delete_post_group' : function(element)
        {
            var noteItem = element.parentNode.parentNode;
            element.onclick = function()
            {
                DM_Widget_Notebox.currentElement = element;
                var id = findParamInClass('id', noteItem);
                ajax_call('notebox', 'delete_group_note', id , DM_Widget_Notebox.noteDeleted);
                return false;
            };
        },
        'a.delete_post_user' : function(element)
        {
            var noteItem = element.parentNode.parentNode;
            element.onclick = function()
            {
                DM_Widget_Notebox.currentElement = element;
                var id = findParamInClass('id', noteItem);
                ajax_call('notebox', 'delete_user_note', id , DM_Widget_Notebox.noteDeleted);
                return false;
            };
        },
        'div.edit_post_user' : function(element)
        {
            var noteItem = element.parentNode.parentNode;
            element.onclick = function()
            {
                var id = findParamInClass('id', noteItem);
                
                ajax_call
                (
                    'notebox', 
                    'edit_post_user', 
                    id,
                    function(status){ 
                        $(noteItem).replace(status);  
                        EventSelectors.start(DM_Widget_Notebox.rules); 
                    }
                );
                
                return false;
            };
        },
        'div.edit_post_group' : function(element)
        {
            var noteItem = element.parentNode.parentNode;
            element.onclick = function()
            {
                var id = findParamInClass('id', noteItem);
                
                ajax_call
                (
                    'notebox', 
                    'edit_post_group', 
                    id,
                    function(status){ 
                        $(noteItem).replace(status);  
                        EventSelectors.start(DM_Widget_Notebox.rules); 
                    }
                );
                
                return false;
            };
        },
        'input.submit_edit_post_user' : function(element)
        {
            var noteItem = element.parentNode.parentNode.parentNode;
            var noteForm = element.parentNode.parentNode;
            element.onclick = function()
            {
                DM_Widget_Notebox.noteSubmitEdit(noteItem, noteForm, 'user');
                return false;
            };
        },
        'input.cancel_edit_post_user' : function(element)
        {
            var noteItem = element.parentNode.parentNode.parentNode;
            element.onclick = function()
            {
                DM_Widget_Notebox.noteCancelEditMode(noteItem, 'user');
                return false;
            };
        },
        'input.submit_edit_post_group' : function(element)
        {
            var noteItem = element.parentNode.parentNode.parentNode;
            var noteForm = element.parentNode.parentNode;
            element.onclick = function()
            {
                DM_Widget_Notebox.noteSubmitEdit(noteItem, noteForm, 'group');
                return false;
            };
        },
        'input.cancel_edit_post_group' : function(element)
        {
            var noteItem = element.parentNode.parentNode.parentNode;
            element.onclick = function()
            {
                DM_Widget_Notebox.noteCancelEditMode(noteItem, 'group');
                return false;
            };
        }
    },
    initialize : function()
    {
        Event.onDOMReady(
            function() {
                EventSelectors.start(DM_Widget_Notebox.rules);
            }
        );
    },
    noteDeleted: function()
    {
        Effect.Fade($(DM_Widget_Notebox.currentElement).parentNode.parentNode);
    },
    noteSubmitEdit: function(noteItem, noteForm, noteType)
    {
        if (
            !noteItem || 
            !noteForm ||
            ( noteType != 'user' && noteType != 'group' )
        ) {
            return;
        }
        
        
        var id = findParamInClass('id', noteItem);
        var postData = Form.serialize(noteForm);
        
        ajax_call
        (
            'notebox', 
            'submit_edit_post_' + noteType, 
            postData,
            function(status)
            { 
                $(noteItem).replace(status);  
                EventSelectors.start(DM_Widget_Notebox.rules); 
            }
        );
    },
    noteCancelEditMode: function(noteItem, noteType)
    {
        if (
            !noteItem || 
            ( noteType != 'user' && noteType != 'group' )
        ) {
            return;
        }
        
        var id = findParamInClass('id', noteItem);
        
        ajax_call
        (
            'notebox', 
            'submit_edit_post_' + noteType, 
            id,
            function(status)
            { 
                $(noteItem).replace(status);  
                EventSelectors.start(DM_Widget_Notebox.rules); 
            }
        );
    },
    textCounter: function(text, counter, maxlimit)
    {
        if (text.value.length > maxlimit)
        {
            text.value = text.value.substring(0, maxlimit);
        }
        else
        {
            counter.innerHTML = text.value.length;
        }
    }
};

DM_Widget_Notebox.initialize();