/*
$(document).ready(function ()
{
	$("a[href*='outfit/fullq159']").attr("href", "http://www.kew159.com/fcp/content/q159home/content");
});
*/
$(document).ready(function ()
{
	$("li.wc_sale a").unbind();
	if ($("div#sale-container").length)
	{
		$("h3.categoryHd").css("display", "none");
		$("div#sale-header").css("margin", "0");
	}
	if ($("div#productsCont div.productCont").length)
	{
		$.getScript("/pws/javascript/quickAddToBasket.js", function ()
		{
			$("div#productsCont").append('<div id="prodRelPreviewContShadow"><div id="prodRelPreviewCont"><div class="content">please wait...</div><p class="prpClose"><a class="close" href="#">close</a></p></div></div>');
			$("p.prpClose a.close").click(function ()
			{
				$("#prodRelPreviewContShadow").hide();
				$("#prodRelPreviewCont .content").html("please wait...");
				return false;
			});
			$("div.productCont", "div#productsCont").each(function (i, obj)
			{
				oID = $(obj).find("img[id^='product_']").attr("id");
				if (oID != null)
				{
					needle = new RegExp(/product_(\d+)/);
					theMatch = oID.match(needle);
					$(obj).append($("<a/>").append($("<span/>").text("quick view")).attr("id", "rp" + theMatch[1]).attr("href", "#").addClass("prodRelViewBtn").bind("click", function (evt)
					{
						quickBuy($(this));
						$("div#prodRelPreviewContShadow").css(
						{
							"position": "absolute",
							"top": $(this).position().top - 300,
							"left": $(this).position().left - 250
						});
						return false;
					}));
					$(obj).append($("<a/>").text("more details").addClass("prodMoreDetails").attr("href", $(obj).find("a").attr("href")));
				}
			})
		})
	}
});



var isjUIloaded = false;
var isJUErequired = false;
var jswlAddSel,jswlEmailaddr;
var calljQuery_wl_loaded = false;
var showWLconfirm = false;





/* Wishlist Functions */

var jswltext_ear = '<p>Please let us know your email address so that we can tell you when your saved styles are available.</p><div class="lbrk"></div><p><label for="wl_email">Email Address</label> <input type="text" name="wl_email" id="wl_email" class="text ui-widget-content ui-corner-all" /> </p>'

function calljQuery_wl()
{
	if (calljQuery_wl_loaded === false)
	{
		calljQuery_wl_loaded = true;
		$('head').append('<link rel="stylesheet" href="/pws/client/style/kew/jquery-ui.css" type="text/css" />');

		$.getScript("/pws/client/javascript/jquery-1.6.4.min.js", function()
		{
			jQuery.getScript("/pws/client/javascript/jquery-ui-dialog.min.js", function()
			{
				jQuery('body').append('<div id="dialog-wlemail" title="E-mail address needed" style="display:none;">' + jswltext_ear +'</div>');

				isjUIloaded = true;

				if (isJUErequired)
				{
					dialog_wlemail();
				}

			});
		});
	}
}


function dialog_wlemail()
{
	jQuery( "#dialog-wlemail" ).dialog(
	{
		autoOpen: true,
		closeOnEscape: false,
		draggable: false,
		modal: true,
		width: 310,
		buttons:
		{
			Cancel: function()
			{
				dialog_wlemail_confirm();
			},
			"Continue" : function()
			{
				regExp = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;

				if ( !(regExp.test( jQuery("input[name='wl_email']").val() ) ) )
				{
					alert("Invalid email address");
				}
				else
				{
					jswlEmailaddr = jQuery("input[name='wl_email']").val();
					jswlAddSel();
				}
			}
		},
		open: function()
		{
			jQuery("div#dialog-wlemail").html( jswltext_ear );
			jQuery("div.ui-dialog[aria-labelledby='ui-dialog-title-dialog-wlemail'] a.ui-dialog-titlebar-close").hide();
			jQuery("div[aria-labelledby='ui-dialog-title-dialog-wlemail'] :button:contains('Continue')").css( { background : "#e30177" , border: "1px solid #e30177" } );
		},
		close: function()
		{
		}
	});

	jQuery("div#dialog-wlemail").dialog( { title: "E-mail address needed" } );
	jQuery("div[aria-labelledby='ui-dialog-title-dialog-wlemail'] :button:contains('Continue')").css( { background : "#e30177" , border: "1px solid #e30177" } );
	jQuery("div#dialog-wlemail").html( jswltext_ear );
}

function dialog_wlemail_confirm()
{
	if (jQuery("div#dialog-wlemail").dialog("isOpen"))
	{
		jQuery("div#dialog-wlemail").dialog( { title: "Oooops" } );
		jQuery("div#dialog-wlemail").html( jQuery("<p/>").html("Are you sure you'd prefer not to find out when this style is available? If you let us know your email address we'll only use it for this purpose - we won't share it with third parties.<br /><br />Would you like to add your email address?") );

		jQuery("div#dialog-wlemail").dialog( { buttons: { "No": function() { jQuery.ajax({url:"http://js.kew-wishlist.co.uk/kew/hasCancelled",dataType:"jsonp",type:"GET"}); jQuery(this).dialog("close"); } , "Yes" : function() { dialog_wlemail(); } } });

		jQuery("div[aria-labelledby='ui-dialog-title-dialog-wlemail'] :button:contains('Yes')").css( { background : "#e30177" , border: "1px solid #e30177" } );
	}
}

function dialog_wlemail_success()
{
	if (jQuery("div#dialog-wlemail").length && jQuery("div#dialog-wlemail").dialog("isOpen"))
	{
		jQuery("div#dialog-wlemail").dialog( { title: "Request Received" } );
		jQuery("div#dialog-wlemail").html( jQuery("<p/>").html("Great! We've received your request, so will let you know when your items are available to buy.<br /><br />Please click at the top of the page if you'd like to view or amend your request list from today.") );


		jQuery("div#dialog-wlemail").dialog( { buttons: { "View List" : function() { window.location = "/fcp/content/wishlist_basket/content"; } , "Continue Shopping": function() { jQuery(this).dialog("close"); } } });

		jQuery("div[aria-labelledby='ui-dialog-title-dialog-wlemail'] :button:contains('View List')").css( { "margin-right" : "90px" } );
		jQuery("div[aria-labelledby='ui-dialog-title-dialog-wlemail'] :button").css( { background : "#e30177" , border: "1px solid #e30177" } );
	}
}



function getWLtime()
{
	theTime = new Date();
	return theTime.getTime();
}
function getMyStorage(strName)
{
	return localStorage.getItem(strName);
}
function setMyStorage(strName, strVal)
{
	if (typeof (localStorage) != "undefined")
	{
		try
		{
			localStorage.setItem(strName, strVal);
		}
		catch (e)
		{}
	}
}

function loadWishList()
{
	$.ajax(
	{
		url: "http://js.kew-wishlist.co.uk/kew/itemCount",
		dataType: "jsonp",
		type: "GET",
		success: function (data)
		{
			if (data)
			{
				if (data.email == false)
				{
					//calljQuery_wl();
				}
				setMyStorage("wl_items", data.cnt);
				setMyStorage("wl_lastUpdated", getWLtime());
				if (data.cnt > 0)
				{
					$("div#wlbagInfo span").text(data.cnt + " items");
					showWishList();
				}
			}
		}
	})
}

function showWishList(blnStorage)
{
	canShowWL = false;
	if (typeof (blnStorage) != "undefined")
	{
		$("div#wlbagInfo span").text(getMyStorage("wl_items") + " items");
		if (getMyStorage("wl_items") > 0)
		{
			canShowWL = true;
		}
	}
	else
	{
		canShowWL = true;
	}
	if (canShowWL)
	{
		$("ul#headerLinks").css(
		{
			right: "355px"
		});
		$("form#searchForm").css(
		{
			right: "315px"
		});
		$("div#wishListBasket").show();
		if ($("div#coming-soon-banner a").length)
		{
			$("div#coming-soon-banner a").show();
		}
	}
}







$(document).ready(function ()
{
	if ($("div#searchResultsCont").length)
	{
		if ($("strong#search_term").length)
		{
			$.ajax(
			{
				url: "http://js.kew-wishlist.co.uk/kew/checkWishlist/" + $("strong#search_term").text(),
				dataType: "jsonp",
				type: "GET",
				success: function (data)
				{
					if (data.exists == true)
					{
						if (data.email == false) { calljQuery_wl(); }
						if ($("div#productsCont").length)
						{
							var HTML = "<p>Below are the products (<span id=\"number_of_products\">" + $("span#number_of_products").text() + "</span>) that match your search &lsquo;" + $("strong#search_term").text() + "&rsquo;.</p><br/><p>The products shown underneath are in stock and ready for you to purchase and enjoy.</p><br/><p>Don&rsquo;t hesitate to get in touch if you have any questions &minus; our email address is <a href=\"mailto:enquiries@kew159.com\">enquiries@kew159.com</a> or you can call us on 020 8487 2001.</p><br/>";
						}
						else
						{
							var HTML = "<p>Below are the products that match your search &rsquo;<strong id=\"search_term\">" + $("strong#search_term").text() + "</strong>&rsquo;.</p><br/><p>Don&rsquo;t hesitate to get in touch if you have any questions &minus; our email address is <a href=\"mailto:enquiries@kew159.com\">enquiries@kew159.com</a> or you can call us on 020 8487 2001.</p><br/>";
						}
						if ($("div#productsCont").length)
						{
							HTML = HTML + "<img src=\"/pws/images/header_in-stock.jpg\" alt=\"In Stock\" />";
						}
						$("h1#searchResultsHeader").after(HTML);
						$("p#counter").remove();
						HTML = "";
						HTML = HTML + "<div class=\"wlCont\"><img src=\"/pws/images/header_coming-soon.jpg\" alt=\"Coming Soon\" /><p style=\"margin:15px;\">These styles will be arriving shortly. Sign up and be the first to know when they are availble to buy.</p>";
						$.each(data.prods, function (i, prod)
						{
							HTML = HTML + '<div class="wlItem" id="wlp' + prod.id + '">';
							HTML = HTML + '<img src="http://static.kew-wishlist.co.uk/product-images/' + prod.img + '.jpg" alt="' + prod.name + '" />';
							HTML = HTML + '<h3>' + prod.name + '</h3>';
							HTML = HTML + '<div class="wlBord"></div>';
							HTML = HTML + '<p><span class="wlInfo">Colours Available:</span><span class="wlDetail">' + prod.colour + '</span></p>';
							HTML = HTML + '<p><span class="wlInfo">Expected Online:</span><span class="wlDetail">' + prod.exp + '</span></p>';
							HTML = HTML + '<p><span class="wlInfo">Price:</span><span class="wlDetail">&pound;' + prod.price + '</span></p>';
							HTML = HTML + '<div class="wlBord"></div>';
							if (prod.inCart == true)
							{
								HTML = HTML + '<p class="wlC">Added</p>';
							}
							else
							{
								HTML = HTML + '<p class="wlC"><a href="#">Email me</a></p>';
							}
							HTML = HTML + '</div>';
						});
						HTML = HTML + "</div>";
						if ($("div#productsCont").length)
						{
							$("div#productsCont").after(HTML);
						}
						else
						{
							$("div#searchResultsCont").append(HTML);
						}
						$("div.wlItem a").click(function (e)
						{
							e.preventDefault();
							var prodID = $(this).parents("div.wlItem").attr("id");
							jswlAddSel = (function()
							{
								$.ajax(
								{
									url: "http://js.kew-wishlist.co.uk/kew/addProduct/",
									data: {
										prodid: prodID.substring(3),
										email: jswlEmailaddr
									},
									dataType: "jsonp",
									type: "GET",
									success: function (data)
									{
										if (data.email == false)
										{
											if (isjUIloaded == false) { isJUErequired = true; }
											else {	dialog_wlemail(); }
										}
										else if (data.status == true)
										{
											dialog_wlemail_success();

											$("div#wlp" + prodID.substring(3)).find("a").remove();
											$("div#wlp" + prodID.substring(3)).append($("<p>").addClass("wlC").text("added").css(
											{
												color: "#dc006b"
											}));
											$("div#wlbagInfo span").text(data.cnt + " items");
											setMyStorage("wl_items", data.cnt);
											setMyStorage("wl_lastUpdated", getWLtime());
											$("ul#headerLinks").css(
											{
												right: "355px"
											});
											$("form#searchForm").css(
											{
												right: "315px"
											});
											$("div#wishListBasket").show();
										}
									}
								});
							});
							jswlAddSel();
						})
					}
				}
			})
		}
	}
	$("div.headerBackground").after("<div id=\"wishListBasket\"><div id=\"wishListBasketOverview\"><div id=\"wlbagInfo\"><a href=\"/fcp/content/wishlist_basket/content\">my coming soon list</a><span></span></div><div id=\"wlbagIcon\"></div></div></div>");
	if (document.location.protocol != "https:")
	{
		if (typeof (localStorage) != "undefined")
		{
			lastUpdated = getMyStorage("wl_lastUpdated");
			if (((getWLtime() - lastUpdated) > 60000) || (isNaN(getWLtime() - lastUpdated)))
			{
				loadWishList();
			}
			else
			{
				showWishList(true);
			}
		}
		else
		{
			loadWishList();
		}
	}
	if ($("div#productsCont").length)
	{
		var theMatch = window.location.pathname.match(/\/fcp\/categorylist\/(?:garment|outfit)\/(fullhat|fulldress|fullskirt|skirts|dresses|q159denim|fullq159|coatsjackets|fullcoat|tops|fulltshirt|knitwear|trousers|fulltrousers|jewellery|fulljewellery|bags|fullbag|footwear|fullshoes|fullscarves|scarves|belts|fullbelt|shirts|fullshirt|cardigans|fullcardigan|sweaters|fulljumpers|hosiery|fullhosiery|as_seen_in|fullloungewear)$/);
		if (theMatch != null)
		{
			$.ajax(
			{
				url: "http://js.kew-wishlist.co.uk/kew/getWishListCategory/" + theMatch[1],
				dataType: "jsonp",
				type: "GET",
				success: function (data)
				{
					if (data.prods)
					{
						if (data.email == false) { calljQuery_wl(); }
						var HTML = "<div class=\"wlCont\"><img src=\"/pws/images/header_coming-soon.jpg\" alt=\"Coming Soon\" /><p style=\"margin:15px;\">These styles will be arriving shortly. Sign up and be the first to know when they are availble to buy.</p>";
						$.each(data.prods, function (i, prod)
						{
							HTML = HTML + '<div class="wlItem" id="wlp' + prod.id + '">';
							HTML = HTML + '<img src="http://static.kew-wishlist.co.uk/product-images/' + prod.img + '.jpg" alt="' + prod.name + '" />';
							HTML = HTML + '<h3>' + prod.name + '</h3>';
							HTML = HTML + '<div class="wlBord"></div>';
							HTML = HTML + '<p><span class="wlInfo">Colours Available:</span><span class="wlDetail">' + prod.colour + '</span></p>';
							HTML = HTML + '<p><span class="wlInfo">Expected Online:</span><span class="wlDetail">' + prod.exp + '</span></p>';
							HTML = HTML + '<p><span class="wlInfo">Price:</span><span class="wlDetail">&pound;' + prod.price + '</span></p>';
							HTML = HTML + '<div class="wlBord"></div>';
							if (prod.inCart == true)
							{
								HTML = HTML + '<p class="wlC">Added</p>';
							}
							else
							{
								HTML = HTML + '<p class="wlC"><a href="#">Email me</a></p>';
							}
							HTML = HTML + '</div>'
						});
						HTML = HTML + "</div>";
						$("div#productsCont").append(HTML);
						$("div.wlItem a").click(function (e)
						{
							e.preventDefault();
							jswlAddSel = $(this).attr("id");
							var prodID = $(this).parents("div.wlItem").attr("id");
							jswlAddSel = (function()
							{
								$.ajax(
								{
									url: "http://js.kew-wishlist.co.uk/kew/addProduct/",
									data: {
										prodid: prodID.substring(3),
										email: jswlEmailaddr
									},
									dataType: "jsonp",
									type: "GET",
									success: function (data)
									{
										if (data.email == false)
										{
											if (isjUIloaded == false) { isJUErequired = true; }
											else {	dialog_wlemail(); }
										}
										else if (data.status == true)
										{
											try { jQuery( "#dialog-wlemail" ).dialog( "close" ); } catch(e) { }
											$("div#wlp" + prodID.substring(3)).find("a").remove();
											$("div#wlp" + prodID.substring(3)).append($("<p>").addClass("wlC").text("added").css(
											{
												color: "#dc006b"
											}));
											$("div#wlbagInfo span").text(data.cnt + " items");
											setMyStorage("wl_items", data.cnt);
											setMyStorage("wl_lastUpdated", getWLtime());
											$("ul#headerLinks").css(
											{
												right: "355px"
											});
											$("form#searchForm").css(
											{
												right: "315px"
											});
											$("div#wishListBasket").show();
										}
									}
								});
							});
							jswlAddSel();
						})
					}
				}
			})
		}
	}
	if ($("div#productInfoCont").length)
	{
		$("div#btnPrintAndTell").append('<div class="addthis_default_style"><a class="addthis_button_compact">Share</a> <a class="addthis_button_email"></a> <a class="addthis_button_facebook"></a> <a class="addthis_button_twitter"></a></div><script type="text/javascript">var addthis_config = { services_exclude: "email, print" }</script><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js"></script>');
	}
	if ($("div#imgMyAccount").length)
	{
		$("h3.siFR").text("Forgotten your password").css(
		{
			"font-size": "14px",
			"font-weight": "bold",
			"margin": "10px 0"
		});
	}
});
