(function($){ 

	//Finding min and max values in array from http://snippets.dzone.com/posts/show/769
	Array.prototype.min = function(){ return Math.min.apply({},this) };
	Array.prototype.max = function(){ return Math.max.apply({},this) };

	$.fn.masonry = function() {
		this.each(function() {
			
			var wall = $(this);
		
			if ( wall.children().length > 0 ) { // check if the element has anything in it
				
				if( wall.children('.masonryWrap').length == 0 ) {      // checks if the masonryWrap div is already there
					wall.wrapInner('<div class=\"masonryWrap\"></div>');
				}
				var mWrap = wall.children('.masonryWrap');
	
				var brick = mWrap.children();
				var brickW = brick.outerWidth(true);
				var colCount = Math.floor( mWrap.width() / brickW ) ;
				
				var colH=new Array();
				for ( i=0; i < colCount; i++) {
					colH[ i ] =  0 ;
				}		
				
				mWrap.css({ position: 'relative' });
				
				brick.css({
						float: 'none',
						position: 'absolute',
						display: 'block'
					})
					.each(function(){
						for ( i=colCount-1; i > -1; i-- ) {
							if ( colH[ i ] == colH.min() ) {
								var thisCol = i;
							}
						}
						$(this).css({
							top: colH[ thisCol ],
							left: brickW * thisCol
						});
						colH[ thisCol ] += $(this).outerHeight(true);
					});
				
				mWrap.height( colH.max() );
			}

			return this; 
		});
	};
})(jQuery);




$(document).ready(function() {

	$('.more, .section').each(function() {
		$(this).wrapInner('<span class="w1"><span class="w2"><span class="w3"><span class="w4"></span></span></span></span>');
		$('.w4',this).prepend('<span class="w4-1"></span>');
		$('p:last-child',this).addClass('last');
	});

	$('.testimonial p').prepend('<img src="/_img/lyt/qmark.gif" style="float: left; margin: 3px 10px 0 1px" />');

	$('input:text,input:password,textarea').not(':hidden').wrap('<div class="txtfield"></div>');
	$('select').wrap('<div class="optfield"></div>');
	$('.toggle').wrap('<div class="toggle-w"></div>');
	$('#nav-primary li, #nav-secondary li').hover(
		function(){ $(this).addClass('hover'); },
		function(){ $(this).removeClass('hover'); }
	);

	$('.button').hover(
		function(){
			$(this).attr({'src': $(this).attr('src').replace('_off','_on')})
		},
		function(){
			$(this).attr('src', $(this).attr('src').replace('_on','_off'));
		}
	);

	$('p.highlight').prepend('<img src="/img/gbl/curl.gif" alt="" />')

	$('.person.thumbnail').each(function() {
		$(this)
		
		.hover(
			function() { $(this).css('cursor','pointer').addClass('hover'); },
			function() { $(this).removeClass('hover'); }
		)
		
		.click(
			function() { window.location = $('a', this).attr('href');}
		);
	});
	
	
	$('.extended-information').masonry();
	
});
