/* AE Image Resize */ (function(d){d.fn.aeImageResize=function(a){var i=0,j=d.browser.msie&&6==~~d.browser.version;if(!a.height&&!a.width)return this;if(a.height&&a.width)i=a.width/a.height;return this.one("load",function(){this.removeAttribute("height");this.removeAttribute("width");this.style.height=this.style.width="";var e=this.height,f=this.width,g=f/e,b=a.height,c=a.width,h=i;h||(h=b?g+1:g-1);if(b&&e>b||c&&f>c){if(g>h)b=~~(e/f*c);else c=~~(f/e*b);this.height=b;this.width=c}}).each(function(){if(this.complete||j)d(this).trigger("load")})}})(jQuery);
/* Random # Generator*/ jQuery.extend({ random: function(X) { return Math.floor(X * (Math.random() % 1)); }, randomBetween: function(MinV, MaxV) { return MinV + jQuery.random(MaxV - MinV + 1); } });

/* Document */
$(document).ready(function() {

	/* @body */
	$('body').wrapInner('<div id="body_wrapper">');
	
	/* @nav */
	$('nav#nav nav li:contains("Home")').replaceWith('<li class="pictos"><a href="javascript:null(0)">H</a></li>');
	$('nav#nav nav li:contains("Archives")').replaceWith('<li class="pictos"><a href="javascript:null(0)">t</a></li>');
	$('nav#nav nav li:contains("RSS")').replaceWith('<li class="pictos"><a href="javascript:null(0)">f</a></li>');
	$('nav#nav nav li:contains("Search")').replaceWith('<li class="pictos"><a href="javascript:null(0)" id="search_toggle">s</a></li>');
	$('nav#nav nav form#site_search div#search_submit:contains("Search")').replaceWith('<div id="search_submit" class="pictos"s>s</div>');
	$('#search_toggle').toggle(
		function() { $('form#site_search').fadeIn('slow'); return false; },
		function() { $('form#site_search').fadeOut('fast'); return false; }
	);

	/* @post_text links */
	$('article.post_text a').filter(function() { return this.hostname && this.hostname !== location.hostname; } ).after('<span class="external_link pictos">R</span>');
	$('article.post_text a').filter(function() { return this.hostname && this.hostname !== location.hostname; } ).attr('target', '_blank');

	/* @post_text media labels */
	$('article.post_text iframe, article.post_text embed').addClass('media');
	$('article.post_text img').addClass('media_img');

	/* @post_text media resize */	
	$(function() { $('img.media_img').aeImageResize({width:506}); });
	$('.media').each(function () {
		var if_lim = 506;
		var if_wid = $(this).attr('width');
		var if_hgt = $(this).attr('height');
		if(if_wid > if_hgt) { var new_if_wid = if_lim; var new_if_hgt = Math.ceil((new_if_wid / if_wid) * if_hgt); }
		else { var new_if_hgt = if_lim; var new_if_wid = Math.ceil((new_if_hgt / if_hgt) * if_wid); }
		$('iframe, embed').css('width', new_if_wid).css('height', new_if_hgt);
	});

	/* Lighter Box */
	$('img.media_img').each(function () {
		var img_src = $(this).attr('src');
		var img_rand = $.random(999999999);
		$(this).wrap('<span class="lb_link" id="' + img_rand + '">');
		$(this).parent().after('<div class="lb_img" id="lb_' + img_rand + '"><img id="lb_img_' + img_rand + '" src="' + img_src + '"></div>');
	});
	var imgControl = function imgControl() {
		$('div.lb_img img').each(function () {
			var lb_img_id = '#' + $(this).attr('id');
			/* image size */
			var max_height = $(window).height() - 50;
			var max_width = $(window).width() - 50;
			$(function() { $(lb_img_id).aeImageResize({width:max_width, height:max_height}); });
			/* image position */
			var img_y = ($(this).attr('height') + 14) * -0.5;
			var img_x = ($(this).attr('width') + 14) * -0.5;
			$(this).css('margin-top', img_y).css('margin-left', img_x);
		});
	}
	$(window).load(function () {
		imgControl();
	});
	$(window).resize(function() {
		imgControl();
	});
	$('span.lb_link').click(function () {
		var lb_id = '#lb_' + $(this).attr('id');
		/* open */
		$(lb_id).fadeIn(200);
		/* close */
		$('div.lb_img').click(function () { $(lb_id).fadeOut(400); });
		$(window).scroll(function() { $(lb_id).fadeOut(400); });
	});

	/* media captions */
	$('article.post_text .media, article.post_text .media_img').each(function () {
		if( $(this).attr('alt') != undefined ) {
			var img_description = $(this).attr('alt');
			$(this).after('<div class="media_caption">' + img_description + '</div>');
		}
	});

	/* footnotes */
	$('article.post_text article span.footnote').each(function () {
		var footnote_id = $(this).html();
		var article_id = $(this).parent().parent().parent().attr('id');
		var footnote_link = article_id + 'x' + footnote_id;
		$(this).wrap('<a class="footnote_link" id="' + 'footnote_marker_' + footnote_link + '" href="#' + 'footnote_' + footnote_link + '">');
	});
	$('article.post_text article section.footnote_content p span.footnote_num').each(function () {
		var footnote_id = $(this).html();
		var article_id = $(this).parent().parent().parent().parent().attr('id');
		var footnote_link = article_id + 'x' + footnote_id;
		$(this).wrap('<a class="footnote_link" id="' + 'footnote_' + footnote_link + '" href="#' + 'footnote_marker_' + footnote_link + '">');
	});
	$('article.post_text article section.footnote_content').before('<div class="footnote_hr"></div>');
	
});
