// Permet l'execution d'une fonction une fois le document DOM charge.
$(document).ready(function(){

    $('.check_step').click( function () {
        p = $(this).parent().parent().parent().parent().parent();
        c1 = p.children('.check');
        c2 = p.children('.noCheck');
        if (c1.css('display') == 'none') {
            c1.css('display', 'block');
            c2.css('display', 'none');
        } else {
            c2.css('display', 'block');
            c1.css('display', 'none');
        }
        $.get(
            '/plugins/bookmarks/ajax_mod_step.php',
            {step : c1.attr('id').replace('check_', ''), id_bookmark_key : $('#id_bookmark_key').val()}
        );
        return false
    });

    $("#mycarousel").jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

});

/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel) {
    $('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

function upd_notes(type) {
    $.post('/plugins/bookmarks/ajax_mod_note.php',
        {id_bookmark_key : $('#id_bookmark_key').val(), type: type, valeur: document.getElementById(type).value},
        function(data) {
            text = 'Une erreur est survenue';
            if (data=='OK') {
                text = 'Vos notes on &eacute;t&eacute; mises &agrave; jour';
            }
            jQuery.noticeAdd({
                 text: text,
                 stay: false,
                 type: 'succes'
            });
        }
    );
    return false;
}

function add_bookmark(id_bookmark_key, id_booked, id_rubrique) {
    if (id_bookmark_key!=null && id_bookmark_key!='' && id_bookmark_key!=0) {
        $.get(
            '/plugins/bookmarks/ajax_add_bookmark.php',
            {id_bookmark_key : id_bookmark_key, id_booked: id_booked, id_rubrique: id_rubrique},
            function(data) {
                text = 'Une erreur est survenue';
                switch(data) {
                    case 'BAD_ARGS':
                    case 'BAD_USER':
                        text = 'Vous devez cr&eacute;er un compte<br /> pour pouvoir y ajouter des favoris.';
                        break;
                    case 'BOOKMARKED':
                        text = 'Cet &eacute;l&eacute;ment fait d&eacute;j&agrave; partie de vos favoris';
                        break;
                    case 'OK':
                        text = 'Cet &eacute;l&eacute;ment a &eacute;t&eacute; ajout&eacute; &agrave; vos favoris';
                        break;
                }
                jQuery.noticeAdd({
                     text: text,
                     stay: false,
                     type: 'error'
                });
            }
        );
    } else {
        jQuery.noticeAdd({
            text: 'Vous devez cr&eacute;er un compte<br /> pour pouvoir y ajouter des favoris.',
            stay: false,
            type: 'error'
        });
    }
    return false;
}

function del_bookmark(id_bookmark) {
    $.get(
        '/plugins/bookmarks/ajax_del_bookmark.php',
        {id_bookmark: id_bookmark},
        function(data) {
            text = 'Une erreur est survenue';
            if (data=='OK') {
                text = 'Cet &eacute;l&eacute;ment a &eacute;t&eacute; retir&eacute; de favoris';
                $('#bookmark' + id_bookmark).hide('slow');
            }
            jQuery.noticeAdd({
                 text: text,
                 stay: false,
                 type: 'error'
            });
        }
    );
    return false;
}
