$(document).ready(function(){

	$("#PrimaryMenu > ul > li:last-child").addClass("last");
	
	$(".SubCategoryListGrid ul li.RowDivider").remove();
	$(".SubCategoryListGrid ul li:last-child").remove();
	
	$("#ProductReviews > a > img").replaceWith("write a review");
	$("#ProductReviews > a").addClass("button");
	
	/*
		This is a short and simple script that will create an Active class for your custom pages menu links(if you have to use stuff like Category pages for your main navigation).
		Keep in mind that this will also work for any other list (like custom Side Vendors, Side Categories, or Side Brands) by just changing the ID Selector. **Credit to Krystin Rice
	*/
	
	var loc_href = window.location;
	
	
    $('#PrimaryMenu a, #SideCategoryList .SideCategoryListClassic a').each(function () {
		
		if (loc_href.pathname == $.trim($(this).attr('href').split('?')[0]) || loc_href.href.split('?')[0] == $.trim($(this).attr('href').split('?')[0]) ) {
			$(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); }
		});
	}

	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
	if (url == '/' || url == "/index.php") {
			var slideshow = $('#cycle');
			slideshow.cycle({
				fx: "fade"
			});
			$('#cycleContainer .prev').click(function(e) { e.preventDefault(); slideshow.cycle('prev'); });
			$('#cycleContainer .next').click(function(e) { e.preventDefault(); slideshow.cycle('next'); });
	};


	//	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
//   });
	
	var shopByPrice = $('.category #SideCategoryShopByPrice').detach();
	$('#SideCategoryList .BlockContent').append(shopByPrice);
	shopByPrice.css({'visibility':'visible'});

	var productDescription = $('#ProductWarranty');
	if (productDescription.length > 0) {
		var toMatch = /\[\[[^\]]+\]\]/gi;
		var matcher = new RegExp(toMatch);
		var results = productDescription.html().match(matcher);
		productDescription.html(productDescription.html().replace(toMatch,''));
		
		if (results!=null) {
			$(window).load(function() { $('#ProductWarranty_Tab, #ProductWarranty').remove(); } );
			var toInsert = []
			$.each(results, function(index, value) {
				var values = $.trim(value.substring(2,value.length-2)).split(':');
				var tempString = "<div class='DetailRow'><div class='Label'>";
				tempString = tempString + values[0] + ":</div><div class='Value'>";
				tempString = tempString + values[1] + "</div></div";
				toInsert.push(tempString);
			});
			$.each(toInsert, function(index, value) {
				$('#ProductDetails .ProductDetailsGrid .ProductSKU').before(value);
			});
		}
	}
	
	var relatedProducts = $('#SideProductRelated ul li');
	if (relatedProducts.length > 4) { relatedProducts.last().remove(); }
	
	$('.CustProdList li:nth-child(4n+4)').addClass('noRightMargin');
	
	var SideCategoryList = $('#SideCategoryList');
	if (SideCategoryList.length > 0) {
		var menuItems = SideCategoryList.find('.SideCategoryListClassic ul li').clone();
		$('.home #SideCategoryList').hide();
		$('#PrimaryMenu .shop').append('<ul>');
		$('#PrimaryMenu .shop ul').append(menuItems);
	}
	
	
});
$(window).load(function() {	
	var ImageHeight = $(".ProductThumb .zoomPad").height();
	var ContainerHeight = $(".ProductThumbImage").height();
		
	ContainerHeight = ContainerHeight / 2;
	ImageHeight = ImageHeight / 2;
		
	var NewTop = (ContainerHeight - ImageHeight);
	$(".ProductThumbImage .zoomPad").css("margin-top", NewTop);    
		
	$(".ProductTinyImageList .TinyOuterDiv").live("mouseover", function() {
		var ImageHeight = $(".ProductThumb .zoomPad").height();
		var ContainerHeight = $(".ProductThumbImage").height();
			
		ContainerHeight = ContainerHeight / 2;
		ImageHeight = ImageHeight / 2;
			
		var NewTop = (ContainerHeight - ImageHeight);
		$(".ProductThumbImage .zoomPad").css("margin-top", NewTop);    
	});
	
	$('#SubCategorySelectBox').change(function() {
		window.location = $(this).children('option:selected').attr('value');
	})
});

