function Filters() {
	this._currentFilters = new Array();
}

Filters.prototype._currentFilters;

Filters.prototype.addCategory = function(category) {
	this._currentFilters.push(category);
	this._currentFilters.sort();
};

Filters.prototype.removeCategory = function(category) {
	this._currentFilters = $.grep(this._currentFilters,function(value) {
			return value != category;
		}
	);
};

Filters.prototype.getCategories = function() {
	return this._currentFilters;
};

Filters.prototype.hasCategory = function(category) {
	var stringOfArray = this._currentFilters.toString();
	if (stringOfArray.search(category) != -1) {return true;};
	return false;
};

var filters = new Filters();

function getCategory (node) {
	//$("textarea").val(node.attr('id'));
	return node.attr('id').substring(4);
};

function getCategoryID (node) {
	//$("textarea").val(node.attr('id'));
	return node.substring(4);
};

function initFilters() {
	$("#mn-filters li a").each(function(index) {
		$(this).addClass("checked");
		//$(this).children("ul").addClass("expandable");
		filters.addCategory(getCategory($(this)));
	});
	//$("li ul li:has(ul)").addClass("expandable");  // added .children('a').
	//$(this).find('li').addClass("expandable");
	//$("li ul li:has(ul li)").addClass("expandable");
	//$(this).children(":has(ul)").addClass("expandable");
	//$('.expandable ul').hide();
}

function checkboxes() {
	$("#mn-filters li a").click(function() {
		var parent = $(this).parent();
		var classes = parent.find("li a");	
		var temp = (parent.find("li a")).attr('id');
		
		if ($(this).hasClass("unchecked"))
		{
			$(this).removeClass("unchecked");
			$(this).addClass("checked");
			parent.find("li a").each(function(i){
				filters.addCategory(getCategory($(this)));
				$(this).removeClass("unchecked");
				$(this).addClass("checked");
				$(this).parent().css("backgroundColor","#690");
			});
			/*
			if (!(classes.size() <= 0)) {
				parent.find("li a").removeClass("unchecked");	
				parent.find("li a").addClass("checked");
				//filters.addCategory(getCategory(parent.find("li a")));
				for (var i=0; i < classes.size(); i++) {
					//filters.addCategory(getCategoryID(classes[i]));
					 $("textarea").val($("textarea").val() + temp);
				};
			};	*/
			filters.addCategory(getCategory($(this)));  // Shows hidden calendar events
			$(this).parent().css("backgroundColor","#690");
		} else {
			$(this).removeClass("checked");
			$(this).addClass("unchecked");	
			parent.find("li a").each(function(i){
				filters.removeCategory(getCategory($(this)));
				$(this).removeClass("checked");
				$(this).addClass("unchecked");	
				$(this).parent().css("backgroundColor","#e4e4e4");		
			});
			/*
			if (!(classes.size() <= 0)) {
				parent.find("li a").removeClass("checked");	
				parent.find("li a").addClass("unchecked");	
				//filters.removeCategory(getCategory(parent.find("li a")));
				for (var i=0; i < classes.size(); i++) {
					//filters.removeCategory(getCategoryID(classes[i]));
					alert(i);
					 //$("textarea").val($("textarea").val() + classes[i]);
				};
			};	
			*/
			filters.removeCategory(getCategory($(this))); // hides hidden calendar events
			$(this).parent().css("backgroundColor","#e4e4e4");		
		}
		
		return false;
	});
}

function filterEvents () {
	$('#mn-filters li a').click(function() {
		// Act on the event
		$('table li').each(function(index) {
			var found = false;
		
			// if the li category does not exist within filters, hide it!
			// don't forget multiple categories
		
			// gets the classes and splits them into an array
			var classes = $(this).attr("class").split(" ");
		
			// see if any of the classes exist within the currently checked filters
			for (var i=0; i < classes.length; i++) {
				if (filters.hasCategory(classes[i])) {
					found = true;
				};
			};
		
			if (found) {
				$(this).show();
			} else {
				$(this).hide();
			};
		});
	});
}

$(document).ready(function(){
	initFilters();

	//expandFilter();
	checkboxes();
	filterEvents();
	//$('.expandable').click();
});