var pg_num = 0;
var click_ok = true;
var pg_limit = 1;
var book_offset = 0;
var cur_book = 0;

function getGrid(whereTo, command){
	
	if(click_ok){
		click_ok = false;
		if(command == "prev" && pg_num > 0){
			pg_num --;
			doWork();
		} else if(command == "next" && pg_num < pg_limit-1){
			pg_num ++;
			doWork();
		} else if(command == "back"){
			new Effect.toggle('nav_back', 'appear', { duration: 0.1, afterFinish: function() { 
				new Effect.toggle('pagination', 'appear', { duration: 0.1 });
	        } });
			$('book_nav').style.display = '';
			$('view_nav').style.display = 'none';
			book_index = all_books.indexOf(cur_book) + book_offset + 1;
			pg_num = Math.ceil(book_index/8.0)-1;
			book_offset = 0;
			doWork();
		} else {
			click_ok = true;
		}
	}

	
	function doWork(){
		$('pagination').innerHTML = "Page " + (pg_num+1) + " of " + pg_limit;
		
		params_img = "page="+pg_num;
		container = $('book_shelf');
	
		new Effect.toggle(container, 'appear', { duration: 0.1, 
			afterFinish: function(){
				new Ajax.Updater(container, whereTo,{
				method: 'post', 
				parameters: params_img,
				onComplete:function(){
					
					new Effect.toggle(container, 'appear', { duration: 0.1, afterFinish:function(){
						click_ok = true;
					} });
					if(command == "back"){
						$('book_nav').style.display = '';
						$('view_nav').style.display = 'none';
					}
				}
			});
		} });
	}		
}

function getBook(whereTo, id, command){
	container = $('book_shelf');
	cur_book = id;
	cur_index = all_books.indexOf(cur_book);
	
	$('book_nav').style.display = 'none';
	if(command == undefined) {
		params_img = "book_id="+id;
		new Effect.toggle(container, 'appear', { duration: 0.1, 
			afterFinish: function(){
				new Ajax.Updater(container, whereTo,{
				method: 'post', 
				parameters: params_img,
				onComplete:function(){
					$('view_nav').style.display = '';
					
					new Effect.toggle(container, 'appear', { duration: 0.1 });
					
					new Effect.toggle('pagination', 'appear', { duration: 0.1, afterFinish: function() { 
						new Effect.toggle('nav_back', 'appear', { duration: 0.1 });
			        } });
					}
				});
			} });
	} else if(command == 'next') {
		book_offset++;
		new_index = cur_index + book_offset;
		if(new_index >= 0 && new_index < all_books.size() && click_ok) {
			params_img = "book_id="+all_books[new_index];
			new Effect.toggle(container, 'appear', { duration: 0.1,
				afterFinish: function(){
					click_ok = false;
					new Ajax.Updater(container, whereTo,{
					method: 'post', 
					parameters: params_img,
					onComplete:function(){
						$('view_nav').style.display = '';
						new Effect.toggle(container, 'appear', { duration: 0.1 });
						click_ok = true;
						}
					});
				} });
		} else book_offset--;
	} else if(command == 'prev') {
		book_offset--;
		new_index = cur_index + book_offset;
		if(new_index >= 0 && new_index < all_books.size() && click_ok) {
			params_img = "book_id="+all_books[new_index];
			new Effect.toggle(container, 'appear', { duration: 0.1, 
				afterFinish: function(){
					click_ok = false;
					new Ajax.Updater(container, whereTo,{
					method: 'post', 
					parameters: params_img,
					onComplete:function(){
						$('view_nav').style.display = '';
						new Effect.toggle(container, 'appear', { duration: 0.1 });
						click_ok = true;
						}
					});
				} });
		} else book_offset++;
	}
}