var t;
$(document).ready(function(){
	$(".subcats")
		.click(function(){
			window.location=$(this).find("a").attr("href"); return false;
		})
		.hover(function(){
			$(this).addClass('subcats2');
		}, function(){
			$(this).removeClass('subcats2');
		});
	$(".catTab")
		.click(function(){
			$(".catTab").removeClass("catTabUp");
			$(this).addClass("catTabUp");
			$(".sectionContainer").removeClass("sectionContainerOpen").eq($(".catTab").index(this)).addClass("sectionContainerOpen");
		});

	var currentViewedCategories = '';
	if(settings.vars.catId) {
		if(settings.vars.catId != '2000047' && settings.vars.catId != '2000066' && settings.vars.catId != '2000055' && settings.vars.catId != '0') {
			currentViewedCategories = settings.vars.catId + "~" + settings.vars.catName + "|"
		}
		if($.returnCookie("recentcvs")) {
			$.returnCookie("recentcvs").slice(0,5).each(function(i){
				if(this[0] != 0) {
					if(settings.vars.catId != this[0]) {
						currentViewedCategories += this[0] + "~" + this[1] + "|";
					}
				}
			});
		}
		if(!settings.vars.singleBrandPresent) {
			// Set the cookie
			var expdate = new Date();
			FixCookieDate(expdate);  // For Mac bug
			expdate.setTime(expdate.getTime() + (720 * 60 * 60 * 1000)); // 720 hours (30 days) expiration
			SetCookie("recentcvs", currentViewedCategories, expdate, "/");
		}
	}

	$(".tooltipNRContainer").not(".tab-content .tooltipNRContainer").mouseover(function(){
		clearTimeout(t);
		t = setTimeout("showToolTip('" + $(this).attr("id") + "')",1000);
		})
	.mouseout(function(){
		clearTimeout(t);
		$(this).find(".tooltipNR").css("display","none");
		});

	$(".productImg img").not(".tab-content .productImg img").mouseover(function(){
		if($(this).attr("id").replace("productImgL","tooltip") != "") {
			clearTimeout(t);
			t = setTimeout("showToolTip('" + $(this).attr("id").replace("productImgL","tooltip") + "')",1000);
		}
		})
	.mouseout(function(){
		if($(this).attr("id").replace("productImgL","tooltip") != "") {
			clearTimeout(t);
			$("#" + $(this).attr("id").replace("productImgL","tooltip")).find(".tooltipNR").css("display","none");
		}
	});

	$(".comparecheckbox").click(function(){
		if($(this).find("input:checkbox").eq(0).is(":checked")){
			$(this).css("background-color", "#f5b100");
		} else {
			$(this).css("background-color", "#ebe7de");
		}
	});

	populateCheckboxes();

	$(".hero-image-buttons .button a img").hover(
		function(){
			$(this).attr("src",$(this).attr("src").replace(".gif","-over.gif"));
		},
		function(){
			$(this).attr("src",$(this).attr("src").replace("-over.gif",".gif"));
	});
});

function showToolTip (name) {
	obj = $("#" + name);
	$(obj).find(".tooltipNR").css("display","block");
	var sku = $(obj).find(".tooltipNR").attr("name");
	var imgL = $("#productImgL" + sku).attr("src");
	var imgL = imgL.replace("_med","_lg");
	var description = $("#tooltip-details" + sku).attr("name");
	var rating = $("#tooltipRating" + sku).html();
	var rating = rating.replace(rating,'<p><b>' +rating+ '</b></p>');
	var wasprice = $("#tooltipWasPrice" + sku).html();
	var price = $("#tooltipPrice" + sku).html();
	var sale = $("#tooltipSale" + sku).html();

	$(obj).find("#tooltip-lgImg" + sku).attr("src",imgL);
	$(obj).find("#tooltip-details" + sku).html('<p>' + description + '</p><p style="padding-bottom: 1px;"><img class="tooltipNR-footer" src="/images/icons/tooltip-footer.gif" width="370" height="20" alt="See complete information on product detail page."></p>');
	$(obj).find("#tooltip-rating" + sku).html(rating);
	$(obj).find("#tooltip-wasprice" + sku).html(wasprice);
	$(obj).find("#tooltip-price" + sku).html(price);
	$(obj).find("#tooltip-sale" + sku).html(sale);

	var pos = $(obj).position();
	$(obj).find(".tooltipNR").css("top",pos.top - 180);
	$(obj).find(".arrow").css("display","block");
	if($(window).width()<(pos.left+650) && pos.left > 460) {
		$(obj).find(".tooltipNR").css("left",pos.left - 420);
		$(obj).find(".arrow").addClass("arrow-left");
	} else {
		$(obj).find(".tooltipNR").css("left",pos.left + 170);
	}
	if(($(obj).find(".tooltipNR").eq(0).position().left + 460)>$(window).width()){
		$(obj).find(".tooltipNR").css("left",$(window).width() - 460);
		$(obj).find(".arrow").removeClass("arrow-left");
		$(obj).find(".arrow").css("display","none");
	}
}

var prodArray = new Array;
function findSelected() {
	for (var i = 0;i < prodArray.length;i++ ) {
			checkedChanged(document.getElementById("Panel" + prodArray[i] + "g"), document.getElementById("Checkbox" + prodArray[i] + "g"));
	}
}

function populateCheckboxes() {
	var compSelectIdHastable = {};
	var elemValue = '';
	var compSelectIds = new Array();
	var checkBoxes = document.getElementsByName('comparisonGrid');
	
	//what is currently stored
	var compSelectIdStr = getCookie("compSelectIds");
	if(compSelectIdStr){
		compSelectIdStr = stringCleanUp(compSelectIdStr);
		
		//creating hashtable of ids
		compSelectIds = compSelectIdStr.split('|');
		for (i=0;i<compSelectIds.length;i++){
			compSelectIdHastable[compSelectIds[i]]= compSelectIds[i];
		}	
	} else {
		//if there are no cookie - nothing to do
		return;
	}
		
	for (i=0; i<checkBoxes.length; i++){
	 	//if the cookie contains this checkbox value => recheck it
		if(compSelectIdHastable.hasOwnProperty(checkBoxes[i].value) ){
			checkBoxes[i].checked = true;
			$(checkBoxes[i]).parent().css("background-color", "#f5b100");
		}	
	}		
}

function checkSubmit(){
	//what is currently stored
	var compSelectIdStr = getCookie("compSelectIds");
	var idCount = 0;
	var compSelectIds = new Array();

	if(compSelectIdStr){
		compSelectIdStr = stringCleanUp(compSelectIdStr);

		//check how many ids stored
		compSelectIds = compSelectIdStr.split('|');
		idCount = compSelectIds.length;
	}

	//show compare chart only if 2 or more items are selected
	if(idCount > 1){
		document.forms.compare.submit();
	}else{
		alert("Please select at least 2 items to compare.");
	}
}

function updateCompData(chkBox){
	var compSelectIdHastable = {};
	var elemValue = '';
	var compSelectIds = new Array();

	//what is currently stored
	var compSelectIdStr = getCookie("compSelectIds");
	if(compSelectIdStr){
		compSelectIdStr = stringCleanUp(compSelectIdStr);

		//creating hashtable of ids
		compSelectIds = compSelectIdStr.split('|');
		for (i=0;i<compSelectIds.length;i++){
			compSelectIdHastable[compSelectIds[i]]= compSelectIds[i];
		}
	}else{
		compSelectIdStr = '';
	}

	//if cookie has 5 ids already => let user know and prevent this selection
	if(chkBox.checked && compSelectIds.length > 4){
		chkBox.checked = false;
		alert("Please select up to 5 items to compare.  The last item you selected is the sixth.");
		return;
	}
	var viewCookie = getCookie('catView');

	if (viewCookie == 'list')
		var checkBoxes = document.getElementsByName('comparisonList');
	else
		var checkBoxes = document.getElementsByName('comparisonGrid');

	for (i=0; i<checkBoxes.length; i++){
	 	//if checkbox is checked and the cookie does not contain this value => add it with delimiter
		if(checkBoxes[i].checked && !compSelectIdHastable.hasOwnProperty(checkBoxes[i].value) ){
			//add to the cookie string
			compSelectIdStr += '|' + checkBoxes[i].value;
		}
	 	//if checkbox is unchecked but the cookie  contain this value => remove the value and update cookie
		if(!checkBoxes[i].checked && compSelectIdHastable.hasOwnProperty(checkBoxes[i].value) ){
			elemValue = ''+ checkBoxes[i].value;
			compSelectIdStr = compSelectIdStr.replace(elemValue,'');
		}
	}

	// prepare to set/update the cookie
	var cookieName = "compSelectIds";
	var cookieValue = stringCleanUp(compSelectIdStr);
	//var expdate = new Date();
	//FixCookieDate(expdate);  // For Mac bug
	//expdate.setTime(expdate.getTime() + (10 * 60 * 1000)); // 10 min expiration
	//setting session cookie
	compSelectIdStr = stringCleanUp(compSelectIdStr);
	SetCookie(cookieName, cookieValue, null, "/");
}

function stringCleanUp(str){
	//replacing '|||' if any with '|'
	str = str.replace('|||','|');

	//replacing '||' if any with '|'
	str = str.replace('||','|');

	//removing heading and trailing '|'
	if(str.indexOf('|') === 0) str = str.slice(1);
	if(str.lastIndexOf('|') === (str.length-1)) str = str.substring(0,str.length-1);

	return str;
}
function random_imglink() {
	var bannerimages=new Array(8);
	//specify random images below
	bannerimages[0]="/images/banners/slogans/slogan1.jpg";
	bannerimages[1]="/images/banners/slogans/slogan2.jpg";
	bannerimages[2]="/images/banners/slogans/slogan3.jpg";
	bannerimages[3]="/images/banners/slogans/slogan4.jpg";
	bannerimages[4]="/images/banners/slogans/slogan5.jpg";
	bannerimages[5]="/images/banners/slogans/slogan6.jpg";
	bannerimages[6]="/images/banners/slogans/slogan7.jpg";
	bannerimages[7]="/images/banners/slogans/slogan8.jpg";
	bannerimages[8]="/images/banners/slogans/slogan9.jpg";
	var ry=Math.floor(Math.random()*bannerimages.length)
	if (ry==0) ry=1;
	//document.write('<a href="http://www2.northerntool.com/catalog/">');
	document.write('<img src="'+bannerimages[ry]+'" width="766" height="100" alt="Northern Tool + Equipment">');
	//document.write('</a>');
}