/* MISC */
String.prototype.beginsWith = function(t, i) { if (i==false) { return 
(t == this.substring(0, t.length)); } else { return (t.toLowerCase() 
== this.substring(0, t.length).toLowerCase()); }};

// String.prototype.endsWith = function(t, i) { if (i==false) { return (t
// == this.substring(this.length - t.length)); } else { return
// (t.toLowerCase() == this.substring(this.length -
// t.length).toLowerCase()); } };

String.prototype.endsWith = function(str)
{
	return (this.match(str+"$") == str);
};

String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,''); };
		
function getH(){
	var h;
	if(document.innerHeight){ h=document.innerHeight;
	} else if(document.documentElement.clientHeight){ h=document.documentElement.clientHeight;
	} else if(document.body){ h=document.body.clientHeight; }
	return h;
}

function getW(){
	var w;
	if(document.innerWidth){ w=document.innerWidth;
	} else if(document.documentElement.clientHeight){ w=document.documentElement.clientWidth;
	} else if(document.body){ w=document.body.clientWidth; }
	return w;
}

function endsWith(testString, endingString){
    if(endingString.length > testString.length) return false;
    return testString.indexOf(endingString)==(testString.length-endingString.length);
}

var bottom_tabs_closed_top = 207;
var bottom_tabs_closed_height = 15;

var bottom_tabs_open_top = 48;
var bottom_tabs_open_height = 173;

/* THE BUZZ */
jQuery(document).ready(function($) {
	$(".messages-button").click(function (e){
		var bottomtabs = $(e.target).closest("#bottom-tabs");

		setCurrentTab(bottomtabs, "messages");
		
		return false;
	});
	
	$(".buzz-button").click(function (e){
		var bottomtabs = $(e.target).closest("#bottom-tabs");

		setCurrentTab(bottomtabs, "buzz");
		
		return false;
	});
	
	$(".testimonials-button").click(function (e){
		var bottomtabs = $(e.target).closest("#bottom-tabs");

		setCurrentTab(bottomtabs, "testimonials");
		
		return false;
	});
	
	$(".ingredients-button").click(function (e){
		var bottomtabs = $(e.target).closest("#bottom-tabs");

		setCurrentTab(bottomtabs, "ingredients");

		return false;
	});
	
	$(".close-button").click(function (e){
		var bottomtabs = $(e.target).closest("#bottom-tabs");
	
		var messagestab = $("#messages-tab", bottomtabs);
		var buzztab = $("#buzz-tab", bottomtabs);
		var testimonialstab = $("#testimonials-tab", bottomtabs);
		var ingredientstab = $("#ingredients-tab", bottomtabs);

		var messagestabcontent = $("#messages-tab-content", bottomtabs);
		var buzztabcontent = $("#buzz-tab-content", bottomtabs);
		var testimonialstabcontent = $("#testimonials-tab-content", bottomtabs);
		var ingredientstabcontent = $("#ingredients-tab-content", bottomtabs);

		var messagesbutton = $(".messages-button", bottomtabs);
		var buzzbutton = $(".buzz-button", bottomtabs);
		var testimonialsbutton = $(".testimonials-button", bottomtabs);
		var ingredientsbutton = $(".ingredients-button", bottomtabs);

		$("img", messagesbutton).attr("src", "/media/upload/image/folders/messages-tab-passive.jpg");
		$("img", buzzbutton).attr("src", "/media/upload/image/folders/buzz-tab-passive.jpg");
		$("img", testimonialsbutton).attr("src", "/media/upload/image/folders/testimonials-tab-passive.jpg");
		$("img", ingredientsbutton).attr("src", "/media/upload/image/folders/ingredient-tab-passive.jpg");

		messagestabcontent.animate({"height": "0px"}, "normal");
		buzztabcontent.animate({"height": "0px"}, "normal");
		testimonialstabcontent.animate({"height": "0px"}, "normal");
		ingredientstabcontent.animate({"height": "0px"}, "normal");

		messagestab.animate({"height": "0px"}, "normal");
		buzztab.animate({"height": "0px"}, "normal");
		testimonialstab.animate({"height": "0px"}, "normal");
		ingredientstab.animate({"height": "0px"}, "normal");

		bottomtabs.animate({"top": bottom_tabs_closed_top + "px", "height": bottom_tabs_closed_height + "px"}, "normal");
		bottomtabs.attr("tabstate", "closed");

		$("#size").css("display", "");
	});
	
	function setCurrentTab(bottomtabs, currenttab){
		var tabstate = bottomtabs.attr("tabstate");

		var messagesbutton = $(".messages-button", bottomtabs);
		var buzzbutton = $(".buzz-button", bottomtabs);
		var testimonialsbutton = $(".testimonials-button", bottomtabs);
		var ingredientsbutton = $(".ingredients-button", bottomtabs);

		var messagestab = $("#messages-tab", bottomtabs);
		var buzztab = $("#buzz-tab", bottomtabs);
		var testimonialstab = $("#testimonials-tab", bottomtabs);
		var ingredientstab = $("#ingredients-tab", bottomtabs);

		var messagestabcontent = $("#messages-tab-content", bottomtabs);
		var buzztabcontent = $("#buzz-tab-content", bottomtabs);
		var testimonialstabcontent = $("#testimonials-tab-content", bottomtabs);
		var ingredientstabcontent = $("#ingredients-tab-content", bottomtabs);

		messagestab.css("display", "none");
		buzztab.css("display", "none");
		testimonialstab.css("display", "none");
		ingredientstab.css("display", "none");

		messagestabcontent.css("display", "none");
		buzztabcontent.css("display", "none");
		testimonialstabcontent.css("display", "none");
		ingredientstabcontent.css("display", "none");
		
		var messagestabcontent = $("#messages-tab-content", bottomtabs);
		var buzztabcontent = $("#buzz-tab-content", bottomtabs);
		var testimonialstabcontent = $("#testimonials-tab-content", bottomtabs);
		var ingredientstabcontent = $("#ingredients-tab-content", bottomtabs);

		$("#size").css("display", "none");

		$("img", messagesbutton).attr("src", "/media/upload/image/folders/messages-tab-passive.jpg");
		$("img", buzzbutton).attr("src", "/media/upload/image/folders/buzz-tab-passive.jpg");
		$("img", testimonialsbutton).attr("src", "/media/upload/image/folders/testimonials-tab-passive.jpg");
		$("img", ingredientsbutton).attr("src", "/media/upload/image/folders/ingredient-tab-passive.jpg");

		var tab = null;
		var tabcontent = null;
		
		if (currenttab == "messages"){
			$("img", messagesbutton).attr("src", "/media/upload/image/folders/messages-tab-active.jpg");
			tab = messagestab;
			tabcontent = messagestabcontent;
		}

		if (currenttab == "buzz"){
			$("img", buzzbutton).attr("src", "/media/upload/image/folders/buzz-tab-active.jpg");
			tab = buzztab;
			tabcontent = buzztabcontent;
		}

		if (currenttab == "testimonials"){
			$("img", testimonialsbutton).attr("src", "/media/upload/image/folders/testimonials-tab-active.jpg");
			tab = testimonialstab;
			tabcontent = testimonialstabcontent;
		}

		if (currenttab == "ingredients"){
			$("img", ingredientsbutton).attr("src", "/media/upload/image/folders/ingredient-tab-active.jpg");
			tab = ingredientstab;
			tabcontent = ingredientstabcontent;
		}
		
		tab.css("display", "");
		tabcontent.css("display", "");

		if (tabstate == "closed"){
			bottomtabs.animate({"top": bottom_tabs_open_top + "px", "height": bottom_tabs_open_height + "px"}, "normal");
			tab.animate({"height": "158px"}, "normal");
			tabcontent.animate({"height": "110px"}, "normal");
		}
		else{
			tab.css("height", "158px");
			tabcontent.css("height", "110px", "normal");
		}
		
		bottomtabs.attr("currenttab", currenttab);
		bottomtabs.attr("tabstate", "open");
	}
});

/* "SHOP" LINKS ON PRODUCT LISTING */
jQuery(document).ready(function($){
	var path = location.href.toLowerCase();

	if (path.indexOf("/fair") > -1){
		$("#shop-eye-cosmetics").css("display", "");
		$("#shop-fair-cosmetics").css("display", "none");
		$("#shop-medium-cosmetics").css("display", "");
		$("#shop-dark-cosmetics").css("display", "");
	}
	else if (path.indexOf("/medium-landing") > -1 || path.indexOf("/medium.html") > -1){
		$("#shop-eye-cosmetics").css("display", "");
		$("#shop-fair-cosmetics").css("display", "");
		$("#shop-medium-cosmetics").css("display", "none");
		$("#shop-dark-cosmetics").css("display", "");
	}
	else if (path.indexOf("/medium-dark-landing") > -1 || path.indexOf("/medium-dark.html") > -1){
		$("#shop-eye-cosmetics").css("display", "");
		$("#shop-fair-cosmetics").css("display", "");
		$("#shop-medium-cosmetics").css("display", "");
		$("#shop-dark-cosmetics").css("display", "none");
	}
	else if (path.indexOf("/eyes") > -1){
		$("#shop-eye-cosmetics").css("display", "none");
		$("#shop-fair-cosmetics").css("display", "");
		$("#shop-medium-cosmetics").css("display", "");
		$("#shop-dark-cosmetics").css("display", "");
	}
});

function highlightCurrentTopLevelMenu(){
	/* select correct menu */
	var path = location.href.toLowerCase();
	var img = null;

	if (path.indexOf("/home") > -1){
		img = jQuery(".top-level-menu[src *= 'home']");
	}
	else if (
			path.indexOf("/fair") > -1
			|| path.indexOf("/medium-landing") > -1 
			|| path.indexOf("/medium.html") > -1 
			|| path.indexOf("/medium-dark-landing") > -1 
			|| path.indexOf("/medium-dark.html") > -1){
		
		img = jQuery(".top-level-menu[src *= 'product']");
	}
	else if (path.indexOf("/lulu-special-offers") > -1){
		img = jQuery(".top-level-menu[src *= 'offers']");
	}
	else if (path.indexOf("/lulu-blog") > -1){
		img = jQuery(".top-level-menu[src *= 'news']");
	}
	else if (path.indexOf("/videos") > -1){
		img = jQuery(".top-level-menu[src *= 'video']");
	}
	else if (path.indexOf("/press") > -1){
		img = jQuery(".top-level-menu[src *= 'press']");
	}
	else if (path.indexOf("/friend-us") > -1 
			|| path.indexOf("/lu-biography") > -1 
			|| path.indexOf("/lulu-tour-dates") > -1
			|| path.indexOf("/photo-gallery") > -1
			){
		img = jQuery(".top-level-menu[src *= 'lulu-passive']");
	}

	if (img != null){
		var imgUrl = img.attr("src");
		if (imgUrl != null){
			imgUrl = imgUrl.replace("passive", "active");
			img.attr("src", imgUrl);
		}
	}
}

/* Cart */
jQuery(document).ready(function($) {
	$.ui.dialog.defaults.bgiframe = true;
	
	$(".add-to-cart").click(function(e){
		var buyNow = $(e.currentTarget);
		var listingItem = $(buyNow).closest(".listing-item");
		var addToCartButton = $(".add-to-cart", listingItem)
		var sku = addToCartButton.attr("sku");

		var sizeSelector = $("#size", listingItem);
		var product_id = sizeSelector.val();

		if (product_id == ""){
			showDialog("Please select a size.", "Missing Information");
			return false;
		}

// var stockStatus = $("option:selected", colorSelector).attr("stockstatus");
// if (stockStatus == "out of stock"){
// showDialog("Sorry, that colour is currently unavailable.", "Out of Stock")
// return false;
// }
		
		var qty = $("#quantity", listingItem).val();
		
		if (qty == ""){
			qty = "1";
		}
		
		if (qty > 6){
			showDialog("Sorry, due to the popularity of Lulu's products, we have to limit orders to 6 of any particular item.", "Over Maximum Order Quantity");
			return;
		}
		
		$(".ajax-loader", listingItem).show();
		var obj = this;
		
		var params = "product_id=" + product_id + "&qty=" + qty;
		
		var result = $.getJSON("/scripts/addToCart.php", params, function(data, textStatus){
			$(".ajax-loader", listingItem).hide();

			if (textStatus == "error"){
				showDialog("There was an error adding this item to your cart.  Please call customer service for assistance.", "Error");
				return;
			}

			if (data.result == "error"){
				showDialog("Sorry, an error occurred while adding the item to your cart.  The error was: '" + data.message + "'");
				return;
			}
			
			$("#quick-cart").slideDown("slow", function(e){	
				if (data.relatedItems.length > 0){
					var productImage = $("#related-product-image");
					var relatedProductName = $("#related-product-name");
					var relatedProductPrice = $("#related-product-price");
					
					var buyNow = $("#related-items #buy-now");
					var moreInfo = $("#related-items #more-info");

					productImage.attr("src", data.relatedItems[0].imgUrl);
					relatedProductName.html(data.relatedItems[0].name); 
					relatedProductName.attr("entity_id", data.relatedItems[0].entity_id);
					relatedProductPrice.html("&#163; " + data.relatedItems[0].price);
					moreInfo.attr("href", data.relatedItems[0].productUrl);
					
					buyNow.click(function(){
						var params = "product_id=" + data.relatedItems[0].entity_id + "&qty=1";
						var result = $.getJSON("/scripts/addToCart.php", params, function(data, textStatus){
							if (data.result == "success"){
								$("#quick-cart-confirmation").html("This item has also been added to your bag.");
							}
							else if (data.result == "error"){
								showDialog("Sorry, there was an error adding the additional item to your card.  The error was " + data.message);
							}
						});
					});
					
					$("#related-items").animate({"left": "-242px", "width": "242px"}, "slow");
				}
				
				setTimeout(function(){
					$("#quick-cart").fadeOut("normal");
					$("#related-items").fadeOut("normal");
				}, 4000);
				
				$("#quick-cart-close").click(function(e){
					$("#quick-cart").hide();
					$("#related-items").hide();
				});
			});
		});
	});
	
	function showDialog(msg, title){
		$("#dialog").dialog( 'destroy' );
		$("#dialog").html(msg);
		
		$("#dialog").dialog({
			buttons: { 
				"Ok": function() { 
					$(this).dialog("close"); 
				}
			} 
			// , closeOnEscape: true
			// , show: 'slide'
		});
		
		$('#dialog').dialog('option', 'title', title);
		$("#dialog").dialog('open');
	}
});	
	
/* Auto scroll on product pages */
jQuery(document).ready(function($) {
	if ($.localScroll != null){
		// Scroll initially if there's a hash (#something) in the url
		$.localScroll.hash({
			target: '.listing-type-list', // Could be a selector or a jQuery
											// object too.
			queue:true,
			duration:1000,
			lazy: true
		});
		
		$.localScroll({
			target: '.listing-type-list', // could be a selector or a jQuery
											// object too.
			queue:true,
			duration:1000,
			lazy: true
		});
}
});

/* Rollover Buttons */
jQuery(document).ready(function($){
	// Rollovers for buttons
	$(".rollover-button").hover(
			function(e){
				var src = $(this).attr("src");
				src = src.replace("passive", "active");
				$(this).attr("src", src);
			},
			function(e){
				var src = $(this).attr("src");
				src = src.replace("active", "passive");
				$(this).attr("src", src);
			}
	);
});

/* Screen Size and backgrounds */
function writeStartupCode(){
	/* write in background image */
	var path = location.href.toLowerCase();

	var bgimage = "br-home.jpg";
	if (path.indexOf("/home") > -1){
		bgimage = "br-home.jpg";
	}
	else if (path.indexOf("/lulu-special-offers") > -1
			|| path.indexOf("/glam-kit.html") > -1
			|| path.indexOf("/time-bomb-kit.html") > -1
			|| path.indexOf("/wrap-yourself-kit.html") > -1
			){
		bgimage = "br-special-offer.jpg";
	}
	else if (path.indexOf("/lulu-blog") > -1){
		bgimage = "br-blog.jpg";
	}
	else if (path.indexOf("/videos") > -1){
		bgimage = "br-video.jpg";
	}
	else if (path.indexOf("/press") > -1){
		bgimage = "br-press.jpg";
	}
	else if (path.indexOf("/time/") > -1
			|| path.endsWith("/time")
			|| path.endsWith("/time-fragrance.html")){
		bgimage = "br-time.jpg";
	}
	else if (path.indexOf("/time-bomb") > -1){
		bgimage = "br-timebomb.jpg";
	}	
	else if (path.indexOf("/time-bomb") > -1){
		bgimage = "br-timebomb.jpg";
	}	
	else if (path.indexOf("/bombshell-body") > -1){
		bgimage = "br-body.jpg";
	}
	else if (path.indexOf("/operation-glam") > -1){
		bgimage = "br-operation-glam.jpg";
	}	
	else if (path.indexOf("/lu-biography") > -1){
		bgimage = "br-bio-tour-gallery.jpg";
	}
	else if (path.indexOf("/lulu-tour-dates") > -1){
		bgimage = "br-bio-tour-gallery.jpg";
	}
	else if (path.indexOf("/lulu-tour-appearances") > -1){
		bgimage = "br-bio-tour-gallery.jpg";
	}
	else if (path.indexOf("/photo-gallery") > -1){
		bgimage = "br-bio-tour-gallery.jpg";
	}
	else if (path.indexOf("/checkout/cart") > -1){
		bgimage = "br-bio-tour-gallery.jpg";
	}

	var bgStyle ="<style>";
	
	if (bgimage != ""){
		bgStyle += ".wrappers_wrapper { background: #fff url(/media/upload/image/backgrounds/" + bgimage + "); }";
	}
	else {
		bgStyle += ".wrappers_wrapper { background: #000 ;}";
	}

	bgStyle += "</style>"; 
	
	document.write(bgStyle);
	
	// screen size adjustments
	
	var h = getH();
	h -= 917;
	if (h < 0) h = 0;
	h /= 2;
	
	document.write("<style>.wrappers_wrapper { top: " + h + "px;}</style>");
	
	if (getW() <= 1024){
		document.write("<style>.wrappers_wrapper { background-image: none; background-color: #000; } .wrapper{ top: 0px; left: 0px; }</style>");
	}	
}

/* subscribe form */
jQuery(document).ready(function($){
	$("#mc-embedded-subscribe-form").attr("target", "_blank");
});
