// When the DOM is ready... kick off the jQuery love.
jQuery(document).ready(function(){

	// Initialise the links
	loaded_links();
	

	// Allow the user to load more and more links via
	// an ajax call, not having to reload the whole page
	jQuery('#load_more_mylinks').click(function(){
		var showing = jQuery('#my_links .link_details').size();

		jQuery.ajax({
			type: "POST",
			url: site_url+ ('/ajax/get_more_mylinks'),
			data: "start=" + (showing + 1),
			success: function(html){
				jQuery("#my_links .link_list").append(html);
				var now_showing = jQuery('#my_links .link_details').size();

				// If less than 5 more items were loaded, remove the load more link
				if(html == '' || ((showing + 5)>now_showing)){
					jQuery('#load_more_mylinks').remove();
				}
				loaded_links();
			}
		});
		return false;
	});
	
    
    
});


/************************************************
 * Since the links are often loaded dynamically,
 * The objects need to be hooked up with jQuery 
 * again, so this function needs to be called
 * every time links are loaded with ajax.
 ***********************************************/ 
function loaded_links(){
	// On hovering over a link, show the actions tab
    jQuery('.link_details').mouseenter(function(){
    	jQuery(this).addClass('link_hover');
    }).mouseleave(function(){
    	jQuery(this).removeClass('link_hover');
    	jQuery(this).find('a.confirm').hide();
    });


    // On clicking the delete link, show a confirmation link
    jQuery('.actions a.delete').click(function(e){
    	e.preventDefault(); 
    	jQuery(this).siblings('.confirm').show();
    	return false;
    });
    
    // On clicking the conirmation link, hide the link and delete it
    jQuery('.actions a.confirm').click(function(e){
    	e.preventDefault(); 
    	jQuery(this).parent().parent().fadeOut('slow');
    	jQuery.get(jQuery(this).attr('href'));
    	return false;
    });
    
    
    /*jQuery('.actions .copy').click(function(){
    	var linkurl = jQuery(this).parent().siblings('.shrunk').find('a').attr('href');
    	console.log(linkurl);
	   	jQuery.clipboard(linkurl);
    });*/
}