$(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.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); }
		});
	}

	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") {
	//RUN CYCLE
		$("#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: 4,
	 	  scroll: 4,
		  speed: 800
    });

	// -n- Cart Items Script
	var cartItems = $('#CartMenu .CartLink span');
	var items = (cartItems.text().length<3) ? "0" : cartItems.text().match(/[0-9]/g).join("");
	cartItems.html("<em>" + items + "</em> ITEMS IN CART").css('visibility','visible');
	
	// -n- Various Margin Fixes
	$('#CategoryHeading .SubCategoryListGrid ul li:nth-child(4n+4)').addClass('noRightMargin');
	$('#LayoutColumn2 .CustProdList.ProductList li:nth-child(4n+4)').addClass('noRightMargin');
	
	// -n- Move the sortbox on the brand and category pages
	var brandSortBox = $('#BrandContent .SortBox').detach();
	$('#LayoutColumn2 .CategoryPagination .insertedSortBox').append(brandSortBox);
	var categorySortBox = $('#CategoryHeading .SortBox').detach();
	$('#LayoutColumn2 .CategoryPagination .insertedSortBox').append(categorySortBox);
	
	
});

$(window).load(function() {
	// -n- After the page loads, set the height of the .Left div to be the same as
	//     the .Content div
	var contentHeight = $('#LayoutColumn2').height();
	if (contentHeight > 870) {
		$('#LayoutColumn1').height($('#LayoutColumn2').height());
	} else {
		$('#LayoutColumn1').height('auto');
	}
	
	// If the window is too small, move the social media icons to the footer
	if ($(window).width()>1088) {
		var wrapper = $('#Wrapper');
		var xx = wrapper.offset().left+950+10;
		var yy = wrapper.offset().top-8;
		
		// Social Media is hidden by default
		$('#SocialMedia').css({
			'left':xx,
			'display':'block',
			'top':yy,
			'width':58
		});
		// -n- Social Media bar scrolling
		$(window).scroll(function() {
			if (!$('#SocialMedia').is(':animated')) {
				var destination = $(window).scrollTop()+111;
				var distance = Math.abs(($(window).scrollTop()+111)-$('#SocialMedia').position().top);
				var speed = (distance/2000)*5000;
				$('#SocialMedia').animate({
					'top':destination
				}, speed, 'linear');
			} else {
				$('#SocialMedia').stop();
				var destination = $(window).scrollTop()+111;
				var distance = Math.abs(($(window).scrollTop()+111)-$('#SocialMedia').position().top);
				
				var speed = (distance/2000)*5000;
				$('#SocialMedia').animate({
					'top':destination
				}, speed, 'linear');
			}
		});
	} else {
		socialMedia = $('#SocialMedia').detach();
		$('#Footer').append(socialMedia);
		socialMedia.css({
			'display':'block'
		});
	}
});
