// Cookie parsing plugin
jQuery.returnCookie = function(cookie) {
	var string = getCookie(cookie);
	var array = new Array();
	//alert(string);
	if(string) {
		$(string.split("|")).each(function(){
			var child = new Array();
			if(this.length > 0) {
				$(this.split("~")).each(function(){
					if(this != "") {
						child.push(this);
					}
				});
				array.push(child);
			} 
		});
	}
	if(array == "") {
		return false;
	} else {
		return $(array);
	}
}

$(document).ready(function(){
	// Recently viewed products
	if($.returnCookie("recentpvs")) {
		$(".rvitable").each(function(){
			var thisObj = this;
			$.returnCookie("recentpvs").slice(0,4).each(function(i){
					var thisCookie = this;
					$(thisObj).find(".cParent").each(function(){
						if(i > 0) {
							$(this).find(".cChild:last").clone().appendTo(this);
						}
						$(this).find(".title:last")
							.find("a").attr("href", "/webapp/wcs/stores/servlet/product_6970_" + thisCookie[0] + "_" + thisCookie[0] + "?cm_sp=Customer%20driven-_-Recently%20Viewed").html(thisCookie[2].replace("\n", ""));
						$(this).find(".productImg:last")
							.find("a").attr("href", "/webapp/wcs/stores/servlet/product_6970_" + thisCookie[0] + "_" + thisCookie[0] + "?cm_sp=Customer%20driven-_-Recently%20Viewed")
							.find("img").attr("name", thisCookie[1]).attr("src", "/images/product/images/" + thisCookie[3] + "_med.jpg").attr("alt",$(this).find(".title:last").text());
						$(this).find(".productImg:last")
							.find("img.medium").attr("src", "/images/product/images/" + thisCookie[3] + "_med.gif");
						$(this).find(".item:last")
							.html("Item#&nbsp;" + thisCookie[1]);
						$(this).find(".productImg:last").find(".inCart65").remove();
						$(this).find(".productImg:last").find(".inCart114").remove();
						if (itemInCart(thisCookie[1])) {
							if($(this).find("img.medium").length > 0){
								$(this).find(".productImg:last a:first").eq(0).find("img.medium").parent().before('<div class="inCart114"><img src="/images/icons/addedtocart114.gif" width="114" height="114" /></div>');
							} else {
								$(this).find(".productImg:last a:first").eq(0).before('<div class="inCart65"><img src="/images/icons/addedtocart.gif" width="65" height="65" /></div>');
							}
						} else {
							$(this).find(".productImg:last").find(".inCart65").remove();
							$(this).find(".productImg:last").find(".inCart114").remove();
						}
					});
			});
		});
	} else {
		$("#rvitable, .rvitable").remove();
	}


	// Recently viewed categories
	if ($.returnCookie("recentcvs")) {
		$.returnCookie("recentcvs").each(function(){
			if(!settings.vars.catId || this[0] != settings.vars.catId) {
				$("#rvctable ul, .rvctable ul")
					.append("<li><a href=\"/webapp/wcs/stores/servlet/category_6970_" + this[0] + "?cm_sp=Customer%20driven-_-Recently%20Viewed\">" + this[1] + "</a></li>");
			} else if (this[0] == settings.vars.catId && $.returnCookie("recentcvs").length == 1) {
				$("#rvctable, .rvctable").remove();
			}
		});
	} else {
		$("#rvctable, .rvctable").remove();
	}
	
	// Recently viewed searches
	if ($.returnCookie("recentsvs")) {
		$.returnCookie("recentsvs").each(function(){
			$("#rvstable ul, .rvstable ul")
				.append("<li><a href=\"/webapp/wcs/stores/servlet/NTESearch?storeId=6970&N=0&Ntk=All&Ntt=" + this[0] + "&Nty=1&D=" + this[0] + "&Ntx=mode+matchallpartial&Dx=mode+matchallpartial?cm_sp=Customer%20driven-_-Recently%20Viewed\">" + this[0] + "</a></li>");
		});
	} else {
		$("#rvstable, .rvstable").remove();
	}
	
	if($.returnCookie("recentpvs") || $.returnCookie("recentcvs") || $.returnCookie("recentsvs")) {
		if($.returnCookie("recentcvs") && !$.returnCookie("recentpvs") && !$.returnCookie("recentsvs")) {
			if($.returnCookie("recentcvs").length == 1) {
			} else {
				$("#rvcButton").css("display", "block");
			}
		} else {
			$("#rvcButton").css("display", "block");
		}
	}
	
	// Recently viewed categories - home page
	if ($.returnCookie("recentcvs")) {
		$.returnCookie("recentcvs").slice(0,4).each(function(){
			$(".rvctable-home")
				.append('<div class="col col-center col3"><a href="/webapp/wcs/stores/servlet/category_6970_' + this[0] + '?cm_sp=Customer%20driven-_-Recently%20Viewed-_-Home%20Page"><img src="/images/categories/rvc/' + this[0] + '.jpg" alt="' + this[1] + '"></a></div>');
		});
	} else {
		$(".rvctable-home").remove();
	}
	
	// Top Sellers
	if($.returnCookie("recentTopSellers")) {
		$.returnCookie("recentTopSellers").slice(0,4).each(function(j){
				var thisCookie = this;
				$(".tstable").find(".cParent").each(function(){
					if(j > 0) {
						$(this).find(".cChild:last").clone().appendTo(this);
					}
					$(this).find(".productImg:last")
						.find("a").attr("href", "/webapp/wcs/stores/servlet/" + thisCookie[1] + "?cm_sp=Customer%20driven-_-Top%20Sellers-_-Home%20Page")
						.find("img").attr("name", thisCookie[0]).attr("src", thisCookie[3]).attr("alt",thisCookie[2]);
					$(this).find(".productImg:last")
						.find("img.medium").attr("src", thisCookie[3]);
					$(this).find(".title:last")
						.find("a").attr("href", "/webapp/wcs/stores/servlet/" + thisCookie[1] + "?cm_sp=Customer%20driven-_-Top%20Sellers-_-Home%20Page").html(thisCookie[2].replace("\n", ""));
					$(this).find(".rating:last")
						.html("<img><b>"+ thisCookie[5] +"</b>")
						.find("img").attr("src", thisCookie[4]);
					$(this).find(".item:last")
						.html("Item#&nbsp;" + thisCookie[0]);
					if (itemInCart(thisCookie[1])) {
						$("#tstable .cParent, .tstable .cParent").eq(0).find(".productImg:last a:first")
							.before('<div class="inCart114"><img src="/images/icons/addedtocart114.gif" width="114" height="114" /></div>');
					} else {
						$("#tstable .cParent, .tstable .cParent").eq(0).find(".productImg:last a:first").find(".inCart65").remove();
					}
				});
		});
	} else {
		$("#tscontent .tscontent").remove();
	}
	
	//Top Sellers Categories
	if ($.returnCookie("topSellerCats")) {
		var topSellers = $.returnCookie("topSellerCats"),
			catNames = "",
			catIds = "";
		$(topSellers[0]).each(function(i){
			if(i == ($(topSellers[0]).length-1)) {
				catNames += this;
			} else {
				catNames += this + " > ";
			}
		});
		$(topSellers[1]).each(function(i){
			if(i == ($(topSellers[1]).length-1)) {
				catIds += this;
			} else {
				catIds += this + "+";
			}
		});
		$(".tscategory").append(catNames).parent().append('<a href="/webapp/wcs/stores/servlet/category_6970_' + catIds + '" class="shop-link">See All</a>');
	};
	
});