
$(document).ready(function() {


if (typeof(localStorage) == 'undefined' ) {

	$("#OlderBrowser").show();

} else {


	//Checks for localstorage and shows the relevant info
	$("#StoreageBoxContainer, .WishListBtn, .HideToggle").show();
	
	
	//Close X span function 
	fn = function () {	
		$(this).parent().remove();//removes the item
		var attrclass = $(this).parent().attr("class");
		$("#" + attrclass + " .WishListBtn").show();//Shows the add to basket on original item
		var storagevalue = $("#StoreageBox").html();//resaves the local storage
		localStorage.setItem("StorageBox", "" + storagevalue + ""); 
		
		if ($('#StoreageBox div').length == 0) {//calculates if the local storage is empty
			$("#StoreageBoxContainer").hide();
			$("#wrapper").css("padding-bottom","0");
		}
		else {
			$("#wrapper").css("padding-bottom","190px");
		}
		
		//Generates URL
		$(".StoreUrl").html("http://www.thecrossstitchguild.com/wishlist/list.php?csg=");//Resets URL
		$("#StoreageBox div").each(function(){//Stored URL in hidden div
		   var attrclass = $(this).attr("class");   
			$(".StoreUrl").append("" + attrclass + "E");
		});
		$("#WishList").attr("href","" + $('.StoreUrl').text() + "");//Adds a link
	
	}
	
	// On page load
	var value = localStorage.getItem("StorageBox");
	$("#StoreageBox").html(value);
	
	var storageToggle = localStorage.getItem("StorageToggle");//Toggle storage
	if (storageToggle == null) {//Local storage toggle
	}
	else {
		$(".HideToggle").hide();
		$(".ShowToggle").show();
		$("#ShowEdit, #HideEdit, #StoreageBox, .StorageItemClear").hide();
	}


	//calculates if the local storage is empty
	if ($('#StoreageBox div').length == 0) {
		$("#StoreageBox").parent().hide();
		$("#wrapper").css("padding-bottom","0");
	}
	else {
		$("#wrapper").css("padding-bottom","190px");
	}
	
	// Hides add to basket on items that have already been selected
	$("#StoreageBox div").each(function(){
	   var attrclass = $(this).attr("class");   
	   $("#" + attrclass + " .WishListBtn").hide();   
	});
	
	$(".StorageItemClear").hide();//hides close (edit mode)
	$("#ShowEdit").click(function () {	
		$(".StorageItemClear").show();
		$(this).hide();
		$("#HideEdit").show();
	});
	$("#HideEdit").click(function () {	
		$(".StorageItemClear").hide();
		$(this).hide();
		$("#ShowEdit").show();
	});
	
	$("#StoreageBox .StorageItemClear").click(fn);//loads in the close function
	
	// Add to basket click function
	$(".WishListBtn").click(function () {
	
		var WLid = $(this).parent().parent().attr("id");
		//Gets all the info and outputs only what we need
		var WLimg = $("#" + WLid + " .ShopProductImage img").attr("src");
		var WLlink = $(this).attr("title");
		var WLtext = $("#" + WLid + " h2").text();
		$("#StoreageBox").append("<div class='" + WLid + "'><a href='http://www.thecrossstitchguild.com/Shop/Product.aspx?product=" + WLlink + "'><img src='" + WLimg + "' /><span>" + WLtext + "</span></a><span class='StorageItemClear'></span></div>");
		
		$(this).hide();//hides add to basket button on original item
		$("#StoreageBoxContainer").show();//shows the footer container if it's hidden
		$("#wrapper").css("padding-bottom","190px");
		
		$(".StorageItemClear, #HideEdit").hide();//hides close (edit mode)
		$("#ShowEdit").show();

		var storagevalue = $("#StoreageBox").html();//resaves the local storage
		localStorage.setItem("StorageBox", "" + storagevalue + "");
		
		$("#StoreageBox, .HideToggle").show();
		$(".ShowToggle").hide();
		
		$("#StoreageBox .StorageItemClear").click(fn);//loads in the close function
		
		//Generates URL
		$(".StoreUrl").html("http://www.thecrossstitchguild.com/wishlist/list.php?csg=");//Resets URL
		$("#StoreageBox div").each(function(){//Stored URL in hidden div
		   var attrclass = $(this).attr("class");   
			$(".StoreUrl").append("" + attrclass + "E");
		});
		$("#WishList").attr("href","" + $('.StoreUrl').text() + "");//Adds a link
			   
	});

	//Generates URL
	$(".StoreUrl").html("http://www.thecrossstitchguild.com/wishlist/list.php?csg=");//Resets URL
	$("#StoreageBox div").each(function(){//Stored URL in hidden div
	   var attrclass = $(this).attr("class");   
		$(".StoreUrl").append("" + attrclass + "E");
	});
	$("#WishList").attr("href","" + $('.StoreUrl').text() + "");//Adds a link
	

	$("#StoreUrl span").click(function () {	
		$(this).parent().fadeOut();
	});
	
	//Toggle URL	
	$(".ShowToggle").click(function () {	
		$("#StoreageBox").slideToggle("fast");
		$(this).hide();
		$(".HideToggle").show();
		$("#ShowEdit").show();
		
		localStorage.removeItem("StorageToggle");//Toggle storage
	});
	$(".HideToggle").click(function () {	
		$("#StoreageBox").slideToggle("fast");
		$(this).hide();
		$(".ShowToggle").show();
		
		$("#ShowEdit, #HideEdit").hide();
		$(".StorageItemClear").hide();//hides close (edit mode)
		
		localStorage.setItem("StorageToggle", "down");//Toggle storage
	});

	
}

});


