$("html").removeClass("no-js");

$.fn.image = function(src, f){ 
	return this.each(function() {
		var i = new Image();
		i.onload = f;
		i.src = src;
		this.appendChild(i);
	});
};
// http://jdbartlett.github.com/innershiv | WTFPL License
window.innerShiv=(function(){var d,r;return function(h,u){if(!d){d=document.createElement('div');r=document.createDocumentFragment();/*@cc_on d.style.display = 'none'@*/}var e=d.cloneNode(true);/*@cc_on document.body.appendChild(e);@*/e.innerHTML=h.replace(/^\s\s*/, '').replace(/\s\s*$/, '');/*@cc_on document.body.removeChild(e);@*/if(u===false)return e.childNodes;var f=r.cloneNode(true),i=e.childNodes.length;while(i--)f.appendChild(e.firstChild);return f}}());
// jQuery plugin based on .load() for use with innerShiv
// http://jdbartlett.github.com/innershiv for more info
// $('selector').loadShiv('example.html selector');
jQuery.fn.loadShiv = function (url, params, callback) {
	var off, selector, self, type;

	if (!this.length || typeof url !== 'string') {
		return this;
	}

	off = url.indexOf(' ');
	if (off >= 0) {
		selector = url.slice(off, url.length);
		url = url.slice(0, off);
	}

	type = 'GET';

	if (params) {
		if (jQuery.isFunction(params)) {
			callback = params;
			params = null;
		} else if (typeof params === 'object') {
			params = jQuery.param(params, jQuery.ajaxSettings.traditional);
			type = 'POST';
		}
	}

	self = this;

	jQuery.ajax({
		url: url,
		type: type,
		dataType: 'html',
		data: params,
		complete: function (res, status) {
			var shivved;

			if (status === 'success' || status === 'notmodified') {
				shivved = jQuery(innerShiv((selector ? '<div>' : '') + res.responseText, false));

				if (selector) {
					shivved = shivved.find(selector);
				}

				self.empty().append(shivved);
			}

			if (callback) {
				self.each(callback, [res.responseText, status, res]);
			}
		}
	});

	return this;
}
// Input placeholder support?
$.support.placeholder = (function() {
    return "placeholder" in document.createElement("input");
})();

$(document).ready(function() {
	// Placeholder support for old browsers
	if (!$.support.placeholder) {
		$("input[placeholder], textarea[placeholder]").focus(function() {
			if (($input = $(this)).val() == $input.attr("placeholder"))
				$input.val("");
		}).blur(function() {
			if (($input = $(this)).val() == "" || $input.val() == $input.attr("placeholder"))
				$input.val($input.attr("placeholder"));
		}).blur().parents("form").submit(function() {
			$(this).find("input[placeholder], textarea[placeholder]").each(function() {
				if (($input = $(this)).val() == $input.attr("placeholder"))
					$input.val("");
			});
		});
	}
	
	// Prevent blank keyword search
	$("header>form").submit(function() {
		if (!$(this).find("input").val()) {
			alert("Please enter a search term.");
			event.preventDefault();
		}
	});
	
	// Menu slide down
	($menu = $("body.collapse #menu")).bind("mouseleave focusout", function() {
		start = $menu.stop(true, true).height();
		end = $menu.removeClass("hover").height();
		$menu.height(start).animate({height: end}, 500, function() {
			$menu.removeAttr("style");
		});
	}).bind("mouseenter focusin", function() {
		start = $menu.stop(true, true).height();
		end = $menu.addClass("hover").height();
		$menu.height(start).animate({height: end}, 500, function() {
			$menu.removeAttr("style");
		});
	});
	
	// Keyboard menu access
	($menus = $("#menu>ul>li")).focusout(function() {
		$menus.removeClass("hover");
	}).focusin(function() {
		$(this).addClass("hover");
	});

	// Product details definition popup
	$("dfn").each(function() {
		($this = $(this)).data("title", $this.attr("title"));
	}).hover(function() {
		($this = $(this)).attr("title", "");
		timeout = setTimeout(function() {
			$this.click();
		}, 500);
	}, function() {
		clearTimeout(timeout);
		($this = $(this)).attr("title", $this.data("title")).children("dl").fadeOut(500, function() {
			$(this).remove();
		});
	}).click(function() {
		clearTimeout(timeout);
		($this = $(this)).children("dl").remove();
		$this.append('<dl class="dfn" style="left: ' + ($this.position().left + $this.innerWidth() / 2) + "px; top: " + ($this.position().top + $this.innerHeight()) + 'px;"><dt>' + $this.text().replace(/(?:^|\s)\w/g, function(match) {return match.toUpperCase();})
 + "</dt><dd>" + (title = $this.data("title")) + "</dd></dl>").children().last().hide().fadeIn(100);
	});

	// Image zoom
	$("a.zoom").click(function(event) {
		($zoom = $('<div id="zoom"><div><a href="" class="close">Close</a></div></div>')).hide().appendTo("body").children().image(this.href, function() {
			$this = $(this);
			setTimeout(function() {
				($zoom = $zoom.fadeIn().children()).toggleClass("tall", $zoom.width() / $zoom.height() < 16/9);
				$this.css({top: ($zoom.height() - $this.height()) / 2});
			}, 1);
		});
		event.preventDefault();
	});

	// Ambient Technologies links
	$('a[href*="ambienttechnologies.com"]').click(function(event) {
		$('<div id="ambient"></div>').hide().appendTo("body").load("/wp-content/themes/monessen/ambient.html", function() {
			$(this).fadeIn();
		});
		event.preventDefault();
	});
	
	// Help Me Choose launch
	$("a[href*=choose]").click(function(event) {
		$('<div id="choose"></div>').hide().appendTo("body").loadShiv(this.href, function() {
			$(this).fadeIn();
		});
		event.preventDefault();
	});
	
	// Side menu expansion
	$("#content aside nav>ul>li>ul>li>ul, #warranty-radio-buttons>li>ul").prev().toggle(function(event) {
		$(this).next().slideDown().parent().addClass("open");
		event.preventDefault();
	}, function(event) {
		($this = $(this)).next().slideUp(function() {
			$this.parent().removeClass("open");
		});
		event.preventDefault();
	}).parent().addClass("dropdown");
	
	($window = $(window)).resize(function() {
		($compare = $("#compare")).toggleClass("absolute", $compare.children().outerHeight(true) > $window.height());
		($zoom_img = ($zoom = $("#zoom").children()).toggleClass("tall", $zoom.width() / $zoom.height() < 16/9).children("img")).css({top: ($zoom.height() - $zoom_img.height()) / 2});
	});
});

// Popup behavior
$("footer~div").live("click", function(event) {
	if ($(event.target).parent("body").length || $(event.target).hasClass("close")) {
		$("footer~div").fadeOut(function() {
			$(this).remove();
		});
		event.preventDefault();
	}
});
$("footer~div td>a+a").live("click", function(event) {
	(($table = ($this = $(this)).closest("table")).find("th").length > 1) ? $table.find("tr>:nth-child(" + ($this.closest("td").index() + 1) + ")").remove() : $("footer~div").click();
	event.preventDefault();
});

