$(function(){
	scrapBookInit();
});

//Vars
var currentItem = 1; // Not 0-based
var currentPage = 1;
var currentCategory = 'all';
var totalItems = 2;
var totalPages = 1;
var navLocked = true;

//Functions
function scrapBookInit(){
	$('.category_link').click(function(){
		var myRel = this.rel;
		setCategory(myRel);
		return false;
	});
	$('#scrapbookPrev').click(function(){
		scrapBookPrev();
		return false;
	});
	$('#scrapbookNext').click(function(){
		scrapBookNext();
		return false;
	});
	$('#scrapbook-items-holder').after('<div style="display:none;" id="scrapbook-current-items-holder">&nbsp;</div>');
	navLocked = false;
	setCategory('all');
}

function scrapBookPrev(){
	if(!navLocked){
		navLocked = true;
		currentItem -= 2;
		if(currentItem < 1){
			currentItem = totalItems;
		}
		goToCurrent();
	}
}

function scrapBookNext(){
	if(!navLocked){
		navLocked = true;
		if(totalItems > 0){
			currentItem += 2;
			if( currentItem > totalItems){
				currentItem = 1;
			}
		}else{
			currentItem = 0;
		}
		goToCurrent();
	}
}

function goToCurrent(){
	if($('#scrapbook-current-items-holder').children().eq(currentItem - 1).length > 0){
		$('#scrapbook-photos-left').append($('#scrapbook-current-items-holder').children().eq(currentItem - 1).clone().hide());
	}else{
		$('#scrapbook-photos-right').append('<div class="item-holder empty"></div>');
	}
	if($('#scrapbook-current-items-holder').children().eq(currentItem).length > 0){
		$('#scrapbook-photos-right').append($('#scrapbook-current-items-holder').children().eq(currentItem).clone().hide());
	}else{
		$('#scrapbook-photos-right').append('<div class="item-holder empty"></div>');
	}
	$('.item-holder:not(.current)','#scrapbook-photos-left').eq(0).fadeIn();
	$('.item-holder.current','#scrapbook-photos-left').each(function(){
		$(this).fadeOut(function(){
			$(this).remove();
		});
	});
	$('.item-holder','#scrapbook-photos-left').addClass('current');
	$('.item-holder:not(.current)','#scrapbook-photos-right').eq(0).fadeIn();
	$('.item-holder.current','#scrapbook-photos-right').each(function(){
		$(this).fadeOut(function(){
			$(this).remove();
		});
		navLocked = false;
	});
	$('.item-holder','#scrapbook-photos-right').addClass('current');
	setTotals();
}

function setTotals(){
   totalItems = $('#scrapbook-current-items-holder').children().length;
   totalPages = Math.ceil(totalItems / 2);
   if(!(totalItems > 0)){
	   currentItem = 0;
   }
   currentPage = Math.ceil(currentItem / 2);
   $('#pageCountCurrent').html(currentPage);
   $('#pageCountTotal').html(totalPages);
}

function setCategory( key ){
	if(!navLocked){
		if(key == 'all'){
			$('#scrapbook-current-items-holder').html($('#scrapbook-items-holder').html());
		}else{
			$('#scrapbook-current-items-holder').empty();
			$('#scrapbook-items-holder').children().each(function(){
				if(this.title.indexOf(key+';') >= 0){
					$('#scrapbook-current-items-holder').append($(this).clone());
				}
			});
		}
		currentItem = 1;
		goToCurrent();
	}
}