Python 2, 120120 118 bytes
i,j=input()
a=j%i
n='|\n'
x='|'+'---'*i+n
print(x+('|'+'|x|'*(j%i)+''|'+'|x|'*a+' '*(i-j%ia)*3+n)*3,'')[j%i<1]+[a<1]+(x+('|'+'|x|'*i+n)*3)*(j/i)+x
Have been meaning to have a go at this one for the last few days. Now that I have finally got time to do it there's already a shorter Python answer. Oh well, just posted as an alternative.
Input taken as width,books