$(document).ready(function()
{
	/* adds logo to top-right
	 */
	$('#sidebar').prepend('<h1>QWF</h1>');
	
	adjustHeight();
	
	$('.container').dropShadow({opacity: 0.4});
	
	/* Obfuscated email addresses
	 */
	$('.Obfuscated').each(deObfuscateEmail);
	
	

	/* open outside links in new tab/window
	 */
	$('a[href^="http"]')
		.not('[href*=' + window.location.hostname + ']')
		.attr('target', '_new')
		.each(function()
			{
				if ($(this).find('img'))
				{
					$(this).css('padding', '0').css('background', 'none');
				}
			});;
		
	/* 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="carte blanche"></a>');
	
	$('#search').focus(function() { $(this).val(''); });
});



$('#cancel').click(function()
{
	$('#form_div').fadeOut().remove();
	$('#profile_div').fadeIn();
});


/**
 * 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(new RegExp(' DOT ', 'g'), '.')
		.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>'));
}

/**
 * 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 adjustHeight()
{
	var win_h = $(window).height();
	var banner_h = $('#banner').outerHeight();
	var content_h = $('#content').outerHeight();
	var footer_h = $('#footer').outerHeight();
	
	var diff = win_h - (banner_h + content_h);

	if (diff > 0)
	{
		$('#content').css('height', content_h + diff - footer_h);
	}
}


/**
 * @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);
   });
};

