$(document).ready(function() 
{
	var width = $(window).width();
	//alert(width);

	//var p = $("div#active");
	//var position = p.position();
	//alert( "left: " + position.left + ", top: " + position.top );
	
	// need the width of active
	//var w = $('div#active').width();
	//alert(w);
	
	flow();
	//where_does_the_nav_go();
});

$(window).resize(function(){ flow(); });

function where_does_the_nav_go()
{
	// does the nav have enough room to appear?
	var p = $("div.active");
	var position = p.position();
	
	// need the width of active
	var w = $('div.active').width();
	var space = w + position.left + 255;
	var final = $(window).width() - space;
	
	if ((final < 225) && (position.left > 225))
	{
		// shift the nav to the other side
		$('div.active #texty').css({ 'left': '-255px', 'text-align': 'right' });
	}
	else
	{
		$('div.active #texty').css({ 'left': w, 'text-align': 'left' });
	}
	
	// it works!
	var pa = $('div.active').position();
	var move = pa.top - 80;
	
	// why does this fail for text-only nodes?
	//$(window).scrollTo({top: move, left:'0'}, 500);
}

function flow()
{
	$('div').remove('.once');
	var thiswidth = 0;
	var bigwidth = ($(window).width() - 55);

	$('div.picture_holder').each(function()
	{
		var thewidth = $(this).width();
		thiswidth = parseInt(thiswidth) + parseInt(thewidth);
		
		if (thiswidth > bigwidth)
		{
			$(this).prev('div.picture_holder').after("<div class='once'><!-- --></div>");
			thiswidth = thewidth;
		}
	});
	
	where_does_the_nav_go();
}


// for now
function show_image(id)
{
	$('.pic').hide();
	$('#p' + id).show();
	$('#num').html(id);

	// swap the text too
	//$('#info-text').html( $('#p' + id + ' div.img-text').html() );

	return false;
}

function move(mode)
{
	var i = 1;
	var current;
	var total = $('.pic').length;

	$('.pic').each(function()
	{
		var check = $(this).css('display');
		if (check == 'block') { current = i; }
		i++;
	});

	if (mode == 'next')
	{
		current = ((current + 1) > total) ? 1 : current + 1;
	}
	else
	{
		current = ((current - 1) == 0) ? total : current - 1;
	}

	show_image( current );
}