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(){
    	var link =$(this).next();    	

        jQuery.ajax({
            url: "/html/memberFavourite.php",
            data : {
                title: link.text(),
                link: link.attr('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('');
                    //});
                }
                if (data.split("-")[0] == "memberFav"){
                	var removeLink = '<a class="favRemove" title="Remove" href="#">Remove</a> ';
                	var favouriteLink = '<a href="' + link.attr('href') + '" title="' + link.text() + '">' + link.text() + '</a> ';
                	jQuery('#favcontent').append('<li id="'+data+'">'+ favouriteLink + removeLink + '</li>');
                }
                
            }
        });
        
        // Stop unwanted scrolling from the  href='#' in the link
        return false;
    });
    
    // Add Favourite
    // AJAX request to add the current page title and location/address to the Member Favourites 
    jQuery('#favourite').click(function(){

        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('');
                    });
                }
                if (data.split("-")[0] == "memberFav"){
                	var removeLink = '<a class="favRemove" title="Remove" href="#">Remove</a> ';
                	var favouriteLink = '<a href="' + location.href + '" title="' + document.title + '">' + document.title + '</a> ';
                	jQuery('#favcontent').append('<li id="'+data+'">'+ favouriteLink + removeLink + '</li>');
                }                
            }
        });
        // Stop unwanted scrolling from the  href='#' in the link
        return false;
    });    
    

    // Remove Favourite
    // AJAX request to remove favourite and hide from current page.
    // Delegate remove click event so it is bound to subsequent 
    // Favourites taht are added from the links page
    jQuery('#favcontent').delegate('.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;
    });

});

