$(document).ready(function(){

	$("#PrimaryMenu > ul > li:last-child").addClass("last");
	
	$(".SubCategoryListGrid ul li.RowDivider").remove();
	$(".SubCategoryListGrid ul li:last-child").remove();
	
	var loc_href = window.location.pathname;
		$('#PrimaryMenu a').each(function () {
		if (loc_href == $(this).attr('href')) {
			$(this).parent("li").addClass('ActivePage');
		}
	});
	
	function textReplacement(input) {
		var originalvalue = input.val();
		input.focus(function() {
			if($.trim(input.val()) == originalvalue){ input.val(""); }
		});
		input.blur(function() {
			if($.trim(input.val()) == ""){ input.val(originalvalue); }
		});
	}
	
	
	
	// =============== PRICE PER POUND CALCULATOR ===================================
	function formatCurrency(num) {
		num = isNaN(num) || num === '' || num === null ? 0.00 : num;
		return parseFloat(num).toFixed(2);
	}
	
	
	getPpP();
	
	function getPpP() {
		var priceExists = $(".ProductDetailsGrid .ProductPrice").length;
		var weightExists = $(".ProductDetailsGrid .VariationProductWeight").length;
		if (priceExists > 0) {
			if (weightExists > 0) {
				var prodWeight = $(".VariationProductWeight");
				_prodWeight = $.trim(prodWeight.text().replace(" LBS", ""));
				prodWeight.parents(".Value").prev(".Label").text("Bag Size:");
				
				var prodPrice = $(".ProductDetailsGrid .Value .ProductPrice").text();
				_prodPrice = prodPrice.replace("$","").replace(",", "");
				
				var PPP = _prodPrice / _prodWeight;
				_PPP = formatCurrency(PPP);
				
				var weightDetailRow = prodWeight.parents(".DetailRow");
				weightDetailRow.after("<div class='DetailRow' id='PpP'>");
				$("#PpP").html("<div class='Label'>Price Per Pound:</div><div class='Value'><span id='pricePP'>&#36;"+ _PPP +"</span></div>");
				
				//alert (_prodPrice +" / "+ _prodWeight +" = "+ _PPP);
			}
		}
	}
	
	
	// ================  TINY IMAGE LIST FIX =================================
	var count = 0;
	$(".ProductTinyImageList li").each(function(index) {
		count++;
	});
	if (count <= 1) { $("#CarouselWrap").css({display: 'none'}); }
	
	
	
	
	
	
	textReplacement($("#search_query"));
	textReplacement($("#nl_first_name"));
	textReplacement($("#nl_email"));
	
	// // loads the cycle or whatever on specific url in this case the home page
	// //GET URL	
	var url = window.location.pathname;
	url = url.toLowerCase();
	// //READ URL
	// // uncomment alert to see what the url is in browser if needed
	// //alert(url);
	if (url == '/' || url == "/index.php") {
		$("#cycle").cycle({
			fx: "fade",
			speed: 600,
			timeout: 5000,
			next: '#cycleNext',
			prev: '#cyclePrev',
			pager:  '#cyclePager'
		});
	};
	$("#cyclePager a").text("");
	// // loads only if the cycle selector exists
	// if ($("#cycle").length > 0){	
		// $("#cycle").cycle({
			// fx: "fade"
		// });
	// };


	//	jCarouselLite example -- you will need to add a <div class="ProductListContainer"> around the <ul>
	//	in the HomeFeaturedProducts Panel

//	$("#HomeFeaturedProducts .ProductListContainer").jCarouselLite({
//        btnNext: "#featured-next",
//        btnPrev: "#featured-prev",
//	      visible: 5,
//	 	  scroll: 5,
//		  speed: 800
//   });

});
