﻿jQuery(document).ready(function(){
  		
	//loop through drop down items
	jQuery(".SearchGridWrapper").hide();
	
	
	//loop through drop down items
	jQuery(".ResultsShowHide a").click(function(){
		
		//look for .SearchCategoryTitle
		jQuery(this).parent().parent().next(".SearchGridWrapper").slideToggle();
		
		//switch the text
		if(jQuery(this).text().toLowerCase() == "hide"){
			jQuery(this).text("Show");
		}
		else {
			jQuery(this).text("Hide");
		}
		return false;
	});
	
	//remove the fixed height that causes problems
	jQuery('.SearchWrapper').css("height", "auto");
	
	//add the button handler for the add / remove result count btns 
	jQuery(".MoreResults a").click(function(){
		changeResultCount(1);
		return false;
	});
	jQuery(".LessResults a").click(function(){
		changeResultCount(-1);
		return false;
	});
	
	//setting the text on the search category bar
	var ViewCount = 2;
	function changeResultCount(value){

		//make sure you don't go too low or high
		if((ViewCount + value) > 0){
			
			//set the new height
			ViewCount = ViewCount + value;
			//value from the css
			var sectionHeight = 100;
			
			jQuery(".jcarousel-container-vertical").each(function(){
				if(ViewCount <= jQuery(this).find(".SearchWrapper").children().length){
					jQuery(this).css("height", (sectionHeight*ViewCount) + "px");
					jQuery(this).find(".jcarousel-clip-vertical").css("height", (sectionHeight*ViewCount) + "px");
				}
			});
			
			//set the new numbers
			jQuery(".NumResults").text(ViewCount);
		}
		else {
			//alert("out of bounds");
		}
	}
	
	function HandleSearch(){
		var sectionStr = "";
		var query = escape(jQuery(".SearchTextBox input").attr("value"));
		
		jQuery(".SectionChk input").each(function(){
			if(jQuery(this).attr("checked")){
				var label = jQuery(this).next("label").text();
				if(sectionStr.length > 0){
					sectionStr += "::";
				}
				sectionStr += label;
			}
		});
		
		searchStr = "?query=" + query;
		if(sectionStr.length > 0){
			searchStr += "&sections=" + sectionStr;
		}
		
		location.search = searchStr;
	}
	
	
	jQuery(".AdvSearch").click(function(){
		HandleSearch();
		
		return false;
	});
	
	jQuery(".SearchTextBox input").keyup(function(event){
		if(event.keyCode == 13){
			HandleSearch();
		}
	});
	
	jQuery(".SearchTextBox input").keydown(function(event){
		if(event.keyCode == 13){
			HandleSearch();
		}
	});
		
	jQuery(".ClearSearch").click(function(){
		var loc = window.location.href.split("?");
		window.location.href = loc[0];
				
		return false;
	});

});


