$(document).ready(function(){
    $('#more-viewing-options input[type="checkbox"], #display-options input[type="checkbox"]').click(function(){
        $(this).parents('form').submit();
    });
});

function updateSavedAdsFooter (id, action) {
    if (action == 'add') {
    
        var containingLi = $('input#save-'+id).parents('li.hlisting');
        
        var adThumbHTML = containingLi.find('div.thumbnail a').html(); // <img> tag for thumbnail
        var adLink = containingLi.find('div.description a').attr('href'); // URL to ad
        var adTitle = containingLi.find('div.description a').html(); // ad title
        var adLocation = containingLi.find('div.description span.locality').text(); // ad location
        
        // check there is a <ul> already there showing saved ads
        if ($('#saved-ads ul.carousel-ads').length > 0) {
            var carouselSavedAds = $('#saved-ads ul.carousel-ads li');
            var carouselCount = $('#saved-ads ul.carousel-ads').length;
            
            // if carousel is already full, drop the last item first
            if (carouselSavedAds.length == 6) {
                carouselSavedAds.slice(5,6).remove();
            }
        // if not, create it
        } else {
            var carouselCount = 0;
            $('#saved-ads div.empty').remove(); // remove the notification
            
            $('#saved-ads').prepend('<ul class="carousel-ads"></ul><p class="view-all"><a href="/cgi-bin/my_saved_ads.pl">View all</a></p>'); // create empty <ul> for ads
        }
        
        
        
        // insert the new saved ad to the carousel
        insertSavedAdInFooter(id, adLink, adTitle, adThumbHTML, adLocation);
        
        
    } else {
        // remove the ad from the carousel
        $('#saved-ads li#saved-ad-'+id).remove();
    }
    
    
    // Tidy up class names
    updateSavedAdsFooterClassNames();
    
}

