jQuery(window).load(
   function(){
		j = jQuery.noConflict();
		
		if (j.browser.msie) {
			j("body").addClass("ie");
		}
		
		/* Header */
		j("#header-8").append(j("#header-9 div.com-amazon-webstore-SignInWidget-2"));
		j("#header-4").append("<span class='smalltext'>Use * as a wildcard for partial matches (e.g., baseb* or louisville*)</span>");
		j("#header-5 .navigationGroup .navigationLinks").hover(function(){
			j(this).prev("a.mainNode").css("backgroundColor", "#2691FF");
		},
		function(){
			j(this).prev("a.mainNode").css("backgroundColor", "#ffffff");
		});
		//search text
		j("#searchKeywords").val('Search by SKU or Keywords');
		j("#searchKeywords").focus(function(){
			j(this).val('');
		});
		j("#searchKeywords").blur(function(){
			if(j(this).val() == '')
				j(this).val('Search by SKU or Keywords');
		});
		
		//Redirect ASI & Promos all links
		j("a[href*='/ASI-Promos-Departments-Products/b/2662275011']").attr("href", "http://www.anacondapromo.com");
		
		/* Browse */
		if (j("body.browse #center-1 h2").length > 0) {
			j("#browseRefinements, .searchHeader").hide();
		}
		if (j("body.browse #center-2:contains('no items available')").length > 0) {
			j("#browseRefinements").hide();
		}
		
		
		
		/* Cart */
		
		if (j("body.cart h1:contains('Your cart is currently empty.')").length > 0) {
			j("#shippingCalculator, #CBAStandardCheckoutButton").hide();
		}
		
		/* PDP */
		if (j("body.detail").length > 0) {
			var info = j("div.productInformation div.information");
			j("div.productDescription").append(j(info));
			
			//tabs 
			j(".productTabs .tab").click(function(){
				jQuery(this).siblings().removeClass("active");
				jQuery(this).addClass("active");
				var tab = j(this).attr("id");
				j(".target-amazon-com-target-pagelets-IncaSimilaritiesWidgetPagelet-1, .productDescription, .videoWidget").hide();
				j("." + tab).show();
			});
			
			j(".productDetailPrice").insertBefore(j("ul.buyBoxActions"));
			
			if (!j.browser.msie)
			j("body.detail #columns #leftColumn").insertAfter(j("#center-right-wrapper"));
			
			//Quantity input box - not dropdown
			j("select#buyboxQuantity").remove();
			j("li#quantityBox").append(j("<input type='text' name='item.0.qty' id='buyboxQuantity'>"));
			
			//Number logic - cannot be decimal or negative
			j("input#buyboxQuantity").keyup(function(){
				var d=j(this).attr('numeric');
		        var value=j(this).val();
		        var orignalValue=value;
		        val=value.replace(/[0-9]*/g, "");
		       	
		       	var msg="Only Integer Values allowed."; 
		       	
		       	if (d=='decimal'){
		       	  value=value.replace(/\./, "");
		          msg="Only Numeric Values allowed.";
		        }
		        
		        if (val != '') {
					orignalValue = orignalValue.replace(/([^0-9].*)/g, "")
					j(this).val(orignalValue);
					//alert(msg);
				}
				
				var quantity = j(this).val();
				j("#buyboxQuantity").val(quantity);
			});
			
			//Dozens Ball logic
			if (j("div.information:contains('Dozen')").length > 0 && j("h2.productTitle:contains('Bucket')").length <= 0) {
				j("span#tooltipId").addClass("hideTooltip");
				var price = j(".productDetailPrice dd.price").text();
				var price = price.replace("$", "");
				var dozenprice = parseFloat(price) * 12;
				var dozenprice = dozenprice.toFixed(2);
				var dozen = "$" + dozenprice + " /dz";
				j(".productDetailPrice dd.price").text(dozen)
				
				
				//Quantity logic
				//Check API for amount available
				var asin = j("input#rPath").next().next("input").val();
				var apiurl = '/api/product/asin/' + asin;
				j.ajax({
					type: "GET",
					url: apiurl,
					dataType: "xml",
					success: function(data, textStatus1, jqXHR1){
						var quantavail = j(data).find("[nodeName=c:quantity]").text();
						j("li#quantityBox").attr("alt", quantavail);
						if (quantavail < 12) {
							j("#right-8, div.buyBox ol.formFields li:not(.productAvailabilityMessage), .productDetailPrice, ul.buyBoxActions").hide();
							j("div.buyBox ol.formFields li.productAvailabilityMessage dd.availability").text("Out of Stock");
						}
					}
				});
				
				j("#buyboxQuantity").blur(function(){
					var quantity = j("input#buyboxQuantity").val();
					if (quantity % 12 === 0) {
					//alert('true');
					}
					else {
						alert("Please enter an amount divisible by 12");
						return false;
					}
				});
				j("button#addToCartButton").click(function(){
					var quantavail = j("li#quantityBox").attr("alt");
					var quantity = j("input#buyboxQuantity").val();
					if (quantity % 12 === 0) {
						if (parseInt(quantavail) < parseInt(quantity)) {
							alert('The inventory quantity you are trying to order is currently unavailable. Please try lowering the quantity or call 1-800-327-0074 to speak to a sales representative.');
							return false;
						}
					}
					else {
						alert("Please enter an amount divisible by 12");
						return false;
					}
				});
			}
			
			//24 pack logic
			if (j("div.information div.productInfo ul li:contains('Sold in Cases of 24 Cans')").length > 0) {
				j("span#tooltipId").addClass("hideTooltip");
				var price = j(".productDetailPrice dd.price").text();
				var price = price.replace("$", "");
				var dozenprice = parseFloat(price) * 24;
				var dozenprice = dozenprice.toFixed(2);
				var dozen = "$" + dozenprice + " /case";
				j(".productDetailPrice dd.price").text(dozen)
				
				
				//Quantity logic
				//Check API for amount available
				var asin = j("input#rPath").next().next("input").val();
				var apiurl = '/api/product/asin/' + asin;
				j.ajax({
					type: "GET",
					url: apiurl,
					dataType: "xml",
					success: function(data, textStatus1, jqXHR1){
						var quantavail = j(data).find("[nodeName=c:quantity]").text();
						j("li#quantityBox").attr("alt", quantavail);
						if (quantavail < 24) {
							j("#right-8, div.buyBox ol.formFields li:not(.productAvailabilityMessage), .productDetailPrice, ul.buyBoxActions").hide();
							j("div.buyBox ol.formFields li.productAvailabilityMessage dd.availability").text("Out of Stock");
						}
					}
				});
				
				j("#buyboxQuantity").blur(function(){
					var quantity = j("input#buyboxQuantity").val();
					if (quantity % 24 === 0) {
					//alert('true');
					}
					else {
						alert("Please enter an amount divisible by 24");
						return false;
					}
				});
				j("button#addToCartButton").click(function(){
					var quantavail = j("li#quantityBox").attr("alt");
					var quantity = j("input#buyboxQuantity").val();
					if (quantity % 24 === 0) {
						if (parseInt(quantavail) < parseInt(quantity)) {
							alert('The inventory quantity you are trying to order is currently unavailable. Please try lowering the quantity or call 1-800-327-0074 to speak to a sales representative.');
							return false;
						}
					}
					else {
						alert("Please enter an amount divisible by 24");
						return false;
					}
				});
			}
		}
		
		if (j("body.cart#pageId1000").length > 0) {
			//Cart Ball Logic
			j(".itemQuantity").blur(function(){
				var sku = j(this).parent().parent().parent().parent().find("input[name^='cart.merchantSKU']").val();
				var title = j(this).parent().parent().parent().children("h4").text();
				if (title.indexOf("Dozen") >= 0) {
					var quantity = j(this).val();
					if (quantity % 12 === 0) {
					//alert('dozen');
					}
					else {
						alert('Please update this quantity to an amount divisible by 12');
						var button = j(this).next().find("button.updateQuantity");
						j(button).click(function(){
							var newquant = j(this).parent().parent().prev().val();
							if (newquant % 12 != 0) 
								return false;
						});
					}
				}
				return false;
			});
			j("button[name='proceedToCheckout']").click(function(event){
				j(".itemQuantity").each(function(){
				var sku = j(this).parent().parent().parent().parent().find("input[name^='cart.merchantSKU']").val();
				var title = j(this).parent().parent().parent().children("h4").text();
				if (title.indexOf("Dozen") >= 0) {
					var quantity = j(this).val();
					var origquantity = j(this).prev().val();
					if (origquantity % 12 === 0 && quantity == origquantity) {
					//alert('dozen');
					}
					else {
						if (quantity != origquantity)
							alert('Please update each ball product quantity to an amount divisible by 12 and click "update"');
						else
							alert('Please update each ball product quantity to an amount divisible by 12');
						event.preventDefault();
						return false;
					}
					
					/*if (quantity % 12 === 0) {
					//alert('true');
					}
					else {
						alert('Please update each ball product quantity to an amount divisible by 12');
						return false;
					}*/
				}
				});
				//j("div.proceedToCheckout form").submit();
				//j("button[name='proceedToCheckout']").trigger('click');
			});
		}
		
		if (j("body.orderPipeline#pageId2100").length > 0) {
			//Cart Ball Logic
			j("li.modifyItemQuantities button[name='appAction_ModifyItemQuantities']").click(function(){
				j("div.setQuantity input").each(function(){
					var title = j(this).parent().parent().find("h4.title").text();
					if (title == 'Rawlings OLB-1 Official League Baseball') {
						var quantity = j(this).val();
						if (j(this).parent().parent().find("div.invalidQuantity").length <= 0) {
							if (quantity % 12 === 0) {
							//alert('true');
							}
							else {
								alert('Please update each ball product quantity to an amount divisible by 12');
								return false;
							}
						}
					}
				});
			});
		}
		
		/* Truncate Text */
		function truncateText (element, length) {
			j(element).each(function() {
				var str = j(this).text();
				var limit = length;
				var bits, i;
				
				bits = str.split('');
				if (bits.length > limit) {
					for (i = bits.length - 1; i > -1; --i) {
					if (i > limit) {
					bits.length = i;
					}
					else if (' ' === bits[i]) {
					bits.length = i;
					break;
					}
					}
					bits.push('...');
				}
				var truncate = bits.join('');
				j(this).text(truncate);
			});
		}
		
		//truncateText(j("body#pageId0.browse div.hpTitlePrice h4 a"), 35);
		if (j("body.cart#pageId1000").length <= 0)
			truncateText(j("h4.title a"), 80);
		truncateText(j("body#pageId0.browse .productDetails h4 a, body.detail .productDetails h4 a"), 80);
		truncateText(j("body#pageId100.cart .product .productDetails h4 a, body#pageId100.cart .product .productDetails p.title a"), 75);
   }
);
