$(window).load(function() {
$('#ancResult .add_block input[alt="Add To Cart - Click Here"]').live('click', function (){
	$(this).fadeOut();
});
/************************************************************************************************************************************ 
     #Header | Global / Mini Cart Toggle / Drop Down / Carousel
************************************************************************************************************************************/
/******************************************** 
     #Main Homepage Banner Rotator
********************************************/
	$('.bannernav li:first-child').addClass('active');

	rotate = function(){
		var slideHeight = $('.rotatebanner img').height();
		var slideID = active.find('a').attr("rel");
		var slide = slideID * slideHeight;
		$('.bannerwrap').animate({ marginTop : -slide });
		
		$('.bannernav li').removeClass('active'); //Remove all active class
		active.addClass('active');
	}; 
	rotateSwitch = function(){
		play = setInterval(function(){ 
			active = $('.bannernav li.active').next(); 
			if ( active.length === 0) { 
				active = $('.bannernav li:first'); 
			}
			rotate(); 
		}, 5000); 
	};
	$('.bannernav li').click(function() {
		active = $(this); 
		clearInterval(play); 
		rotate(); 
		rotateSwitch();
		return false; 
	});
	rotateSwitch(); //Run function on launch
/******************************************** 
     #Main Homepage Banner Rotator
********************************************/	
	/*$('li.ymm span').click(function(){
		$(this).next().slideToggle();							
	});
	if($('li.ymm span').html().replace(' <a href="#">(edit)</a>','').length >= 25){
		var vehicleInfo = $('li.ymm span').html().replace(' <a href="#">(edit)</a>','').slice(0,25);//This reduces the amount of characters for the vehicle information.
		$('li.ymm span').html(vehicleInfo+'&hellip; <a href="#">(edit)</a>');
	}*/
/******************************************** 
     #Store Banner - Global
********************************************/
	var storeView = $('.storequickview').height();
	var storeViewHeight = storeView + 70;

	$(".storequickview").animate({ top: -storeViewHeight , pacity: 0}, 1);
	
	$(".storebanner a.trigger").toggle(function(){
		$(this).parent().stop().animate({height: storeViewHeight}, 300);					  
		$(this).parent().find(".storequickview").stop().animate({
			top: '-5px',
			opacity: 1
		}, function() {$(this).css("z-index","9999")});
	}, function() {
		$(this).parent().stop().animate({height: '50px'});
		$(this).parent().find(".storequickview").stop().animate({
			top: -storeViewHeight,
			opacity: 1
		}).css("z-index","0");
		return false;
	});
/******************************************** 
     #Mega Drop Down Menu
********************************************/
	//On Hover Over
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
		$(this).find("a").addClass('hover');
		(function($) {
			//Function to calculate total width of all ul's
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() { //for each ul...
					rowWidth += $(this).width(); //Add each ul's width together
				});
			};
			jQuery.fn.calcColWidth = function() {
				subColnum = 0;
				$(this).each(function() { //for each ul...
					subColnum = $(this).find('ul').size(); //Find how many UL's to determine background image colum #
				});
			};
		})(jQuery); 
		if ( $(this).find(".row").length > 0 ) { //If row exists...
	
			var biggestRow = 0;	
			var mostCol = 0;			
	
			$(this).find(".row").each(function() {	//for each row...
				$(this).calcSubWidth(); //Call function to calculate width of all ul's
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
				
				$(this).calcColWidth();
				if(subColnum > mostCol) {
					mostCol = subColnum;
				}
			});
			subColHeight = (mostCol * 960) - 960;
			$(this).find(".sub").css({'width' : biggestRow , 'background' : 'url(/images/layout/nav_sub_bg.png) no-repeat -' + subColHeight + 'px bottom'}); //Set width + bg
			$(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
	
		} else { //If row does not exist...
	
			$(this).calcSubWidth();  //Call function to calculate width of all ul's
			$(this).find(".sub").css({'width' : rowWidth}); //Set Width
	
		}
	}
	//On Hover Out
	function megaHoverOut(){
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
		  $(this).hide();  //after fading, hide it
	  });
	  $(this).find("a").removeClass('hover');
	}

	//Set custom configurations
	var config = {
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
		 interval: 3, // number = milliseconds for onMouseOver polling interval
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		 timeout: 40, // number = milliseconds delay before onMouseOut
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};
	$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("ul#topnav li .row:last-child").css({'background':'none'}); //Fade sub nav to 0 opacity on default
	$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations
/************************************************************************************************************************************ 
     #Content | Tabs / Misc
************************************************************************************************************************************/
/******************************************** 
     #Equal Height
********************************************/
	$.fn.equalHeight= function(){
		var tonneauCat = location.search;
		//This is only for IE7 and below because they have a bug with display:clear
		if($.browser.msie){
			$('ul.gridview li:nth-child(3n+1)').addClass("clear");
			if(jQuery.browser.version == 7.0 || jQuery.browser.version == 6){
				$("ul.gridview li .thumb a img").each(function (){
					var Himg = parseInt($(this).height(), 10 );
					if(Himg == 0){
						Himg = 195;//This catches a bug were the images haven't load yet. We assume the image height is 145px. This is our usual size.
					}
					if(Himg < 180){
						var Hctn = parseInt($(this).parent().height(), 10 );
						var paddingTop = (Hctn/2)-(Himg/2);
						//console.log("Himg: " + Himg + "px | Hctn: " + Hctn + "px | paddingTop: " + paddingTop+"px");
						$(this).css("padding-top",paddingTop);
					}
				});
				if(tonneauCat == "?catid=3&subcat=141&ptid=3135" || tonneauCat == "?t_c=3&t_s=141&t_pt=3135"){
					$("ul.gridview li").css("height","380px");
				}
				else{
					$("ul.gridview li:not(ul.gridview li ul li).clear").each(function (){
						var liIndex = $("ul.gridview li:not(ul.gridview li ul li)").index(this);
						var liHeight = 0;
						$("ul.gridview li:not(ul.gridview li ul li)").slice(liIndex, liIndex+3).each(function (){ 
							var thisLiHeight = $(this).height();
							if(thisLiHeight > liHeight){
								liHeight = thisLiHeight;
							}
						});
						$("ul.gridview li:not(ul.gridview li ul li)").slice(liIndex, liIndex+3).css("height",liHeight);
					});
				}
			}
		}
		/*function equalHeightAdjust(group) {
			tallest = 0;
			group.each(function() {
				var thisHeight = $(this).height();
				if(thisHeight > tallest) {
					tallest = thisHeight;
				}
			});
			group.height(tallest);
			if ($('.testimonials').is(':visible')) {
				var tallest = tallest + 20;
				$('.testimonials').css({'height' : tallest});
			}
		}
		if( $(this).hasClass('gridview')) { //When running the equal height on a col4
			$(this).each(function() {
				equalHeightAdjust($(this).find('li')); //Run it on each listitem
			});
		} else {
			equalHeightAdjust($(this)); //Run default equal height function
		}*/
	}
	//Load if list has only a col4 thumb view
	$(window).load(function(){
		if ( $('.products ul.gridview:first').hasClass("listview") ) {
		} else if ( $('ul.gridview').is(':visible') ) {
			$('.products ul.gridview:first').equalHeight();
		} else if ($('.testimonials').is(':visible')) {
			$('.testimonials blockquote').equalHeight();
		}
	});
	
	$('.equalHeight, .gridview').equalHeight();
/******************************************** 
     #Teaser truncate '...'
********************************************/
	$.fn.showhide = function(){ 
		fullText = $(this).html(); //Get full HTML of 
		teasText = fullText.substring(0,40) + "..."; //Cut down html  to xxx characters (teaser version)
		$(this).html(teasText); //Replace with new teaser version
	};//end function
	
	$('#videos h3 a').each(function() { //On each of the #videos headings...
		$(this).showhide(); //Run showhide function
	});
/******************************************** 
     #Select Option Jump
********************************************/
	$('select.jump').change(function() {
		 window.location=$(this).attr('value');									
	});
/******************************************** 
     #Testimonials Rotator
********************************************/
	$('.testimonials blockquote:first-child').show();
	$(function(){
		$('.testimonials blockquote:gt(0)').hide();
		setInterval(function(){
		  $('.testimonials blockquote:first-child').fadeOut()
			 .next().fadeIn()
			 .end().appendTo('.testimonials');}, 
		  15000);
	});
/******************************************** 
     #Carousel
********************************************/
	/////Carousel Function + Smart Columns
	$.fn.carousel = function(){ 
		
		var rotatorLi = $(this).find("ul.rotator li").width();
		var rotatorSum = $(this).find("ul.rotator li").size();
		var carousel = $(".carousel").width();
	
		var rotatorMaxLi = Math.floor(carousel / rotatorLi); //See how many lists can fit in the carousel viewport
		var rotatorSlideSum =  Math.ceil(rotatorSum / rotatorMaxLi); //See how many slides (sections viewable in carousel viewport) we will need
	
		var adjustLi = (carousel / rotatorMaxLi); //Perfect width that would fit in carousel viewport
		var adjustRotator = (adjustLi * rotatorSum); //Get width of adjusted rotator
		
		var lastSlideSum = Math.floor(adjustRotator / carousel); //Get the whole number of slides that can fit in carousel (for remainder of slides)
		var lastSlide = (carousel * lastSlideSum) - adjustRotator; //Get the distance of the remaining slides
	
		//Get tallest list item in carousel (prevents truncation)
		var tallest = 0;
		$(this).find("ul.rotator li").each(function() {									
			var rotatorLiHeight = $(this).height();
			if (rotatorLiHeight > tallest) {
				tallest = rotatorLiHeight + 20; //20 takes in consideration of padding 10px 0;
			}
		});
		$(this).find(".carousel").css({ 'padding-bottom' : '10px'}); //Adjust Height of Carousel
	
		$(this).find("ul.rotator li").css({ 'width' : adjustLi}); //Adjust width
		$(this).find("ul.rotator").css({ 'width' : adjustRotator}); //Adjust width
	
			
		var sum = 0; //Set Count for clicks
				
		if (carousel < adjustRotator) { //If the list is bigger than the carousel viewport
			$(this).find("a.right-scroll").click(function() {
				if(sum < (rotatorSlideSum-1)) {	
					sum++;
					$(this).parent().find("a.left-scroll").removeClass("deactive");
					switch(sum){
						case rotatorSlideSum: //on the last slide...
							$(this).addClass("deactive");
							break;
						case rotatorSlideSum-1: //second to last slide...
							$(this).addClass("deactive");
							if (lastSlide < -1) {
								$(this).parent().find("ul.rotator").animate({ marginLeft: "+=" + lastSlide }, 250);
							}
							else {
								$(this).parent().find("ul.rotator").animate({ marginLeft: "-=" + carousel }, 250);
							}
							break;
						default: //on click else
							$(this).parent().find("ul.rotator").animate({ marginLeft: "-=" + carousel }, 250);
							break;
					}
				}
				return false;
			}); //end switch case
		
			$(this).find("a.left-scroll").addClass("deactive");
			$(this).find("a.left-scroll").click(function() { 
				if(sum > 0) {	
					sum--;
					$(this).parent().find("a.right-scroll").removeClass("deactive");
					switch(sum){
						case 0: //if back to original slide...
							if (lastSlide < -1 && rotatorSlideSum == 2 ) { 
								$(this).parent().find("ul.rotator").animate({ marginLeft: "-=" + lastSlide }, 250);
							}
							else {
								$(this).parent().find("ul.rotator").animate({ marginLeft: "+=" + carousel }, 250);
							}
							$(this).addClass("deactive");
							break;
						case rotatorSlideSum-2: //1st click back from the last slide...
							if (lastSlide < -1) {
								$(this).parent().find("ul.rotator").animate({ marginLeft: "-=" + lastSlide }, 250);
							}
							else {
								$(this).parent().find("ul.rotator").animate({ marginLeft: "+=" + carousel }, 250);
							}
							break;
						default:
							$(this).parent().find("ul.rotator").animate({ marginLeft: "+=" + carousel }, 250);
							break;
					}
					
				}
				return false;
			}); //end switch case
		} else { //if there is only one slide...
			$(this).find("a.right-scroll, a.left-scroll").addClass("deactive");
		}//end if carasouel statement
	};//end function


	////Trigger Carousel + Component Functions
	$('.carousel_placement').each(function() { //for each carousel run the following code...
		 tabID = $(this).attr('id'); //get the ID of the tab content
		$('#' + tabID).find("div[class^='carousel']").carousel();	//run the carousel function on that ID
	});
/******************************************** 
     #TABS
********************************************/
	function tabbedNavigation(){
		var tabItems = [];
		var locationHash = location.hash;
		$("ul.tabs li a").each(function(){ 
			var items = $(this).attr("href");
			tabItems.push(items);
		});
		if(locationHash === "") {
			//If its a manufacture page first tab should not be automatically selected
			if ($('#manpage').is(':visible')) {
			} else {				
				$("ul.tabs").each(function(){
					$("li:first", this).addClass("active").show(); //Activate first tab
				});
			}
			if ($('#brands').is(':visible')) {
			} else { //if not only show first content container
			//When page loads...
				$(".tab_content").hide(); //Hide all content
				$(".tab_container").each(function(){
					$(".tab_content:first",this).show(); //Show first tab content
				});
			}
		}
		else{
			var tabItemsIndex = jQuery.inArray(locationHash, tabItems);
			
			if(tabItemsIndex != -1){
				var targetedTab = $("ul.tabs li a[href="+locationHash+"]");
				var targetedTabContent = $(".tab_content"+locationHash);
				
				targetedTabContent.parent().find(".tab_content").each(function(){
					$(this).hide();
				}); //Hide all content
				targetedTab.parent().siblings().removeClass("active");
				targetedTab.parent().addClass("active");
				targetedTabContent.fadeIn();
			}
			else{//Don't do anything if there is the href doesn't point to a tab (like an anchor link)
			
			/*console.log("test: "+tabItems+" | "+tabItemsIndex);
			
				$(".tab_content").hide(); //Hide all content
				$("ul.tabs").each(function(){
					$("li:first", this).addClass("active").show(); //Activate first tab
				});
				$(".tab_container").each(function(){
					$(".tab_content:first",this).show(); //Show first tab content
				});*/
			}
		}
		//On Click Event
		$("ul.tabs li").click(function() {
			/*$("ul.tabs li").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab*/
			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			/*$(".tab_content").hide(); //Hide all tab content
			$(activeTab).fadeIn(); //Fade in the active ID content*/
			
			if ( activeTab == "#activeAll" ) { //Shows all content for all brands
				location.href = "";
			} else {
				location.href = activeTab;
			}
			return false;
		});
	};	
	/******************************* ORGINAL TABS CODE *****************************/
	/*
	//If its a manufacture page first tab should not be automatically selected
	if ($('#manpage').is(':visible')) {
	} else {
		$("ul.tabs").each(function(){
			$(this).find('li:first-child').addClass("active"); //Activate first tab
		});
	}
	
	if ($('#brands').is(':visible')) {
	} else { //if not only show first content container
	//When page loads...
		$(".tab_content").hide(); //Hide all content
		$('.tab_container').each(function(){
			$(this).find('.tab_content:first-child').show(); //Show first tab content
		});
	}

	if ($('#manpage').is(':visible')) {
	} else { //if not only show first content container
		$("ul.tabs li").click(function() {
			$(this).parent().find("li").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			
			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			
			if ( activeTab == "#activeAll" ) { //Shows all content for all brands
				$(".tab_content").show();
			} else {
				$(this).parent().next().find(".tab_content").hide();	
			}
			
			$(activeTab).show().detectSwitchView(); //Fade in the active ID content and check for list view
			$(activeTab).find('.carousel').carousel();

			
			switch(activeTab) { //Different scenarios...
				case '#prodvideos':  //When video tab is clicked...
					$('#prodvideos ul.gridview').equalHeight();
					break;
				default: 
			}
			return false;
		});
	}
	********************************************************************************/
	
	//Vertical Tabs on Homepage
	$(" ul.nav li:first").addClass("vert_selecttab").show(); //Activate first tab
	$(".vert_tabcontent:first").show(); //Show first tab content
	
	$('#store ul.nav li a').each(function(){
		var cont_ID = $(this).attr('href');
		if ( $(cont_ID).html().trim() ) {
		} else {
			$(this).parent().addClass('deactive');
		}	
	});
	
	//On Click Event
	$("ul.nav li").live('click',function() {
								  
		if ( !$(this).hasClass('deactive') ) {		
			$("ul.nav li").removeClass("vert_selecttab"); //Remove any "active" class
			$(this).addClass("vert_selecttab"); //Add "active" class to selected tab
			$(".vert_tabcontent").hide(); //Hide all tab content
			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			$(activeTab).show(); //Fade in the active ID content
		}
		return false;
	});
	
	var hasTabs = $("ul.tabs").size();
	if(hasTabs >= 1){
		
		var checkTabLink = $(".tabs li a").attr("href").search(/^\//);
		
		if(checkTabLink == -1){
			var lastHash = '';
			function pollHash() {
				if(lastHash !== location.hash) {
					lastHash = location.hash;
					// hash has changed, so do stuff:
					tabbedNavigation();//We look at the URL to see if we need to show a specific slide.
				}
			}
			setInterval(pollHash, 1000);
			tabbedNavigation();
			if ( $("#map").length > 0 ) {
			map.checkResize();
			}
		}
	}
/******************************************** 
     #MISC 
********************************************/
	//#col4 adjustment on 4th list item
	$('ul.col4 li:nth-child(4n+4), ul.col3 li:nth-child(3n+3),  .thumbs img:last').css({'margin-right':'0'}); //kills right margin 
	
	//Filter Label Animation and Fade
	$('img.findit').animate({top: '-85px', opacity: '1'}, 500);
	
	//IEfix for Tree list
	$('ul.tree li:last-child').css({'background-position': 'left -6px', 'background-repeat': 'no-repeat'}); 

	//Input tooltip
	$('.formtip').focus(function(){
		if (this.value == this.defaultValue ) {
			$(this).attr({value: '' });
		}
		$(this).addClass('focused');
	}).blur(function() {
		if (this.value == '') {
			this.value = this.defaultValue;
			$(this).removeClass('focused');
		}
	});
	
	$('a[rel=prettyPhoto]').prettyPhoto({theme: 'facebook'});
	
	//Fix IE6 :first-child on topnav
	$('ul#topnav > li > a:first-child').addClass('topnav_li_a');
	
	$('.col50:nth-child(2n+1):not(.intro)').css({'margin-right':'10px'});
	$('.col50:nth-child(2n+2):not(.intro)').css({'margin-left':'10px'});

	$('.intro .col50:first').css({'margin':'0 10px 0 0'});
	$('.intro .col50:eq(1)').css({'margin':'0 0 0 10px'});
/******************************************** 
     #Toggle
********************************************/	
	//Hide (Collapse) the toggle containers on load
	$(".toggle_container").hide(); 
	$('.sidecol ul.filter .toggle_container:first').show().parent().find('strong').addClass('active');

	//Switch the "Open" and "Close" state per click
	$(".trigger").click(function(){
		var triggerTarget = $(this).attr("rel");
		if(triggerTarget){
			//This takes a string with the format: show=string1,string2,stringX&hide=string1,string2,stringX
			//using this format we can show or hide several targeted elements. ~Brenda
			
			var triggerTargetShow = triggerTarget.replace(/&hide=.*/,"").replace("show=","");
			var triggerTargetHide = triggerTarget.replace(/.*hide=/,"");			
			
			var splitTargetHide = triggerTargetHide.split(",");
			for(i = 0; i < splitTargetHide.length; i++){
				$(splitTargetHide[i]).slideUp();
				//console.log("Hide: "+splitTargetHide[i]+" "+i);
			}
			
			var splitTargetShow = triggerTargetShow.split(",");
			for(j = 0; j < splitTargetShow.length; j++){
				$(splitTargetShow[j]).show().removeClass("invis").slideDown();
				//console.log("Show: "+splitTargetShow[j]+" "+j);
			}
			
			return false;
		}
		else{
			$(this).toggleClass("active");
			$(this).next(".toggle_container").slideToggle("fast");
			if ($('#checkout').is(':visible')) {
				return true;
			} else {
				return false;
			}
		}
	});
/******************************************** 
     #Accordion
********************************************/
	$(".accordion .content").hide();
	$(".accordion h3:first").addClass("open").next().show();
	$(".accordion h3").click(function() {
		if( $(this).hasClass("open") ) {
		} else {
			$(".accordion h3").removeClass("open");
			$(".accordion .content").slideUp();
			$(this).next().slideToggle();
		}
		$(this).addClass("open");
		return false;
	});
/******************************************** 
     #Switch View
********************************************/	
	//Function to adjust listview/thumbview
	$.fn.switchView = function(){ 
		var thisTabContent = $(this).parents(".tab_content"); //Find active tab content
		
		if( $(this).val() == 'thumbs' ) {//This is thumb view
		
			$(thisTabContent).find('ul#ulPartTypes').removeClass('listview').addClass("gridview").equalHeight();
			$(this).parent().find('label').removeClass('listview');
			
			if ( $(thisTabContent).attr('id') == ('videos') ) {
				$('#videos h3').each(function() { //On each of the #videos headings...
					$(this).find('a').showhide();
				}); 
			}
			
		} else {//This is detailed view		
			$(thisTabContent).find('ul.gridview li:nth-child(3n+1)').removeClass("clear");
			$(thisTabContent).find('ul.gridview li').css("height","auto");
			$(thisTabContent).find('ul#ulPartTypes').removeClass('gridview').addClass("listview");
			$(this).parent().find('label').addClass('gridview');
			if ( $(thisTabContent).attr('id') == ('videos') ) {
				$('#videos h3').each(function() { //On each of the #videos headings...
					$(this).find('a').html($(this).find('a').attr('title'));
				}); 
			}
		}
	};//end function

	//Detect if listview/thumbnail is default
	$.fn.detectSwitchView = function(){ 
		if ( $(this).find("ul#ulPartTypes").hasClass("listview") ) {
			$(".switchview select").val('details');
		} else {
			$(".switchview select").val('thumbs');
		}
	}
	//Run detect on first tab content
	$('.tab_content:first').detectSwitchView();
	
	//On Select Change
	$(".switchview select").change(function(){ 
		$(this).switchView();
	});
/******************************************** 
     #Product Thumb Viewer
********************************************/
	$(".thumbs a:first-child").addClass("active");
	$(".thumbs a").click(function() {
		var mainImage = $(this).attr("href"); //Find Image Name
		$(".thumbs a").removeClass("active");
		$(this).addClass("active");
		$(".lrg_view").attr({href : mainImage + '&w=600'}).find("img").attr({ src: mainImage + '&w=301' });
		return false;		
	});
/******************************************** 
     #On Sale Now Page 
********************************************/
	//Sort Promo with Cat ID /*--Special Deals Page--*/
	$("#catidselector select").change(function() {
		$("ul.col1 li, .js_message").fadeOut('fast');
		var catidVal = $(this).val(); //Get Cat ID
		//var catName = $("#catidselector select :selected").text(); Unused variable
		if ($("li").hasClass("catid_" + catidVal)) { //If selection matches cat ID
			$("li.catid_" + catidVal).fadeIn('fast');
		} else if ( catidVal == '-1' ) { //If selection is default
			$("li").show();
		}
	});
/******************************************** 
     #Popups // Custom Popups for inline markup + images
********************************************/
	$('a.poplight[href^=#]').click(function() {
		var popID = $(this).attr('name'); //Get Popup Name
		var popURL = $(this).attr('href'); //Get Popup href to define size
				
		//Pull Query & Variables from href URL
		var query= popURL.split('?');
		var dim= query[1].split('&');

		//Fade in the Popup then set width
		$('#' + popID).fadeIn().css({ 'width': Number(dim[0].split('=')[1]) });
		
		//Define margin for center alignment (vertical + horizontal)
		var popMargTop = ($('#' + popID).height() + 40) / 2;
		var popMargLeft = ($('#' + popID).width() + 40) / 2;
		
		//Apply Margin to Popup
		$('#' + popID).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		//Fade in Background
		$('body').append('#fade');
		$('#fade').fadeIn();
		
		return false;
	});
	
	function processPopImage(){
		var popMargTop = ($('img.view_lrg').height() + 40) / 2; 
		var popMargLeft = ($('img.view_lrg').width() + 40) / 2;
		$('#poplight').css({ 
			'width': $('img.view_lrg').width(),
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft,
			'visibility' : 'visible'
		});	
	}	

	
	//IMAGES
	$('a.poplight[href*=.jpg] , a.poplight[href*=.gif] , a.poplight[href*=.png]').click( function() {
		$('#poplight').remove();									   
		
		var popURL = $(this).attr('href');
		var img = new Image();
		$('.bodywrap').append('<div id="fade"></div>');
		$('#fade').css('filter', 'alpha(opacity=80)').fadeIn().after('<div id="preload"><img src="/images/misc/ajax-loader.gif" alt="Loading..." /></div><div id="poplight"><a href="#" class="close"><img src="/images/buttons/close_pop.png" class="btn_close" title="Close" /></a><img src="'+ popURL + '" class="view_lrg" /></div>');
		
		$(img).load(function() {
			$('#preload').fadeOut().remove();
			processContent();
		}).attr('src', popURL);

		return false;
	});

	$('a.close, #fade').live('click', function() {
	  	$('#fade , .popup_block').fadeOut();
		$('#poplight , #preload, #fade').remove();
		return false;
	});
/************************************************************************************************************************************ 
     #CHECKOUT
************************************************************************************************************************************/
/******************************************** 
     #International Shipping  / Checkout  Step 1
********************************************/
	$(".validate_NONUS").hide();	
	$.fn.countryDetect= function(){
		var countrySelect = $('.country_select option:selected').val();
		$(".validate_US, .validate_NONUS").hide();
		if (countrySelect == '0') {
		} else if (countrySelect == 'US') {
			$(".validate_US").fadeIn();
		} else {
			$(".validate_NONUS").fadeIn();
		}	
	}
	$(".country_select select:selected").countryDetect(); //Run if already selected
	$(".country_select select").change(function() { // Run when option changes
		$(this).countryDetect();
	});	
/******************************************** 
     #Terms Printing - Checkout Step 2
********************************************/
	//Hides everything but the terms and conditions (for print only)
	$(".print_trigger").click(function() {
		$("#checkout").addClass('hidePrint');	
		var terms = $(this).next().text();
		$('#content').append("<div class='showPrint'></div>");
		$('.showPrint:first').html("<h1>Promotional Terms</h1>" + terms);
		return true;
	});
	//brings back default print settings when billing section is closed
	$('.billing a').click(function() { 
		$("#checkout").removeClass('hidePrint');
		$('.showPrint').remove();
	});
/********************** 
	For Hard Parts Image Map - Highlight 
**********************/
	$("area").click(function() {
		var anchorID = $(this).attr("href");
		var anchorIDfloat = parseFloat(anchorID.substring(1));
		$("ul.partlist li").removeClass("highlight");
		$("ul.partlist li.partnum_" + anchorIDfloat).addClass("highlight");
	});
/******************************************** 
     #Tables 
********************************************/
	$("table.tabdata").filter(function(){
		/* Apply THead Background*/
		$("thead tr:last-child th",this).css("background","#f1f1f1 url('/images/theme/tbl_th_bg.jpg') repeat-x bottom");
		/* Remove Extra Borders */	
		$("tbody tr:first-child td",this).css("border-top","none");	
		$("tbody tr td:last-child, thead tr th:last-child",this).css("border-right","none");
	});
	/* Apply odd Class - Only Works on Simple Tables*/
	$("table.tabdata:not(.no-odd)").each(function(){
			$("tbody tr:odd",this).addClass("odd");
	});
/******************************************** 
    Lightbox
********************************************/
	//Var Browser is at the beggining of the document.
	var browser = navigator.userAgent;
	var lightboxAnimation = true;
	if((browser.match(/iPhone/i)) || (browser.match(/iPad/i)) || (browser.match(/maemo/i)) || (browser.match(/android/i))  || (browser.match(/webos/i)) || (browser.match(/MSIE 6/i)) || (browser.match(/MSIE 7/i)) || (browser.match(/MSIE 8/i)) ) {
		lightboxAnimation = false;
	}
	$(".vehicle_lightbox_wrapper, .closeLightbox").live("click",function (){
		var errorsOnPage;
		var buttonAction;
		var growlNotificationSize;
		
		function hideGrowlNotification(){
			$(".growl_notification").parent().hide();
		}
		
		function hide(buttonAction){
			errorsOnPage = $(".lightbox_container .errormsg").size();
			//alert("Errors: "+errorsOnPage+" | Action: "+buttonAction);
			if(errorsOnPage === 0 || buttonAction === "cancel"){
				$(".filter_lightbox #products.tab_content").find(".info").removeClass("invis");
				var YMMFiltersSize = $(".filter_lightbox").size();
				if(YMMFiltersSize === 1){
					$("body").removeClass("filter_lightbox");
					$(".filterwrap").parent().addClass("dropdown_container");
					$(".vehicle_details").next("p").hide();
				}
				clearInterval(YMMLightboxScreenPosition);
				if(lightboxAnimation === true){
					$(".vehicle_lightbox_wrapper").fadeOut().animate({opacity:0.7});
					$(".lightbox_container:not(#ymm_filter.lightbox_container)").animate({top:-99999},{duration:4000,complete:function (){$(this).hide()}});
				}
				else{
					$(".vehicle_lightbox_wrapper").hide().css("opacity","0.7");
					$(".lightbox_container:not(#ymm_filter.lightbox_container)").hide();
				}
				growlNotificationSize = $(".growl_notification").size();
				if(growlNotificationSize >= 1){
					setTimeout(hideGrowlNotification,5000);
				}
				clearInterval(verifyFormHasBeenProcessed);
				//$(".vehicle_lightbox_wrapper").remove();
			}
		}
		
		if($(this).attr("alt")){
			buttonAction = $(this).attr("alt").toLowerCase();
		}
		else{
			buttonAction = "cancel";
		}
		if(buttonAction === "save" || buttonAction === "submit"){
			var hiddenInputValue;
			var verifyFormHasBeenProcessed;
			
			verifyFormHasBeenProcessed = setInterval(function(){
				hiddenInputValue = parseInt($(".lightbox_container input#ctl00_ContentPlaceHolder1_hf_BillingAddress_CreateEdit_IsSPD, .lightbox_container #ctl00_ContentPlaceHolder1_pDetailD_partFitment_IsSPD").attr("value"),10);
				if(hiddenInputValue === 1){//Error on Page
					if($(".lightbox_container.partdetail")){//Part Detail Error Handling
						errorsOnPage = $(".lightbox_container.partdetail .errormsg").size();
						//alert("Errors: "+errorsOnPage);
						if(errorsOnPage === 0)
						{
							clearInterval(verifyFormHasBeenProcessed);	
							$(".lightbox_container #ctl00_ContentPlaceHolder1_pDetailD_partFitment_IsSPD").attr("value","0");						
							hide(buttonAction);
						}
					}
					else{
						clearInterval(verifyFormHasBeenProcessed);
						hide(buttonAction);
					}
				}
			}, 100);
		}
		else{
			hide(buttonAction);
		}
	});
	function lightboxSlideOnScreen (getLightboxTarget){
		var screenWidth;
		var screenHeight;
		var screenScrollPosition;
		var lightboxWidth;
		var lightboxHeight;
		var lightboxTopPoss;
		var lightboxLeftPoss;
		
		function lightBoxGetPosition (getLightboxTarget){
			screenWidth = $(window).width();
			screenHeight = $(window).height();
			screenScrollPosition = jQuery(window).scrollTop();
			lightboxWidth = $("#"+getLightboxTarget+".lightbox_container").width();
			lightboxHeight = $("#"+getLightboxTarget+".lightbox_container").height();
			lightboxTopPoss = (((screenHeight-200)-lightboxHeight)/2)+screenScrollPosition;
			lightboxLeftPoss = (screenWidth-lightboxWidth)/2;
			//console.log(screenHeight+" | "+lightboxHeight+" | "+screenScrollPosition);
			return screenWidth, screenHeight, screenScrollPosition, lightboxWidth, lightboxHeight, lightboxTopPoss, lightboxLeftPoss;
		}
		
		lightBoxGetPosition (getLightboxTarget);
		$(".lightbox_container").hide();
		$("#"+getLightboxTarget+".lightbox_container").css({"left":lightboxLeftPoss});
		$("#"+getLightboxTarget+".lightbox_container").css({"top":screenScrollPosition-lightboxHeight});
		$("#"+getLightboxTarget+".lightbox_container").show();
		
		if(lightboxAnimation === true){
			$("#"+getLightboxTarget+".lightbox_container").stop().animate({top:lightboxTopPoss});
		}
		else{
			$("#"+getLightboxTarget+".lightbox_container").stop().css("top",lightboxTopPoss).show();
		}
		
		YMMLightboxScreenPosition = setInterval(function(){
			lightBoxGetPosition (getLightboxTarget);
			if(lightboxAnimation === true){
				$(".vehicle_lightbox_wrapper").css({"width":screenWidth,"height":screenHeight});
				$("#"+getLightboxTarget+".lightbox_container").stop().animate({"top":lightboxTopPoss,"left":lightboxLeftPoss});
			}
		}, 500);
	};
	$(".openLightbox").live("click",function (){		
		getLightboxTarget = $(this).attr("rel");		
		$(".vehicle_lightbox_wrapper").hide().css("opacity","0.7").fadeIn();
		$(".lightbox_container").hide();
		lightboxSlideOnScreen (getLightboxTarget);
	});
	var YMMLightBoxSize=$(".filter_lightbox").size();
	if(YMMLightBoxSize >=1){
		lightboxSlideOnScreen (".lightbox_container");
	}
/******************************************** 
Search Filter 
********************************************/
	$(".filter_lightbox .searchfilter ul li").hide();
	$(".filter_lightbox .searchfilter ul li:first-child , .searchfilter").fadeIn('slow');
	$(".filter_lightbox .searchfilter ul").each(function() {
	  $(this).wrap('<div class="filterwrap" />');
	});
	
	$(".filter_lightbox .searchfilter ul li:first-child, .dropdown_container .searchfilter ul li:first-child").click(function() {
	  $(".filter_lightbox .searchfilter ul li, .dropdown_container .searchfilter ul li").hide();
	  $(".filter_lightbox .searchfilter ul li:first-child, .dropdown_container .searchfilter ul li:first-child").show();
	  $(".filter_lightbox .searchfilter ul, .dropdown_container .searchfilter ul").removeClass('dropdown').parent().removeClass('overlap');
	  $(this).addClass('firstchild').parent().addClass('dropdown').find("li").show().parent().parent().addClass('overlap');
	  return false;
	});
	
	$(".filter_lightbox .searchfilter ul li:not(:first-child), .dropdown_container .searchfilter ul li:not(:first-child)").click(function() {
	  window.location = $(this).find("a").attr("href"); return false;
	});
	
	$(document).click(function() { //Click anywhere and...
	  $(".filter_lightbox .searchfilter ul li, .dropdown_container .searchfilter ul li").hide();
	  $(".filter_lightbox .searchfilter ul li:first-child, .dropdown_container .searchfilter ul li:first-child").show();
	  $(".filter_lightbox .searchfilter ul, .dropdown_container .searchfilter ul").removeClass('dropdown');
	});
	$(".filter_lightbox .searchfilter ul li, .dropdown_container .searchfilter ul li").click(function(e) {
	  e.stopPropagation(); //Prevents the subpanel ul from closing on click
	});
	
	//Check if filter exists, if not pull out the accordion
	$('.filter_lightbox .acc_container').each(function() {
		if ($(this).find('.filter_lightbox .crumb:empty').length) {
			if ($(this).find('.filter_lightbox .searchfilter').length) {
			} else {
				$(this).prev().hide();
			}
		}
	});	
});
