jQuery(document).ready(function() {

    // Add Favourite
    // AJAX request to add the current page title and location/address to the Member Favourites 
    jQuery('#favourite').click(function(){
        $(this).css('background', 'url(http://overcomingms.php5.innovanet.co.nz/images/favourites-on.gif)').hide().fadeIn('fast').fadeOut('fast', function(){
        $(this).css('background', '').show();
        });

        jQuery.ajax({
            url: "/html/memberFavourite.php",
            data : {
                title: document.title,
                link: location.href,
                action: 'add'
                },
            async: true,
            type: "POST",
            success: function(data) {
                if (data == "") {
                    jQuery('#message').html('Please login to save favourites');
                    jQuery('#message').show().delay(3000).fadeOut('slow', function(){
                        jQuery(this).html('');
                    });
                }
            }
        });
        // Stop unwanted scrolling from the  href='#' in the link
        return false;
    });

    // Remove Favourite
    // AJAX request to remove favourite and hide from current page.
    jQuery('.favRemove').click(function(){
        // Link parent will be the li element which we want to remove.
        var linkparent = jQuery(this).parent(); 
        var documentcode = linkparent.attr('id').split('-')[1];
        jQuery.ajax({
            url: "/html/memberFavourite.php",
            data : {
                documentcode: documentcode,
                action: 'delete'
                },
            async: false,
            type: "POST",
            success: function(data) {
                linkparent.slideUp('slow');
            }
        });  
        // Stop unwanted scrolling from the  href='#' in the link
        return false;
    });

});

