$(document).ready(
	function () {
		// MSIE fixes
		if ($.browser.msie) {
			// last-child fixes
			$("#userBar ul li:last, #footer ul li:last").css({border: "none"});
			$("#primaryNavigation li:last").css({margin: "0"});
			$("#shop #primaryNavigation li:last a").css({width: "106px"});
			$("#shop #content ul.productListing:last").css({backgroundImage: "none"});
			$("#shop #productDetail #details div.links li:last").css({margin: "0"});
			$("#shop #content #checkoutprogress li:last").css({"margin-right": "0"});
			// [attr] fixes
			$("input[type='checkbox']").css({border: "none"});
			// IE6 fixes
			if ($.browser.version == '6.0') {
				$("#primaryNavigation li.shopButton a span").css({backgroundImage: "none", padding: 0});
			}
		}
		// product image switcher thingamajig
		$("#imageContainer > ul > li > a").click(
			function (e) {
				var lgImgUrl = $(this).attr("href");
				var tnImgUrl = $(this).attr("title");
				var largeImage = $("img#largeImage");
				largeImage
					.fadeOut(
							 500,
							 function () {
							 	largeImage.attr({
													"src":tnImgUrl,
													"alt":lgImgUrl 
												});
								largeImage.fadeIn(500);
							 }
							);
				e.preventDefault();
			}
		);
		$("img#largeImage")
			.hover(
				function () {$(this).css({"cursor":"pointer"});},
				function () {$(this).css({"cursor":"default"});}
			);
		// BEGIN FAUX LIGHTBOX
		// closing function
		endItAll = function () {$("#popup").fadeOut("250", function() {$("#popup").remove(); $("#sheet").fadeOut("100", function() {$("#sheet").remove();});});}
		// launching function
		$("img#largeImage").click(function (e) {
			if ($("#popup").length != 0) {endItAll;}
			var popup = $('<div id="popup" class="clearfix">');
			var i = new Image();
			var closeButton = $('<div id="close">')
				.css({
						position: "absolute",
						width: "auto",
						height: "auto",
						textAlign: "right",
						paddingRight: "20px",
						fontWeight: "bold",
						top: "20px",
						right: "10px",
						margin: "0 12px 12px 0",
						width: "100px",
						height: "30px",
						background: "transparent url(/static/images/closeButton.gif) right center no-repeat",
						display: "none",
						zIndex: "9999999"
					});
			var sheet = $('<div id="sheet">')
				.css({
						position: "absolute",
						top: 0,
						left: 0,
						width: $(document).width(),
						height: $(document).height(),
						background: "#000",
						opacity: "0.75",
						display: "none",
						zIndex: "99999"
					 });
			popup.append(i).append(closeButton);
			$(i).css({display: "block", margin: "0 auto 5px"}).attr("src", $(this).attr("alt"))
				.load(
					  function () {
						var viewportOffset = window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
						var viewportHeight = $(document).height() - viewportOffset;
						var topOffset = viewportOffset + (viewportHeight/2);
						popup.css({
									  padding: "10px",
									  background: "#000",
									  position: "absolute",
									  top: topOffset,
									  left: "50%",
									  marginTop: -(i.height / 2),
									  marginLeft: -(i.width / 2),
									  display: "none",
									  zIndex: "999999",
									  width: i.width
								  });
						$("body").append(sheet);
						$("body").append(popup);
						sheet.fadeIn("100", function() {popup.fadeIn("250");});
					  }
					 );
			popup.bind("mouseenter",
					   function () {
						   $(this).css({cursor: "pointer"});
						   if (closeButton.is(":hidden")) {
							   closeButton.fadeIn("200");
						   }
						});
			popup.bind("mouseleave", function () {$(this).css({cursor: "default"}); closeButton.fadeOut("200");});
			sheet.hover(
							function () {$(this).css({cursor: "wait"});},
							function () {$(this).css({cursor: "default"});}
						);
			$("*").click(endItAll);
			e.preventDefault();
		});
		// END FAUX LIGHTBOX
		
		// external opens in new window - no more target
		$("a[href^='http']").each(function () {if(($(this).attr("href").indexOf("ecosumo") > 11) ||($(this).attr("href").indexOf("ecosumo") == -1)){$(this).attr('target','_blank');}});
		
		// CCV helper
		var killCCV = function () {$("#shop #content fieldset a#ccv_help img").fadeOut(200);}
		$("#shop #content fieldset a#ccv_help").mouseover(
			   function (e) {
				   var i = new Image();
				   $(i).css({
							"left":"20px",
							"position":"absolute",
							"top":"-95px",
							"z-index":"999999",
							"display": "none"
							}).attr("src", $(this).attr("href"));
				   $(this).append(i);
				   $(i).fadeIn(200);
				   e.preventDefault();
			   }
		);

		$("input").focus(killCCV);
		$("#shop #content fieldset a#ccv_help").mouseout(killCCV);
	}
);
