/****************************************************************
*
*	Filter Object
*	Author: Michael Turnwall - Digitaria Inc.
*	Created: 5.22.2007
*	Filter elements
*
****************************************************************/

// id = id of filter options form or container
// className = class assigned to elements that can be filtered
// contID = optional starting point to start looking for the class name
// maxLength = number of items to show at a time
function Filter(id,className,contID,maxLength,stripeIt)
{
	this.contID = contID;
	this.id = id;
	this.filterCont = document.getElementById(id);
	this.filterOptions = this.filterCont.getElementsByTagName("input");
	
	// dealing with a table or a bunch of divs?
	if(className.toLowerCase() == "tr")
	{
		var rows = document.getElementById(contID).getElementsByTagName("tr");
	
		this.filterElements = new Array();
		for(var i = 1; i < rows.length; i++)
		{
			this.filterElements.push(rows[i]);
		}
	}
	else
	{
		this.filterElements = this.getElementsByClassName(className,contID);
	}
	
	for(var i = 0; i < this.filterOptions.length; i++)
	{
		if(this.filterOptions[i].value.match("all"))
			this.filterOptions[i].checked = true;
		else
			this.filterOptions[i].checked = false;
	}
	
	this.max = maxLength;
	this.stripeIt = stripeIt;
	this.showAll(true);
	this.initEvents();
}
/*----------------------*/

// attach events to the filter options
Filter.prototype.initEvents = function()
{
	var self = this;	// so we can access this object from the custom event
	for(var i = 0; i < this.filterOptions.length; i++)
	{
		//this.filterOptions[i].onclick = function(){self.scanValues(this.value)};
		//this.filterOptions[i].onkeypress = function(){self.scanValues(this.value)};
		$(self.filterOptions[i]).click(function(){ self.scanValues(this.value) });
		$(self.filterOptions[i]).keypress(function(){ self.scanValues(this.value) });
	}
}
/*----------------------*/

// function that allows getting elements by their class name. Returns the found elements in an array
// arguments: className - class name to search for, element - a starting point to start the search
// if no element is specified, all the nodes on the page are searched
Filter.prototype.getElementsByClassName = function(className,element)
{
	if(element)
	{
		var el = document.getElementById(element);
		var children = el.getElementsByTagName('*');
	}
	else
	{
		var children = document.getElementsByTagName('*') || document.all;
	}
	
	var elements = [];
	var regexp = new RegExp(className,"ig");
	for(i = 0; i < children.length; i++)
	{
		if(children[i].className.match(regexp))
			elements.push(children[i]);
	}
	if(elements.length > 0)
		return elements;
	else
		return false;
}

// show all the items
Filter.prototype.showAll = function(firstTime)
{
	for(var i = 0; i < this.filterElements.length; i++)
	{
		//this.filterElements[i].className += " a"; // cheating, add the "a" so we can replace with a reg exp
		className = this.filterElements[i].className;	// assign elements classname to manipulate it
		
		// reset className to original
		className = className.replace(/\sshow|\shide/,"");
		
		var regexp = new RegExp("(\\sshow$|\\shide$)|"+className+"(?!\\S)");
		//alert(regexp);
		if(i < this.max)
			className = className.replace(regexp,className+" show");
		else
			className = className.replace(regexp,className+" hide");
		
		// take the edited className and give it back to the element
		this.filterElements[i].className = className;
	}
	$("#calendarContent>h4").show();

}
/*----------------------*/

// clear the board so we have a clean slate to put the filter events on
Filter.prototype.hideAll = function()
{
	for(var i = 0; i < this.filterElements.length; i++)
	{
		this.filterElements[i].className = this.filterElements[i].className.replace(/(\sshow|\shide)/," hide");
	}			
}
/*----------------------*/

// look through the filter options to see what's selected
Filter.prototype.scanValues = function(value)
{
	this.checkedFilters = new Array();
	if(!value.match("all"))
	{
		this.filterOptions[0].checked = false;
	}
	else
	{
		for(var i = 1; i < this.filterOptions.length; i++)
		{
			this.filterOptions[i].checked = false;
		}
		var all = true;
	}
	for(var i = 0; i < this.filterOptions.length; i++)
	{
		if(this.filterOptions[i].checked)
		{
			this.checkedFilters.push(this.filterOptions[i].value);
			
		}

	}
	if(this.checkedFilters.length == 0 || all == true)
	{
		this.showAll();
		this.filterOptions[0].checked = true;
		return true;
	}
	this.showFiltered();
}
/*----------------------*/

// turn the filter elements on
Filter.prototype.showFiltered = function()
{
	this.hideAll();

	var stripeThese = new Array();
	var count = 0;
	for(var i = 0; i < this.filterElements.length && count < this.max; i++)
	{
		for(var j = 0; j < this.checkedFilters.length; j++)
		{
			var regexp = new RegExp("_"+this.checkedFilters[j]+"_","i");
			if(this.filterElements[i].id.match(regexp))
			{
				this.filterElements[i].className = this.filterElements[i].className.replace(/hide/,"show");
				stripeThese.push(this.filterElements[i]);
				//fadeIn(this.filterElements[i].id,0,100,10);
				count++;
				break;
			}					
		}
	}
	this.checkDividers();
}
Filter.prototype.checkDividers = function() {
	$("#calendarContent>ul").each(function(i) {
		var hideTitle = true;
		$(".calendarFilterItem", "#calendarContent>ul:eq("+i+")").each(function(f) {
			
			if($("#calendarContent>ul:eq("+i+") .calendarFilterItem:eq("+f+")").css("display") != "none")
				hideTitle = false;
		});
		if(hideTitle)
			$("#calendarContent>h4:eq("+i+")").hide();
		else
			$("#calendarContent>h4:eq("+i+")").show();
	});
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          