$(function()
{	
	$('body').addClass('JS');
	
	showFlashMsg();

	//if (!$('#nav_secondary > div').length) { $('#nav_secondary').remove(); }
	
	/* avoid IE completely
	 */
	if (!$.browser.msie && !$.browser.opera)
	{
		$('#login_prompt, #front_page_callout, #login, #forgot_password_form, .SideBar').dropShadow(
			{opacity: 0.2, blur:3, left:3, top:3}
		);
		
	}

	$('.dropShadow').corner('10px');
	$('#login_prompt').corner('10px');
	$('#front_page_callout').corner('10px');
	$('#login').corner('10px');
	$('#forgot_password_form').corner('10px');
	//$('.SideBar').corner('10px');
	$('#wrapper').corner('10px');
	
	$('#scroller').cycle({
		//fx: 'scrollUp',
		fx: 'fade',
		timeout: 6000,
		delay: -2000,
		pause: true
	});
	
	/* Obfuscated email addresses. See bootstrap.php
	 */
	$('.Obfuscated').each(deObfuscateEmail);
	
	/* can be either an A or SPAN
	 */
	$('#nav_secondary tbody td').children(':last').css('border-bottom', '1px solid #b0d3e2');

	/* click the link displays "forgot password" form
	 */
	$('#forgot_password_trigger').click(function()
	{
		$('#forgot_password').slideDown();
		return false;
	});
	
	/* clicking the header displays section content
	 */
	$('#past_events h2').click(function()
	{
		$(this).next('ul').slideToggle();
	});
	
	/* open relevant section content
	 */
	if (window.location.pathname.match(/events/) && window.location.hash)
	{
		$(window.location.hash).next('ul').slideToggle();
	}
	

	/* open outside links in new tab/window
	 */
	$('a[href^="http"], a[href$="pdf"], a[href$="ppt"], a.SearchLink')
		.not('[href*=' + window.location.hostname + ']')
		.attr('target', '_new');
		
	/* undo 'external link' icon for anchors wrapping an image and
	 * links in the front page scroller box
	 */
	$('a[href^="http"] img').parent('a')
		.css('background', 'none').css('padding-right', '0');
	$('#scroller a')
		.css('background', 'none').css('padding-right', '0');
		
		
	/* add styling for crappy browsers
	 */
	if ($.browser.msie)
	{		
		$('#footer p:not(:first)').each(function()
		{
			$(this).css('border-left-width', '2px')
				.css('border-left-style', 'solid')
				.css('border-left-color', '#FFF');
		});
	}
	
	
	/* Events archive: click the header displays section content
	 */
	$('#presentations div').hide();
	$('#presentations h2').css('cursor', 'pointer').click(function()
	{
		$(this).next('div').slideToggle();
	});

	
	$.loading.element = '#indicator';
	$.loading.align = 'center';
	
	if (typeof(tb_init) != 'undefined') tb_init('a.MemberInfoTrigger');
	
	
	//alert($('#wrapper').height());
	
	if ($('#content').height() < $('#sidebar').height())
	{
		$('#content').height($('#sidebar').height() + 10);
	}
	
});


/**
 * Callback from Thickbox opening
 */
function TB_openCallBack()
{
    DD_roundies.addRule('#TB_window', 10, true);
}

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



function flashMsg(msg, show, is_error)
{
	var msg = $('<div id="flash_msg">'+msg+'<p><a href="http://'+window.location.host+'/" title="close">dismiss</a></p></div>');
		
	if (is_error) msg.addClass('error-message');
	
	msg.appendTo($('body'));
	
	if (show) showFlashMsg();
}


/**
 * Display flash messages
 */
function showFlashMsg()
{
	//$('#flash_msg').not('.FlashMembershipExpiration').hide().appendTo('#banner').center({top: 10})
	$('#flash_msg').not('.FlashMembershipExpiration').hide().center({top: 10})
	.animate({opacity: 'show'}, 'normal', function()
	{
		/* don't fade out errors
		 */
		if (!$(this).hasClass('error-message'))
		{
			$(this).fadeOut(8000);
		}
	})
	.find('a.HideFlash').click(hideFlash);
	
	$('#flash_msg.FlashMembershipExpiration a.HideFlash').click(hideFlash);
	
	function hideFlash(ev)
	{
		$('#flash_msg').hide();
		ev.preventDefault();
	}
}

function initDatePicker()
{
	if ($.datepicker)
	{
		$('.DatePicker').datepicker(
		{
			dateFormat: 'yy-mm-dd',
			beforeShow: function(input)
			{
				$('#ui-datepicker-div').appendTo($('#TB_ajaxContent'));
			}
		});
	}
}

function stripeRows(selector)
{
	$(selector + ' tr:nth-child(even)').addClass('Odd');	
}

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