/* jQuery functions for Esquire Theme by Matthew Buchanan v1.20 */

$.jqwidont.auto(false);

function webkitSearch() {
	var defaultValue = "Search..."; // Default Value
	if ($.browser.safari) {
		var sf = document.getElementById("searchfield");
		sf.setAttribute("type", "search");
		sf.setAttribute("autosave", "saved.data");
		sf.setAttribute("results", "10");
		sf.setAttribute("placeholder", defaultValue);
		$("#searchfield").addClass("webkit");
		$("#search .formbutton").hide();
	} else {
		$("#searchfield").val(defaultValue);
		$("#searchfield").css("color", "#888");
		$("#searchfield").focus( function() {
			if ($(this).val() == defaultValue) $(this).val("");
			$(this).css("color", "#333");
		});
		$("#searchfield").blur( function() {
			if ($(this).val() == "") {
				$(this).val(defaultValue);
				$(this).css("color", "#888");
			}
		});
	}
}

function handleImage(ref) {
	if( ref.width() < 400 )
		ref.css({"padding-left":(560-ref.width())/2+"px","padding-right":(560-ref.width())/2+"px"});
	if( ref.height() < 300 )
		ref.parent().parent().parent().addClass("short");
}

$(function() {
	// Handle sidebar nav tooltips
	$(".buttons a").hover(function() {
		$("#buttontext").html($(this).attr("title"));
	}, function() {
		$("#buttontext").html("&nbsp;");
	});

	// Toggle search field
	$("#showsearch").click(function(ev){
		ev.preventDefault();
		$("#search").slideToggle();
	});

	// Handle sidebar avatar enlargement
	$("#avatars").each(function() {
		var avatarSize = 41;
		var avatarsPerRow = 22;
		var avatarCount = $("#avatars a").size();
		var rowCount = Math.ceil(avatarCount / avatarsPerRow);
		var avatarsTop = $("#avatars").position().top;
		var avatarsOffset = Math.max(Math.min((2 - rowCount) * avatarSize, 0), -(avatarsTop-15));
		var avatarsWidth = (avatarCount < avatarsPerRow) ? Math.max(avatarCount*avatarSize, avatarCount*4) : 902;
		var avatarsHeight = (avatarCount < 5) ? avatarSize : avatarSize*2;

		$("#avatars").height(avatarsHeight);
		if( avatarCount < 9 ) $("#showavatars").css("visibility", "hidden");

		$("#avatars a").hover(function() {
			$(this).find("img").animate({opacity: 0.5}, 200);
		}, function() {
			$(this).find("img").animate({opacity: 1}, 200);
		});
		$("#showavatars").click(function(ev) {
			ev.preventDefault();
			if( $("#showavatars").hasClass("hide") )
				$("#avatars").animate({ width: "164px", height: avatarsHeight, marginTop: 0, padding: "0 0 0 0" })
					.next().removeClass("hide");
			else
				$("#avatars").animate({ width: avatarsWidth+"px", height: rowCount*avatarSize, marginTop: avatarsOffset, padding: "5px 5px 5px 0" })
					.next().addClass("hide");
		});
	});

	// Handle sidebar social icons
	$("#social a").hover(function() {
		$(this).prepend("<div></div>").find("div").addClass("hit").css({opacity:0}).animate({opacity: 0.5}, 200);
	}, function() {
		$(this).find("div").animate({opacity: 0}, 200, function() {$(this).remove();});
	});

	// Move header to correct position in page markup, to sit beside the datebox
	var datebox = $("#posts .post:first .datebox");
	$("#header").remove().insertAfter(datebox).show();

	// Replace missing paragraph tags if no block-level elements in content block
	$(".content:not(:has(p,dl,div,noscript,blockquote,form,hr,table,fieldset,address,ol,ul,h1,h2,h3,h4,h5,h6))").wrapInner("<p></p>");

	// Allow ampersands to be styled in headings
	$("h1").each(function() {
		var hed = $(this).html();
		hed = hed.replace(/&amp;/gi, "<span class='amp'>&amp;</span>");
		$(this).html(hed);
	});

	// Handle frame adjustments on photo posts
	$(".photo .frame img").load(function() {
		handleImage($(this));
	});

	// Handle permalink date toggling
	$(".permalink a").hover(function() {
		ob = $(this).find("span");
		timeago = ob.text();
		ob.text(ob.attr("rel"));
	}, function() {
		ob.text(timeago);
	});

	// Handle hover states for reblog notes
	$(".via").hover(function() {
		$(this).addClass("via-hover");
	}, function() {
		$(this).removeClass("via-hover");
	});

	// Hide last slash in tag list
	$(".tags span:last-child").hide();

	// Fix widows in selected elements
	$("#sidebar p.desc,.content p,.content li,.content blockquote,.text h1,.chat h1").widont();

	// Fix for weird browser behaviour on dropcap
	if (!$.browser.safari()) {
		$(".text .content > p:first-child").addClass("fixcap");
	}

	// Fix font issue under windows by removing Adobe Caslon Pro
	if ($.browser.mac()) $("body").addClass("mac");

	// Use custom search field in webkit browsers
	webkitSearch();
});

$(window).load(function() {
	// Process blockquotes
	WASABICUBE.process(".wczz", "1.5em", "1em");

	$(".photo .frame img").each(function() {
		handleImage($(this));
	});
});