$(document).ready(function() {

	// Create Chantal Desloges object
	
	CD.menuWidth = function(){
	
	var col1_widths = [],
			col2_widths = [],
			col3_widths = [];
	
		$('#menu-top-menu li:nth-child(3n+1)').each(function(){
		
			for( $i=1;$i<=3;$i++ ){
				
				$("#menu-top-menu li:nth-child(3n+"+$i+")").each(function(){
					
					$(this).addClass('col-' + $i);
								
				});
			} 
		
			for( $i=1;$i<=3;$i++ ){
				$('#menu-top-menu li.col-' + $i).each(function(){
					if($i == 1) active_array = col1_widths;
					if($i == 2) active_array = col2_widths;
					if($i == 3) active_array = col3_widths;

					active_array.push( $(this).width() );
					
				});
			}
		
		});
	
		var col1Max = Math.max.apply(Math, col1_widths),
				col2Max = Math.max.apply(Math, col2_widths),
				col3Max = Math.max.apply(Math, col3_widths),
				marginR = parseFloat($('#menu-top-menu li').css('margin-right').replace('px','')),
				totalW = col1Max + marginR + col2Max + marginR + col3Max;
				
			for( $i=1;$i<=3;$i++ ){
				$('#menu-top-menu li.col-' + $i).each(function(){
					if($i == 1) width = col1Max;
					if($i == 2) width = col2Max;
					if($i == 3) width = col3Max;
					$(this).width(width);
				});
			}
						
			$('ul#menu-top-menu').css({
				'width' : totalW + 'px',
				'margin-left' : 940 - 280 - totalW // 940 (page container, 280 (logo width)
			});
		
		// Hide the nav until all widths have been correctly set.			
		$('nav[role=navigation] .menu ul li a').each(function(){
			$(this).addClass('show');
		});
	};
		
	CD.smoothScroll = function(){
	
	  function filterPath(string) {
	  return string
	    .replace(/^\//,'')
	    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
	    .replace(/\/$/,'');
	  }
	  var locationPath = filterPath(location.pathname);
	  var scrollElem = scrollableElement('html', 'body');
	
	  $('a[href*=#]').each(function() {
	    var thisPath = filterPath(this.pathname) || locationPath;
	    if (  locationPath == thisPath
	    && (location.hostname == this.hostname || !this.hostname)
	    && this.hash.replace(/#/,'') ) {
	      var $target = $(this.hash), target = this.hash;
	      if (target) {
	        var targetOffset = $target.offset().top;
	        $(this).click(function(event) {
	          event.preventDefault();
	          $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
	            location.hash = target;	            	
	          });
	        });
	      }
	    }
	  });
	
	  // use the first element that is "scrollable"
	  function scrollableElement(els) {
	    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
	      var el = arguments[i],
	          $scrollElement = $(el);
	      if ($scrollElement.scrollTop()> 0) {
	        return el;
	      } else {
	        $scrollElement.scrollTop(1);
	        var isScrollable = $scrollElement.scrollTop()> 0;
	        $scrollElement.scrollTop(0);
	        if (isScrollable) {
	          return el;
	        }
	      }
	    }
	    return [];
	  }
	
	} // end smoothScroll
	
	
	
	
	$('.team_bios').each(function(){
		if( $(this).height() < 220){
			$(this).find('.button').addClass('hide');
		}
	});
	
	$('.button').toggle(
		function(e) {
		e.preventDefault();
			var bio = $(this).closest('.team_bios'),
				bioH = bio.height(),
				li = $(this).closest('li'),
				button = $(this);
			if(bioH > 220){
			li.stop().animate({
				height: bioH + button.height() + 'px'
			}, 800, function(){
				button.text('Close');
			} );
			}
			else {
				button.addClass('hide');			
			}
		},
		function(e){
			e.preventDefault();	
			var bio = $(this).closest('.team_bios'),
				bioH = bio.height(),
				li = $(this).closest('li'),
				button = $(this);

			li.stop().animate({
				height: '220px'
			}, 800, function(){
				button.text('Read More');
			} );
		}
	);
	
	
	// var open = false;
	// $('.button').click(function(e) {
	// 	e.preventDefault();
	//     if (open) {
	//         $(this).parent('p').siblings('.bio_text').animate({height:'90px'});
	//     }
	//     else {
	//         $(this).parent('p').siblings('.bio_text').animate({height:'100%'});
	//     }
	//     open = !open;
	// });

	
	// Hide video until ready to play
	CD.showHomeVideo = function(){		
		$('#home_video').css('visibility', 'visible');
	};
	
	// Convert phone number lunk in menu to plain text
	CD.removePhoneLink = function(){
		$('nav[role=navigation] .menu ul li.phone-number a').each(function(){
			$(this).contents().unwrap();
		})
	};
	
	
	CD.detectOS = function(){
		var useragent = navigator.userAgent,
				text = 'Windows' ;

		// Add class to html if it's Windows
		if( useragent.indexOf( text ) != -1 ){
			$('html').addClass('os-windows');
		}
	}
	
	CD.testimonials = function(){
		if( $('body').hasClass('page-testimonials')){
			$('.page-testimonials .post_content #testimonials').masonry({
			  itemSelector: 'blockquote'
			});	
		}
	}
	
	// Init
	CD.init = function(){
		CD.menuWidth();	
		if($('body').hasClass('page-questions-answers')){
			CD.smoothScroll();
		}	
		CD.removePhoneLink();
		CD.detectOS();
		CD.testimonials();
	}
	
	CD.init();
		
}); /* end of as page load scripts */

$(window).load(function(){
	CD.showHomeVideo();
});



 
