/**
 *
 * Handles dynamic listing filtration based on boxes that have been clicked on
 * the side.
 *
 */

var listing = "i"

$(document).ready(function() {


	var totalChecked = $("form.filter").find("input:checkbox[checked]").size();

	// If more than 1 checkbox is checked
	if(totalChecked > 1) {
		// Add the Select All checkbox to the beginning
		$('form.filter div').prepend('<label class="SelectAll"><input type="checkbox" name="selectall" value"selectall" checked="checked"> Select All</label>')
	
	} else {
		$('form.filter div').prepend('<label class="SelectAll"><input type="checkbox" name="selectall" value"selectall"> Select All</label>')
	}
	
	// Select All Functionality
	$('form.filter div input[name="selectall"]').click(function() {
		if($(this).attr('checked')) {
			$('form.filter div input').each(function() {
				$(this).attr('checked','checked');
			});
		} else {
			$('form.filter div input').each(function() {
				$(this).removeAttr('checked');
			});
		}
	});

	/**
	 *
	 * The actual click event really just handles what to do with the
	 * "select all" feature and then calls filterFeed()
	 *
	 */
	$("form.filter input:checkbox").click(function(){
		// Count how many boxes are checked
		var totalChecked = $("form.filter").find("input:checkbox[checked]").size();
		var totalBoxes = $("form.filter").find("input").size();
		
		if(totalChecked < totalBoxes) {
			$('form.filter div input[name="selectall"]').removeAttr('checked');
		}

		if(totalChecked > 0) {
			
			filterFeed($(this));	// If more than 0, filter the feed
			
		} else {
			// If no boxes are checked, add error message
			$('#stream .posts').html('<h4>TheChange is still growing!</h4><p>No results could be found that match your search criteria.</p>').hide(0).fadeIn(600);
			$('#stream .more').remove();
		}
	});

	$("ul.community-navigation li a").click(function(){

		var loggedOut = $("#UserInfo").find("#LogIn").length;
		if(loggedOut == 0) {
		
			listing = $(this).attr("alt");

			$(this).parent().parent().children("li").each(function(){
				$(this).removeClass("active");
			});
	
			$(this).parent().addClass("active");
		
			filterFeed(null);
		} else {
			window.location = "http://brad.thechange.com/accounts/registration/"
		}

		

		return false;

	});

});

function checkTotalFeedThings() {
	var totalLoaded = $("#stream .posts").children("div:not(.loaded)").size();
	var totalOld = $("#stream .posts").children("div").size();
	var morelink = true;
	
	//alert(totalLoaded);
	
	if(totalLoaded < 20) {
		if(totalOld != 0) {
			//$("#stream .more").parent().append("<span class=\"moreStatus\">No more content to load.</span>").hide(0).fadeIn(300);
			$("#stream .more").remove();
			morelink = false;
		} else {
			$('#stream .posts').html('<h4>TheChange is still growing!</h4><p>No results could be found that match your search criteria.</p>').hide(0).fadeIn(600);
			$('#stream .more').remove();
		}
	}
}


function filterFeed(checkbox) {

	var ft = 100; // Fade time

	var interests = listing_filter_checkbox(checkbox);

	url = "http://" + document.location.hostname + ":" + document.location.port + document.location.pathname + "community/subset/";

	var params = {
		"interests": interests,
		"selection": listing
	}

	/*
	 *
	 * JQuery does things the mangled, PHP way, so we have to perpetuate the
	 * stupidity.
	 *
	 */
	var mangled_interests = ""
	for (i in interests) {
		mangled_interests += "interests%5B%5D=" + interests[i] + "&";
	}

	$.get(url, params, function(html){
			html = '\
			<div class="posts">\
			' + html + '\
			</div>\
			\
			<div class="pagination">\
				<a href="' + url + '?' + mangled_interests + '" alt="2" class="more">More</a>\
			</div>\
		';
		$("#stream").fadeOut(ft, function() {
			$("#stream").html(html).fadeIn(ft, function(){
				// Re-bind the .click() event on the "more" link
				$("#stream .pagination a.more").click(feed_getMore);
				checkTotalFeedThings()
			});
		});
	});

}


function listing_filter_checkbox(obj) {

	if (obj) {
		if (obj.hasClass("selectall")) {

			var toggle = (obj.attr("checked")) ? true : false;
			$("form.filter input:checkbox").each(function(){
				obj.attr("checked", toggle);
			});

		} else {

			var selectall = $("form.filter input.selectall");
			if (selectall.attr("checked") && !obj.attr("checked")) {
				selectall.attr("checked", false);
			}

		}
	}

	var interests = []
	$("form.filter input:checkbox").each(function(){
		if ($(this).attr("checked")) {
			interests.push($(this).val());
		}
	});

	return interests;

}

