
	$(document).ready(function(){

		/**************************************************
			loop through all href tags and for any that are
			marked rel=external, make those open in a new
			windows.  this allows us to be w3c compliant
			xhtml since you can no longer use the target attr
		**************************************************/
		$("a[rel$='external']").click(function(){
			this.target = "_blank";
		});

		/**************************************************
			toggles topnav
		**************************************************/
		$("#topNav>ul>li>a>img").not($("#topNav>ul>li>a>img.navOn")).each(function () {
			var src = $(this).attr("src");
			var offState = src;
			var temp = new Array();
			temp = src.split('.');
			var onState = temp[0] + "_on." + temp[1];
			$(this).hover(
				function() {
					//alert($(this).attr("src"));
					$(this).attr("src", onState);
				},
				function() {
					$(this).attr("src", offState);
				}
			);
		});

		/**************************************************
			removes child ul from top nav if there are no subcats
		**************************************************/
		$("#nav>li>ul").each( function(){
			if($(this).children("li").html() == null) {
				$(this).remove();
			}
		});

		/**************************************************
		handles the mini cart flyout
		stop prop to prevent the body click call below from firing
		**************************************************/
		initBindings();


		/**************************************************
			clears out the search box default text
		**************************************************/
		$("#searchBoxHeaderInput").focus(function() {
			schval = $(this).val();
			if(schval == "Search Keyword or Item #") {
				$(this).val("");
			}
		});

		$("#searchForm").submit(function() {
			schval = $("#searchBoxHeaderInput").val();
			if(schval == "Search Keyword or Item #") {
				$("#searchBoxHeaderInput").val("");
			}
		});

		/**************************************************
			IE6 PNG Fix
		**************************************************/
		if ( (typeof $('body').supersleight == 'function') != false ) {
			$('body').supersleight({shim: '/img/common/transparent.gif'});
		}

		/**************************************************
		browser functionality overrides
		**************************************************/
		$("#signInEmailAddress").focus();

		/**************************************************
			toggle expand collapse
		**************************************************/

		/* first hide all details - do this here rather than css in case js is turned off, users can still view */
		$(".collapsible").not('.showFirst').hide();

		$(".expandButton").each( function() {
			$(this).click( function() {
				var details = $(this).toggleClass('collapseButton');
				$(this).siblings(".collapsible").slideToggle();
				return false;
			});

		});


		//Initially hide all options/methods/events
		$('div.option-description, div.option-examples, div.event-description, div.event-examples, div.method-description, div.method-examples').hide();

		//Make list items collapsible
		$('div.option-header h3, div.event-header h3, div.method-header h3').live('click', function() {
			var details = $(this).parent().toggleClass('header-open');
			if ( showExamples ) {
				details.nextAll().toggle();
			} else {
				details.next().toggle();
			}
		});

		//$("").toggle(selectedEffect,options,500);
		//$("#button").click(function() {
		//	runEffect();
		//	return false;
		//});

		$('#ddSavedAddresses').change(function() {
			$('.shippingColumn .current').hide().toggleClass('current');
			$('#savedAddress' + (parseInt($(this).attr("selectedIndex")) + 1)).show().toggleClass('current');
		});

//		$('#selectAnotherCard').change(function() {
//			$('.billingColumn .current').hide().toggleClass('current');
//			$('#ccList' + (parseInt($(this).attr("selectedIndex")) + 1)).show().toggleClass('current');
//		});

		$('#savedBillingAddress').change(function() {
			$('#ccDifferentBox .current').hide().toggleClass('current');
			$('#billingAddressList' + (parseInt($(this).attr("selectedIndex")) + 1)).show().toggleClass('current');
		});

		if ($('#savedBillingAddress').length) {
			$('#ccDifferentBox .current').hide().toggleClass('current');
			$('#billingAddressList' + (parseInt($('#savedBillingAddress').attr("selectedIndex")) + 1)).show().toggleClass('current')
		}

		jQuery('.button_showRules').live('click', function() {
			jQuery('.shadowBackground').fadeIn('fast');
			$('html, body').animate({scrollTop:0}, 'fast');
			return false;
		});

		jQuery('.button_closeRules').live('click', function() {
			jQuery('.shadowBackground').fadeOut('fast');

			return false;
		});

		//universal script for buttons marked with printButton class
		$(".printButton").click(function(){
			print();
		});

		//same thing for buttons having print-page class
		$(".print-page").click(function(){
			print();
		});


		/***********************************
		 * begin add to cart button ajax calls
		 *
		 * the actual ajax part has been moved to a function  to allow other buttons to support it without rewriting it
		 */
		$(".addToCartButton").click(function(e){
			addToCartAjax($(this));
			return false;
		});

		/***********************************
		 * end add to cart ajax functionality
		 */

		/***********************************
		 * universal error and success messaging scripts
		 *
		 *
		 */
		if ( (typeof $(document).getUrlParam == 'function') != false ) {
			if($(document).getUrlParam("errorMsg") > "") {
				var msg = $(document).getUrlParam("errorMsg");
				switch(msg) {
		            case "1": output = "There was an error processing your request.";
		            break;
		            case "2": output= "Please select an option from the dropdown list.";
		            break;
		            default: output ="";
		        }

				$("#errorMessageBox").text(output).show();
			}
			if($(document).getUrlParam("successMsg") > "") {
				var msg = $(document).getUrlParam("successMsg");
				switch(msg) {
		            case "1": output = "Your request was processed successfully.";
		            break;
		            case "2": output= "Thank you for your submission.";
		            break;
		            case "3": output= "Thank you for submitting your review. Your review will be processed shortly.";
		            break;
		            default: output ="";
		        }
				$("#successMessageBox").text(output).show();
			}
		}

		$(".showTruncated").click( function(e) {
			$(this).parents(".truncated").hide();
			$(this).parents(".truncated").siblings(".notTruncated").show();
			e.preventDefault();
		});
		$(".hideTruncated").click( function(e) {
			$(this).parents(".notTruncated").hide();
			$(this).parents(".notTruncated").siblings(".truncated").show();
			e.preventDefault();
		});

		$('#reviewText').keyup(function(){
			 limitChars('reviewText', 2000, 'charLimit');
		});

		$('.whatsThis').click(function() {
		    window.open(this.href,'WhatsThis','width=575,height=430');
		    return false;
		});
		$('.hazmatFee').click(function() {
		    window.open(this.href,'HazmatFee','width=300,height=200');
		    return false;
		});

	});

	//ajax add to cart
	function addToCartAjax(obj) {
		href = obj.attr("href");
		//alert("getting " + href);
		$.get(href,
			function(data){
				$.ajax( {
		        	url: '/common/miniCartAjax.jsp?rnd=' + random_string(),
		            success: function(html) {
		                $('#miniCartPositioner').empty().append(html);
		                initBindings();
		                miniCartDirection("down");
		                $('html, body').animate({scrollTop:0}, 'slow');
		            }
		        });
			}
		);
	}


	function random_string() {
		var time = String((new Date()).getTime()).replace(/\D/gi, '');
		return time;
	}
	
	//common resued functions
	function miniCartDirection(direction) {
		if(direction == "down") {
			$('#miniCartFlyoutHolder').slideDown('fast', function() {
				$('.scroll-pane').jScrollPane();
			});
		} else {
			$('#miniCartFlyoutHolder').slideUp('fast', function() {

			});
		}
	}


	function initBindings() {
		$('#miniCartHolder').click( function(e) {
			if($('#miniCartFlyoutHolder').is(":visible")) {
				miniCartDirection("up");
			} else {
				miniCartDirection("down");
			}
			e.stopPropagation();
		});
		$('#miniCartFlyoutClose').click( function(e) {
			miniCartDirection("up");
			e.stopPropagation();
		});
		$('#miniCartFlyoutHolder').click( function(e) {
			e.stopPropagation();
		});
		$('body').click( function(e) {
			if($('#miniCartFlyoutHolder').is(":visible")) {
				miniCartDirection("up");
			}
		});
	}


	//For splitting and filling the phone numbers into 3 separate fields
	function fillPhoneNumber(phoneIdName) {
		var phoneNumber = $('#phoneNumber1').val() + $('#phoneNumber2').val() + $('#phoneNumber3').val();
		$('#' + phoneIdName).val(phoneNumber);
	}

	function splitPhoneNumber(phoneIdName) {
		var phoneNumber = $('#' + phoneIdName).val(phoneNumber);
		if (phoneNumber) {
			$('#phoneNumber1').val(phoneNumber.substring(0,3));
			$('#phoneNumber2').val(phoneNumber.substring(3,6));
			$('#phoneNumber3').val(phoneNumber.substring(6));
		}

	}

	function limitChars(textid, limit, infodiv, txt) {
		var text = $('#'+textid).val();
		var textlength = text.length;
		if(textlength > limit) {
			$('#' + infodiv).html('0 characters remaining');
			$('#'+textid).val(text.substr(0,limit));
			return false;
		}
		else {
			$('#' + infodiv).html((limit - textlength) + ' ' + txt);
			return true;
		}
	}


	//check for images that are missing
	$(window).bind('load', function() {
		$('img').not("#mycarousel li img").each(function() {
			if((typeof this.naturalWidth != "undefined" &&
				this.naturalWidth == 0 )
				|| this.readyState == 'uninitialized' ) {
				lastSlash = $(this).attr("src").lastIndexOf("/");
				prefix = $(this).attr("src").substr(0,lastSlash);
				$(this).attr("src", prefix+"/default.jpg");
			}
		});
	})

