var isIE = false /*@cc_on || true @*/;
var isIE6 = isIE /*@cc_on && !!(!window.XMLHttpRequest && document.compatMode) @*/; 

$(function()
{	
	/* obfuscated email addresses
	 */
	setPageContentHeight();
	
	/* obfuscated email addresses
	 */
	$('.Obfuscated').each(deObfuscateEmail);
	
	/* alternate how the side images are floated
	 */
	 $('.Schedule:even').each(function(){
			 $(this).contents('img').css('float', 'left').css('margin-left', '0');
	 });
	
	/* adjust blockquote quote images
	 */
	$('blockquote').each(i18nBlockquote);
	
	/* attach captions to select images
	 */
	$('.AltCaptioned').each(addCaption);
});

/**
 * Adjust the height of the content div to put the footer at the
 * bottom of the window
 */
function setPageContentHeight()
{
	var doc_h = $(document).height();
	var win_h = $(window).height();
	
	/* this won't work properly if the stylesheet has a rule to
	 * force HTML to have min-height: 100.1% (to force a scrollbar for every page)
	 */
	if (doc_h <= win_h)
	{
		var banner_h = $('#banner').outerHeight({ margin: true });
		var footer_h = $('#footer').outerHeight({ margin: true });
		
		/* have to subtract the content padding
		 */
		var content_padding = parseInt($('#content').css('padding-top'))
			+ parseInt($('#content').css('padding-bottom'));
		
		/* adjust content height
		 */
		$('#content').height(doc_h - (banner_h + footer_h + content_padding));
	}
}



/**
 * De-obfuscate printed email addresses which are of the type:
 *
 * <span class="Obfuscated">someone AT gmail DOT com<span>
 *
 * The class is only necessary to find the emails to pass here.
 *
 * @author brian ally, zijn digital
 **/
function deObfuscateEmail(i)
{
	var title = 'contact by email';
	var search = [' AT ', ' DOT '];
	var replace = ['@', '.'];
	var replaced_text = '';

	/* get the text
	 */
	var content = $(this).text();

	if (content)
	{
		/* search & replace text
		 */
		for (var j = 0; j < search.length; j++)
		{
			var reg_exp = new RegExp(search[j], 'g');
			content = content.replace(reg_exp, replace[j]);
		}
		 
		/* create anchor node
		 */
		var link = document.createElement('a');
		link.setAttribute('title', title);
		link.setAttribute('href', 'mailto:' + content);
		
		/* create new text node and append to anchor
		 */
		var text_node = document.createTextNode(content);
		
		link.appendChild(text_node);
		
		/* swap in anchor for text
		 */
		$(this).replaceWith(link);
	}
}

/**
 * Swap out quote images for French versions
 */
function i18nBlockquote(i)
{
	//if ($.browser.msie && $.browser.version < 7) return;
	if (isIE6) return;
	if ($(this).attr('lang') == 'fr')
	{
		$(this).addClass('BlockquoteFRClose');
		$(this).contents('p:first-child').addClass('BlockquoteFROpen');
	}
}

/**
 * Wraps an image with a div and inserts a caption based on
 * the image's alt attribute
 */
function addCaption(i)
{
	var width = $(this).width();
	var float_style = $(this).css('float');
	var margin = $(this).css('margin');
	
	/* create container and assign image width along with
	 * img float and margin style
	 */
	$(this).css('float', 'none').css('margin', 0).wrap('<div class="CaptionBox"></div>');
		
	$(this).parent('div.CaptionBox')
		.css('width', width)
		.css('float', float_style)
		.css('margin', margin);
		
	$(this).parent('div.CaptionBox').append('<div class="Caption">'+$(this).attr('alt')+'</div>');
}

