// JavaScript Document

var home_background_rotate_interval = 6000;

function bb_rotate_bg(current) {
	var total = $('.bg').length;
	var current = current % total;
	var next = (current + 1) % total;

	$('.home-quote-'+current).fadeOut(2000);
	$('.home-quote-'+next).fadeIn(2000);
	$('.bg').eq(current).fadeOut(2000, function() {
		// re-order the z-index
		$('.bg').each(function(i) {
			$(this).css('zIndex', -100 + ((total - i) + current) % total);
		});
		$(this).show();
		setTimeout(function() {bb_rotate_bg(current+1);}, home_background_rotate_interval);
	});
}

function bb_more(name) {
	$('.'+name).show();
	$(this).closest('li').hide();
}
function bb_home_ready() {
	setTimeout(function() { bb_rotate_bg(0); }, home_background_rotate_interval);
}
function bb_portfolio_ready() {
	$('.portfolio-caption').hide().queue('portfolio-caption');
	$('.ngg-thumbnail a').mouseover(function() {
		var html = $(this).closest('.ngg-albumcontent').find('.ngg-description').html();
		$('.portfolio-caption').html(html).show();
	});
	$('.ngg-thumbnail a').mouseout(function() {
		$('.portfolio-caption').hide();
	});
	$('.portfolio-tags a').click(function() {
		var href = $(this).attr('href');
		var result = true;
		if (href.substr(0,1) === '#') {
			href = '.' + href.substr(1);
			$('.ngg-albumcontent ' + href).fadeIn('slow');
			$('.ngg-albumcontent .ngg-thumbnail').not(href).fadeOut('slow');
		} else {
			$('.ngg-albumcontent .ngg-thumbnail').fadeIn('slow');
			result = false;
		}
		$('.portfolio-tags a.selected').removeClass('selected');
		$(this).addClass('selected');
		return result;
	});
}

function bb_gallery_ready(gid) {
	$('#gallery-slider').show().anythingSlider({
		autoPlay	    : false,  // If autoPlay is on, this can force it to start stopped
		startStopped    : false,  // If autoPlay is on, this can force it to start stopped
		width           : 795,   // Override the default CSS width
		height 			: 477,   // Override the default CSS width
//		delay			: 10000,
		theme           : 'bb-gallery',
		buildArrows		: false,
		buildNavigation : true,
//		easing          : 'easeInElastic',
		resizeContents	: false,
		onInitialized: function(e, slider) {
			$('#gallery-controls').find('.prev, .next').click(function(){
				if ($(this).is('.prev')) {
					slider.goBack();
				} else {
					slider.goForward();
				}
			});
		},
		//				autoPlayLocked  : true,  // If true, user changing slides will not stop the slideshow
		//				resumeDelay     : 10000, // Resume slideshow after user interaction, only if autoplayLocked is true (in milliseconds).
		onSlideComplete : function(slider){
			// alert('Welcome to Slide #' + slider.currentPage);
		}
	});
	$('.portfolio-tags li.top > a').click(function() {
		$('.portfolio-tags ul.submenu.selected').slideUp();
		$('ul.submenu', $(this).parent()).slideDown().addClass('selected');
		return false;
	});
	if (gid) {
		$('.gallery-id-'+gid).addClass('selected');
		$('.gallery-id-'+gid+':first').closest('ul.submenu').show();
		$('a.top',$('.gallery-id-'+gid+":first").closest('li.top')).addClass('selected');
	}
}
function bb_testimony_ready() {
	$('#testimony-slider').anythingSlider({
		autoPlay	    : false,  // If autoPlay is on, this can force it to start stopped
		startStopped    : false,  // If autoPlay is on, this can force it to start stopped
		width           : 500,   // Override the default CSS width
		delay			: 10000,
		theme           : 'bb-testimony',
		buildArrows		: false,
		buildNavigation : false,
		easing          : 'easeInElastic',
		resizeContents	: true,
		onInitialized: function(e, slider) {
			$('#testimony-controls').find('.prev, .next').click(function(){
				if ($(this).is('.prev')) {
					slider.goBack();
				} else {
					slider.goForward();
				}
			});
		},
		//				autoPlayLocked  : true,  // If true, user changing slides will not stop the slideshow
		//				resumeDelay     : 10000, // Resume slideshow after user interaction, only if autoplayLocked is true (in milliseconds).
		onSlideComplete : function(slider){
			// alert('Welcome to Slide #' + slider.currentPage);
		}
	}).show();
}

function bb_team_ready() {
	var member = null, member_id = '#team-member-'+document.location.hash.substr(1);
	member = $(member_id);
	if (member.length !== 0) {
		$('#the-team .team-member').hide();
		$(member_id).addClass('active').show();
		$(member_id+"-menu").addClass('selected');
	} else {
		$('#the-team-menu a.first').addClass('selected');
		$('#the-team .first').addClass('active');
	}
	$('#the-team-quote').html($('#the-team .active .quote').html());

	$('#the-team-menu a').click(function () {
		$('#the-team-menu a').removeClass('selected');
		$(this).addClass('selected');
		$('#the-team .active').hide();
		var id = $(this).attr('id');
		id = id.substr(0,id.length-"-menu".length);
		$('#team-member-'+id).fadeIn().addClass('active');
		$('#the-team-quote').html($('#team-member-'+id+' .quote').html());
	});
}

function bb_capabilities_ready() {
	$('.capability-block-header a').click(function() {
		var hr = $(this).attr('href');
		if ($(':visible', hr).length === 0) {
			$('.capability-section:visible').slideUp();
			$(hr).slideDown();
			$('.capability-block-header a.selected').removeClass('selected');
			$(this).addClass('selected');
		}
	});
}

function bb_resize_bg(why) {
	var theWindow        	= $(window),
	$bg              		= $(".bg"),
	aspectRatio      		= $bg.width() / $bg.height();
	var windowAspect		= theWindow.width() / theWindow.height();
	if (windowAspect < aspectRatio ) {
		$bg.removeClass().addClass('bg bgheight');
	} else {
		$bg.removeClass().addClass('bg bgwidth');
	}
}

var bb_images = ['portfolio','testimony','capabilities','evolution'];
var bb_logo_src = $('#bb-logo img').attr('src');
var bb_logo_src_prefix = bb_logo_src ? bb_logo_src.split('/') : [];
bb_logo_src_prefix.pop();
bb_logo_src_prefix = bb_logo_src_prefix.join('/');

function bb_logo_over(i) {
	$('#bb-logo img').attr('src', bb_logo_src_prefix + '/bb-the-'+bb_images[i]+'.png');
}
function bb_logo_out() {
	$('#bb-logo img').attr('src', bb_logo_src);
}

$(document).ready(function(){
	var i = 0, animate = null;

	if (window.navigator.userAgent.indexOf('MSIE 6') >= 0) {
		if (confirm("This web site is not supported by Internet Explorer 6 - Would you like to be directed to Microsoft's web site to upgrade to a more modern version of Internet Explorer?")) {
			document.location = 'http://www.microsoft.com/ie';
		}
	}
	animate = setInterval(function() {
		var new_src;
		if (i >= bb_images.length) {
			clearInterval(animate);
			new_src = bb_logo_src;
		} else {
			new_src = bb_logo_src_prefix + '/bb-the-'+bb_images[i]+'.png';
		}
		$('#bb-logo img').attr('src',new_src);
		i++;
	}, 500);

	$(window).resize(function() { bb_resize_bg("resize"); }).trigger("resize");
	$(".bg").load(function() { bb_resize_bg("bgload"); });

	if (window.selected && window.selected.length > 0) {
		$(window.selected.join(",")).addClass('selected');
	}
});

