function print_r(theObj){if(theObj.constructor==Array||theObj.constructor==Object){document.write("<ul>");for(var p in theObj){if(theObj[p].constructor==Array||theObj[p].constructor==Object){document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");document.write("<ul>");print_r(theObj[p]);document.write("</ul>");}else{document.write("<li>["+p+"] => "+theObj[p]+"</li>");}}document.write("</ul>");}}


$().ready(function() {
	
	// the current selections
	var selections = {disc_item:"(any)",cat:"(any)",app_ftw:"(any)",gender:"(any)",quarter:"(any)",catalog:"(any)",art_num:"",extras:""};
	
	// the columns of unique, avaible selections
	disc_items = new Array();
	cats = new Array();
	app_ftws = new Array();
	genders = new Array();
	art_nums = new Array();
	quarters = new Array();
	catalogs = new Array();
	
	/************************************************************
	 * Builds each filter-column's list of unique values from which to select
	 */
	
	{
		disc_items.push("(any)");
		$("#filter_disc_item").append("<span>(any)</span>");
		
		cats.push("(any)");
		$("#filter_cat").append("<span>(any)</span>");
		
		app_ftws.push("(any)");
		$("#filter_app_ftw").append("<span>(any)</span>");
		
		genders.push("(any)");
		$("#filter_gender").append("<span>(any)</span>");
		
		quarters.push("(any)");
		$("#filter_quarter").append("<span>(any)</span>");	
		
		catalogs.push("(any)");
		$("#filter_catalog").append("<span>(any)</span>");		
	}
	
	jQuery.each(items, function(i, val){
		if (jQuery.inArray(val.disc_item, disc_items) == -1) {
			disc_items.push(val.disc_item);
			$("#filter_disc_item").append("<br/><span>"+val.disc_item+"</span>");
			
		}
			
		if (jQuery.inArray(val.cat, cats) == -1) {
			cats.push(val.cat);
			$("#filter_cat").append("<br/><span>"+val.cat+"</span>");
		}
			
		if (jQuery.inArray(val.app_ftw, app_ftws) == -1) {
			app_ftws.push(val.app_ftw);
			$("#filter_app_ftw").append("<br/><span>"+val.app_ftw+"</span>");
		}
			
		if (jQuery.inArray(val.gender, genders) == -1) {
			genders.push(val.gender);
			$("#filter_gender").append("<br/><span>"+val.gender+"</span>");
		}
		
		if (jQuery.inArray(val.quarter, quarters) == -1) {
			quarters.push(val.quarter);
			$("#filter_quarter").append("<br/><span>"+val.quarter+"</span>");
		}
		
		if (jQuery.inArray(val.catalog, catalogs) == -1) {
			catalogs.push(val.catalog);
			$("#filter_catalog").append("<br/><span>"+val.catalog+"</span>");
		}
		
		if (jQuery.inArray(val.art_num, art_nums) == -1) {
			art_nums.push(val.art_num);			
		}
	});	
	
	
	
	/************************************************************
	 * update_table
	 */	
	function update_table(){
		//var displayed_items=items;
		$("#dynamic_table").html("");
		var mod_2=0;
		
		selected_art_nums=new Array();
		jQuery.each(items, function(i, val){
			if ((selections.disc_item == "(any)" || selections.disc_item == val.disc_item) &&
				(selections.cat == "(any)" || selections.cat == val.cat) &&
				(selections.app_ftw == "(any)" || selections.app_ftw == val.app_ftw) &&
				(selections.gender == "(any)" || selections.gender == val.gender) &&
				(selections.quarter == "(any)" || selections.quarter == val.quarter) &&
				(selections.catalog == "(any)" || selections.catalog == val.catalog) &&
				(selections.art_num == "" || selections.art_num == val.art_num)) {
				
				selected_art_nums.push(val.art_num);
				
				//$("table tr:nth-child(even)").addClass("striped");
				var data="<table><tr><td class='first_td'>"+val.disc_item+"</td><td>"+val.cat+"</td><td class='small_td'>"+val.app_ftw+"</td><td class='small_td'>"+val.gender+"</td><td class='small_td'>"+val.quarter+"</td><td class='small_td'>"+val.catalog+"</td><td>"+val.art_num+"</td></tr></table><span class='hide'>"+ ( (val.extras!='')?"<br/>"+val.extras:"") +"</span>";
				if(mod_2++%2==0){
					zebra="<div class='rounded zebra'><div class='hd'><div class='c'></div></div><div class='bd'><div class='c'>"+
						data+
						"</div></div><div class='ft'><div class='c'></div></div></div>";
				}else{
					zebra="<div class='rounded blank'><div class='hd'><div class='c'></div></div><div class='bd'><div class='c'>"+
						data+
						"</div></div><div class='ft'><div class='c'></div></div></div>";					
				}
				$("#dynamic_table").append(zebra);	
			}
		});
				
		$("#dynamic_table>div").bind("mouseenter",function(){
			$(this).attr("id", "selected").find(".hide").css('display','block');	
		}).bind("mouseleave",function(){
			$(this).attr("id","").find(".hide").css('display','none');
		});
		
		$("#show_selections td:eq(0)").text(selections.disc_item);
		$("#show_selections td:eq(1)").text(selections.cat);
		$("#show_selections td:eq(2)").text(selections.app_ftw);
		$("#show_selections td:eq(3)").text(selections.gender);
		$("#show_selections td:eq(4)").text(selections.quarter);
		$("#show_selections td:eq(5)").text(selections.catalog);				
		$("#show_selections td:eq(6)").text( (selections.art_num)?selections.art_num:'(any)' );
	}
	update_table();
	
	
	
	/************************************************************
	 * OnClick Filters
	 */
	
	$("#collapsible span")
	
	.click(function(){
		switch($(this).parent().attr('id')){
			case 'filter_disc_item':selections.disc_item=$(this).text(); break;
			case 'filter_cat':selections.cat=$(this).text(); break;
			case 'filter_app_ftw':selections.app_ftw=$(this).text(); break;
			case 'filter_gender':selections.gender=$(this).text(); break;
			case 'filter_quarter':selections.quarter=$(this).text(); break;
			case 'filter_catalog':selections.catalog=$(this).text(); break;
			case 'filter_art_num':selections.art_num=$(this).text(); break;
		}	
		$(this).css('font-weight','bold').attr('selected','true')
			   .siblings().css('font-weight','normal').attr('selected','false');			
		update_table();
	})	
	
	.mouseover(function(){
		if ($(this).attr('selected') != 'true') {
			$(this).css('font-weight', 'bold');
		}
	})
	
	.mouseout(function(){
		if ($(this).attr('selected') != 'true') {
			$(this).css('font-weight', 'normal');
		}
	});	
	
	$("#filter_reset").click(function(){
		
		$("#collapsible span").filter(function(index){
			return $(this).text="(any)";
		}).css('font-weight','bold').attr('selected','true')
			.siblings().css('font-weight','normal').attr('selected','false');	
		$("#art_num").val('');
		
		selections.disc_item="(any)";
		selections.cat="(any)";
		selections.app_ftw="(any)";
		selections.gender="(any)";
		selections.quarter="(any)";
		selections.catalog="(any)";
		selections.art_num="";
		update_table();
	});

	/************************************************************
	 * Dropdown
	 */
		
	$("#dropdown").bind("mouseenter",function(){
		if (!$('#collapsible').is(':visible')) {
			$("#collapsible").slideDown();
		}
	})
	.bind("mouseleave",function(){
		if ( $('#collapsible').is(':visible') && !$('div.ac_results').is(':visible')) {
			$("#collapsible").slideUp();
		}
	});
		
	/************************************************************
	 * Autocomplete
	 */
	$("#art_num").autocomplete(items, {
		minChars: 0,
		width: 121,
		matchContains: true,
		autoFill: false,
		formatItem: function(row) {
			if($('.autoComplete').val()==''){
				selections.art_num='';
				update_table();
			}
			return row.art_num;
		},
		formatMatch: function(row) {
			
			return row.art_num;
		},
		formatResult: function(row) {	
				
			return row.art_num;
		}
	}).result(function(event,data,formatted){			
		selections.art_num=data.art_num;		
		update_table();
	});	
});



