jQuery().ready(function(){

	$('a.rollOver').mouseover(function(){window.status=this.title;return true;})
.mouseout(function(){window.status='Done';return true;});

		// Genzyme Filter Prototype 
		$("#filterForm input[type='radio']").each(function(){
			$(this).attr("checked", "")
				
		});
		var dataCopy = new dataObj();	
		$("#formLayer").css({
			opacity: 0,
			display: "block"
		});
		
		// this function takes the 'dataCopy' var and removes all the objects which dont match the filter
		var filterData = function(node){
			if(node){
				//somebody clicked on something
				var type = $(node).parent().children("input").attr("type");
				var dontActivate;
				if(type=="radio"){
					if($(node).parent("li").hasClass("active")){
						dontActivate = true;
					}
					$(node).parent("li").parent("ul").children("li").removeClass("active")
					
				}
				if(dontActivate != true){
					thisLi = $(node).parent("li");
					if($(thisLi).hasClass("active")){
						$(thisLi).removeClass("active");
					} else {
						$(thisLi).addClass("active");
					}
				}
				
			}
			var filterCats = new Array;
			$("#filterForm li.active").each(function(){filterCats.push($(this).children('input').attr("value"))});
			if(filterCats.length != 0){
				//filter stuff out of array depending on category
				var filteredProducts = new Array();

				
				
				var findCriteria = function(){
					var whichPersona = $("#personas li.active input").attr("value");
					var whichDiseases = new Array();
						$("#diseases li.active input").each(function(){
							whichDiseases.push($(this).attr("value"))
						});
					var whichCategory = $("#categories li.active input").attr("value");
					
		
					var criteriaList = new Array();
					
					if(whichPersona){
						var criteriaList = criteriaList.concat(whichPersona);
					} else {
						var myList = new Array();
						$("#personas li input").each(function(){
							myList.push($(this).attr("value"))
						});
						var criteriaList = criteriaList.concat(myList);
					}

					if(whichDiseases.length > 0){
						var criteriaList = criteriaList.concat(whichDiseases);
					} else {
						var myList = new Array();
						myList.push("0-0-0");
						$("#diseases li input").each(function(){
							myList.push($(this).attr("value"))
						});		
						var criteriaList = criteriaList.concat(myList);
					}
										
					if(whichCategory){
						var criteriaList = criteriaList.concat(whichCategory);
					} else {
						var myList = new Array();
						
						$("#categories li input").each(function(){
							myList.push($(this).attr("value"))
						});
						var criteriaList = criteriaList.concat(myList);
					}

					return criteriaList;
				}
				filterCats = findCriteria()
				for (l = 0; l < dataCopy.products.length; l++){
					var myProduct = dataCopy.products[l];
					var personaMatch = false;
					var diaguseMatch = false;
					var categoryMatch = false; 
					
					for(f = 0; f < filterCats.length; f++){		
						if(myProduct.persona1 == filterCats[f] || myProduct.persona2 == filterCats[f] || myProduct.persona3 == filterCats[f] || myProduct.persona4 == filterCats[f] || myProduct.persona5 == filterCats[f]){
							personaMatch = true;
							break;
						}
					}			
					for(f = 0; f < filterCats.length; f++){		
						if(filterCats[f]=="0-0-0"){
							if(myProduct.diaguse1 == "0" && myProduct.diaguse2 == "0" && myProduct.diaguse3 == "0"){
								diaguseMatch = true;
								break;
							}
						} else {
							if(myProduct.diaguse1 == filterCats[f] || myProduct.diaguse2 == filterCats[f] || myProduct.diaguse3 == filterCats[f]){
								diaguseMatch = true;
								break;
							}
						}
					}
					for(f = 0; f < filterCats.length; f++){		
						if(myProduct.category == filterCats[f]){
							categoryMatch = true;
							break;
						}
					}
					if(categoryMatch == true && diaguseMatch == true && personaMatch == true){
						filteredProducts.push(myProduct)
					} else {
						myProduct
					}
					
					
				}
				
				//write out the HTML block at the bottom.
				var contentInsert = new Array();
				if(filteredProducts.length > 0){
					contentInsert.push("<div class='result_div1'>")
					$("#sec7").html("PRODUCT SEARCH RESULTS - " + filteredProducts.length );
					$("#sec9").html("PRODUCT SEARCH RESULTS - " + filteredProducts.length );
					var count = -1;
						for (i= 0; i < filteredProducts.length; i++){
						count = count + 1;
						if(count == Math.round(filteredProducts.length/2)){
							contentInsert.push("</div><div class='result_div2'>");
							count = 0;
						}
						myProduct = filteredProducts[i];	
						contentInsert.push("<a href=\"" + myProduct.url + "\" class=\"rollOver\" title=\""+ myProduct.name +"\">" + myProduct.name + "</a><br/>");
					}
						contentInsert.push("</div>")
					
				} else {
					$("#sec7").html("PRODUCT SEARCH RESULTS - 0" );
					$("#sec9").html("PRODUCT SEARCH RESULTS - 0" );
					contentInsert.push("<h4>No Products Matched .. </h4>");
				}
		
				$("#sec6").html(contentInsert.join("") + "<br/>");
			} else {
				
				var allProducts = new Array();
				var allcontent = new Array();
				allcontent.push("<div class='result_div1'>")
				var allcount = -1;
				for (l = 0; l < dataCopy.products.length; l++)
				{
					allcount = allcount + 1;
						if(allcount == Math.round(dataCopy.products.length/2)){
							allcontent.push("</div><div class='result_div2'>");
							allcount = 0;
						}
					var myProduct = dataCopy.products[l];	
					$("#sec7").html("PRODUCT SEARCH RESULTS - " + dataCopy.products.length );
					$("#sec9").html("PRODUCT SEARCH RESULTS - " + dataCopy.products.length );
					allcontent.push("<a href=\"" + myProduct.url + "\" class=\"rollOver\" title=\""+ myProduct.name +"\">" + myProduct.name + "</a><br/>");
				}
				allcontent.push("</div>")
				$("#sec6").html(  allcontent.join("")+"<br/>");	
			}
		}
		
		//run when people interact with the select
		$("#filterForm label").click(function(){
			hideSec(9);
			showAllSec(7);
			showAllSec(8);
			showAllSec(6);
			filterData($(this));
		});
		var showLayer = function(){
			$("#sec2").fadeOut("fast")
			$("#sec3").fadeOut("slow")
			$("#sec5").animate({
				top: "110px",
				height: "11px",
				opacity: .8
			}, 1500)
			$("#formLayer").animate({
				top: "123px",
				height:"254px",
				opacity: .65
			} , 1500)
			$("#sec4").animate({
				top: "124px",
				height: "253px"
			} , 1500)
		}

		//state for go button
		var goButtonClicked = false;

		$("#goButton").click(function(){
			goButtonClicked = true;
			showLayer();
		});

		window.setTimeout(function(){
			if(goButtonClicked == false){
				showLayer();
			}
		},3000); 

		// run on page load
		filterData();
	
		
	});

