$(document).ready(
    function()
    {
        if (identified) {
            // Event to change title fields of the current page while editing
            $('input[name=title]').keyup(
                function()
                {
                    $('.input_title').html($(this).val());
                }
            );
            
            // Event to capture click of a content item
            $('.delete_button').click(
                function()
                {
                    pagetitle = $(this).next().html().replace(/^\s+/,'').replace(/\s+$/,'');
                    id = $(this).attr('id').substr(8);
                    
                    if (confirm('Weet u zeker dat u de pagina "' + pagetitle + '" wilt verwijderen? Dit kan niet ongedaan worden gemaakt!')) {
                        window.location = rootUrl + 'content/cms/delete-page/id/' + id;
                    }
                }
            );
            
            // Drop / drag settings for submenus
            $('ul.submenu').sortable(
                {
                    cancel:     '.cancel',
                    stop:       function(event, ui) {
                                    // Repaint the list
                                    $('li', this).removeClass('first');
                                    $('li:first', this).addClass('first');
                                    
                                    // Sent the sorting results to the server
                                    $.post(rootUrl + 'content/cms/order-pages/group/' + $('#contentItemGroupId').val(), $(this).sortable('serialize'));
                                }
                }
            );
            
            // Drop / drag settings for contentphotos
            $('ul.contentphotos').sortable(
                {
                    cancel:     '.cancel',
                    stop:       function(event, ui) {
                                    // Sent the sorting results to the server
                                    $.post(rootUrl + 'content/cms/order-photos/id/' + $('#contentItemId').val(), $(this).sortable('serialize'));
                                }
                }
            );
            
            // Right click function for photos
            $('ul.contentphotos > li').contextMenu(
                'photoContextMenu', 
                {
                    bindings: {
                        'delete_photo': function(t) {
                                            $.post(rootUrl + 'content/cms/delete-photo/', 'id=' + $('#contentItemId').val() + '&position=' + $(t).attr('id').substr(4));
                                            $(t).hide('slow');
                                            if ($('#singlePhoto').attr('id') != undefined) {
                                                $('.contentphotos').append('<li><span id="upload_button" class="button"><img src="' + rootUrl + 'public/images/default/common/icons/picture_add.png" alt="Foto toevoegen"/> &nbsp; Foto toevoegen</span></li>');
                                                bindUploadButton();
                                            }
                                        }
                    }
                }
            );
            
            // Creating defined ajax uploaders
            if ($('#upload_button').attr('id') != undefined) {
                bindUploadButton();
            }
        }
    }
);

function bindUploadButton()
{
    new AjaxUpload(
                'upload_button', 
                {
                    action:         rootUrl + 'content/cms/add-photo/id/' + $('#contentItemId').val(),
                    onComplete:     function(file, response) {
                                        $('ul.contentphotos > li:last').before('<li id="pid_' + ($('ul.contentphotos > li').length - 1) + '"><img src="' + response + '?r=' + randomString() + '" alt="" /></li>');
                                        $('ul.contentphotos > li').contextMenu(
                                            'photoContextMenu', 
                                            {
                                                bindings: {
                                                    'delete_photo': function(t) {
                                                                        $.post(rootUrl + 'content/cms/delete-photo/', 'id=' + $('#contentItemId').val() + '&position=' + $(t).attr('id').substr(4));
                                                                        $(t).hide('slow');
                                                                        if ($('#singlePhoto').attr('id') != undefined) {
                                                                            $('.contentphotos').append('<li><span id="upload_button" class="button"><img src="' + rootUrl + 'public/images/default/common/icons/picture_add.png" alt="Foto toevoegen"/> &nbsp; Foto toevoegen</span></li>');
                                                                            bindUploadButton();
                                                                        }
                                                                    }
                                                }
                                            }
                                        );
                                        if ($('#singlePhoto').attr('id') != undefined) {
                                            $('#upload_button').remove();
                                        }
                                    }
                }
            );
}
