﻿var ww = ww || {};
ww.hoverAlt = function () {
	var title = $("<div id=\"title\">test</div>");
	$(".list").append(title.hide());

	$(".list p.img").hover(function () {
		var it = $(this);
		var pos = it.offset();
		title.css("top", pos.top + it.height() + "px").css("left", pos.left + "px").html(it.find("img").attr("alt")).delay(120).fadeIn(150);
	}, function () { title.clearQueue().hide() });
}
ww.animateSearchBox = function () {
	$("header input#q").focusin(function () {
		$(this).select().animate({ width: "300px" }, 100);
	}).focusout(function () {
		$(this).animate({ width: "150px" }, 500);
	});
}
ww.setPostNav = function (ctx) {
	$.post("/handlers/updatepostnav.ashx", { ctx: ctx });
}
ww.imgPositions = [];
ww.setupPost = function () {
	if (window.navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) return;

	$("p.img img,p.img iframe").load(ww.updateImgNav);
	$("p.img:first img").load(function () {
		var notice = "<div class=\"notice\">Use &larr; and &rarr; keys to navigate through photos</div>";
		$(this).parent().css("position", "relative").append(notice);
	})

	var page = $(document);
	page.keyup(function (e) {
		ww.hideNavNotice();
		var src = $(e.target)
		if (src.is("input") || src.is("textarea")) return;
		e.preventDefault();

		if (e.which == 39) {
			var current = page.scrollTop() + 1 + 20;
			for (var i = 0; i < ww.imgPositions.length; ++i) {
				if (ww.imgPositions[i] > current) {
					$("html,body").animate({ scrollTop: ww.imgPositions[i] - 20 }, 700);
					break;
				}
			}
		}
		else if (e.which == 37) {
			var current = page.scrollTop();
			for (var i = ww.imgPositions.length - 1; i >= 0; --i) {
				if (ww.imgPositions[i] < current) {
					$("html,body").animate({ scrollTop: ww.imgPositions[i] - 20 }, 700);
					break;
				}
			}
		}
	})
	page.scroll(ww.hideNavNotice);
	page.click(ww.hideNavNotice);
}
ww.hideNavNotice = function () {
	$("p.img div.notice").fadeOut(500);
}
ww.updateImgNav = function () {
	ww.imgPositions = [];
	var images = $("p.img, #pager");
	images.each(function (index, value) { ww.imgPositions.push($(value).offset().top); })
}
ww.animateSearchBox();
$(".list p.img").click(function () {
	window.location = $(this).find("a").attr("href");
})
