if($.browser.mozilla)
{
	$("body").addClass("FontsLoading");
	$(window).load(function() {
		//$("body").css('display', 'none').removeClass('FontsLoading').fadeIn('slow');
		$("body").removeClass('FontsLoading').fadeIn('slow');
	});
};

$(function()
{

	/* Toggle debug
	 */
	$(".cake-sql-log").hide().before('<a href="#" class="toggle-cake-sql-log">debug</a>');
	$(".toggle-cake-sql-log").click( function()
	{
		$(".cake-sql-log").toggle();
		return false;
	});
	
	initFlashMsg();
	
	/* Obfuscated email addresses. See bootstrap.php
	 */
	$('.Obfuscated').each(deObfuscateEmail);
	
	//$('a.BtnLink').each(buttonLink);
	
	stripeRows('#content tbody');
	
	initThumbs();
	
	setBG();
	
	initArchiveLinks();
	
	/* open outside links in new tab/window
	 */
	$('a[href^="http"], a[href$="pdf"], a[href$="ppt"]')
		.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.
	 **/
	$('header h1').wrap('<a href="/" title="home"></a>');
	
	if ($.loading)
	{
		$.loading.element = '#loading';
		$.loading.align = 'center';
		//$.loading.mask = true;
		//$.loading.maskCss = {background:'#000', opacity:.6}
	}
	
	initSearch();
	//$(window).load(setFooter).scroll(setFooter).resize(setFooter);	
});

/*
function setFooter(ev)
{
	var window_h = $(window).height();
	var header_h = $('header').outerHeight(true);
	var content_h = $('#content').outerHeight(true);
	var footer_h = $('footer').outerHeight(true);
	
	log('window height: ' + window_h);
	log('header height: ' + header_h);
	log('content height: ' + content_h);
	log('footer height: ' + footer_h);
	
	if (window_h > header_h + content_h + footer_h)
	{
		var pad = window_h - (header_h + footer_h);
		$('#content').height(pad);
		
		log('new content height: ' + pad);
	}
}
*/


function initArchiveLinks()
{
	$('#archive_list > ul > li > a').click(function(e)
	{
		e.preventDefault();
		$('#archive_list > ul > li > ul:visible').slideUp('fast');
		$(this).next('ul').slideDown('fast');
	});
	
	var path = window.location.pathname;
	
	$('#archive_list a[href = '+path+']').css('color', 'green').closest('ul').show();
	
}

function initThumbs()
{
	if ($('div.Thumb').length)
	{
		$('div.Thumb').equalHeights();
	
		shadowImages("div.Thumb a:has(img)");
		
		/* remove existing hidden divs if content loaded w/ AJAX
		 */
		$('div[class^=pp_], .ppt').remove();
		
		$("div.Thumb a:has(img)").prettyPhoto({
			theme:'light_rounded',
			caption_parent: 'div.Thumb',
			caption_element: 'div.Caption',
			overlay_gallery: true
		});
		/* light_rounded / dark_rounded / light_square / dark_square / facebook */
	}
}


/**
 * Hide search prompt when user focuses input
 *
 * @param	void
 * @return	void
 */
function initSearch()
{
	$('#search_q')
		.focus(function() { $(this).val(''); })
		.blur(function() { if ($(this).val() == '') $(this).val('search'); });
}


/**
 * Wrap elements to create drop shadow effect
 *
 * @param	void
 *
 *<div class="shadow-one">
 *		<div class="corner-a"></div>
 *		<div class="corner-b"></div>
 *		<div class="shadow-two">
 *			<div class="shadow-three">
 *				<div class="shadow-four">
 *					ELEMENT
 *	</div></div></div></div> 
*/
function shadowImages(selector)
{
	/* save IE6 from itself
	 */
	if ($.browser.msie && $.browser.version.substr(0,1) < 7)
	{
		return;
	}
	
	$(selector).each(function()
	{
		var $parent = $(this).closest('div');
		var p_height = $parent.outerHeight(true);
		
		var img_h = $(this).find('img').height();

		$(this).wrap('<div class="shadow-one"><div class="shadow-two"><div class="shadow-three"><div class="shadow-four"></div></div></div></div>');
	
		$(this).closest('div.shadow-one').each(function()
		{
			var img_w = $(this).find('img').width();
			
			$(this).css('width', img_w + 'px')
				// what was this for??
				//.css('left', ($parent.width()/2) - (img_w/2))
				;
			
			$(this).prepend('<div class="corner-a"></div><div class="corner-b"></div>');
			$parent.css('height', p_height + 'px');
		});
	});
}


/**
 * Convert certain links to appear as buttons
 *
 */
function buttonLink()
{
	return $(this).html('<span><span>' + $(this).text() + '</span></span>')
		.addClass('Button')
		.removeClass('BtnLink');
}

function initFlashMsg()
{
	$('a.HideFlash').live(
		'click',
		function(ev)
		{
			$('#flash_msg').hide();
			ev.preventDefault();
		}
	);
	showFlashMsg();
}
/**
 * Display flash messages
 */
function showFlashMsg()
{
	$('#flash_msg').center({vertical: false, inside: '#content', transition: 1000})

	/* don't fade out errors
	 */
	if (!$('#flash_msg').hasClass('error-message'))
	{
		$('#flash_msg').fadeOut(8000);
	}
}

function flashMsg(msg)
{
	$(msg).appendTo($('body'));
	showFlashMsg();
}

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

function setBG()
{
	/* save IE6 from itself
	 */
	if ($.browser.msie && $.browser.version.substr(0,1) < 7)
	{
		return;
	}
	
	$('#container:not(.CropLayout)').wrap('<div id="container_shadow"></div>');
	
	adjustBackground();
	$(window).resize(adjustBackground);
}

/**
 * Attempt to centre tiling background image
 */
function adjustBackground()
{
	/* width of background image
	 */
	var bg_w = 200;
	var content_w = 950;
	var window_w = $(window).width();
	var side_w = (window_w - content_w) / 2;
	
	$('body').css('background-position', '' + (side_w % bg_w) + 'px 0');
}


/**
 * 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') || '';
	
	/* subject may be concatenated with title
	 */
	if (title.indexOf('|') >= 0)
	{
		var parts = title.split('|');
		title = parts[0];
		address += '?subject=' + parts[1];
	}
	
	$(this).replaceWith($('<a href="mailto:' + address + '" title="' + title + '">' + text + '</a>'));
}

/* http://plugins.jquery.com/project/autocenter
 */
(function($){
     $.fn.extend({
          center: function (options) {
               var options =  $.extend({ // Default values
                    inside:window, // element, center into window
                    transition: 0, // millisecond, transition time
                    minX:0, // pixel, minimum left element value
                    minY:0, // pixel, minimum top element value
                    vertical:true, // booleen, center vertical
                    withScrolling:true, // booleen, take care of element inside scrollTop when minX < 0 and window is small or when window is big
                    horizontal:true // booleen, center horizontal
               }, options);
               return this.each(function() {
                    var props = {position:'absolute'};
                    if (options.vertical) {
                         var top = ($(options.inside).height() - $(this).outerHeight()) / 2;
                         if (options.withScrolling) top += $(options.inside).scrollTop() || 0;
                         top = (top > options.minY ? top : options.minY);
                         $.extend(props, {top: top+'px'});
                    }
                    if (options.horizontal) {
                          var left = ($(options.inside).width() - $(this).outerWidth()) / 2;
                          if (options.withScrolling) left += $(options.inside).scrollLeft() || 0;
                          left = (left > options.minX ? left : options.minX);
                          $.extend(props, {left: left+'px'});
                    }
                    if (options.transition > 0) $(this).animate(props, options.transition);
                    else $(this).css(props);
                    
                    return $(this);
               });
          }
     });
})(jQuery);

function log()
{
	if (window.console && window.console.log)
	{
		if (arguments.length)
		{
			console.log("%o", arguments);
			//console.debug.apply( console, arguments );
		}
	}
	else
	{
		alert( [].join.apply( arguments, [' '] ) );
	}
}

jQuery.fn.log = function ()
{
	if (window.console && window.console.log)
	{
		if (arguments.length)
		{
			//console.log("%s: %o", msg, this);
			console.log("%o args: %o", this, arguments);
		}
		else
		{
			console.log(this);
		}
	}
	return this;
};


/**
* jQuery Plugin Function
*
* Make all elements same height according to “tallest” one found :)
* Original code : http://codesnipp.it/code/441
*/

jQuery.fn.equalHeights = function()
{
	return this.height(
		Math.max.apply(
			null,
			this.map(function() {
				return jQuery(this).height()
			}).get()
		)
	);
};

