BFC = {
    initRecentArticles: function() {
		$("#recent-articles .scrollable").scrollable({ vertical: true });	
    },

    initTabStyling: function() {
		$("#home ul.tabs").tabs("div#featured-articles > article"); 

		$("div#community-members ul.tabs").tabs("div#community-members > div");

		$("#community-activity #home-community-recent .tabs ul").tabs("#home-community-recent div.tabs > div");

		$("#community-activity #recent-from-the-editors .tabs ul").tabs("#recent-from-the-editors div.tabs > div");

		$("#community-activity #recent-from-the-community .tabs ul").tabs("#recent-from-the-community div.tabs > div");

		$("#external-web ul.tabs").tabs("#external-web > div");
		
		$("#our-blog ul.tabs").tabs("#our-blog > div");
		
    },

  	initTrends: function() {
  	  // build the trend items string from the selected checkboxes
      function getTrendItems() {
        var params = '';
        jQuery('ul#themes-correlations').find('li.checked input').each(function(position, i) {
          // build up the string from the selected items (leave them selected!)
          params = params + jQuery(i).attr("value") + ',';
        });
        return params;
      }
      
      function startedLoading() {
        // show a spinner, disable checkboxes, whatever...
      }
      
      function finishedLoading() {
        // revert changes introduced with startedLoading()
      }
      
      // make sure AJaX is not called too many times in a short time
      var updateFx = null;
      function updateResults() {
        updateFx = updateResultsAJaX;
        setTimeout(function() {
          if (updateFx != null) {
            updateFx();
            updateFx = null;
          }
        }, 300);
      }
      
      // perform an AJaX call and update the results
      function updateResultsAJaX() {
        startedLoading();
        
        // ajaxurl SHOULD be defined but it may not be unless we are in admin
        var ajaxurl = '/wp-admin/admin-ajax.php';
        
        var trendItems = getTrendItems();
        //console.log("updateResults", trendItems);

        if (trendItems.length > 0) {
          var send_data = {
            action: 'trends_listing',
            slugs: trendItems,
            // what fields to return
            fieldset: ['post_format', 'post_type', 'post_taxonomy', 'post_name', 'post_content', 'post_author'],
            // format can be json | html | default = print_r style
            format: 'html'
          };
          jQuery.ajax({
            type: 'POST',
            url: ajaxurl,
            data: send_data,
            dataType: 'html',
            success: function(return_data, status) {
              jQuery('#trends-results').empty().html(return_data);
              finishedLoading();
            }
          })
        } else {
          // nothing selected -> clear page
          jQuery('#trends-results').empty();
          finishedLoading();
        }
      }
      
      function initCheckboxes() {
        var slugs = jQuery('#trend_items_text').val();
        if (slugs && slugs.length > 0) {
          jQuery.each(slugs.split(/\s*,\s*/), function(index, value) {
            if (value.length > 0) {
              jQuery('ul#themes-correlations li.' + value + " input").click().change();
            }
          });
        }
      }
    
      // update results when checkboxes are checked
      jQuery('ul#themes-correlations li input').change(function(event) {
        var elem = jQuery(this);
        var parent = elem.parent('ul#themes-correlations li');
        if (elem.is(':checked')) {
          parent.addClass('checked');
          parent.removeClass('unchecked');
        } else {
          parent.addClass('unchecked');
          parent.removeClass('checked');
        }
        
        updateResults();
      });
    
      // make sure clicking on links has the same effect as clicking on checkboxes
      jQuery('ul#themes-correlations li span').click(function(event) {
        var checkbox = jQuery(this).parent('ul#themes-correlations li').children('input');
        checkbox.click().change();
        event.preventDefault();
      });
      
      initCheckboxes();
  	},

    initComments: function() {
		$('h4 #more-link').append('<span class="open_contact"> + </span> <span class="close_contact hide"> - </span>');
		$('h4 #more-link').click(function() {
		    $(this).find('span.hide').removeClass('hide').siblings('span').addClass('hide');
		});
		$('h4 #more-link').click(function() {
		    $('#comments').slideToggle(400);
		    return false;
		});

		$('#commentform').submit(function() {
			if ($('#terms-of-use').is(':checked')) {
				return true;
			} else {
				alert("Please check the checkbox saying you agree with terms");
				return false;
			}
		});
    }
};

$(document).ready(function() {
	BFC.initRecentArticles();
	BFC.initTabStyling();
	BFC.initComments();
	BFC.initTrends();
});
