// homepage image swapper code.
function get_cookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return get_cookie_val (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function get_cookie_val(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
var bikky = document.cookie;
var today = new Date();
var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000); // plus 28 days


function set_cookie(name, value) { // use: set_cookie("name", value);
    if (name != null && name != "") {
		document.cookie= name + "=" + escape(value) + "; expires=" + expiry.toGMTString() + "; path=/;";
		//alert('setting cookie = ' + name + "=" + escape(value) + "; expires=" + expiry.toGMTString());
	} else {
		alert('cookie name cannot be null');
	}
	bikky = document.cookie; // update bikky
}

// CONFIGURE HERE 
			var PRODUCTSBYPROPERTY = {
				/* property names must match id of <input */
				'aliphatic' : ['ChronoFlex AL','ChronoSil','ChronoThane T','HydroThane','HydroMed'],
				'antimicrobial' : ['ChronoFlex AL','ChronoFlex AR','ChronoFlex C','ChronoSil','ChronoThane P','ChronoThane T'],
				'aromatic' : ['ChronoFlex AR','ChronoFlex C','ChronoSil','ChronoThane P','HydroThane','PolyBlend'],
				'films' : ['ChronoFlex AL','ChronoFlex AR','ChronoFlex C','ChronoPrene','ChronoSil','ChronoThane P','ChronoThane T','HydroMed','HydroThane','PolyBlend'],
				'hydro' : ['HydroMed','HydroThane'],
				'injection' : ['ChronoFlex AL','ChronoFlex C','ChronoPrene','ChronoSil','ChronoThane P','ChronoThane T','HydroThane','PolyBlend'],
				'implantables' : ['ChronoFlex AL','ChronoFlex AR','ChronoFlex C','ChronoSil'],
				'solutionform' : ['ChronoFlex AL','ChronoFlex AR','ChronoFlex C','ChronoSil','ChronoThane P','ChronoThane T','HydroMed','HydroThane'],
				'under30' : ['ChronoFlex AL','ChronoFlex AR','ChronoFlex C','ChronoPrene','ChronoSil','ChronoThane P','ChronoThane T','HydroMed','HydroThane','PolyBlend'],
				'usp' : ['ChronoFlex AL','ChronoFlex AR','ChronoFlex C','ChronoPrene','ChronoSil','ChronoThane P','ChronoThane T','HydroThane']
				
			}
			
var COOKIENAME = "productschecked";

// reset buttons may occur on /product_select.html (matrix page) or in product list. cover both here.
$(document).ready(function () {
	$(".resetbutton").click(function() {
		set_cookie(COOKIENAME,"");
		window.location.reload();
		return false; 
	});
});

function highlight_product_nav(array_of_attributeids) {
	/* this is an "AND" search, that is the more checkboxes you select, the less highlighted products you get.
			loop through product array and if not matching remove it.
		*/
		/* clear all selected items to start */
	$("#product_nav li").removeClass("selected");
	if (array_of_attributeids.length == 0) { return; }
	
	$("#product_nav li").each(function() {
		/* strip leading,trailing whitepsace, trademark chars. So person doing configuration doesn't go nuts. */
		var clean_product_name = $(this).text().replace(/^\s+/ig,'').replace(/\s+$/ig,'').replace(/[^\x20-\x5a]/gi,''); 
		
		var showproduct = true; // assume product will be shown. then, eliminate by looping through each checked attr, eliminating possiblities.
	
		// check if product carries the attribute. might be best to move this into a function.
		//console.log("array_of_attributeids.length = " + array_of_attributeids.length);
		for (var j = 0; j < array_of_attributeids.length; j++) {
		
			var id = array_of_attributeids[j];
		// $("#product_filter input:checked").each(function() {
			
			var onechosen = false; // temporary variable. product name will be there with other products so use this to ensure one matches.
			for (var i = 0; i < PRODUCTSBYPROPERTY[id].length; i++) {
				
				if (PRODUCTSBYPROPERTY[id][i] == clean_product_name ) {
					// console.log('comparing ' + PRODUCTSBYPROPERTY[id][i] + " to " + clean_product_name);
					onechosen = true;
				}
			}
			
			if (onechosen != true) {
				showproduct = false;
			}
			 
		}
		
		if (showproduct == true ) {
			$(this).addClass("selected");
		}
		
		// end possible function
		
	});
} // end function highlight_product_nav()
