
function removeEmptyMenuItems() {
	$("#menu_container ul li a").each(function() {
		if ($(this).text() == "") {
			$(this).parent().remove();
		}
	});
}

function removeLastRightMenuBorder() {
	$(".extendedmenu .groups").each(function() {
		$(".group:last", this).css("border-right", "none");
	});
}

function checkOplinkPositions() {
	var text = $(".oplnk4").text();
	if (text == "Logout") {
		$(".oplnk4").css("left", "373px");
		$(".oplnk6").css("left", "373px");
	}
}

function trimPaperTrailElements() {
	$("#papertrail ul li").each(function() {
		var html = $(this).html();
		while (html.indexOf("&nbsp;") != -1) {
			html = html.replace("&nbsp;", " ");
		}
		html = html.trim();
		$(this).html(html);
	});
}

function pageCorrections() {
	var pathname = window.location.pathname.toLowerCase();
	pathname = pathname.substring(pathname.lastIndexOf("/") + 1);
	
	if (pathname == "login.aspx") $("h2:first").replaceWith("<h1>" + $("h2:first").text() + "</h1>");

	if (pathname == "basket.aspx") $(".center_col").prepend("<h1>Basket</h1><br />");

	if (pathname == "checkout.aspx") {
		$("h1").text("Checkout").after("<br />");
		var $legend = $(".center_col legend");
		if ($legend.text() == "Notes") $legend.text("Instructions for delivery");
	}

	if (pathname == "register.aspx") {
		$("h1:first").remove();
		$("h4:first").remove();
		$("h2:first").replaceWith("<h1>" + $("h2:first").text() + "</h1>");
		$(".center_col div").css({"padding": "0 20px"});
		$(".center_col fieldset").css({"height": "auto", "min-height": "0"});
	}
	
	if (pathname == "wish.aspx") {
		$(".center_col").prepend("<h1>Wishlist</h1><br />");
		var $link = $("#ctl00_MainContent_lnkEmptyWish");
		if (!$link.attr("href")) $link.replaceWith("<span class=\"empty\">There are no items currently in your WishList</span>");
	}

	if (pathname == "index") {
		$(".slideshows img").each(function() {
			var href = $(this).attr("href");
			if (href) {
				$(this).css("cursor", "pointer");
				$(this).click(function() {
					window.location.href = href;
				});
			}
		});
	}
}

$.fn.addBow = function() {
	var $parent = $(this);
	$parent.css("postion", "relative");
	var $image = $("img", this);
	if ($image.attr("src").indexOf("noimage-small.jpg") != -1) return;
	$image.one("load", function() {
		var x = $image.position().left + parseInt($image.css("padding-left")) + parseInt($image.css("margin-left")) - 5;
		var y = $image.position().top + parseInt($image.css("padding-top")) + parseInt($image.css("margin-top")) - 5;
		var $bow = $("<img src=\"./images/bow2.png\" alt=\"Christmas bow\" class=\"bow\" />");
		$bow.css({
			"position": "absolute",
			"left": x,
			"top": y,
		});
		$parent.append($bow);
	}).each(function() {
		if (this.complete) $(this).load();
	});
}

function addBows() {
	$(".item a, .mnu_item a, .supergimg, .home_item").each(function() {
		$(this).addBow();
	});
}

function addSnow() {
	if ($("#etlcont")) {
		$("#etlcont").css("padding-bottom", "120px");
	}
	$("body").css("margin-bottom", "120px");
	$("body").append("<div id=\"snow\"></div>");
	$("div#snow").css({
		"width": "100%",
		"height": "86px",
		"background": "url(./images/snowbtmborder.png) repeat-x",
		"position": "fixed",
		"bottom": "0px",
		"z-index": "14000",
	});
}

var isChristmas = false;

$(function() {
	removeEmptyMenuItems();
	removeLastRightMenuBorder();
	checkOplinkPositions();
	trimPaperTrailElements();
	pageCorrections();
	if (isChristmas) {
		addBows();
		addSnow();
	}
});
