// ----------------------------------------------------------
//  Author: Brian Hall <steven.brian.hall@gmail.com>
//  Document: sbh.js
//	Description: The main Javascript file for the SBH Labs website.
// ----------------------------------------------------------

	//  Name: Configure Sections         												
	//  Description: Configure each section container and their corresponding next/prev buttons.
	//	Parameters: N/A																					
	function configure_sections(){
		// Loop through each section
		$('.section_container').each(function(){
			var element = $(this).parents('div:eq(1)');
			var total_sections = $(this).children('.section').size();
			var current_section = 1;
					
			// Next Button
			$(element).find('.next').click(function(event){
				if(current_section < (total_sections)){ $(element).find('.section_container').animate({'top':'-=163px'},900); current_section += 1; }
			});
			
			// Previous Button
			$(element).find('.prev').click(function(event){
				if(current_section > 1){ $(element).find('.section_container').animate({'top':'+=163px'},900); current_section -= 1; }
			});			
		});
	}

	//  Name: Configure Galleries	       				
	//  Description: Configure each gallery and their corresponding next/prev buttons.
	//	Parameters: N/A
	function configure_galleries(){
		// Loop through each gallery
		$('.gallery_container').each(function(){
			var element = $(this).parents('div:eq(2)');
			var total_images = $(this).find('.ngg-galleryoverview').children('.ngg-gallery-thumbnail-box').size();
			var current_image = 1;
			
			// Down Button
			$(element).find('.down').click(function(event){
				if(current_image < (total_images-1)){ $(element).find('.gallery_container').animate({'top':'-=60px'},400); current_image += 1; }
			});
			
			// Up Button
			$(element).find('.up').click(function(event){
				if(current_image > 1){ $(element).find('.gallery_container').animate({'top':'+=60px'},400); current_image -= 1; }
			});
		});
	}
	
	//  Name: View							         										
	//  Description: Expand the requested job, minimize the	previously loaded one.									
	//	Parameters: jQuery DOM Element											
	function view(element){
		if(typeof loaded != 'undefined'){
			if($(element).attr('id') != $(loaded).attr('id')){
				unload(loaded);
				load(element);
				loaded = element;
			}
		}else{
			load(element);
			loaded = element;
		}
	}
	
	//  Name: Load
	//  Description: Configure each section container and their corresponding next/prev buttons.
	//	Parameters: jQuery DOM Element
	function load(element){
		// Fade Image In
		$(element).fadeTo(300, 1, function(){ 
			// Resize Image
			$(element).find(".image").animate({ 'height':'230px' }, 700, function(){
				// Load Information
				$(element).find(".info").slideToggle(500, function(){
					// Load Gallery
					$(element).find(".gallery").animate({'opacity':'show'},700);
					// Drop Title Down
					$(element).find(".small_title").animate({ 'top':'187px' }, 700).effect("highlight", {}, 4000);	
				});
			});
		});
	}
	
	//  Name: Configure Bubbles
	//  Description: Configure the bubble to appear above the hover class elements and take on their
	//								title attribute as the inner HTML.
	//	Parameters: N/A
	function configure_bubbles(){
		$('.hover').hover(function(){
			// Get the hover element's current position.
			var x = $(this).position().left;
			var y = $(this).position().top;
			// Define the bubble's offset.
			var offset_x = 10;
			var offset_y = 65;
			// Move the bubble to the appropriate spot.
			$("#bubble").stop(true,true).css({'left':x+offset_x,'top':y-offset_y});
			// Change the bubble's text to the hover class element's title.
			$("#bubble").html($(this).attr('alt'));
			// Make the bubble appear.
			$("#bubble").stop(true,true).animate({'opacity':'show', 'top':'-=5px'}, "slow");
			}, function(){
				// Make the bubble disappear.
				$("#bubble").animate({'opacity':'hide','top':'-=5px'},"fast");
			});	
	}	

	//  Name: Unload
	//  Description: Unload the existing job.
	//	Parameters: jQuery DOM Element
	function unload(element){
		// Fade Image Out
		$(element).fadeTo(400, 0.6,function(){
			$(element).find(".gallery").hide(400);
			// Hide Information
			$(element).find(".info").hide("blind", {direction: 'vertical' }, 600);
			// Hide Gallery
		});
		// Resize Image
		$(loaded).find(".image").animate({ 'height':'90px' },600, function(){
				$(element).find(".small_title").animate({ 'top':'30px' }, 400);
		});
	}

	//  Name: Home Animation
	//  Description: Perform the animation on the homepage.
	//	Parameters: N/A
	function home_animation(){
		// Animate the first line
		$("#line_1").animate({'bottom':'50px','opacity':'show'},800,function(){
			// Animate Brian's picture
			$("#brian").animate({'opacity':'show'},800);
			// Show the arrow
			$("#arrow").animate({'opacity':'show','top':'-20px'},400,function(){
				// Hide the arrow
				$("#arrow").animate({'opacity':'hide','top':'-10px'},600,function(){
					// Show the tagline
					$(".tagline").animate({'left':'208px'},600,function(){
						// Show line 1
						$("#stripe_1").slideDown(500,function(){
							// Show line 2
							$("#stripe_2").slideDown(500,function(){
								// Show line 3
								$("#stripe_3").slideDown(500);
							});
						});
					});
				});
			});
		});
	}
