var lion;

lion = {
    init: function() {
        lion.messaging.init();
        lion.comments.init();
        lion.companies.init();
        lion.community.init();
        lion.users.init();
    },

    messaging: {
        init: function() {
            $('#message-reply-body').live('keydown', function(e, ui) {
                if (e.which == 13 || e.keyCode == 13) {
                    if ($('#messaging-send-enter').attr('checked')) {
                        $('#messaging-reply').submit();
                        return false;
                    }
                }
            })
        },

        focus: function() {
            $('#message-reply-body').focus();
        }

    },

    comments: {
        init: function() {
            $('#comment-body').live('keydown', function(e, ui) {
                if (e.which == 13 || e.keyCode == 13) {
                    if ($('#comment-send-enter').attr('checked')) {
                        $('#comment').submit();
                        return false;
                    }
                }
            })
        }

    },

    companies: {
        init: function() {
            $('.company-cell-edit').live('click', function() {
                if ( $(this).attr('contenteditable') == undefined ) {
                    $(this).editableText({ newlinesEnabled: false });
                    $(this).click();
                }
            });

            $('.company-cell-edit').live('change', function() {
                airy.socket.emit('post', '/companies/change/', { 'company_id': $('#company-name').attr('company'),
                                                                 'field': $(this).attr('field'),
                                                                 'value': $(this).text()
                });
            });

            $('.companies-add-complex-field').live('click', function() {
                airy.socket.emit('get', '/companies/company/' + $('#company-name').attr('company') + '/add/' + $(this).attr('field'));
            });

            $('.companies-delete-complex-field').live('click', function() {
                airy.socket.emit('post', '/accounts/profile/delete/', { 'field': $(this).attr('field'), 'object_id': $(this).attr('target') });
                $(this).parent().remove();
            });
        }
    },

    community: {
        init: function() {
            $('.community-delete-comment').live('click', function() {
                var a = airy.socket.emit('post', '/community/comment/delete/', {'comment_id': $(this).attr('comment-id')});
            });
        }
    },

    users: {
        init: function() {
            $('.profile-cell-edit').live('click', function() {
                if ( $(this).attr('contenteditable') == undefined ) {
                    $(this).editableText({ newlinesEnabled: false });
                    $(this).click();
                }
            });

            $('.profile-cell-edit').live('change', function() {
                airy.request('post', '/accounts/profile/change/', { 'field': $(this).attr('field'), 'value': $(this).text() }, true);
            });

            $('.delete-complex-field').live('click', function() {
                airy.request('post', '/accounts/profile/delete/', { 'field': $(this).attr('field'), 'object_id': $(this).attr('target') }, true);
                $(this).parent().remove();
            });

            $('.add-complex-field').live('click', function() {
                airy.request('get', '/accounts/profile/' + $(this).attr('field') + '/add', null, true);
            });

            $('.cancel-on-form').live('click', function() {
                airy.ui.insert("#" + $($(this).parent()).parent().attr('id'), "");
            });
        },

        picture_init: function() {
            var holder = document.getElementById('profile-picture');

            if (typeof window.FileReader === 'undefined') {
                $('#profile-picture-drop').remove();
                return;
            }

            holder.ondragover = function () { $(this).addClass('drop'); return false; };
            holder.ondragend = function () { $(this).removeClass('drop'); return false; };
            holder.ondrop = function (e) {
                $(this).removeClass('drop');
                e.preventDefault();

                var file = e.dataTransfer.files[0],
                    reader = new FileReader();

                reader.onload = function (event) {
                    var data = {'picture': {'name': file.name, 'content': event.target.result}};
                    airy.request('post', '/accounts/profile/picture/upload', data, true);
                };
                reader.readAsDataURL(file);

                return false;
            };
        }
    },

    notify: {
        success: function(text, title) {
            lion.notify.add(text, title, '/static/js/gritter/images/success.png');
        },

        info: function(text, title) {
            lion.notify.add(text, title, '/static/js/gritter/images/info.png');
        },

        warning: function(text, title) {
            lion.notify.add(text, title, '/static/js/gritter/images/warning.png');
        },

        error: function(text, title) {
            lion.notify.add(text, title, '/static/js/gritter/images/error.png');
        },

        add: function(text, title, image) {
            if (!title) {
                title = ' ';
            }
            $.gritter.add({
                'title': title,
                'text': text,
                'image': image,
            })
        }
    },

    editor: function() {
        $('textarea').each(function() {
            $(this).wysiwyg({
                initialContent: $(this).html()
            });
            var cssLink = document.createElement("link");
            cssLink.href = "/static/stylesheets/foundation.css";
            cssLink.rel = "stylesheet";
            cssLink.type = "text/css";
            frames['id_body-wysiwyg-iframe'].document.body.appendChild(cssLink);
            var cssLink = document.createElement("link");
            cssLink.href = "/static/stylesheets/style.css";
            cssLink.rel = "stylesheet";
            cssLink.type = "text/css";
            frames['id_body-wysiwyg-iframe'].document.body.appendChild(cssLink);
        });
    }
}

$(function() {
    // jquery loaded - let's start!

    lion.init();
})
