(function(window, document, $, undefined) {

	$(document).ready(function() {

		var hash = window.location.hash.replace("#", "");

		/**
		 * Mobile friendly menus
		 */

		$(window).bind("load resize", function() {
			if($(document).width() < 768) {
				$("#menu-main-navigation, #sidebar .navigation").toSelect({ className: "navigation" });
				$("#footer span").replaceWith(" ");
			}
		});

		$("a.voucher").click(function() {
			window.open(this.href, 'voucher', 'left=50,top=50,width=830,height=203,menubar=1');
			return false;
		});

		$(".link-all").each(function() {
			var link = $(this).find("a:first");
			if(link.length) {
				$(this).css({ cursor: "pointer" }).bind("click", function() {
					window.location = link.attr("href");
					return false;
				});
			}
		});

		$("#header div.brochure, #header div.online").hover(function() {
			$(this).find("a").css("text-decoration", "underline");
		}, function() {
			$(this).find("a").css("text-decoration", "none");
		});

		var setEmailRequired = (function() {
			$("<span></span>")
				.addClass("req")
				.attr("title", "Required")
				.text("*")
				.appendTo("#brochure_email_label");
		});

		if(1 == $("#brochure_format").val()) {
			setEmailRequired();
		}

		$("#brochure_format").bind("change", function() {
			if(1 == $(this).val()) {
				setEmailRequired();
			} else {
				$("#brochure_email_label span.req").remove();
			}
		});

		$(".thumbnail-frame a").lightBox({
			imageLoading:  "/wp-content/themes/appeal/images/lightbox/lightbox-ico-loading.gif",
			imageBtnClose: "/wp-content/themes/appeal/images/lightbox/lightbox-btn-close.gif",
			imageBtnPrev:  "/wp-content/themes/appeal/images/lightbox/lightbox-btn-prev.gif",
			imageBtnNext:  "/wp-content/themes/appeal/images/lightbox/lightbox-btn-next.gif",
			imageBlank:    "/wp-content/themes/appeal/images/lightbox/lightbox-blank.gif"
		});

		$("#search_query_large").placeholder({
			inactive: "#777",
			active: "#333"
		});

		$("input.pseudo").each(function() {
			var button = $("<a />")
				.addClass("more pseudo")
				.bind("click", function() {
					$(this).parents("form").submit();
					return false;
				})
				.attr("href", "#")
				.text($(this).val());
			$(this).replaceWith(button);
		});

		$("#referrer-box select").live("change", function() {

			var other = $("input[name=referrer_specify]").val(),
				selected = [];

			$("#referrer-box select").each(function() {
				selected.push($(this).val());
			});

			$.get("/lib/referrers.php", { selected: selected.join(","), other: other, ajax: "true" }, function(data) {
				$("#referrer-results").html(data);
				$("input[name=referrer_specify]").placeholder({
					preventSubmit: false
				});
			});

		});

		/* animates a slideshow image */
		var animateImage = (function(image) {
			if(!image instanceof jQuery) {
				image = $(image);
			}
			/* do animation stuff recursively. on each call to
			 * this function, check image still exists/is visible */
		});

		/* toggles the visibility of a submenu */
		var toggleSubMenuVisibility = (function() {

			var visible = $(this).siblings("ul:visible"),
				hidden = $(this).siblings("ul:hidden");

			if(visible.length) {
				visible.slideUp("fast");
				$(this).css("background-image", "url(" + templateUrl + "/images/layout/arrow-down-white.png)");
			} else if(hidden.length) {
				hidden.slideDown("fast");
				$(this).css("background-image", "url(" + templateUrl + "/images/layout/arrow-up-white.png)");
			}

			return false;

		});

		/* template for menu arrow links */
		var arrow = $("<a></a>")
			.addClass("expand-arrow")
			.text("+/-")
			.attr("href", "#");

		$("#sidebar ul.navigation li").each(function() {

			var children = $(this).find("ul");

			if(children.length) {
				if($(this).hasClass("current-menu-ancestor") || $(this).hasClass("current-menu-item") || $(this).hasClass("current-page-ancestor")) {

					/* create arrow link in "up" state */
					arrow.clone()
						.addClass("closed")
						.bind("click", toggleSubMenuVisibility)
						.appendTo($(this));

				} else {

					/* hide submenus */
					children.hide();

					/* create arrow link in "down" state" */
					arrow.clone()
						.addClass("open")
						.bind("click", toggleSubMenuVisibility)
						.appendTo($(this));

				}
			}

		});

		$("#pikame").PikaChoose({
			carousel: true,
			transition: [1],
			showCaption: false,
			speed: 15000,
			showTooltips: false,
			animationFinished: function(self) {
				/* current image can be wrapped with the jQuery object
				 * and accessed using $(self.image). can be passed to
				 * animateImage(). */
			}
		});

		$("input.placeholder").placeholder({
			active: "#000",
			inactive: "#777"
		});

		$("#faqs-accordion div.answer").hide();

		$("#faqs-accordion h2")
			.css("cursor", "pointer")
			.bind("click", function() {
				if($(this).siblings("div.answer").is(":visible")) {
					$(this).siblings("div.answer").slideUp("fast");
					window.location.hash = "";
				} else {
					$(this).parents("#faqs-accordion").find("div.answer").slideUp("fast");
					$(this).siblings("div.answer").slideDown("fast");
					window.location.hash = $(this).parent("li").attr("id");
				}
			})

		if($("#faqs-accordion").length && hash.length) {
			$("#" + hash + " div.answer").show();
		}

		$("form.brochure").submit(function() {

			var referrerSpecify = $("#brochure_referrer_specify");

			if(referrerSpecify.length) {

				var value = referrerSpecify.val(),
					label = $("label[for='brochure_referrer_specify']").text();

				if(!value.length || value == label) {
					alert("Please specify how you heard about us");
					referrerSpecify.focus();
					return false;
				}

			}

		});

	});

}(window, document, window.jQuery));
