var json_backgrounds;
var curr_index = "none";
var prev_index = "";
var next_index = "";

function loadBackground(which,button)
{
	$('#background img').attr('src',json_backgrounds[which].imagefile);
	curr_index = which;
	writeButtons();
}

function writeButtons()
{

	var last_index = json_backgrounds.length - 1;
	var curr_background = $('#background img').attr('src');
	if(curr_index == "none")
	{
		for(x=0; x<json_backgrounds.length; x++)
		{
			if(curr_background == json_backgrounds[x].imagefile)
			{
				curr_index = json_backgrounds[x].number;
				break;
			}
		}
	}
	if(curr_index == "none")
	{
		next_index = 0;
		prev_index = last_index;
	}
	else 
	{
		if( (parseInt(curr_index) - 1) < 0){ prev_index = last_index; }
		else { prev_index = parseInt(curr_index) - 1; }
		if( (parseInt(curr_index) + 1) > last_index){ next_index = 0; }
		else { next_index = parseInt(curr_index) + 1; }
	}
	var buttonsHTML = "";
	buttonsHTML+='<a id="'+prev_index+'" href="javascript:loadBackground(\''+prev_index+'\',\'Left\');"><img class="backgroundPrev" src="/images/photo_prev.png" width="50" height="50" border="0" alt="Previous Image"/></a>';
	buttonsHTML+='<a id="'+next_index+'" href="javascript:loadBackground(\''+next_index+'\',\'Right\');"><img class="backgroundNext" src="/images/photo_next.png" width="50" height="50" border="0" alt="Next Image"/></a>';

	$('#hmButtons').html("");
	$('#hmButtons').html(buttonsHTML);
}
