$(function(){

	$('blockquote').each(function(){
		var $p = $('p', this);
		$p.eq(0).prepend('<span class="before">&nbsp;</span>').end();
		$p.eq($p.length - 2).append('<span class="after">&nbsp;</span>').end();
	});
	
	if($.browser.msie)
		$('body').append('<span class="shim">&nbsp;</span>');
	
});

$(function(){
	if(!$('#pages-home').is('html')) return;
	// Wrap the quotes in a fixed height div,
	// so that the scrollbar appearing/disappearing 
	// doesn't make the content jump.
	var $quotes = $('#main').append('<div id="quotes"></div>').find('#quotes');
	var h = 0;	
	$('#main blockquote').each(function(){
		var $this = $(this);
		h = Math.max($this.height(), h);
		$quotes.append($this.remove());		
	});
	$quotes.height(h);	
	$quotes.find('blockquote').rotate();	
});

$(function(){
	if(!$('#pages-edit').is('html')) return;
	
	$('#add-book-button').bind('click', function(e) {
		$('#add-book-form').show({ height: 'slow' });		
		e.preventDefault();
	});
	
	$('#add-book-commit').bind('click', function(e){
		var s = "\n\n[![%title](%image)](%link)\n[%title](%link)\n**%author** %text\n- - -\n";
		s = s.replace(/\%title/g, $('#add-book-title')[0].value);
		s = s.replace(/\%author/g, $('#add-book-author')[0].value);
		s = s.replace(/\%link/g, $('#add-book-link')[0].value);
		s = s.replace(/\%image/g, $('#add-book-image')[0].value);
		s = s.replace(/\%text/g, $('#add-book-text')[0].value);
		$('#page_body').insertAtCursor(s);
		$('#add-book-form').hide({ height: 'slow' });
		return false;
	});
	
});

$(window).bind('load', function(){
	if(!$.browser.safari) return;	
	var $footer = $('#footer');
	$footer.css({
		bottom: 'auto',
		top: ($('body')[0].offsetHeight - $footer[0].offsetHeight) + 'px'
	});
});
