/**
 * ShopLaptop.net - Site Nav Javascript (Requires jQuery)
 */


// jQuery Fires on Page Loaded
$("document").ready(function(){ 	
	
/*
 ----------------[ Page Header Functions ]------------------
*/
	// Highlight searchbox on select
	$(".textfield").focus(function(){
		$(this).css("background","#ffffff");
		$(this).css("border","2px solid #0000FF");
	});
	$(".textfield").blur(function(){
		$(this).css("background","#FFFFFF");
		$(this).css("border","1px solid #666666");
		
	});
	
	
	// On Submit Functions for the Search Box
	$("#searchform").submit(function(){
	
		// Format Text to make it search using AND
		var QueryField = $("#searchform [name=query]");
		querywords = QueryField.val().replace(/\+/g, "").split(" ");
		var finalquery = "";
		for (var lp=0; lp < querywords.length; lp++) {
			if (querywords[lp].length > 0) {
				finalquery = finalquery+" +"+querywords[lp];
			}
		}
		QueryField.val(finalquery);
	});
/*
 ----------------[ Page Header Functions ]------------------
*/


	
/*
 ----------------[ Drill Down For Category Pages ]------------------
*/	
	// Removes item filter
	function ClearFilter () {
		$(".contentsrow").each(function (i){
			$(this).show();
			$(this).css("background","");
		});
		$("#filtertext").val("");
		$("#clearfilter").css("visibility","hidden");
		$("#resulttitle").text("Available Items")
		$("#resultnumber").text("");
		
	}

	// Sets Item Filter
	function ApplyFilter (FilterText) {
		$(".contentsrow").hide();
		var nMatches = 0;
		var nTotalItems = 0;
		$(".contentsrow").each(function (i){
			
			if ( $(this).text().toUpperCase().indexOf(FilterText.toUpperCase()) != -1 ) {
				$(this).show();
				nMatches++ ;
				$(this).removeClass("zebralight");
				$(this).removeClass("zebradark");
				
				if ($(this).parent().attr("class") != 'contents-multi') {
					if (nMatches % 2 == 0) {
						$(this).addClass("zebradark");
					} else {
						$(this).addClass("zebralight");	
					}
				}
			}
			nTotalItems++;
			
		});
		$("#clearfilter").css("visibility","visible");
		$("#resulttitle").text("Filtered Results");
		$("#resultnumber").text("   Showing "+nMatches.toString() + " of "+ nTotalItems.toString()+" Items");
	}


	// Filter on Keypress
	$("#filtertext").keyup(
			function (keycode){
				if ($(this).val() != "") {
					ApplyFilter($(this).val());
				} else {
					ClearFilter ();
				}
			}
		);

	// Set Click Event For Clear List
	$("#clearfilter").click(
		function(){
			ClearFilter();
			}
	);

	// Show drilldown when JS is enabled and there are many results
	var nMinToShowFilter = 10;
	var nTotalItems = 0;
	$(".contentsrow").each(function (i){
		nTotalItems++;
		
		// Apply Zebra Stripes To Table
			if ($(this).parent().attr("class") != 'contents-multi') {
				if (nTotalItems%2) {
					$(this).addClass("zebradark");
				} else {
					$(this).addClass("zebralight");
				}
			}
	});
	
	if (nTotalItems > nMinToShowFilter) {
		$("#drilldown").show();
		$("#filtertext").focus();
	}
		
	
	//Clear filter box on page-reload
	$("#filtertext").val("");
/*
 ----------------[ Drill Down For Category Pages ]------------------
*/



});
