/* 
 * http://drupal.org/node/1052226#comment-4052684
 * how to make jquery work with drupal 7
 */ 
( function($) {
	
	$( document ).ready( function() {
		
		var	$body				 = $('body').addClass('js'),
				$mainContent = $('#main-content'),
				$info				 = $('#information-panels'),
				$casestudies = $('.view-casestudies', $mainContent),
				$csH2	 			 = $('h2', $casestudies),
				$csSelected,
				isHome = ($body.hasClass('front')) ? 1 : 0,
				imgs,
				qs; 
				
		$('body').addClass('js');
		$('.content', $casestudies).hide();
		
		$('.question h2 a').contents().unwrap();
		$('.view-casestudies h2 a').contents().unwrap();
		
		if(isHome){
			slideshow($('.team-member'),3700);
			slideshow($('.testimonial'),3900);
		}
		
		$csH2
			.each(function(i){
				closeCasestudy($(this));
				$(this).click(function(){
					$csSelected = i;
					openCasestudy( $(this) );
				});
			});
	
	} ); /* CLOSE DOCUMENT READY */
	
	function slideshow($obj,delay){
		var n = $obj.length,
				o = 0,
				nx = 0,
				i = setInterval( function() {
					nx++;
					if( o == n ){ o = 0; }
					if( nx == n ){ nx = 0; }
					
					$obj.eq(nx).css({
						'opacity':1,
						'z-index':1
					});
					
					$obj.eq(o)
						.css('z-index',2)
						.animate({
								'opacity':0
								},{
									'duration':500,
									'complete':function(){
										$(this).css('z-index',0);
										o++;
									}
								});
				}, delay );
	}
	
	function openCasestudy($obj){
		var closed = ($obj.hasClass('closed')) ? 1 : 0;
		if(closed){
			$obj
				.removeClass('closed')
				.addClass('open')
				.parent().find('.content').slideDown();
			//closeAllCasestudies($obj);
		}else{
			closeCasestudy($obj);
		}
	}
	
	function closeAllCasestudies(){
		$csH2.each(function(i) {
			if(i != $csSelected){
				closeCasestudy($(this));
			}
		});
	}
	
	function closeCasestudy($obj){
		$obj
			.removeClass('open')
			.addClass('closed')
			.parent().find('.content').slideUp();
	}
} ) ( jQuery );;

