var hero = {
	timer:0,
	speed:600,
	fade:300,
	player:{},
	currentPlayer:null,
	carousel:null,
	isPlaying:null,
	isCarouselPaused:false,
	init: function(){
		hero.carousel = $('#hero-carousel');
		// setup the click events for the carousel nav
		$("#hero-carousel-controls li").click(function(){
			if($(this).hasClass('active')) return;
			var that = this;
			var i = Number($(this).html()) - 1;
			hero.clearVideo(i);
			return false;
		});
		hero.player = new Object();
		// setup the ready events for each vimeo video
		$('#hero-carousel-item-video iframe').each(function(){
			$f(this).addEvent('ready', hero.playerReady);
		});
	},
	playerReady: function(player_id){
		// a vimeo player is ready, so setup some other events
		hero.player[player_id] = Froogaloop(player_id);
		hero.player[player_id].addEvent('play', function(){
			hero.isPlaying = true;
		});
		hero.player[player_id].addEvent('pause', function(){
			hero.isPlaying = false;
		});
		hero.player[player_id].addEvent('finish', function(){
			hero.isPlaying = false;
			hero.clearVideo();
		});
	},
	updateNav: function(){
		hero.clearVideo();
		var vars = $('#hero-carousel').data('nivo:vars');
		var currSlide = vars.currentSlide + 1;
		if(currSlide == vars.totalSlides){
			currSlide = 0;
		}
		$('#hero-carousel-controls li').removeClass('active');
		$($('#hero-carousel-controls li')[currSlide]).addClass('active');
	},
	playVideo: function(id){
		if(hero.player['vimeoPlayer'+id] == null){
			hero.playerReady('vimeoPlayer'+id);
		}
		hero.currentPlayer = hero.player['vimeoPlayer'+id];
		hero.isCarouselPaused = true;
		hero.carousel.data('nivoslider').stop();
		var playerId = 'vimeoPlayer'+id;
		var $playerId = '#'+playerId;
		$($playerId).show();
		$('#hero-carousel-item-video').fadeIn(hero.speed, function(){
			hero.currentPlayer.api('play');
		})
	},
	clearVideo: function(i){
		if($('#hero-carousel-item-video').css('display')!='none'){						// video is playing or is visible in a paused state
			if(hero.isPlaying) hero.currentPlayer.api('pause');							// pause the video before fading out
			$('#hero-carousel-item-video').fadeOut(hero.speed, function(){				// fade out the video player
				$('#hero-carousel-item-video iframe').hide();							// once complete hide the video player container
				if(i==undefined){
					hero.carousel.data('nivoslider').start();							// handle a finish event and just play the next slide
				}else{
					hero.carousel.data('nivoslider').gotoAndPlay(i);					// jump to a specific slide
				}
			});
		}else if(i==null){
			hero.carousel.data('nivoslider').start();									// handle a finish event and just play the next slide
		}else{
			hero.carousel.data('nivoslider').gotoAndPlay(i);							// jump to a specific slide
		}
		hero.isCarouselPaused = false;
	},
	mouseover: function(){
		if(!hero.isCarouselPaused){
			hero.carousel.data('nivoslider').stop();
		}
	},
	mouseout: function(){
		if(!hero.isCarouselPaused){
			hero.carousel.data('nivoslider').start();
		}
	}
};
function initCustom(){
	// init carousel
	$('#hero-carousel').nivoSlider({
		effect:'fold',
		slices:15,
		animSpeed:600,
		pauseTime:8000,
		startSlide:0,
		directionNav:false,
		directionNavHide:false,
		controlNav:false,
		keyboardNav:true,
		pauseOnHover:false,
		captionOpacity:1,
		afterLoad:function(){
			hero.init();
		},
		beforeChange:function(){
			hero.updateNav();
		}
	});
}
