function fade_30_all_nav(){
	$('#products_trigger').fadeTo(300, 0.3);
	$('#muscle_recovery_trigger').fadeTo(300, 0.3);
	$('#benefits_trigger').fadeTo(300, 0.3);
	$('#nutrition_trigger').fadeTo(300, 0.3);
}

function show_all_nav(){
	$('#products_trigger').fadeTo(300, 1.0);
	$('#muscle_recovery_trigger').fadeTo(300, 1.0);
	$('#benefits_trigger').fadeTo(300, 1.0);
	$('#nutrition_trigger').fadeTo(300, 1.0);
}

function hide_all_nav(){
	$('#products_trigger').hide();
	$('#muscle_recovery_trigger').hide();
	$('#benefits_trigger').hide();
	$('#nutrition_trigger').hide();
}

function fade_out_all_container(bottle_is_rear){
	$('#rr_nutrition_container').fadeOut(300);
	$('#rr_products_container').fadeOut(300);
	$('#rr_benefits_container').fadeOut(300);
	$('#rr_muscle_recovery_container').fadeOut(300);
	if(bottle_is_rear)
	{
		$('#rr_bottle .bottle_front').show();
		$('#rr_bottle .bottle_back').hide();
	}
}

function fade_out_all_video()
{
	$('.rr_cinema').fadeOut(300);
}

function hide_all_video()
{
	$('.rr_cinema').hide();
}

function hide_bottle_back()
{
	$('.bottle_back').hide();
}

function hide_all_container(){
	$('#rr_nutrition_container').hide();
	$('#rr_products_container').hide();
	$('#rr_benefits_container').hide();
	$('#rr_muscle_recovery_container').hide();
}

function shake_it()
{
	$('#rr_bottle').effect('shake', { times: 3, distance: 10 }, 30);
}

function replace_to_back()
{
	$('#rr_bottle').effect('shake', { times: 1, distance: 10 }, 20, function(){
		$('#rr_bottle .bottle_front').fadeOut(100);
		$('#rr_bottle .bottle_back').fadeIn(100);
		$('#rr_bottle').effect('shake', { times: 6, distance: 10 }, 20);
	});
	
}

function theater_availability(video_array_count, current_video_index)
{
	//console.log(video_array_count);
		//console.log(current_video_index)
	
	
	if((Math.round(video_array_count / 2)) - current_video_index < 1)
	{
		//console.log('hiding right nav');
		$('#theater_nav_right_action').hide();
	}
	
	else if(video_array_count == 2)
	{
		//console.log('hiding right nav');
		$('#theater_nav_right_action').hide();
	}
	
	
	else
	{
		//console.log('showing both');
		$('#theater_nav_left_action').show();
		$('#theater_nav_right_action').show();
	}
	
	if(current_video_index == 1)
	{
		$('#theater_nav_left_action').hide();
	}
	
	else if(current_video_index != 1)
	{
		$('#theater_nav_left_action').show();
	}
	
	
}

function update_theater(current_video_index, video_array_count, video_array)
{
	var current_video_index = current_video_index + 1;
	
	theater_availability(video_array_count, current_video_index);
	
	var max_video_pages = Math.round(video_array_count / 2);
	
	if(current_video_index < max_video_pages)
	{
		//console.log(current_video_index);
		if(current_video_index <= 1)
		{
			//console.log('first page');
			var current_index_1 = ((current_video_index * 2) - 2);
			var current_index_2 = (current_index_1 + 1);
		}
		else
		{
			//console.log('not first page');
			var current_index_1 = ((current_video_index * 2) - 1) - 1;
			var current_index_2 = (current_index_1 + 1);
		}
		
		//console.log('1showing video: '+current_index_1);
		//console.log('2showing video: '+current_index_2);
		$('#thumbs').hide();
		$('#thumbs').html('<div class="rr_video" id="' + video_array[current_index_1] + '"><a href="#' + video_array[current_index_1] + '" onclick="return false" id="youtube_player"><img src="http://img.youtube.com/vi/' + video_array[current_index_1] + '/default.jpg" /></a></div><div class="rr_video" id="' + video_array[current_index_2] + '"><a href="#' + video_array[current_index_2] + '" onclick="return false" id="youtube_player"><img src="http://img.youtube.com/vi/' + video_array[current_index_2] + '/default.jpg" /></></div>');
		$('#thumbs').fadeIn(300);
	}
	
	else //if(current_video_index == max_video_pages)
	{
		if((current_video_index * 2) - video_array_count == 1)
		{
			var current_index_1 = ((current_video_index * 2) - 2);
			//console.log('3showing video: '+current_index_1);
			$('#thumbs').hide();
			$('#thumbs').html('<div class="rr_video" id="' + video_array[current_index_1] + '"><a href="#' + video_array[current_index_1] + '" onclick="return false" id="youtube_player"><img src="http://img.youtube.com/vi/' + video_array[current_index_1] + '/default.jpg" /></a></div>');
			$('#thumbs').fadeIn(300);
		}
		else //if((current_video_index * 2) - video_array_count == 0)
		{
			var current_index_1 = ((current_video_index * 2) - 2);
			var current_index_2 = (current_index_1 + 1);
			//console.log('4showing video: '+current_index_1);
			//console.log('5showing video: '+current_index_2);
			$('#thumbs').hide();
			$('#thumbs').html('<div class="rr_video" id="' + video_array[current_index_1] + '"><a href="#' + video_array[current_index_1] + '" onclick="return false" id="youtube_player"><img src="http://img.youtube.com/vi/' + video_array[current_index_1] + '/default.jpg" /></a></div><div class="rr_video" id="' + video_array[current_index_2] + '"><a href="#' + video_array[current_index_2] + '" onclick="return false" id="youtube_player"><img src="http://img.youtube.com/vi/' + video_array[current_index_2] + '/default.jpg" /></a></div>');
			$('#thumbs').fadeIn(300);
		}
	}
	
	return current_video_index;
	
}

$(document).ready(function() {

	hide_all_container();
	hide_all_video();
	hide_bottle_back();
	
	var bottle_is_rear = false;
	
	$(window).load(function() {
	 $('#rr_loading').hide()
	 $('.rr_app').fadeIn(200);
	});
	
	
	
	$('#products_trigger').live('click', function() {
		shake_it();
		fade_30_all_nav();
		$('#rr_products_container').fadeIn(300);
	});
	
	$('#muscle_recovery_trigger').live('click', function() {
		shake_it();
		fade_30_all_nav();
		$('#rr_muscle_recovery_container').fadeIn(300);
	});
	
	$('#nutrition_trigger').live('click', function() {
		shake_it();
		replace_to_back();
		fade_30_all_nav();
		$('#rr_nutrition_container').delay(1600).fadeIn(300);
		bottle_is_rear = true;
	});
	
	$('#benefits_trigger').live('click', function() {
		shake_it();
		fade_30_all_nav();
		$('#rr_benefits_container').fadeIn(300);
	});
	
	$('.rr_all_exit').live('click', function() {
		show_all_nav();
		fade_out_all_container(bottle_is_rear);
		fade_out_all_video();
		bottle_is_rear = false;
	});
	
	
	$('#products_trigger').hover(
		
		function(){
			$('.rr_trigger.products a').animate({left: '260'}, { duration: 300, queue: false });
		},
		function(){
			$('.rr_trigger.products a').animate({left: '270'}, { duration: 400, queue: false });
	});
	
	$('#muscle_recovery_trigger').hover(
		function(){
			$('.rr_trigger.muscle_recovery a').animate({left: '255'}, { duration: 300, queue: false });
		},
		function(){
			$('.rr_trigger.muscle_recovery a').animate({left: '265'}, { duration: 400, queue: false });
	});
	
	$('#benefits_trigger').hover(
		function(){
			$('.rr_trigger.benefits a').animate({left: '65'}, { duration: 300, queue: false });
		},
		function(){
			$('.rr_trigger.benefits a').animate({left: '55'}, { duration: 400, queue: false });
	});
	
	
	$('#products_trigger').css('left', '0px');
	$('#muscle_recovery_trigger').css('left', '0px');
	$('#nutrition_trigger').css('left', '0px');
	$('#benefits_trigger').css('left', '0px');
	
	//when a container is open, if you're hovering over it and click, do nothing. if you click outside the container, close it (we close all for safety)
		mouse_is_inside=false;
		
	$('.rr_info_container').hover(function(){ 
	        mouse_is_inside=true; 
	    }, function(){ 
	        mouse_is_inside=false; 
	    });
	    
	$('.rr_cinema').hover(function(){ 
	        mouse_is_inside=true; 
	    }, function(){ 
	        mouse_is_inside=false; 
	    });
	
	$('body').mouseup(function(){ 
	        if(! mouse_is_inside){
	        	fade_out_all_container(bottle_is_rear);
	        	show_all_nav();
	        	fade_out_all_video();
	        	mouse_is_inside=false;
	        	bottle_is_rear = false;
	        }
	 });
	
	
	/////////////////THEATER////////////////
	//this is the youtube chooser, what we've called the "theater"
	
	var video_array = new Array('L5c55_GlXOM', 'MrnSRJ4E4Bg');
	var video_array_count = video_array.length;
	
	var current_video_index = 0;
	current_video_index = update_theater(current_video_index, video_array_count, video_array);
	
	$('#theater_nav_right_action').click(function(){
	
		current_video_index = update_theater(current_video_index, video_array_count, video_array);
	});
	
	$('#theater_nav_left_action').click(function(){
	
		current_video_index = update_theater(current_video_index-2, video_array_count, video_array);
	});
	
	
	
	/////////////////CINEMA////////////////
	//this is the youtube player, what we call the "Cinema"
	
	$('.rr_video a').live('click', function() {
		$('.rr_cinema').html('');
		
		var youtube_url = $(this).parent().attr('id');
		
		//alert(youtube_url);
		//console.log(youtube_url);
		
		$('.rr_cinema').html('<object style="height: 250px; width: 360px"><param name="movie" value="http://www.youtube.com/v/'+ youtube_url +'?version=3?version=3&rel=0&color1=0xb1b1b1&color2=0xcfcfcf&hd=1&showsearch=0&showinfo=0&feature=player_embedded"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/'+ youtube_url +'?version=3?version=3&rel=0&color1=0xb1b1b1&color2=0xcfcfcf&hd=1&showsearch=0&showinfo=0&feature=player_embedded" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="360" height="250"></object><div class="exit"><a href="#" class="exit_button rr_all_exit" id="video_exit"><img class="x-icon" src="http://dev.rockinrefuel.com/wp-content/themes/rockinrefuel/images/rr-close-x.png" />CLOSE</a></div>');
		
		$('.rr_cinema').fadeIn(300);
	});			
});
