$(document).ready(
    function()
    {
        // Event to capture click of a content item
        $('.newsitem_delete_button').click(
            function()
            {
                id = $(this).attr('id').substr(9);
                if (confirm('Weet u zeker dat u het geselecteerde nieuwsbericht wilt verwijderen? Dit kan niet ongedaan worden gemaakt!')) {
                    window.location = rootUrl + 'news/cms/delete-item/id/' + id;
                }
            }
        );
        
        // Right click function for photos
        $('img.context').contextMenu(
            'photoContextMenu', 
            {
                bindings: {
                    'delete_photo': function(t) {
                                        $.post(rootUrl + 'news/cms/delete-photo', 'id=' + $(t).attr('id').substr(6));
                                        $('<span id="upload_button_' + $(t).parent().attr('id').substr(6) + '" class="button"><img src="' + rootUrl + 'public/images/default/common/icons/picture_add.png" alt="Foto toevoegen"/> &nbsp; Foto toevoegen</span>').insertAfter('#' + $(t).parent().attr('id') + ' > h2');
                                        $(t).hide('slow', function(){ $(this).remove();});
                                        
                                        bindNewsItemUploadButtons();
                                    }
                }
            }
        );
        
        // Creating defined ajax uploaders
        bindNewsItemUploadButtons();
    }
);

function bindNewsItemUploadButtons()
{
    $('.newsitem').each(
            function(i, el)
            {
                id = 'upload_button_' + $(this).attr('id').substr(9);
                
                // Make sure the button exists
                if ($('#' + id).attr('id') != undefined) {
                    bindNewsItemUploadButton(id);
                }
            }
        );
}

function bindNewsItemUploadButton(id)
{
    new AjaxUpload(
                id,
                {
                    action:         rootUrl + 'news/cms/add-photo/id/' + id.substr(14),
                    onComplete:     function(file, response) {
                                        $('#' + id).replaceWith('<img class="context" src="' + response + '?r=' + randomString() + '" alt="" id="photo_' + id + '" />');

                                        $('img.context').contextMenu(
                                            'photoContextMenu', 
                                            {
                                                bindings: {
                                                    'delete_photo': function(t) {
                                                                        $.post(rootUrl + 'news/cms/delete-photo', 'id=' + $(t).parent().attr('id').substr(9));
                                                                        $('<span id="upload_button_' + $(t).parent().attr('id').substr(9) + '" class="button"><img src="' + rootUrl + 'public/images/default/common/icons/picture_add.png" alt="Foto toevoegen"/> &nbsp; Foto toevoegen</span>').insertAfter('#' + $(t).parent().attr('id') + ' > h2');
                                                                        $(t).hide('slow', function(){ $(this).remove();});
                                                                        bindNewsItemUploadButtons();
                                                                    }
                                                }
                                            }
                                        );
                                    }
                }
            );
}
