$(document).ready(function()
{
	setFooter();
	
	/* Obfuscated email addresses
	 */
	$('.Obfuscated').each(deObfuscateEmail);
	
	$('hr').replaceWith('<div class="HR">~&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;~&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;~</div>');

	/* shadowy goodness
	 */
	$('dl.Gallery dt img').load(applyDropShadows);
	
	fixWidows();

	$('.GalleryText p:first, #content p:first').attr('id', 'firstPara');
	$('.GalleryText > p:last:not(.Footnote), #content > p:last:not(.Footnote)').attr('id', 'lastPara');
	
	/* open outside links in new tab/window
	 */
	$('a[href^="http"]')
		.not('[href*=' + window.location.hostname + ']')     
		.attr('target', '_new');
		
	/* Put a link back to home around the logo image.
	 * Because this image is actually a level-1 header with image
	 * replacement, placing an anchor around it manually would cause
	 * the page to invalidate. Placing it inside the header would also
	 * not be ok. This function wraps a link around it after the page has
	 * loaded.
	 **/
	$('#logo').wrap('<a href="/" title="Soysauce Comics"></a>');
});

function fixWidows()
{
	var pall, pa, pb;
	
	$('#content > p:not(.Stanza), #bio p').each(function()
	{
		pall = $(this).html();
		pa = pall.slice(0, pall.lastIndexOf(' '));
		pb = '&nbsp;' + pall.slice(pall.lastIndexOf(' ')+1);
		$(this).html(pa + pb);
	});	
}


/**
 * De-obfuscate printed email addresses which are of the type:
 *
 * <span class="Obfuscated" title="some title">
 * some link text [ someone AT gmail DOT com ]
 * <span>
 *
 * The braces around the address part are hard-wired here; probably shouldn't be
 *
 * @author brian ally, brian | zijn-digital | com
 **/
function deObfuscateEmail(i)
{	
	var content = $(this).text();
	
	/* grab the part inside the braces, swap out placeholders, and trim
	 */
	var obfuscated = content.match(/\[(.*)\]/);
	var address = obfuscated[1]
		.replace(' AT ', '@')
		.replace(' DOT ', '.')
		.replace(/^\s+|\s+$/g, '');
		
	/* get everything before the braces and trim
	 */
	var text = content.match(/.?[^[]+/);
	
	text = (text[0] != content)
		? text[0].replace(/^\s+|\s+$/g, '')
		: address;	// if there's no text part, use the address
	
		
	var title = $(this).attr('title') || '';
	
	$(this).replaceWith($('<a href="mailto:' + address + '" title="' + title + '">' + text + '</a>'));
}

/**
 * Zac Wasielewski's osDropShadow updated for jQuery
 *
 * Note that this should only be called on images that
 * have already fully loaded.
 *
 * @author brian ally, brian | zijn-digital | com
 */
function applyDropShadows()
{
	$(this).wrap('<div class="osDropShadow"></div>')
		.wrap('<div></div>')
		.wrap('<div></div>')
		.css('margin', '0');
}



/**
 * Checks the height of the content plus height of banner and places 
 * the footer at either the bottom of the page or immediately following
 * content. Until browsers can consistently keep a footer at the bottom of
 * the page when content is too short, this'll have to do.
 *
 * @param void
 * @returns void
 **/
function setFooter()
{
	var agent = navigator.userAgent.toLowerCase();
	var platform = navigator.platform.toLowerCa
	
	if (document.getElementById)
	{
		var footer = document.getElementById('footer');
		
		if (footer)
		{
			var banner = document.getElementById('banner');
			var main = document.getElementById('content');
			var contentHeight, windowHeight, bannerHeight;

			if (document.documentElement)
			{
				windowHeight = document.body.clientHeight;
			 	bannerHeight = banner.offsetHeight;	
			 	contentHeight = main.offsetHeight;
			}
			else
			{
			 	windowHeight = window.innerHeight;
				bannerHeight = document.defaultView.getComputedStyle(banner, '').getPropertyValue('height');
				bannerHeight = parseInt(eval(bannerHeight.substring(0, bannerHeight.indexOf('p'))));
				contentHeight = document.defaultView.getComputedStyle(main, '').getPropertyValue('height');
				contentHeight = parseInt(eval(contentHeight.substring(0, contentHeight.indexOf('p'))));
			}
		 
			if ( (bannerHeight + contentHeight) < (windowHeight - 20) )
			{
				footer.style.position = "absolute";
				footer.style.top = (windowHeight - 20) + "px";
			}
			// for winIE 5 and up
			else if ( (/msie ((5\.[056789])|([6789]))/.test(agent)) && (platform == 'win32') )
			{
				footer.style.position = "absolute";
				footer.style.top = (bannerHeight + contentHeight) + "px";
			}
			else
			{
				footer.style.position = "static";
			}
			
			footer.style.display = 'block';
		}
	}
}




/**
 * @author Alexandre Magno
 * @desc Center a element with jQuery
 * @version 1.0
 * @example
 * $("element").center({
 *
 * 		vertical: true,
 *      horizontal: true
 *
 * });
 * @obs With no arguments, the default is above
 * @license free
 * @param bool vertical, bool horizontal
 * @contribution Paulo Radichi
 *
 */
jQuery.fn.center = function(params) {

		var options = {

			vertical: true,
			horizontal: true,
			top: 0

		}
		op = jQuery.extend(options, params);

   return this.each(function(){

		//initializing variables
		var $self = jQuery(this);
		//get the dimensions using dimensions plugin
		var width = $self.width();
		var height = $self.height();
		//get the paddings
		var paddingTop = parseInt($self.css("padding-top"));
		var paddingBottom = parseInt($self.css("padding-bottom"));
		//get the borders
		var borderTop = parseInt($self.css("border-top-width"));
		var borderBottom = parseInt($self.css("border-bottom-width"));
		//get the media of padding and borders
		var mediaBorder = (borderTop+borderBottom)/2;
		var mediaPadding = (paddingTop+paddingBottom)/2;
		//get the type of positioning
		var positionType = $self.parent().css("position");
		// get the half minus of width and height
		var halfWidth = (width/2)*(-1);
		var halfHeight = ((height/2)*(-1))-mediaPadding-mediaBorder;
		
		// initializing the css properties
		var cssProp = {
			position: 'absolute'
		};

		if(op.vertical) {
			cssProp.height = height;
			cssProp.top = '50%';
			cssProp.marginTop = halfHeight;
		}
		if(op.top) {
			cssProp.height = height;
			cssProp.top = op.top;
			cssProp.marginTop = halfHeight;
		}
		if(op.horizontal) {
			cssProp.width = width;
			cssProp.left = '50%';
			cssProp.marginLeft = halfWidth;
		}
		//check the current position
		if (positionType == 'static') {
			$self.parent().css("position","relative");
		}
		//aplying the css
		$self.css(cssProp);
   });
};

