  We develop the theory of maximal representations of the fundamental group of
a compact connected oriented surface with boundary, into a group of Hermitian
type. For any such representation we define the Toledo invariant, for which we
establish properties such as uniform boundedness on the representation variety,
additivity under connected sum of surfaces and congruence relations. We thus
obtain geometric properties of the maximalrepresentations, that is
representations whose Toledo invariant achieves the maximum value: we show that
maximal representations have discrete image, are faithful and completely
reducible and they always preserve a maximal tube type subdomain. This extends
to the case of a general Hermitian group some of the properties of the
representations in Teichmuller space, as well as results due to Goldman,
Toledo, Hernandez, Bradlow--Garcia-Prada--Gothen. An announcement of these
results in the case of surfaces without boundary -- where the role of tube type
domains had already been emphasized -- appeared in 2003 by the same authors.
  The congruence relations for the Toledo invariant involve a rotation number
function related to a continuous homogeneous quasimorphism which gives an
explicit way to compute the Toledo invariant. This rotation number generalizes
constructions due to Ghys, Barge--Ghys, and Clerc--Koufany.
  We establish moreover properties of boundary maps associated to maximal
representations which generalize naturally, for the causal structure of the
Shilov boundary, monotonicity properties of quasiconjugations of the circle.
This, together with the congruence relations leads to the result that the
subset of maximal representations is always real semialgebraic.
