(function($) {
	if (!$) {
		window.console && console.log("Scripts init: jQuery not found");
		return;
	}

	// Initalize Nivo Slider
	if (!$.fn.nivoSlider) {
		window.console && console.log("Nivo Slider init: Nivo Slider library not found");
	} else {
		$(window).load(function(){
			var slider = $("#slider"),
			    oldEffect;
			slider.nivoSlider({
				"effect": "fadeRight",
				"pauseTime": 5000,
				"controlNav": false,
				"directionNavHide": false
			}).find(".nivo-directionNav").
				find("a").mouseover(function() {
					oldEffect = slider.data("nivo:vars").effect;
					slider.data("nivo:vars").effect = $(this).is(".nivo-prevNav") ? "foldLeft" : "fold";
				}).mouseout(function() {
					slider.data("nivo:vars").effect = oldEffect;
				}).end().
				append($(document.createElement("a")).addClass("nivo-stopNav").text("Pause")).
				find(".nivo-stopNav").click(function() {
					var data = slider.data('nivo:vars');
					data.stop = !data.stop;
					$(this).toggleClass("stopped");
				});
		});
	}

	// Handle the absolutely positioned header and memory selector
	$(function() {
	    // Set the padding for #content depending on the height of #finder
	    $("#content").css("padding-top", $("#finder").outerHeight() + 18);

	    // Set the padding for #container depending on the height of #headerInfo, #headerTop, and #header
	    var combinedHeight = 0;
	    $("#headerInfo, #headerTop, #header").each(function() {
		combinedHeight += $(this).outerHeight();
	    });
	    $("#container").css("padding-top", combinedHeight + 12);
	});

	// Initialize the CSS dropdowns for IE, and prevent clicks of the # links
	$(function() {
		var pageId = window.location.href.match(/\/([^\/]+\.html)/);
		pageId = pageId && pageId[1] || false;
		$("#headerBottom .links").
			find("li").hover(function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); }).end().
			find("a[href='#']").click(function() { return false; }).end().
			find("a[href='" + pageId + "']").parents("li:last").addClass("selected").end().end().
			find("> ul > li").each(function() {
				var obj = $(this);
				obj.find("> ul").css("top", obj.find("> a").outerHeight());
			}).end().
			mouseleave(function() {
				$(this).find("li").trigger("mouseout");
			});
	});
	$(window).load(function() {
	    $("#headerBottom .links > ul > li").each(function() {
		var obj = $(this);
		obj.find("> ul").css("top", obj.find("> a").outerHeight());
	    });
	});

	// IEEEEEEEEEE
	$(window).load(function() {
	    $("#headerInfo").each(function() {
		var a = $(this).width();
	    });
	});

	// Initialize the dropdowns
	// source: http://www.jankoatwarpspeed.com/post/2009/07/28/reinventing-drop-down-with-css-jquery.aspx
	$(function() {
		var dropdowns = $(".dropdown").each(function() {
			var dropdown = $(this).
				find("dt a").click(function() {
					dropdowns.not(dropdown).find("dd ul").hide();
					dropdown.find("dd ul").toggle();
					return false;
				}).end().
				find("dd ul li a").click(function() {
					dropdown.removeClass("dropdownDefaultValue").
						find("dt a span").html($(this).html()).end().
						find("dd ul").hide();
					if (!dropdown.hasClass("dropdownNavigation")) {
						return false;
					}
				}).end();
		});

		$(document).bind("click", function(e) {
			if (!$(e.target).parents().hasClass("dropdown")) {
				$(".dropdown dd ul").hide();
			}
		});
	});

	// Fix alpha-transparent PNGs
	if (window.DD_belatedPNG) {
		$(function() {
			DD_belatedPNG.fix("#logo img, #finder .bottom .button img, #headerBottom .images img, #finder .top h3 img, #finderImg, #footer .top .bg, #specials .info .button");
		});
	}

	// Initialize PIE
	$(function() {
		if (window.PIE) {
			$("#bg, #header, #headerTop, #headerBottom .links a, " + /* "#headerBottom .links ul, " + */ "#nav, #finder, #finder .top, #alphabet a, #footer .bg, #headerTop input.submit, #footer .bg .border, #ys_pageMessage .bg .border, #specials").each(function() {
				PIE.attach(this);
			});
		}
	});

	$(function() {
		// Initialize the "Scan my PC" button
		$("#finder .button").click(function() {
			OpenDetectionPage("memx", "0", "memx");
			return false;
		});

		// Initialize the Live Chat button
		$(".liveChatButton").
			click(function() {
				window.open($(this).attr("href"),
					"chatWindow",
					"width=490,height=404,resizable=0,scrollbars=no,menubar=no,status=no");
				return false;
			}).
			hover(function() { top.status = "Chat with a company representative"; }, function() { top.status = ""; });
	});
})(window.jQuery);

