jQuery(document).ready
(
	function($)
	{
		var topOffset = 67;

		if( $(window).scrollTop() > topOffset )
		{
			$("div#nav_container_inner").detach().appendTo("div.superbar-bottom");
			$("div#superbar").addClass("hover").css("top", -80).animate
			(
				{ top: 0 },
				100,
				function()
				{
				}
			);
		}

		$(window).scroll
		(
			function(event)
			{
				if( $(this).scrollTop() > topOffset )
				{
					$("div#nav_container_inner").detach().appendTo("div.superbar-bottom");
					//$("div#superbar").addClass("hover");
					if( !$("div#superbar").hasClass("hover") )
					{
						$("div#superbar").addClass("hover").css("top", -80).animate
						(
							{ top: 0 },
							100,
							function()
							{
							}
						);
					}
				}
				else
				{
					$("div#nav_container_inner").detach().appendTo("div#nav_container")
					//$("div#superbar").removeClass("hover");
					$("div#superbar").animate
					(
						{ top: -80 },
						100,
						function()
						{
							$(this).removeClass("hover");
						}
					);
				}
			}
		);
		
		var timerfunction;
		$("div#nav_container_inner").hover
		(
			function(event)
			{
				clearTimeout(timerfunction);
				$(this).stop().animate
				(
					{ height: 64 },
					300,
					function()
					{
						$(this).addClass("open");
						$(this).attr("style", "");
					}
				);
			},
			function(event)
			{
				clearTimeout(timerfunction);
				timerfunction = setTimeout
				(
					function()
					{
						$("div#nav_container_inner").stop().delay(100).animate(
							{ height: 32 },
							300,
							function()
							{
								$(this).removeClass("open");
								$(this).attr("style", "");
								$(this).children("ul").children("li").removeClass("current");
								//$("ul#newnav > li > ul.level0").css("z-index", "auto");
							}
						);
					},
					300
				);
			}
		);

		$("div#nav_container_inner > a").mouseenter
		(
			function(event)
			{
				event.stopPropagation();
				$("div#nav_container_inner").mouseleave();
			}
		);

		$("div#nav_container_inner > ul#newnav > li").mouseenter
		(
			function(event)
			{
				clearTimeout(timerfunction);
				$(this).closest("div").stop().animate
				(
					{ height: 64 },
					300,
					function()
					{
						$(this).addClass("open");
						$(this).attr("style", "");
					}
				);
			}
		);

		var timerfunction2 = 0;
		$("ul#newnav > li.level0").hover
		(
			function(event)
			{
				var thisthis = $(this);
				if( $("div#nav_container_inner").hasClass("open") )
				{
					timerfunction2 = setTimeout
					(
						function()
						{
							thisthis.siblings().removeClass("current");
							thisthis.addClass("current");
							thisthis.siblings("li").children("ul").css("z-index", "auto");

							thisthis.children("ul").css("z-index", 1000);
						},
						500
					);
				}
				else
				{
					$(this).siblings().removeClass("current");
					$(this).addClass("current");
					$(this).siblings("li").children("ul").css("z-index", "auto");

					$(this).children("ul").css("z-index", 1000);
				}
			},
			function(event)
			{
				clearTimeout(timerfunction2);
				//$(this).removeClass("current");
			}
		);

		$("ul#newnav > li.level0 > ul.level0 > li.level1").hover
		(
			function(event)
			{
				$(this).addClass("current");
				$("ul.level1", $(this)).addClass("current").attr("style", "display: none;");
				$("ul.level1", $(this)).stop().slideDown(200);
			},
			function(event)
			{
				$(this).removeClass("current");
				$("ul.level1", $(this)).stop().slideUp
				(
					200,
					function()
					{
						$("ul.level1", $(this)).removeClass("current");
					}
				);
			}
		);
		
		$("ul.mini-products-list > li.product > div.details > div.accessories").live
		(
			"mouseenter",
			function(event)
			{
				$(this).children("div").addClass("show");
			}
		)
		$("ul.mini-products-list > li.product > div.details > div.accessories").live
		(
			"mouseleave",
			function(event)
			{
				$(this).children("div").removeClass("show");
			}
		);
		
		checkHeight($);

		$("div#compare-container > div.compare > ul > li.title > div.sku").live
		(
			"click",
			function(event)
			{
				event.preventDefault();
				event.stopPropagation();

				var divc = $(this).parent().closest("div");
				var offset = $(this).closest("ul").height() - 20; // 501;
				
				if( ($("div#compare-view").hasClass("two") && divc.hasClass("faucetbar")) )
				{
					offset -= 20; //= 481;
				}
				
				if( !divc.hasClass("transitioning") )
				{
					if( divc.hasClass("activated") )
					{
						$("div.comparebarrier").animate( { opacity: 0 }, 500, function() { $(this).remove(); } );
						divc.removeClass("activated").addClass("transitioning").animate( { top: "+=" + offset }, 500, function() { $(this).removeClass("transitioning").attr("style", ""); } );
						if( divc.hasClass("faucetbar") )
							divc.siblings("div.compare").animate( { top: -20 }, 500, function() { $(this).attr("style", ""); } );
						else
							divc.siblings("div.compare").animate( { top: -40 }, 500, function() { $(this).attr("style", ""); } );
					}
					else
					{
						$("<div class=\"comparebarrier\"></div>").appendTo("div.page").css("opacity", 0).animate
						(
							{ opacity: 1 },
							500,
							function()
							{
								$(this).click
								(
									function(event)
									{
										$("div#compare-container > div.compare.activated > ul.titlecol > li.title > div.sku").click();
										$(this).unbind("click");
									}
								)
							}
						);
						divc.addClass("transitioning").animate( { top: "-=" + offset }, 500, function() { $(this).removeClass("transitioning").addClass("activated"); } );
						divc.siblings("div.compare").animate( { top: 0 }, 500);
					}
				}
			}
		);
		
		$("div#compare-container > div.compare > ul > li.title > div.ecks").live
		(
			"click",
			function(event)
			{
				event.preventDefault();
				event.stopPropagation();
				var divcompare = $(this).closest("div.compare");
				
				var sku = $(this).closest("ul").attr("sku");
				var sid = $(this).closest("ul").attr("sid");
				var sorf = "";

				if( $(this).parent().closest("div").hasClass("sinkbar") )
					sorf = "sink";
				else if( $(this).parent().closest("div").hasClass("faucetbar") )
					sorf = "faucet";
				else
					return false;

				$('ul#product-grid > li > div.qvbackground > div.product > div.nv > div[sid="' + sid + '"]').removeClass("compared");
				$('div.sink-title-bar > div[sid="' + sid + '"]').removeClass("compared");

				removeColumn($, sorf, sku);

				if( divcompare.hasClass("activated") )
					divcompare.attr("style", "top: -" + divcompare.children("ul.titlecol").height() + "px;");

				if( divcompare.children("ul").length == 1 )
					$("div.comparebarrier").click();

				$.get("/compare/" + sorf + "s/remove/pid/" + sid, function( data, textStatus, jqXHR ) { }, "html");
			}
		);
		
		$("div.cart-block > div.content > ul.mini-products-list > li.product > div.details > div.accessories > span > a").live
		(
			"click",
			function(event)
			{
				event.preventDefault();

				var imagegroup = $(this).closest("div.product").children("a.product-image-link");

				var e = $(this);
				var cycle1 = function() { e.animate( { opacity: 0.5 }, 250, cycle2); };
				var cycle2 = function() { e.animate( { opacity: 1.0 }, 250, cycle1); };

				cycle1();

				var ajaxurl = $(this).attr("href") + "isAjax/1";
				if( document.location.protocol == "https:" )
					ajaxurl = ajaxurl.replace("http:", "https:")
				if( document.location.hostname.search(/www\./) == -1 )
					ajaxurl = ajaxurl.replace("www.", "");

				$.ajax
				(
					{
						url: ajaxurl, //$(this).attr("href") + "isAjax/1",
						dataType: 'json',
						error: function(jqXHR, textStatus, errorThrown)
						{
							e.stop().css("background", "#000");
							//$("div#cartoverlay").removeClass("adding").addClass("error").animate( { opacity: 0 }, 1000, function() { $(this).remove(); } );
						},
						success: function(data, textStatus, jqXHR)
						{
							//$("div#cartoverlay").removeClass("adding").animate( { opacity: 0 }, 1500, function() { $(this).remove(); $("div.fadeout").click(); } );
							$("div.cart-block").replaceWith(data.sidebar);
						},
						type: "GET"
					}
				);
			}
		);
	}
);

function appendColumn($, sorf, sku)
{
	var orig = $("div#compare-container > div.current > ul[sku=" + sku + "]");
	var dest = $("div#compare-container > div.compare." + sorf + "bar");
	var columnno = -1;

	dest.children("ul").each
	(
		function(index, Element)
		{
			if( $("li.title > div.sku > span", $(this)).html() == sku )
			{
				columnno = $(this).index();
			}
		}
	);
	if( columnno == -1 )
	{
		dest.append(orig.clone());
		dest.children().last().css( { "opacity": 0, "top": -60 } )
			.animate ( { opacity: 1, top: 0 }, 100, function() { $(this)
			.animate ( { top: -30 }, 100, function() { $(this)
			.animate ( { top: 0 }, 100, function() { $(this)
			.animate ( { top: -10 }, 100, function() { $(this)
			.animate ( { top: 0 }, 0, function() { $(this)
			.animate ( { top: -5 }, 100, function() { $(this).animate ( { top: 0 }, 100 ); } ); } ); } ); } ); } ); } );
		$("div#compare-container > div.compare > ul").removeClass("first").removeClass("last");
		$("div#compare-container > div.compare > ul:nth-child(2)").addClass("first");
		$("div#compare-container > div.compare > ul:last-child").addClass("last");
	}
	var emptyuls = $("div#compare-container > div.compare > ul.titlecol.last");
	if( emptyuls.length == 0 )
		$("div#compare-view").addClass("two");
	else
		$("div#compare-view").removeClass("two");

	checkHeight($);
}

function removeColumn($, sorf, sku)
{
	var orig = $("div#compare-container > div.current > ul[sku=" + sku + "]");
	var dest = $("div#compare-container > div.compare." + sorf + "bar");
	//var title = $("ul.titlecol > li.title > a > span", dest);
	var columnno = -1;

	dest.children("ul").each
	(
		function(index, Element)
		{
			if( $("li.title > div.sku > span", $(this)).html() == sku )
			{
				columnno = $(this).index();
			}
		}
	);
	if( columnno != -1 )
	{
		$(dest.children("ul").get(columnno)).remove();
		$("div#compare-container > div.compare > ul").removeClass("first").removeClass("last");
		$("div#compare-container > div.compare > ul:nth-child(2)").addClass("first");
		$("div#compare-container > div.compare > ul:last-child").addClass("last");
	}
	var emptyuls = $("div#compare-container > div.compare > ul.titlecol.last");
	if( emptyuls.length == 0 )
		$("div#compare-view").addClass("two");
	else
		$("div#compare-view").removeClass("two");

	checkHeight($);
}

function checkHeight($)
{
	var sinkuls = $("div#compare-container > div.compare.sinkbar > ul");
	var faucetuls = $("div#compare-container > div.compare.faucetbar > ul");
	var alluls = [ sinkuls, faucetuls ];
	var accscount = [ 0, 0 ];
	$.each
	(
		alluls,
		function(index, Element)
		{
			var theuls = $(this);
			for( var i = 0; i < theuls.length; i++ )
			{
				var ul = theuls.eq(i);
				var count = $("li.accs > img", ul).length;
				if( count > accscount[index] )
					accscount[index] = count;
			}
			if( accscount[index] == 0 )
			{
				theuls.each
				(
					function(index, Element)
					{
						$(this).children("li.accs").removeClass("halfpint").addClass("nopint");
					}
				);
			}
			else if( accscount[index] <= 2 )
			{
				theuls.each
				(
					function(index, Element)
					{
						$(this).children("li.accs").removeClass("nopint").addClass("halfpint");
					}
				);
			}
			else
			{
				theuls.each
				(
					function(index, Element)
					{
						$(this).children("li.accs").removeClass("nopint").removeClass("halfpint");
					}
				);
			}
		}
	);
}
