function cycleImages(){
    var active = $('#slider .active');
    var next = (active.next().length > 0) ? active.next() : $('#slider a:first');
    next.css('z-index',2);
	active.fadeOut(1500,function(){
		active.css('z-index',1).show().removeClass('active');
		next.css('z-index',3).addClass('active');
    });
}

$(document).ready(function(){
	$("#SideCategoryList").navaccord()
	setInterval('cycleImages()', 7000);

	$("#PrimaryMenu > ul > li:last-child").addClass("last");

	$("#slider").cycle({
		fx: 'fade'
	});

	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"));
	
	// Back Button on Classes Page
	if ($("#PageBreadcrumb ul li:nth-child(2) a").html() == "Classes") {
		$(".Content").prepend("<a class='button' href='/classes/'>Back</a>")
	}

	// Custom Home Featured Product
	if ($(".homefeat").length > 0 ) {
		var wanted = [$(".homefeat.feat .homefeatcontent .ProductDetails").html(), $(".homefeat.feat .homefeatcontent .ProductPriceRating").html()].join("<br />")
		var img = $(".homefeat.feat .homefeatcontent .ProductImage").find("img").attr("src")
		$(".homefeat.feat .homefeatcontent").html(wanted)
		$(".homefeat.feat").css("background","url("+img+") no-repeat 100% 40px")
	};
	
	//John hates my freedom and hapiness:(
	
	if ($("#CategoryBreadcrumb").length > 0 ) {
		var active = [$(".Breadcrumb ul li:nth-child(2) a").html(), $(".Breadcrumb ul li:nth-child(2)").html()]
		$(".SideCategoryListClassic ul li a").each(function() {
			if ($(this).html() == active[0] || $(this).html() == active[1]) {
				var header = $(this).html()
				$(this).hide().parent().children(".subarrow").hide()
				$(this).hide().parent().children(".child").css("padding-left","0");
				$("#SideCategoryList > h2").html(header)
				$(this).parents(".SideCategoryListClassic ul").addClass("activeme")
				if ($(this).parents("ul").html().indexOf("dropable") == -1) {
					$("#SideCategoryList").hide()
				}
			}
			else {$(this).parents(".SideCategoryListClassic ul").hide()}
			$(".activeme").show()
			$(".activeme").children(".dropable").children(".child").show().children(".dropable.expand").children(".child").show()
		});
	}
	
	// Loops through and creates dynamic banners for all pages
	if ($(".Breadcrumb").length > 0) {
		var bannerpages = ["classes", "events", "forthekids", "gifts", "needlepoint", "needles", "notions", "patterns", "yarn", "kits"]
		var pageheader = $(".Breadcrumb ul li:nth-child(2)").html().toLowerCase().replace((/( )/g),"")
		
		for (x in bannerpages) {
			if (pageheader.indexOf(bannerpages[x]) >=0) {
				$("#Wrapper").prepend("<div class='cathead'><img src='/templates/__custom/img/"+bannerpages[x]+".png' alt='' /></div>")
			}
		}
	}
	
	
	
	// Custom Dynamic Top Menu Links
	if ($(".loginout").html().indexOf("Sign in") >= 0){
		$(".loginout").html("<a href='/login.php?action=create_account' style='border-right:1px solid #000' >Create Account</a>")
	}

	// Style cart numbers
	var crtnumber = [($(".crttext p").html()), ($(".crttext strong").html())].join(" ")
	if (crtnumber == " ") {
		$(".crttext").html("(0 items) $0.00")
	}
	else {$(".crttext").html(crtnumber)}
	
	
	// Remove unused elements
	var awesome = [$("#SidePopularVendors"), $(".Left #SideNewsletterBox"), $("#SideLiveChatServices"), $(".Right"), $("#SideNewProducts")]
	for (i in awesome) {
		awesome[i].remove()
	}
	
/* 	if ((window.location.pathname).indexOf("checkout") >= 0) {
		$(".ExpressCheckoutBlock").each(function() {
			var label = $(this).find(".Label").find("span").remove().find("a").remove().html()
			var step = $(this).attr("id").replace((/(CheckoutStep)|(" ")/g),"")
			$(".checkoutprocess ul").prepend("<li><a href='#' onclick='return ExpressCheckout.ChangeStep('"+step+"');>"+label+"</a></li>")
			
			//checkoutprocess
		});
	} */
	
//	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
//   });


	/*
		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).addClass('activePageLink');
		}
		});

});
