var current_index = 0;
var per_list = 10;

$(document).ready(function(){
	var new_list = $("<ul></ul>")
		.attr('id','photos_list')
		.insertBefore($('#photos'));
	$('#photos').hide();
	changeList(current_index,per_list);
	changeTo($('#photos_list li:first-child a'));
});

function changeTo(el){
	$('#photos_list a').each(function(){
		$(this).removeClass('selected');
	});
	$(el).addClass('selected');
	var new_img = $("<img />")
		.addClass('.photo')
		.attr('src',$(el).attr('href'))
		.attr('alt',$(el).html())
	var new_title = $("<h3></h3>")
		.addClass('photo_title')
		.html($(el).html());
	var new_description = $("<div></div>")
		.addClass('photo_description')
		.html($(el).attr('rel'));
	$('#photo').fadeOut("fast",function(){
		$('#photo')
			.empty()
			.append(new_img)
			.append(new_title)
			.append(new_description)
			.fadeIn("slow");								
	});
}
function changeList(from,to){
	current_index = from;
	$('#photos_list').empty();
	$('#photos li').slice(from,to).clone().prependTo('#photos_list');
	if(to > $('#photos li').length){ $('#photo_next').unbind().click(function(e){e.preventDefault();}); }
	else{
		$('#photo_next').unbind().click(function(e){
			e.preventDefault();
			changeList(current_index+per_list,current_index+(per_list*2));
		});
	}
	if(from == 0){ $('#photo_previous').unbind().click(function(e){e.preventDefault();}); }
	else{
		$('#photo_previous').unbind().click(function(e){
			e.preventDefault();
			changeList(current_index-per_list,current_index);
		});
	}
	$('#photos_list a').each(function(){
		$(this).click(function(e){
			e.preventDefault();
			changeTo(this);
		});
	});
}
