﻿(function($) {

	$.fn.tabbedCallout = function() {

		return this.each(function() {
			var wrapper = $(this),
				btns = $(".tabbed-buttons a", wrapper),
				content = $(".tabbed-callout-content > div", wrapper);

			content.each(function(i) {
				if (i === 0) {
					$(this).show();
				} else {
					$(this).hide();
				}
			});

			btns.each(function(i) {
				$(this).click(function() {
					var self = $(this),
						toShow = self.attr("href").substring(1);

					content.hide();
					$("." + toShow, wrapper).show();
					btns.removeClass("activeTab");
					self.addClass("activeTab");
					return false;
				});
			});

		});

	}

	$.fn.moreExpander = function() {

		this.each(function() {
			var wrapper = $(this),
				ctx = wrapper.find(".tabbed-content"),
				moreItems = $(".activity-post-details:gt(4)", ctx);

			moreItems.hide();

			wrapper.find(".more-expander a[href=#more]").live("click", function() {
				return toggleStates($(this), "less");
			});
			
			wrapper.find(".more-expander a[href=#less]").live("click", function() {
				return toggleStates($(this), "more");
			});
			
			function toggleStates(self, nextOp) {
			    var section = self.parent().parent();
			    
			    self.attr("href", "#" + nextOp).text(nextOp).attr("class", nextOp);
				var theRest = $(".activity-post-details:gt(4)", section);
				
				if(nextOp === "more") {
				    theRest.hide("slow");
				} else {
				    theRest.show("slow");
				}
				
				return false;
			}

		});

		return this;

	}

})(jQuery);
