(function($) {
	$.fn.rotate = function(options) {
	
	//alert('rotate');
		
		var total = $(this).children().length - 1;
		
		var defaults = {
		 	end: total,
			which: 0,
			prev: total,
			pause: 5000,
			fadeSpeed: 500,
			hoverPause: true,
			hovering: false
		};
			
		var options = $.extend(defaults, options);
		
		var indicatorsHTML = '';
		
		for(var i = 0; i <= total; i++ ) {
			indicatorsHTML += '<div><div class="active"></div><!-- / .active --></div>';
		}
		
		$('#indicators').append(indicatorsHTML);
		
		return this.each(function() {
			//alert($(this));
			var $kids = $(this).children();
					$kids.animate({opacity: 0}, 0).css('visibility', 'visible').eq(options.which).css('z-index', '5').animate({opacity: 1}, 0);
					//$kids.animate({opacity: 0}, 0).eq(options.which).css('z-index', '5').animate({opacity: 1}, 0);
			
			var $indicators = $('#indicators').children().children('.active');
					$indicators.animate({opacity: 0}, 0).eq(options.which).animate({opacity: 1}, 0);
			
			(function fadeit() {
				
				$kids.eq(options.prev).animate({opacity: 0}, options.fadeSpeed).css('z-index', '5');
				$kids.eq(options.which).animate({opacity: 1}, options.fadeSpeed).css('z-index', '10');
				
				$indicators.eq(options.prev).animate({opacity: 0}, options.fadeSpeed);
				$indicators.eq(options.which).animate({opacity: 1}, options.fadeSpeed);
				
				options.which = (options.which == options.end) ? 0 : options.which+1;
				options.prev = (options.which == 0) ? options.end : options.which-1;
				
				setTimeout(fadeit, options.pause);
				
			})();
				
		});
	};
})(jQuery);

$(document).ready( function() {
	//alert('here');
	
	$('.acitem').each(function(index) {
		myHeight = $(this).height();
		$(this).css('height', myHeight+'px');
		//alert(myHeight);
	});
	
	$('.acmenu').initMenu();
	
	/*$('#slides').cycle({
		fx:      'fade',
		speed:    500,
		timeout:  5000
	});*/

	// On our team page
	if ($('#our_team_window').length > 0) {
		
		var $otw = $('#our_team_window'),
				$ot = $('#our_team'),
				$bl = $('#bio_left'),
				$br = $('#bio_right'),
				$bios = $('#bio_left, #bio_right'),
				picture_width = $ot.children().outerWidth(true),
				total_kids = $ot.children().length-1, 
				current_kid = $ot.find('.active_member').index()+1;
		
		// make magic happen
		if ($ot.children().length > 8) {
			$ot.css('padding-left', 25);
			$otw.css('width', 790);
			$bios.show();
			
			if (current_kid != 0) {
				// move slider to center if possible
				if (current_kid > 3) {
					var spots = current_kid - 4;
							spots = (current_kid + 3 >= total_kids) ? total_kids - 7 : spots;
					
					$ot.css({left: -1 * spots * picture_width}, 0);
				}
			}
			
			var current_position = 0;
			
			function setOpacity() {
				current_position = -1 * parseInt($ot.css('left').replace('px')) / parseInt(picture_width);
			
				var blFader = (current_position == 0) ? 1 : 1;
				$bl.fadeTo(0, blFader);
				
				var brFader = (current_position >= (total_kids - 7)) ? 1 : 1; 
				$br.fadeTo(0, brFader);
			}
			
			setOpacity();
			
			var sliding = false;
			
			$bios.click( function() {
				
				if (!sliding) {
					sliding = true;
					current_position = -1 * parseInt($ot.css('left').replace('px')) / parseInt(picture_width);
					
					// left click
					if (this.id == 'bio_left') {
						if (current_position > 0) {
							$ot.animate({left: '+='+picture_width}, 200, function() {
								sliding = false;
								setOpacity();
							});
						} else {
							sliding = false;
						}
					// right click
					} else {
						if (current_position < (total_kids - 7)) {
							$ot.animate({left: '-='+picture_width}, 200, function() {
								sliding = false;
								setOpacity();
							});
						} else {
							sliding = false;
						}
					}
				}
				
				setOpacity();
				return false;
			});
		}
	}
	
	
	$('#our_team a, .footer_right .staff a').hover( function() {
		
		if (!$(this).hasClass('active_member')) {
			var $me = $(this),
					$kid = $me.find('img');
			if (!$me.parent().hasClass('staff')){
				$me.stop().animate({opacity: 1}, 500);
			}
			$kid.stop().animate({opacity: 1}, 500);
		}
		
	}, function() {
		
		if (!$(this).hasClass('active_member')) {
		
			var $me = $(this),
					$kid = $me.find('img');
			if (!$me.parent().hasClass('staff')){
				$me.stop().animate({opacity: .3}, 500);
			}
			$kid.stop().animate({opacity: 0}, 500);
		}
		
	});
	
	//if ($('#slides').length > 0) {
		//$('#slides').rotate();
	//}

/*	$('#slides').cycle({ 
		fx:     'fade',
		speed:  500,
		timeout: 2000,
		//next:   '#next_slide',
		//prev:   '#prev_slide'
		pager:  '#indicators'
	});
	
	$('#slides').hover(
		function() {
			$('#slides').cycle('pause');
		}, 
		
		function () {
			$('#slides').cycle('resume');
		}
	);
	
	$('#indicators').hover(
		function() {
			$('#slides').cycle('pause');
		}, 
		
		function () {
			$('#slides').cycle('resume');
		}
	);*/
});
