(function($){ 
	$(function(){
		// Comments
		if($('#commentform').length > 0){
			$('#commentform').validate();
		}
		$('#subnavigation li:last').addClass('last');
		
		//Homepage alerts fade
		var alerts = $('#alert-update .single-alert');
		if(alerts.length > 1) {
			var total = alerts.length - 1;
			var i = 0;
			// Fade every 6 seconds
		    setInterval(function() {
		          alerts.eq(i).fadeOut(400);
		          i++;
		          if(i > total)
		          	i = 0;
		          alerts.eq(i).fadeIn(400);
		    }, 5500);
		}		
		
		var total_items = $('#carousel ul li').length;				
		if(total_items) {				
			var pages = Math.ceil(total_items / 3);
			var goArray = new Array();						
			for( i = 0; i < pages; i++) {				
				goArray[i*3] = "#carousel-pagination .page-item-"+i;			
			}
			
			//home carousel
			$("#carousel").jCarouselLite({
				auto: 8000,
				speed: 500,
				visible: 3,
				scroll: 3,
				btnNext: "#carousel-next",
				btnPrev: "#carousel-prev",
				btnGo: goArray,
				afterEnd: updatePagination
			});		
		}
		function updatePagination(visible) {
			var newpage = $('#carousel ul li').index(visible[0]);
			newpage = newpage - 3; // Compensate for padding
			newpage = newpage / 3;
			$('#carousel-pagination .active').removeClass('active');
			$('#carousel-pagination a').eq(newpage).addClass('active');
		}
		
		//Show-Hide Script for Speaker and Team Bios
		if($('a.showhide').length > 0){
				$('div.hidden').hide();
				$('a.showhide').click(function(){
			$(this).parent().find('div.hidden').slideToggle();
			if($(this).hasClass('expanded')){
				$(this).removeClass('expanded');
				$(this).text('Show Biography');
					} else {
				$(this).addClass('expanded');
				$(this).text('Hide Biography');
				}
			return false;
			});
		}
		
		//Show-Hide Script for Adventures
		if($('a.showhide-adventures').length > 0){
			$('div.hidden-adventures').hide();
			$('a.showhide-adventures').click(function(){
			$('div.hidden-adventures').slideToggle();
			if($(this).hasClass('expanded')){
				$(this).removeClass('expanded');
				$(this).text('Show Full Description');
			} else {
				$(this).addClass('expanded');
				$(this).text('Hide Full Description');
			}
			return false;
			});
		}

		//Show-Hide Script for 2010 TEDx SideBar
		if($('a.showhide-tedx10').length > 0){
				$('div.hidden-tedx10').hide();
				$('a.showhide-tedx10').click(function(){
			$('div.hidden-tedx10').slideToggle();
			if($(this).hasClass('expanded')){
				$(this).removeClass('expanded');
				$(this).text('Show TEDxBoston 2010');
			} else {
				$(this).addClass('expanded');
				$(this).text('Hide TEDxBoston 2010');
			}
			return false;
			});
		}
		
		//Show-Hide Script for 2009 TEDx SideBar
		if($('a.showhide-tedx09').length > 0){
				$('div.hidden-tedx09').hide();
				$('a.showhide-tedx09').click(function(){
			$('div.hidden-tedx09').slideToggle();
			if($(this).hasClass('expanded')){
				$(this).removeClass('expanded');
				$(this).text('Show TEDxBoston 2009');
					} else {
				$(this).addClass('expanded');
				$(this).text('Hide TEDxBoston 2009');
				}
			return false;
			});
		}

		//Fade in the Alert message
		if($('span.alert').length > 0){
			// hide divs
			$('span.alert').hide();
			// slide effect
			$('span.alert').fadeIn(900);
		}
		
		//Fade in the Alert message
		if($('input.button-contact').length > 0){
			$('div.success-message').hide();
			$('input.button-contact').click(function(){
				$('form#contact-form').slideUp('fast');
				$('div.success-message').slideDown('fast');
				return false;
			});
		}
		
		//Animate Names on Homepage Speakers Grid
		$('li','.speaker-grid-home').hover(function() {
				rollover = $('.mouseover', $(this));
				delay = setTimeout(function() {
					clearTimeout(delay);
					rollover.animate({bottom : '1px'}, 80);
				}, 80);
			}, function() {
				clearTimeout(delay);
				$('.mouseover', $(this)).animate({bottom : '-133px'}, 80);
			});
			
	});
})(jQuery);

