  We analyze the existence, stability, and internal modes of gap solitons in
nonlinear periodic systems described by the nonlinear Schrodinger equation with
a sinusoidal potential, such as photonic crystals, waveguide arrays,
optically-induced photonic lattices, and Bose-Einstein condensates loaded onto
an optical lattice. We study bifurcations of gap solitons from the band edges
of the Floquet-Bloch spectrum, and show that gap solitons can appear near all
lower or upper band edges of the spectrum, for focusing or defocusing
nonlinearity, respectively. We show that, in general, two types of gap solitons
can bifurcate from each band edge, and one of those two is always unstable. A
gap soliton corresponding to a given band edge is shown to possess a number of
internal modes that bifurcate from all band edges of the same polarity. We
demonstrate that stability of gap solitons is determined by location of the
internal modes with respect to the spectral bands of the inverted spectrum and,
when they overlap, complex eigenvalues give rise to oscillatory instabilities
of gap solitons.
