// JavaScript Document

$(document).ready(function(){
	var i=0;
	$('#previews .thumbnails img').each(function(){
		i++; $(this).attr('rel',i);
	});
	
	$('#previews .thumbnails img').click(function(){
		var rel = $(this).attr('rel');
		var rev = $(this).attr('rev');
		
		$('#show').stop(true,true).fadeOut(function(){
			$('#show').hide().attr('src',rev);
			$('#show').attr('rel',rel);
			$('#show').fadeIn();
		});
		
		checkPaging(rel)
		
		return false;
	});
	
	$('#full-image .next').click(function(){
		var rel = $('#show').attr('rel');
		rel = (rel*1)+1;
		$('#show').stop(true,true).fadeOut(function(){
			$('#show').hide().attr('src',$('.thumbnails img[rel="'+rel+'"]').attr('rev'));
			$('#show').fadeIn();
			$('#show').attr('rel',rel);
		});
		checkPaging('next');
		return false;
	});
	
	$('#full-image .prev').click(function(){
		var rel = $('#show').attr('rel');
		rel = (rel*1)-1;
		$('#show').stop(true,true).fadeOut(function(){
			$('#show').hide().attr('src',$('.thumbnails img[rel="'+rel+'"]').attr('rev'));
			$('#show').fadeIn();
			$('#show').attr('rel',rel);
		});
		checkPaging('prev');
		return false;
	});
	
});

function checkPaging(next_prev){
	var rel = $('#show').attr('rel');
	var currpage = $('#currpage').val();
	$('#full-image .next').fadeIn();
	$('#full-image .prev').fadeIn();
	
	if(next_prev=='next'){
		rel =(rel*1)+1;
		if(rel>15){
			window.location.href ='/gallery/'+((currpage*1)+1)+'#gallery';
		}
	}else if(next_prev=='prev'){
		rel =(rel*1)-1;
		if(rel==0){
			if(currpage>1){
				window.location.href ='/gallery/'+((currpage*1)-1)+'#gallery';
			}else{
				$('#full-image .prev').fadeOut();
			}
		}
	}else{
		if(page==1 && next_prev==1){
			$('#full-image .prev').fadeOut();
		}
	}
}
