
(function($) {
	var opts = new Array;	//options

	//ie wasn't cool about display:none on select options
	//so we have to add and remove from the selects
	//filteroptions holds that data for us
	var filteroptions = {};
	var mostrecentselect = "all";
	var tense = "past";
	
	$.fn.ShowFilter = $.fn.showfilter = function(options){ //naming the function we will be able to call

		$("select", $(this)).change(selectchangeevent);
		$("#filter .filterreset").click(filterresetclick);
		$("#listnav nav li").click(navclick);

		tense = $("article nav li.viewing a").attr('id');

		populatefilteroptions();

		arrangefilters();
		resortschedule();
		arrangelist();

		$("#filter select").closest("select").each(selectchangeevent);

		//resort schedule
	}

	function resortschedule(){//needs to be fixed to account for sections
		var futureshows = $("#listview table.future").remove();
		for(var i=futureshows.length-1; i >= 0; i--)
			$("section#listview").append(futureshows[i]);
	}

	function selectchangeevent(){

		mostrecentselect = this;
		
		dofilterchanges(this);

		dolistchanges(this);
	}

	function dolistchanges(el){

		var selectedfilter = $(el).val();

		var selecteds = []; //$(el).prevAll();
		$(el).prevAll("select").each(function (){selecteds.unshift($(this).val());});


		selecteds.unshift(tense);
		selecteds.push(selectedfilter);


		$("#listview table." + selectedfilter).show();
		for(i in selecteds)$("#listview table:not(." + selecteds[i] + ")").hide();



		$("h2").hide();

		var h2arr = $("#listview h2");

		for(i in h2arr){
			var val = h2arr[i].className;
			if($("table."+val).is(":visible"))$("h2."+val).show();
		}

	}

	function dofilterchanges(el){
		var selectedfilter = $(el).val();
		addalloptions(el);
		$("option", $(el).next()).first()  //first option in next select (the all option)
					.attr("class", selectedfilter) //set class
					.val(selectedfilter);// set val //.html("heeey");
		$($("option:not(." + selectedfilter + ")", $(el).nextAll())).remove();

		if($("option", $(el).next()).length > 1)$(el).next().show();
		else $(el).next("select").hide();
		
		$(el).next().nextAll("select").hide();
	}

	function populatefilteroptions(){
		var selects = $("#filter select");

		for(var i = 0; i < selects.length; i++){
			$(selects[i]).attr('id', 'select'+i);
			var options = $("option", $(selects[i]));
			var newarr = [];
			for(var o = 0; o < options.length; o++){
				var optionob = {'klass': $(options[o]).attr('class'),
								'val': $(options[o]).val(),
								'htm': $(options[o]).html()};
				newarr.push(optionob);
			}
			filteroptions['select' + i] = (newarr);
		}
	}

	function addalloptions(el){

		var selects = [];

		if(el == ""){
			$("#filter option").remove();
			$("#filter select").each(function (){selects.push($(this).attr('id'));});
		}
		else {
			$("option", $(el).nextAll("select")).remove();
			$(el).nextAll("select").each(function (){selects.push($(this).attr('id'));});
		}
		
		for(var i=0; i<selects.length; i++){
			var selectel = selects[i];
			for(j=0; j<filteroptions[selectel].length; j++){
				$("#"+selectel).append("<option class='" + filteroptions[selectel][j].klass + "' value='" + filteroptions[selectel][j].val + "' >" + filteroptions[selectel][j].htm + "</option>");
			}
		}
	}

	function filterresetclick(){
		//$("option").show();
		addalloptions("");
		$("#filter select").first().nextAll("select").hide();
		$("section#listview table").show();
		$("select").val("0");
	}

	function navclick(){

		//change the look of the tab itself
		$("#listnav nav li").removeClass("viewing");
		$(this).addClass("viewing");

		//get the id of the tab clicked
		var tab = $("a", $(this)).attr('id');  //#present, #future, #past


		tense = tab;
		dolistchanges(mostrecentselect);

		return false;
	}

	function arrangefilters(){
		$("#filter select").first().show();
	}

	function arrangelist(){
		//the nextall skips the first
		$("#listview h2").remove();
		$("option", $("#filter select").first()).nextAll().each(makecategorysection);
	}

	function makecategorysection(){
		var klass = $(this).val();
		var arr = $("table."+klass).remove();
		var expr = /_/g;
		$("section#listview").append("<h2 class='" + klass + "'>"+klass.replace(expr, " ")+"</h2>");
		$("section#listview").append(arr);
	}

	
	// default values
	$.fn.ShowFilter.defaults = {		
		//list: false
	};
	
})(jQuery);

