//called from flash after menu slides
function fadeClient() {
	$("#nav_client").fadeIn("slow");
}

function fadeHome() {
	$("#nav_home").fadeIn("slow");
}

function fadeReels() {
	$("#nav_reels").fadeIn("slow");
}

function fadeSpace() {
	$("#nav_space").fadeIn("slow");
}

function fadeToolbox() {
	$("#nav_toolbox").fadeIn("slow");
}

function fadeInfo() {
	$("#nav_info").fadeIn("slow");
}

function fadeNews() {
	$('#news-content').fadeIn('slow', function() {
		$('#home-flash').remove();
		// initialize scrollable (do it after news is shown to avoid no-display rendering issue)
		Hootenanny.Scrollable.init( {slider_height: 273, content_height: 360, slider_change: home_slider_change } );
	});
}

$(document).ready(function() {
	// check for visited cookie
	if ($.cookie('hootenanny_intro') == null) {
		// set the cookie
		$.cookie('hootenanny_intro', 'veteran'); // cookie expires after closing the browser
		
		// set appropriate styles
		$('#nav div').css('display', 'none');
		$('.subnav').css('display', 'none');
		$('#news-content').hide();
		$('#home-flash').show();
		
		// show first time flash movies
		swfobject.embedSWF("/swf/header.swf", "flashnav", "840", "160", "9.0.0", "", {}, { wmode:"transparent"}, {});
		swfobject.embedSWF("/swf/intro.swf", "home-flash", "840", "400", "9.0.0", "", {}, { wmode:"transparent"}, {});
	} else {
		swfobject.embedSWF("/swf/header2.swf", "flashnav", "840", "160", "9.0.0", "", {}, { wmode:"transparent"}, {});
		
		$('#news-content').show();
		$('#home-flash').hide();
		
		// initialize scrollable
		Hootenanny.Scrollable.init( {slider_height: 273, content_height: 360, slider_change: home_slider_change } );
	}
});

// store currently shown entry
var visible_entry = 0;

function home_slider_change(offset) {
	// get container
	var container = Hootenanny.Scrollable.get_content_wrapper();
	
	// get slider value
	var slider_value = Hootenanny.Scrollable.get_slider_value();
	
	if (slider_value == 0) {
		$(container).attr('scrollTop', '0px');
	}
	
	// get container scroll position
	var scroll = $(container).attr('scrollTop');
	
	// loop through entries
	$(container).find('.entry').each(function(i) {
		var entry_top = parseInt($(this).position().top);
		var entry_height = parseInt($(this).height());
		
		// find the first fully visible entry in the viewport (give 20px of slack)
		if (entry_top >= -20) {
			if (parseInt(i) != parseInt(visible_entry)) {
				// set visible entry
				visible_entry = parseInt(i);
			
				// if the entry has an image, show it
				if ($(this).find('.image').length > 0) {
					// find currently displayed image
					var currently_shown_image = $('.news-image-wrapper:not(.hidden):first');
					
					// get source of new image
					var new_image_source = $(this).find('.image:first img:first').attr('src');
					
					// find image to display
					var image_match = $('.news-image-wrapper img[src="' + new_image_source + '"]');
					var image_to_show = $(image_match).parent('.news-image-wrapper:first');
					
					// fade out old image
					$(currently_shown_image).fadeOut('fast', function() {
						$('.news-image-wrapper').addClass('hidden');
						$(image_to_show).fadeIn('fast', function() {
							$(image_to_show).removeClass('hidden');
						});
					});
				}
			}
			
			return false;
		}
	});
}