// JavaScript Document

var entry_count=0;
var more_photos_count = 0;

var active_entry=0;
var more_photos_active_entry = 0;

// For slideshow
var t = null;

$(document).ready(function(){
	
	// SLIDESHOW
	
	// The number of images in the slideshow
	entry_count = $("#slideshow_img img").length;
	// Collects all the photos in the main slideshow
	var $slideshow_photos = $("#slideshow_img img");
	
	// Create slideshow navigation
	for(var i = 0; i < entry_count; i++)
	{
		var link_class_str = "";
		if(i == 0)
		{
			link_class_str = "class=\"current\"";	
		}
		$('#slideshow_nav').append('<a ' + link_class_str + 'href="#" rel="' + i + '">&nbsp;</a>');
	}
	
	// Collects all the anchor links in the main slideshow navigation
	var $slideshow_nav = $("#slideshow_nav a");
	
	// Hide all the images in the slideshow
	$("#slideshow_img img").hide();
	
	// Hide the "back" and "next" slideshow navigation buttons
	$("#slideshow_img .btn a").hide();
	
	// Show the "next" or "back" navigation button when the pointer is over the parent div (.btn)
	$("#slideshow_img .btn").mouseover(function(){
	    $(this).find("a").show();											
	});
	
	// Hide the "next" or "back" navigation button when the pointer moves off the parent div (.btn)
	$("#slideshow_img .btn").mouseout(function(){
	    $(this).find("a").hide();									
	});
	
	// Logic for the "next" and "back" navigation buttons
	$("#slideshow_img .btn a").click(function(){
		clearTimeout(t);
		if($(this).attr("rel") == "next")
		{
			$slideshow_nav.eq(active_entry).removeClass('current');
			active_entry = active_entry<(entry_count-1)?(Number(active_entry) + 1):0;
			$slideshow_nav.eq(active_entry).addClass('current');
			$slideshow_photos.hide();
			$slideshow_photos.eq(active_entry).show();
			return false;
		}
		else
		{
			$slideshow_nav.eq(active_entry).removeClass('current');
			active_entry = active_entry!=0?(Number(active_entry) - 1):entry_count-1;
			$slideshow_nav.eq(active_entry).addClass('current');
			$slideshow_photos.hide();
			$slideshow_photos.eq(active_entry).show();
			return false;
		}
	});
	
	// This is for the start of the slideshow. Shows the first image in the slideshow
	$slideshow_photos.eq(0).show();
	
	// Navigation logic for the small slideshow button at the bottom of the #slideshow_img area
	$('#slideshow_nav a').click(function(){
		clearTimeout(t);
		$('#slideshow_nav a').removeClass('current');
		$(this).addClass('current');
		$("#slideshow_img img:eq("+active_entry+")").hide();
		active_entry = $(this).attr('rel');
		$("#slideshow_img img:eq("+active_entry+")").show();
		return false;
	});
	
	// More Photos
	
	// The number of images in the More Photos widget
	more_photos_count = $("#more_photos img").not('#more_photos_btn').length;
	
	// Builds the button navigation
	for(var i = 0; i < more_photos_count; i++)
	{
		var link_class_str = "";
		if(i == 0)
		{
			link_class_str = "class=\"current\"";	
		}
		$('#more_photos_nav').append('<a ' + link_class_str + 'href="#" rel="' + i + '">&nbsp;</a>');
	}
	
	// Collects all the photos in the More Photos widget
	var $more_photos = $("#more_photos img");
	$more_photos.hide();
	$more_photos.eq(0).show();
	
	// Collects all the anchor links in the More Photos navigation
	var $more_photos_nav = $('#more_photos_nav a');
	
	// Navigation logic for the More Photos widget
	$('#more_photos_nav a').click(function(){
		$('#more_photos_nav a').removeClass('current');
		$(this).addClass('current');
		more_photos_active_entry = $(this).attr('rel');
		$more_photos.hide();
		$more_photos.eq(more_photos_active_entry).show();
		return false;
	});
	
	// Navigation for the "More Photos" button
	/*$('#more_photos_btn').click(function(){
		$more_photos_nav.eq(more_photos_active_entry).removeClass('current');
		more_photos_active_entry = more_photos_active_entry<(more_photos_count-1)?(Number(more_photos_active_entry) + 1):0;
		$more_photos_nav.eq(more_photos_active_entry).addClass('current');
		$more_photos.hide();
		$more_photos.eq(more_photos_active_entry).show();
		return false;
	});*/
	
});

// Automatically advances the main slideshow after an arbitrary period of time
function advance_slideshow(){
	$("#slideshow_img img:eq("+active_entry+")").hide();
	$('#slideshow_nav a').removeClass('current');
	active_entry=active_entry<(entry_count-1)?active_entry+1:0;
	$('#slideshow_nav a:eq(' + active_entry + ')').addClass('current');
	$("#slideshow_img img:eq("+active_entry+")").show();
	t = setTimeout(advance_slideshow, 5000);
	return false;
}

advance_slideshow();